forked from geodmms/xdgnjobs

ulysseskao
2014-06-12 28132c5faadb4467205abc736964af3ecb24b1c6
xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeoserverIntegrateConfigJob.java
@@ -1,30 +1,23 @@
package com.ximple.eofms.jobs;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
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.*;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder;
import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder23;
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.geotools.data.jdbc.JDBCUtils;
import org.geotools.geometry.GeneralEnvelope;
import org.jdom.Element;
import org.opengis.feature.type.FeatureType;
import org.opengis.feature.type.GeometryDescriptor;
import org.quartz.JobDataMap;
@@ -34,12 +27,20 @@
import org.xml.sax.SAXException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class GeoserverIntegrateConfigJob extends OracleConvertDgn2PostGISJob {
    final static Log logger = LogFactory.getLog(GeoserverIntegrateConfigJob.class);
@@ -765,36 +766,6 @@
        return result;
    }
    private String buildDefaultWMSLayerNames(String namespace, List xgeosConfigs, GSLayerGroupEncoder lgEncoder) {
        StringBuilder sbLayers = new StringBuilder();
        boolean first = true;
        for (Object value : xgeosConfigs) {
            if (!first) {
                sbLayers.append(',');
            } else {
                first = false;
            }
            XGeosDataConfig xgeosConfig = (XGeosDataConfig) value;
            StringBuilder sbLayerName = new StringBuilder(namespace);
            sbLayerName.append(':');
            sbLayerName.append("fsc-");
            sbLayerName.append(xgeosConfig.getFSC()).append("-c");
            sbLayerName.append(xgeosConfig.getCOMP()).append("-l");
            sbLayerName.append(xgeosConfig.getLEV()).append("-w");
            sbLayerName.append(xgeosConfig.getWEIGHT());
            String layerName = sbLayerName.toString();
            sbLayers.append(layerName);
            if (lgEncoder != null) {
                lgEncoder.addLayer(layerName);
            }
        }
        return sbLayers.toString();
    }
    protected String[] retrieveTargetStoreAllViewNames(Connection connection) {
        try {
            final int TABLE_NAME_COL = 3;
@@ -820,138 +791,6 @@
            logger.warn(e.getMessage(), e);
        }
        return null;
    }
    private void resetFeatureTypesMapping(JobExecutionContext executionContext, GeoServerRESTManager manager)  {
        try {
            GeoServerRESTReader reader = manager.getReader();
            GeoServerRESTPublisher publisher = manager.getPublisher();
            Connection connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            if (!checkCurrentRepositoryStatus(connection, DataReposVersionManager.VSSTATUS_CONFIG)) {
                return;
            }
            List<String> styleList = reader.getStyles().getNames();
            Set<String> styles = new TreeSet<String>(styleList);
            XGeosDataConfigMapping mapping = getConfigMapping();
            HashMap<String, String> defaultStyles = buildDefaultStylesMapping(mapping);
            try {
                String[] dsFTypeNames =  retrieveTargetStoreAllViewNames(connection);
                for (String featureTypeName : dsFTypeNames) {
                    FeatureType featureType = null;
                    try {
                        featureType = targetDataStore.getFeatureSource(featureTypeName, Transaction.AUTO_COMMIT).getSchema();
                    } catch (IOException e) {
                        logger.warn(e.getMessage(), e);
                    }
                    if (featureType == null) continue;
                    RESTLayer layer = reader.getLayer(DEFAULT_NAMESPACE, featureTypeName);
                    if (layer != null) {
                        // publisher.removeLayer(DEFAULT_NAMESPACE, featureTypeName);
                        if (!publisher.unpublishFeatureType(DEFAULT_NAMESPACE, DEFAULT_STORENAME, featureTypeName)) {
                            logger.info("Cannot remove featureType:" + featureTypeName);
                        }
                    }
                    final GSFeatureTypeEncoder fte = new GSFeatureTypeEncoder();
                    // fte.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.REPROJECT_TO_DECLARED);
                    fte.setProjectionPolicy(GSResourceEncoder.ProjectionPolicy.FORCE_DECLARED);
                    fte.addKeyword("KEYWORD");
                    fte.setTitle(featureTypeName);
                    fte.setName(featureTypeName);
                    String srs = "EPSG:" + SRSID_TWD97_ZONE121;
                    if (!this._useZone121) {
                        srs = "EPSG:" + SRSID_TWD97_ZONE119;
                    }
                    fte.setNativeCRS(srs);
                    fte.setSRS(srs); // srs=null?"EPSG:4326":srs);
                    String defaultStyle = getDefaultFeatureTypeStyleId(styles, defaultStyles, featureType);
                    final GSLayerEncoder le = new GSLayerEncoder();
                    le.setDefaultStyle(defaultStyle);
                    if (!publisher.publishDBLayer(DEFAULT_NAMESPACE, DEFAULT_STORENAME, fte, le)) {
                        logger.info("Create Feature Failed. [" + featureTypeName + "]");
                    }
                }
            } finally {
                // if (dataStore != null) dataStore.dispose();
                if (connection != null)
                    JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
            }
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        }
    }
    private void resetGeoServerConfig(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Connection connection = null;
        try {
            connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            transferXGeosVersionStatus(connection, DataReposVersionManager.VSSTATUS_LINKVIEW,
                                       DataReposVersionManager.VSSTATUS_CONFIG, false);
            transferThemesVersionStatus(connection, DataReposVersionManager.VSSTATUS_LINKVIEW,
                DataReposVersionManager.VSSTATUS_CONFIG, false);
            URL geoServerURL = new URL(_geoServerURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(geoServerURL, _geoServerUser, _geoServerPass);
            GeoServerRESTReader reader = manager.getReader();
            List<String> 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(DEFAULT_GEODMMS_NAMESPACE));
            }
            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, manager);
            resetGeoserverWMSConfig(jobExecutionContext, connection, manager, true);
            resetWMSVirtualLayerMapping(jobExecutionContext, connection, manager, true);
            transferXGeosVersionStatus(connection, DataReposVersionManager.VSSTATUS_CONFIG,
                DataReposVersionManager.VSSTATUS_USING, true);
            transferThemesVersionStatus(connection, DataReposVersionManager.VSSTATUS_CONFIG,
                DataReposVersionManager.VSSTATUS_USING, true);
            Date lastUpdate = Calendar.getInstance().getTime();
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
            throw new JobExecutionException("resetGeoServerConfig has error-" + e.getMessage(), e);
        } catch (URISyntaxException e) {
            logger.warn(e.getMessage(), e);
            throw new JobExecutionException("resetGeoServerConfig has error-" + e.getMessage(), e);
        } finally {
            if (connection != null)
                JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
        }
    }
    protected String getDefaultFeatureTypeStyleId(Set<String> styles, HashMap<String, String> defaultStyles, FeatureType featureType) {
@@ -1048,220 +887,5 @@
            return styleName;
        } else
            return defaultStyleName;
    }
    protected void resetGeoserverWMSConfig(JobExecutionContext executionContext, Connection connection,
                                           GeoServerRESTManager manager, boolean masterMode)
        throws JobExecutionException, IOException {
        if ((masterMode) && (!checkCurrentRepositoryStatus(connection, DataReposVersionManager.VSSTATUS_CONFIG))) {
            return;
        }
        GeoServerRESTReader reader = manager.getReader();
        GeoServerRESTPublisher publisher = manager.getPublisher();
        List<String> baseMapNames = reader.getLayers().getNames();
        XGeosDataConfigMapping configMapping = getConfigMapping();
        if (configMapping.getMapping().isEmpty()) {
            logger.warn("XGeosDataConfigMapping is empty! Pleace check XGeosDataConfig file.");
            return;
        }
        LinkedList defaultMapNames = new LinkedList(configMapping.getMapping().keySet());
        if (defaultMapNames.isEmpty()) {
            logger.warn("DefaultMapNames is emptyin XGeosDataConfigMapping! Pleace check XGeosDataConfig file.");
        }
        for (Object key : baseMapNames) {
            String baseMapTitle = (String) key;
            if (configMapping.getMapping().containsKey(baseMapTitle)) {
                int index = defaultMapNames.indexOf(baseMapTitle);
                if (index != -1)
                    defaultMapNames.remove(index);
                List configs = (List) configMapping.getMapping().get(baseMapTitle);
                String defaultLayerNames = buildDefaultWMSLayerNames(DEFAULT_NAMESPACE, configs, null);
                // wmsConfig.getBaseMapLayers().put(baseMapTitle, defaultLayerNames);
                logger.info(baseMapTitle + ":" + defaultLayerNames);
            } else {
                logger.warn(key.toString() + "-lv='" + baseMapTitle + "' cannot found config information in XGeosDataConfigMapping.");
            }
        }
        for (Object key : defaultMapNames) {
            List configs = (List) configMapping.getMapping().get(key);
            GSLayerGroupEncoder lge = new GSLayerGroupEncoder23() {
                @Override
                protected void addToRoot(Element... elements) {
                    for (Element e : elements) {
                        if (e != null) {
                            getRoot().addContent(e.cloneContent());
                        }
                    }
                }
            };
            lge.setBounds("EPSG:3826",293838.061931726,2758423.49415501,311845.457747425,2768966.72993585);
            String defaultLayerNames = buildDefaultWMSLayerNames(DEFAULT_NAMESPACE, configs, lge);
            // logger.info(key + ":" + defaultLayerNames);
            // logger.info(lge.toString());
            // wmsConfig.getBaseMapLayers().put(key, defaultLayerNames);
            String layerGroupName = key.toString();
            RESTLayerGroup layerGroup = reader.getLayerGroup(DEFAULT_NAMESPACE, layerGroupName);
            if (layerGroup == null) {
                if (!publisher.createLayerGroup(DEFAULT_NAMESPACE, layerGroupName, lge)) {
                    logger.warn("Cannot create layergroups:" + layerGroupName + "-" + lge.toString());
                }
            } else {
                publisher.configureLayerGroup(DEFAULT_NAMESPACE, layerGroupName, lge);
            }
        }
    }
    private void resetWMSVirtualLayerMapping(JobExecutionContext jobExecutionContext, Connection connection,
                                             GeoServerRESTManager manager, boolean masterMode)
        throws JobExecutionException, IOException {
        if ((masterMode) && (!checkCurrentRepositoryStatus(connection, DataReposVersionManager.VSSTATUS_CONFIG))) {
            return;
        }
        GeoServerRESTReader reader = manager.getReader();
        List<String> layernames = reader.getLayers().getNames();
        ArrayList<String> baseMapNames = new ArrayList<String>(layernames);
        // Map baseMapLayers = wmsConfig.getBaseMapLayers();
        // Map baseMapEnvelopes = wmsConfig.getBaseMapEnvelopes();
        for (Object key : baseMapNames) {
            String baseMapTitle = (String) key;
            if (baseMapTitle.startsWith("pg")) {
                // GeneralEnvelope envelope = (GeneralEnvelope) baseMapEnvelopes.get(baseMapTitle);
                GeneralEnvelope selectedEnvelope = null;
                // String baseLayersValue = (String) wmsConfig.getBaseMapLayers().get(baseMapTitle);
                RESTLayerGroup layerGroup = reader.getLayerGroup(DEFAULT_NAMESPACE, baseMapTitle);
                String baseLayersValue = layerGroup.getName();
                String[] layerNames = null;
                if (baseLayersValue != null) {
                    layerNames = baseLayersValue.split(",");
                } else {
                    logger.info("vl='" + baseMapTitle + "' is empty value.");
                    continue;
                }
                // RESTLayer layer = reader.getLayer(DEFAULT_NAMESPACE, baseMapTitle);
                // layer.Type.
                ArrayList<String> newLayerNames = new ArrayList<String>();
                for (int i = 0; i < layerNames.length; i++) {
                    String layerName = layerNames[i].trim();
                    newLayerNames.add(layerName);
                    /*
                    Integer layerType = catalog.getLayerType(layerName);
                    if (layerType != null) {
                        newLayerNames.add(layerName);
                        if (layerType.intValue() == MapLayerInfo.TYPE_VECTOR) {
                            FeatureTypeInfo ftype = catalog.getFeatureTypeInfo(layerName);
                            ftype = ((ftype != null) ? ftype
                                : catalog.getFeatureTypeInfo(layerName
                                .substring(layerName.indexOf(":") + 1, layerName.length())));
                            if (selectedEnvelope == null) {
                                ReferencedEnvelope ftEnvelope = null;
                                try {
                                    if (ftype.getBoundingBox() instanceof ReferencedEnvelope
                                        && !ftype.getBoundingBox().isNull()) {
                                        ftEnvelope = ftype.getBoundingBox();
                                    } else {
                                        // TODO Add Action Errors
                                        return;
                                    }
                                } catch (IOException e) {
                                    logger.warn(e.getMessage(), e);
                                    return;
                                }
                                selectedEnvelope = new GeneralEnvelope(new double[]{
                                    ftEnvelope.getMinX(), ftEnvelope.getMinY()
                                },
                                                                       new double[]{ftEnvelope.getMaxX(), ftEnvelope.getMaxY()});
                                selectedEnvelope.setCoordinateReferenceSystem(ftEnvelope
                                    .getCoordinateReferenceSystem());
                            } else {
                                final CoordinateReferenceSystem dstCRS = selectedEnvelope
                                    .getCoordinateReferenceSystem();
                                ReferencedEnvelope ftEnvelope = null;
                                try {
                                    if (ftype.getBoundingBox() instanceof ReferencedEnvelope) {
                                        ftEnvelope = (ReferencedEnvelope) ftype.getBoundingBox();
                                        ftEnvelope.transform(dstCRS, true);
                                    } else {
                                        // TODO Add Action Errors
                                        return;
                                    }
                                } catch (TransformException e) {
                                    logger.warn(e.getMessage(), e);
                                    return;
                                } catch (FactoryException e) {
                                    logger.warn(e.getMessage(), e);
                                    return;
                                } catch (IOException e) {
                                    logger.warn(e.getMessage(), e);
                                    return;
                                }
                                ReferencedEnvelope newEnvelope = new ReferencedEnvelope(dstCRS);
                                newEnvelope.init(selectedEnvelope.getLowerCorner().getOrdinate(0),
                                                 selectedEnvelope.getUpperCorner().getOrdinate(0),
                                                 selectedEnvelope.getLowerCorner().getOrdinate(1),
                                                 selectedEnvelope.getUpperCorner().getOrdinate(1));
                                newEnvelope.expandToInclude(ftEnvelope);
                                selectedEnvelope = new GeneralEnvelope(new double[]{
                                    newEnvelope.getMinX(), newEnvelope.getMinY()
                                },
                                                                       new double[]{newEnvelope.getMaxX(), newEnvelope.getMaxY()});
                                selectedEnvelope.setCoordinateReferenceSystem(dstCRS);
                            }
                        }
                    } else {
                        logger.warn("Cannot found layer " + layerName + " in " + baseMapTitle);
                    }
                    */
                }
                if (layerNames.length != newLayerNames.size()) {
                    StringBuilder layerBuilder = new StringBuilder();
                    boolean bFirst = true;
                    for (String newlayerName : newLayerNames) {
                        if (!bFirst) {
                            layerBuilder.append(',');
                        } else bFirst = false;
                        layerBuilder.append(newlayerName);
                    }
                    // baseMapLayers.put(baseMapTitle, layerBuilder.toString());
                    logger.info(baseMapTitle +":"+ layerBuilder.toString());
                }
                /*
                if (selectedEnvelope != null) {
                    if (envelope != null) {
                        envelope.setCoordinateReferenceSystem(selectedEnvelope
                            .getCoordinateReferenceSystem());
                        envelope.setEnvelope(selectedEnvelope);
                        baseMapEnvelopes.put(baseMapTitle, envelope);
                    } else {
                        baseMapEnvelopes.put(baseMapTitle, selectedEnvelope);
                    }
                }
                */
            }
        }
    }
}