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.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 org.quartz.JobExecutionException; import org.xml.sax.SAXException; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.sql.*; 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 DEFAULT_NAMESPACE = "xtpc"; private static final String XGEOSDATACONFIG_PATH = "xgeosdataconfig.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 int MAGIC_BLOCKSIZE = (64 * 1024 * 1024) - (32 * 1024); private static final String QUERY_VIEWDEFSQL = "SELECT table_name, view_definition FROM information_schema.views " + "WHERE table_schema = ? AND table_name LIKE "; private static final String CREATE_VIEWSQL = "CREATE OR REPLACE VIEW \"%s\" AS SELECT * FROM \"%s\".\"%s\""; private static final String EXTRAWHERE_VIEWSQL = " WHERE \"%s\".level = %s AND \"%s\".symweight = %s"; private static final String ALTER_VIEWSQL = "ALTER TABLE \"%s\" OWNER TO "; // 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 String _geoServerURL; protected String _geoServerUser; protected String _geoServerPass; private long queryTime = 0; private long queryTimeStart = 0; public Log getLogger() { return logger; } protected AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath, boolean profileMode, boolean useTransform) { return super.prepareJobContext(targetSchemaName, filterPath, profileMode, useTransform); } protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException { super.extractJobConfiguration(jobDetail); JobDataMap dataMap = jobDetail.getJobDataMap(); _geoServerURL = dataMap.getString(GEOSERVER_BASEURL); _geoServerUser = dataMap.getString(GEOSERVER_USER); _geoServerPass = dataMap.getString(GEOSERVER_PASS); if (_geoServerURL == null) { logger.warn("GEOSERVER_URL is null"); throw new JobExecutionException("Unknown GEOSERVER_URL."); } if (_geoServerUser == null) { logger.warn("GEOSERVER_USER is null"); throw new JobExecutionException("Unknown GEOSERVER_USER."); } if (_geoServerPass == null) { logger.warn("GEOSERVER_PASS is null"); throw new JobExecutionException("Unknown GEOSERVER_PASS."); } } protected XGeosDataConfigMapping getConfigMapping() { if (xgeosDataConfigMapping == null) { Digester digester = XGeosConfigDigesterUtils.getXGeosConfigDigester(); final URL configDataURL = XGeosDataConfigMapping.class.getResource(XGEOSDATACONFIG_PATH); try { xgeosDataConfigMapping = (XGeosDataConfigMapping) digester.parse(configDataURL); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SAXException e) { logger.warn(e.getMessage(), e); } } return xgeosDataConfigMapping; } @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { super.execute(jobExecutionContext); createTargetDataStore(); if (getTargetDataStore() == null) { logger.warn("Cannot connect source postgreSQL database."); throw new JobExecutionException("Cannot connect source postgreSQL database."); } if (isProfileMode()) { queryTime = 0; } long t1 = System.currentTimeMillis(); String targetSchemaName; try { resetPostgisViewMapping(jobExecutionContext); resetGeoServerConfig(jobExecutionContext); } finally { disconnect(); } } /** * 重新建立所有重新建立所有PostGIS中的資料庫視景 * * @param executionContext 批次執行的關係 */ private void resetPostgisViewMapping(JobExecutionContext executionContext) { assert executionContext != null; try { Connection connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT); String ownerName = _pgUsername; String currentTargetSchema = retrieveCurrentSchemaName(connection, DataReposVersionManager.VSSTATUS_READY); if (currentTargetSchema == null) { logger.info("Cannot found schema that status is VSSTATUS_READY[" + DataReposVersionManager.VSSTATUS_READY + "]"); return; } ArrayList realTableNames = new ArrayList(); retrieveAllRealTableName(connection, currentTargetSchema, realTableNames); HashMap viewDefs = retrieveViewDef(connection, "public", "fsc%"); HashMap tempViewDefs = retrieveViewDef(connection, "public", "indexshape%"); viewDefs.putAll(tempViewDefs); tempViewDefs = viewDefs = retrieveViewDef(connection, "public", "lndtpc%"); viewDefs.putAll(tempViewDefs); for (String tableName : realTableNames) { resetPostgisDataView(connection, viewDefs, ownerName, currentTargetSchema, tableName); } resetExtraPostgisDataView(connection, ownerName, currentTargetSchema, realTableNames); updateCurrentRepositoryStatus(connection, currentTargetSchema, DataReposVersionManager.VSSTATUS_LINKVIEW); // String[] featureNames = dataStore.getTypeNames(); // logger.info("featureNames[] size = " + featureNames.length); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } finally { // if (dataStore != null) dataStore.dispose(); } } private void retrieveAllRealTableName(Connection connection, String targetSchema, ArrayList realTableNames) throws SQLException { ResultSet rsMeta = null; try { rsMeta = connection.getMetaData().getTables("", targetSchema, "fsc%", new String[]{"TABLE"}); while (rsMeta.next()) { String tableName = rsMeta.getString(3); realTableNames.add(tableName); } rsMeta.close(); rsMeta = null; rsMeta = connection.getMetaData().getTables("", targetSchema, "index%", new String[]{"TABLE"}); while (rsMeta.next()) { String tableName = rsMeta.getString(3); realTableNames.add(tableName); } rsMeta.close(); rsMeta = null; rsMeta = connection.getMetaData().getTables("", targetSchema, "lndtpc%", new String[]{"TABLE"}); while (rsMeta.next()) { String tableName = rsMeta.getString(3); realTableNames.add(tableName); } } finally { if (rsMeta != null) rsMeta.close(); } } private void resetPostgisDataView(Connection connection, HashMap viewDefs, String ownerName, String schemaName, String tableName) throws SQLException { String[] splits = tableName.split("-"); if (splits.length > 3) { // feature table StringBuilder viewBuilder = new StringBuilder(); viewBuilder.append(splits[0]); viewBuilder.append('-'); viewBuilder.append(splits[1]); viewBuilder.append('-'); viewBuilder.append(splits[2]); viewBuilder.append(splits[3]); String viewName = viewBuilder.toString(); if (viewDefs.containsKey(viewName)) { String viewDef = viewDefs.get(viewName); int pos = viewDef.indexOf("FROM"); String subView = viewDef.substring(pos + 4); // String[] viewSources = subView.split("\\."); String[] viewSources = subView.split("(\\.\"|\")"); if (!viewSources[0].equalsIgnoreCase(schemaName)) { createOrReplaceView(connection, schemaName, tableName, viewName, ownerName); } } else { createOrReplaceView(connection, schemaName, tableName, viewName, ownerName); } } else { splits = tableName.split("_"); if (splits.length > 0) { StringBuilder viewBuilder = new StringBuilder(); viewBuilder.append(splits[0]); if (splits.length > 1) viewBuilder.append(splits[1]); if (splits.length > 2) viewBuilder.append(splits[2]); String viewName = viewBuilder.toString(); if (viewDefs.containsKey(viewName)) { String viewDef = viewDefs.get(viewName); int pos = viewDef.indexOf("FROM"); String subView = viewDef.substring(pos + 4); String[] viewSources = subView.split("(\\.\"|\")"); if (!viewSources[0].equalsIgnoreCase(schemaName)) { createOrReplaceView(connection, schemaName, tableName, viewName, ownerName); } } else { createOrReplaceView(connection, schemaName, tableName, viewName, ownerName); } } } } private void resetExtraPostgisDataView(Connection connection, String ownerName, String currentSchema, ArrayList realTableNames) { try { // ArrayList extraViewNames = new ArrayList(); XGeosDataConfigMapping configMapping = getConfigMapping(); MultiMap configMultiMap = configMapping.getMapping(); for (Object key : configMultiMap.keySet()) { List values = (List) configMultiMap.get(key); for (Object value : values) { XGeosDataConfig xgeosConfig = (XGeosDataConfig) value; short tid = xgeosConfig.getFSC(); short cid = xgeosConfig.getCOMP(); StringBuilder sbTable = new StringBuilder("fsc-"); sbTable.append(tid).append("-c-"); sbTable.append(cid); int index = realTableNames.indexOf(sbTable.toString()); if (index == -1) { logger.debug("Cannot found-" + xgeosConfig.toString()); continue; } StringBuilder sbView = new StringBuilder("fsc-"); sbView.append(tid).append("-c"); sbView.append(cid).append("-l"); sbView.append(xgeosConfig.getLEV()).append("-w"); sbView.append(xgeosConfig.getWEIGHT()); // extraViewNames.add(sbView.toString()); createOrReplaceExtraView(connection, currentSchema, sbTable.toString(), sbView.toString(), ownerName, xgeosConfig); } } } catch (SQLException e) { logger.warn(e.getMessage(), e); } } private HashMap retrieveViewDef(Connection connection, String schemaName, String tablePattern) throws SQLException { PreparedStatement stmt = connection.prepareStatement(QUERY_VIEWDEFSQL + "'" + tablePattern + "'"); stmt.setString(1, schemaName); // stmt.setString(2, tablePattern); HashMap result = new HashMap(); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String tableName = rs.getString(1); String viewDef = rs.getString(2); result.put(tableName, viewDef); } rs.close(); stmt.close(); return result; } private void createOrReplaceView(Connection connection, String schemaName, String tableName, String viewName, String ownerName) throws SQLException { PrintfFormat pf = new PrintfFormat(CREATE_VIEWSQL); String sql = pf.sprintf(new Object[]{viewName, schemaName, tableName}); Statement stmt = connection.createStatement(); stmt.execute(sql); pf = new PrintfFormat(ALTER_VIEWSQL + ownerName); sql = pf.sprintf(viewName); stmt.execute(sql); stmt.close(); // connection.commit(); } private void createOrReplaceExtraView(Connection connection, String schemaName, String tableName, String viewName, String ownerName, XGeosDataConfig xgeosConfig) throws SQLException { PrintfFormat pf = new PrintfFormat(CREATE_VIEWSQL); String sql = pf.sprintf(new Object[]{viewName, schemaName, tableName}); PrintfFormat pfWhere = new PrintfFormat(EXTRAWHERE_VIEWSQL); sql += pfWhere.sprintf(new String[]{tableName, Short.toString(xgeosConfig.getLEV()), tableName, Short.toString(xgeosConfig.getWEIGHT())}); Statement stmt = connection.createStatement(); stmt.execute(sql); pf = new PrintfFormat(ALTER_VIEWSQL + ownerName); sql = pf.sprintf(viewName); stmt.execute(sql); stmt.close(); // connection.commit(); } private Timestamp retrieveCurrentSchemaTimestamp(Connection connection, short status) throws SQLException { StringBuilder sbSQL = new StringBuilder("SELECT vstimestamp, vsschema, vsstatus FROM "); sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME); sbSQL.append(" WHERE vsstatus = "); sbSQL.append(status); sbSQL.append(" ORDER BY vsid"); Timestamp result = null; Statement stmt = null; ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery(sbSQL.toString()); // get first result if (rs.next()) { result = rs.getTimestamp(1); } return result; } finally { if (rs != null) rs.close(); if (stmt != null) stmt.close(); } } private void updateCurrentRepositoryStatus(Connection connection, String schemaName, short newStatus) throws SQLException { StringBuilder sbSQL = new StringBuilder("UPDATE "); sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME).append(' '); sbSQL.append(" SET vsstatus = "); sbSQL.append(newStatus); sbSQL.append(", vstimestamp = CURRENT_TIMESTAMP WHERE vsschema = '"); sbSQL.append(schemaName).append("'"); Statement stmt = null; try { stmt = connection.createStatement(); stmt.executeUpdate(sbSQL.toString()); } finally { if (stmt != null) stmt.close(); } } /* private boolean checkCurrentRepositoryStatus(DataStore dataStore, short status) { try { if (dataStore instanceof PostgisDataStore) { PostgisDataStore pgDataStore = (PostgisDataStore) dataStore; DataSource dataSource = pgDataStore.getDataSource(); Connection connection = dataSource.getConnection(); return checkCurrentRepositoryStatus(connection, status); } } catch (SQLException e) { logger.warn(e.getMessage(), e); } return false; } */ private boolean checkCurrentRepositoryStatus(Connection connection, short status) { try { return (retrieveCurrentSchemaName(connection, status) != null); } catch (SQLException e) { logger.warn(e.getMessage(), e); return false; } } private String retrieveCurrentSchemaName(Connection connection, short status) throws SQLException { StringBuilder sbSQL = new StringBuilder("SELECT vsschema, vstimestamp, vsstatus FROM "); sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME); sbSQL.append(" WHERE vsstatus = "); sbSQL.append(status); sbSQL.append(" ORDER BY vsid"); String result = null; Statement stmt = null; ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery(sbSQL.toString()); // get first result if (rs.next()) { result = rs.getString(1); } return result; } finally { if (rs != null) rs.close(); if (stmt != null) stmt.close(); } } private HashMap buildDefaultStylesMapping(XGeosDataConfigMapping configMapping) { HashMap result = new HashMap(); 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 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); GeoServerRESTManager manager = new GeoServerRESTManager(geoServerURL, _geoServerUser, _geoServerPass); GeoServerRESTReader reader = manager.getReader(); List workSpaces = reader.getWorkspaceNames(); boolean found = false; for (String name : workSpaces) { if (name.equalsIgnoreCase(DEFAULT_NAMESPACE)) { found = true; break; } } GeoServerRESTPublisher publisher = manager.getPublisher(); if (!found) { publisher.createWorkspace(DEFAULT_NAMESPACE, new URI("http://tpc.ximple.com.tw/geodmms")); } RESTDataStore dataStore = reader.getDatastore(DEFAULT_NAMESPACE, DEFAULT_STORENAME); if (dataStore == null) { GeoServerRESTStoreManager storeManager = manager.getStoreManager(); GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(DEFAULT_STORENAME); store.setHost(_pgHost); store.setPort(Integer.parseInt(_pgPort)); store.setDatabase(_pgDatabase); store.setSchema(_pgSchema); store.setUser(_pgUsername); store.setPassword(_pgPassword); storeManager.create(DEFAULT_NAMESPACE, store); } resetFeatureTypesMapping(jobExecutionContext, reader); XGeosDataConfigMapping configMapping = getConfigMapping(); MultiMap configMultiMap = configMapping.getMapping(); for (Object key : configMultiMap.keySet()) { 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, ""); */ } } } catch (MalformedURLException e) { logger.warn(e.getMessage(), e); } catch (URISyntaxException e) { 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 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; } }