| | |
| | | package com.ximple.eofms.jobs; |
| | | |
| | | import com.vividsolutions.jts.geom.*; |
| | | import com.ximple.eofms.geoserver.config.XGeosDataConfig; |
| | | import com.ximple.eofms.geoserver.config.XGeosDataConfigMapping; |
| | | import com.ximple.eofms.jobs.context.AbstractOracleJobContext; |
| | | import com.ximple.eofms.util.PrintfFormat; |
| | | import com.ximple.eofms.util.XGeosConfigDigesterUtils; |
| | | import it.geosolutions.geoserver.rest.GeoServerRESTManager; |
| | | import it.geosolutions.geoserver.rest.GeoServerRESTPublisher; |
| | | import it.geosolutions.geoserver.rest.GeoServerRESTReader; |
| | | import it.geosolutions.geoserver.rest.decoder.RESTDataStore; |
| | | import it.geosolutions.geoserver.rest.decoder.RESTStyleList; |
| | | import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder; |
| | | import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder; |
| | | import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder; |
| | | import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder; |
| | | import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager; |
| | | import org.apache.commons.collections.MultiMap; |
| | | import org.apache.commons.digester3.Digester; |
| | | import org.apache.commons.digester3.binder.DigesterLoader; |
| | | import org.apache.commons.digester3.xmlrules.FromXmlRulesModule; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.geotools.data.Transaction; |
| | | import org.opengis.feature.type.FeatureType; |
| | | import org.opengis.feature.type.GeometryDescriptor; |
| | | import org.quartz.JobDataMap; |
| | | import org.quartz.JobDetail; |
| | | import org.quartz.JobExecutionContext; |
| | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | public class GeoserverIntegrateConfigJob extends OracleConvertDgn2PostGISJob { |
| | | final static Log logger = LogFactory.getLog(GeoserverIntegrateConfigJob.class); |
| | |
| | | private static final String SKIPCONFIGJOB = "SKIPCONFIGJOB"; |
| | | private static final String MASTERMODE = "MASTERMODE"; |
| | | private static final String EPSG = "EPSG:"; |
| | | private static final String DEFAULTNAMESPACE = "xtpc"; |
| | | private static final String DEFAULT_NAMESPACE = "xtpc"; |
| | | private static final String XGEOSDATACONFIG_PATH = "xgeosdataconfig.xml"; |
| | | private static final String XGEOSRULES_NAME = "DefaultXGeosDataConfigRules.xml"; |
| | | private static final String GEOSERVER_BASEURL = "GEOSERVER_URL"; |
| | | private static final String GEOSERVER_USER = "GEOSERVER_USER"; |
| | | private static final String GEOSERVER_PASS = "GEOSERVER_PASS"; |
| | |
| | | // private static final String GRANT_VIEWSQL = "GRANT SELECT ON TABLE \"%s\" TO public"; |
| | | private static final int SRSID_TWD97_ZONE119 = 3825; |
| | | private static final int SRSID_TWD97_ZONE121 = 3826; |
| | | public static final String DEFAULT_STORENAME = "pgDMMS"; |
| | | |
| | | private static XGeosDataConfigMapping xgeosDataConfigMapping = null; |
| | | |
| | |
| | | |
| | | protected XGeosDataConfigMapping getConfigMapping() { |
| | | if (xgeosDataConfigMapping == null) { |
| | | final URL rulesURL = XGeosDataConfigMapping.class.getResource(XGEOSRULES_NAME); |
| | | assert rulesURL != null; |
| | | |
| | | DigesterLoader loader = DigesterLoader.newLoader( new FromXmlRulesModule() { |
| | | @Override |
| | | protected void loadRules() { |
| | | loadXMLRules(rulesURL); |
| | | } |
| | | |
| | | }); |
| | | Digester digester = loader.newDigester(); |
| | | /* |
| | | File rootDir = GeoserverDataDirectory.getGeoserverDataDirectory(); |
| | | File xfmsConfigDir; |
| | | |
| | | try { |
| | | xfmsConfigDir = GeoserverDataDirectory.findConfigDir(rootDir, "xdgnjobs"); |
| | | } catch (ConfigurationException cfe) { |
| | | logger.warn("no xmark dir found, creating new one"); |
| | | //if for some bizarre reason we don't fine the dir, make a new one. |
| | | xfmsConfigDir = new File(rootDir, "xdgnjobs"); |
| | | } x |
| | | |
| | | File xfmsConfigFile = new File(xfmsConfigDir, XGEOSDATACONFIG_PATH); |
| | | try { |
| | | xgeosDataConfigMapping = (XGeosDataConfigMapping) digester.parse(xfmsConfigFile); |
| | | } catch (IOException e) { |
| | | logger.warn(e.getMessage(), e); |
| | | } catch (SAXException e) { |
| | | logger.warn(e.getMessage(), e); |
| | | } |
| | | */ |
| | | Digester digester = XGeosConfigDigesterUtils.getXGeosConfigDigester(); |
| | | final URL configDataURL = XGeosDataConfigMapping.class.getResource(XGEOSDATACONFIG_PATH); |
| | | try { |
| | | xgeosDataConfigMapping = (XGeosDataConfigMapping) digester.parse(configDataURL); |
| | |
| | | super.execute(jobExecutionContext); |
| | | |
| | | createTargetDataStore(); |
| | | if (getSourceDataStore() == null) { |
| | | logger.warn("Cannot connect source oracle database."); |
| | | throw new JobExecutionException("Cannot connect source oracle database."); |
| | | } |
| | | |
| | | if (getTargetDataStore() == null) { |
| | | logger.warn("Cannot connect source postgreSQL database."); |
| | |
| | | sql = pf.sprintf(viewName); |
| | | stmt.execute(sql); |
| | | stmt.close(); |
| | | connection.commit(); |
| | | // connection.commit(); |
| | | } |
| | | |
| | | private void createOrReplaceExtraView(Connection connection, String schemaName, String tableName, String viewName, |
| | |
| | | sql = pf.sprintf(viewName); |
| | | stmt.execute(sql); |
| | | stmt.close(); |
| | | connection.commit(); |
| | | // connection.commit(); |
| | | } |
| | | |
| | | private Timestamp retrieveCurrentSchemaTimestamp(Connection connection, short status) throws SQLException { |
| | |
| | | } |
| | | } |
| | | |
| | | private HashMap<String, String> buildDefaultStylesMapping(XGeosDataConfigMapping configMapping) { |
| | | HashMap<String, String> result = new HashMap<String, String>(); |
| | | |
| | | for (Object key : configMapping.getMapping().keySet()) { |
| | | List xgeosConfigs = (List) configMapping.getMapping().get(key); |
| | | for (Object value : xgeosConfigs) { |
| | | XGeosDataConfig xgeosConfig = (XGeosDataConfig) value; |
| | | |
| | | StringBuilder sbView = new StringBuilder("fsc-"); |
| | | sbView.append(xgeosConfig.getFSC()).append("-c"); |
| | | sbView.append(xgeosConfig.getCOMP()).append("-l"); |
| | | sbView.append(xgeosConfig.getLEV()).append("-w"); |
| | | sbView.append(xgeosConfig.getWEIGHT()); |
| | | |
| | | String viewName = sbView.toString(); |
| | | if (!result.containsKey(viewName)) { |
| | | result.put(viewName, xgeosConfig.getFTYPE()); |
| | | } else { |
| | | if (xgeosConfig.getFTYPE() != null) { |
| | | if (!result.get(viewName).equals(xgeosConfig.getFTYPE())) |
| | | logger.info("Style Define Diff:" + result.get(viewName) + " - " + xgeosConfig.getFTYPE()); |
| | | } else { |
| | | logger.warn("xgeosConfig getFTYPE() is null - " + xgeosConfig.toString()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private void resetFeatureTypesMapping(JobExecutionContext executionContext, GeoServerRESTReader reader) { |
| | | try { |
| | | Connection connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT); |
| | | if (!checkCurrentRepositoryStatus(connection, DataReposVersionManager.VSSTATUS_LINKVIEW)) { |
| | | return; |
| | | } |
| | | |
| | | RESTStyleList styleList = reader.getStyles(); |
| | | |
| | | // Map styles = dataConfig.getStyles(); |
| | | |
| | | XGeosDataConfigMapping mapping = getConfigMapping(); |
| | | HashMap<String, String> defaultStyles = buildDefaultStylesMapping(mapping); |
| | | |
| | | try { |
| | | /* |
| | | String[] dsFTypeNames = dataStore.getTypeNames(); |
| | | |
| | | for (String featureTypeName : dsFTypeNames) { |
| | | String ftKey = dsConf.getId() + DataConfig.SEPARATOR + featureTypeName; |
| | | FeatureTypeConfig ftConfig = dataConfig.getFeatureTypeConfig(ftKey); |
| | | if (ftConfig == null) { |
| | | if (!createFeatureTypeConfig(dataConfig, dsConf, dataStore, styles, featureTypeName, defaultStyles)) { |
| | | LOGGER.info("Create Feature Failed. [" + featureTypeName + "]"); |
| | | } |
| | | } else { |
| | | updateFeatureTypeConfig(ftConfig, dataStore, styles, defaultStyles); |
| | | } |
| | | } |
| | | */ |
| | | } finally { |
| | | // if (dataStore != null) dataStore.dispose(); |
| | | } |
| | | } catch (IOException e) { |
| | | logger.warn(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | private void resetGeoServerConfig(JobExecutionContext jobExecutionContext) { |
| | | try { |
| | | URL geoServerURL = new URL(_geoServerURL); |
| | |
| | | List<String> workSpaces = reader.getWorkspaceNames(); |
| | | boolean found = false; |
| | | for (String name : workSpaces) { |
| | | if (name.equalsIgnoreCase(DEFAULTNAMESPACE)) { |
| | | if (name.equalsIgnoreCase(DEFAULT_NAMESPACE)) { |
| | | found = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | GeoServerRESTPublisher publisher = manager.getPublisher(); |
| | | if (!found) { |
| | | GeoServerRESTPublisher publisher = manager.getPublisher(); |
| | | publisher.createWorkspace(DEFAULTNAMESPACE, new URI("http://tpc.ximple.com.tw/geodmms")); |
| | | publisher.createWorkspace(DEFAULT_NAMESPACE, new URI("http://tpc.ximple.com.tw/geodmms")); |
| | | } |
| | | |
| | | RESTDataStore dataStore = reader.getDatastore(DEFAULTNAMESPACE, "pgDMMS"); |
| | | RESTDataStore dataStore = reader.getDatastore(DEFAULT_NAMESPACE, DEFAULT_STORENAME); |
| | | if (dataStore == null) { |
| | | GeoServerRESTStoreManager storeManager = manager.getStoreManager(); |
| | | GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder("pgDMMS"); |
| | | GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(DEFAULT_STORENAME); |
| | | store.setHost(_pgHost); |
| | | // store.setPort(_pgPort); |
| | | store.setPort(Integer.parseInt(_pgPort)); |
| | | store.setDatabase(_pgDatabase); |
| | | store.setSchema("public"); |
| | | store.setSchema(_pgSchema); |
| | | store.setUser(_pgUsername); |
| | | store.setPassword(_pgPassword); |
| | | storeManager.create(DEFAULTNAMESPACE, store); |
| | | storeManager.create(DEFAULT_NAMESPACE, store); |
| | | } |
| | | |
| | | |
| | | resetFeatureTypesMapping(jobExecutionContext, reader); |
| | | |
| | | XGeosDataConfigMapping configMapping = getConfigMapping(); |
| | | MultiMap configMultiMap = configMapping.getMapping(); |
| | |
| | | List values = (List) configMultiMap.get(key); |
| | | for (Object value : values) { |
| | | XGeosDataConfig xgeosConfig = (XGeosDataConfig) value; |
| | | |
| | | /* |
| | | publishPostGISLayer(publisher, DEFAULT_NAMESPACE, DEFAULT_STORENAME, |
| | | xgeosConfig.getPG(), |
| | | "EPSG:" + SRSID_TWD97_ZONE121, ""); |
| | | */ |
| | | } |
| | | } |
| | | |
| | |
| | | logger.warn(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | public boolean publishPostGISLayer(GeoServerRESTPublisher publisher, String workspace, String storename, String layername, String srs, |
| | | String defaultStyle) { |
| | | |
| | | final GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder(); |
| | | |
| | | fte.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.REPROJECT_TO_DECLARED); |
| | | fte.addKeyword("KEYWORD"); |
| | | fte.setTitle(layername); |
| | | fte.setName(layername); |
| | | fte.setSRS(srs); // srs=null?"EPSG:4326":srs); |
| | | final GSLayerEncoder layerEncoder = new GSLayerEncoder(); |
| | | layerEncoder.setDefaultStyle(defaultStyle); |
| | | return publisher.publishDBLayer(workspace, storename, fte, layerEncoder); |
| | | } |
| | | |
| | | protected String getDefaultFeatureTypeStyleId(Map styles, HashMap<String, String> defaultStyles, FeatureType featureType) { |
| | | String ftName = featureType.getName().getLocalPart(); |
| | | boolean isNormalFeature = false; |
| | | boolean isLandBased = false; |
| | | boolean isIndex = false; |
| | | boolean isSmallIndex = false; |
| | | boolean isSymbol = false; |
| | | GeometryDescriptor geomAttrType = featureType.getGeometryDescriptor(); |
| | | Class geomType = geomAttrType.getType().getBinding(); |
| | | if (defaultStyles.containsKey(ftName)) { |
| | | String defaultStyleName = defaultStyles.get(ftName); |
| | | String styleName = retrieveDefaultStyle(styles, defaultStyleName, "unknown"); |
| | | if (!styleName.equals("unknown")) { |
| | | return styleName; |
| | | } |
| | | } |
| | | |
| | | if (ftName.indexOf("fsc") != -1) { |
| | | isNormalFeature = true; |
| | | } |
| | | if (ftName.indexOf("indexshape") != -1) { |
| | | isIndex = true; |
| | | } |
| | | if (ftName.indexOf("indexshapes") != -1) { |
| | | isSmallIndex = true; |
| | | } |
| | | if (ftName.indexOf("lnd") != -1) { |
| | | isLandBased = true; |
| | | } |
| | | /* |
| | | if (featureType.find("symbol") != -1) { |
| | | isSymbol = true; |
| | | } |
| | | */ |
| | | |
| | | if (Point.class.equals(geomType)) { |
| | | if (isSymbol) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Symbol", "point"); |
| | | } else if (isIndex) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_TpclidText", "point"); |
| | | } else { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Text", "point"); |
| | | } |
| | | } else if (LineString.class.equals(geomType)) { |
| | | if ((!isIndex) && (!isLandBased)) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Conductor", "line"); |
| | | } else if (isIndex) { |
| | | if (isSmallIndex) |
| | | return retrieveDefaultStyle(styles, "pgTPC_INDEXSHAPES", "line"); |
| | | |
| | | return retrieveDefaultStyle(styles, "pgTPC_INDEXSHAPE", "line"); |
| | | } else if (isLandBased) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_LndcityLine", "line"); |
| | | } |
| | | } else if (MultiPoint.class.equals(geomType)) { |
| | | if (isSymbol) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Symbol", "point"); |
| | | } else { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Text", "point"); |
| | | } |
| | | } else if (Polygon.class.equals(geomType)) { |
| | | if ((!isIndex) && (!isLandBased)) { |
| | | return retrieveDefaultStyle(styles, "polygon", "polygon"); |
| | | } else if (isIndex) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_INDEXSHAPE", "polygon"); |
| | | } else if (isLandBased) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_LndcityPolygon", "polygon"); |
| | | } |
| | | } else if (LinearRing.class.equals(geomType)) { |
| | | if (!isIndex) { |
| | | return retrieveDefaultStyle(styles, "polygon", "polygon"); |
| | | } else { |
| | | return retrieveDefaultStyle(styles, "pgTPC_INDEXSHAPE", "polygon"); |
| | | } |
| | | } else if (MultiLineString.class.equals(geomType)) { |
| | | if ((!isIndex) && (!isLandBased)) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_Conductor", "line"); |
| | | } else if (isLandBased) { |
| | | return retrieveDefaultStyle(styles, "pgTPC_LndcityLine", "line"); |
| | | } else { |
| | | return retrieveDefaultStyle(styles, "pgTPC_INDEXSHAPE", "line"); |
| | | } |
| | | } else if (MultiPolygon.class.equals(geomType)) { |
| | | return "polygon"; |
| | | } |
| | | |
| | | return "pgTPC_Symbol"; |
| | | } |
| | | |
| | | private static String retrieveDefaultStyle(Map styles, String styleName, String defaultStyleName) { |
| | | if (styles.containsKey(styleName)) { |
| | | return styleName; |
| | | } else |
| | | return defaultStyleName; |
| | | } |
| | | |
| | | } |