package org.oskari.usercontent;

import feign.FeignException;
import fi.nls.oskari.db.ConnectionInfo;
import fi.nls.oskari.db.DatasourceHelper;
import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;


/**
 * Created by SMAKINEN on 4.9.2015.
 */
public class UserlayerHelper {

    public static final String MODULE_NAME = "userlayer";

    private static final Logger LOG = LogFactory.getLogger(UserlayerHelper.class);

    public static void setupUserlayers(final String srs)
            throws Exception {
        Geoserver geoserver = GeoserverPopulator.getGeoserver(MODULE_NAME);

        // Creating a namespace creates a workspace
        // (with ws you can only give a name, with ns you can also provide the uri)
        Namespace ns = new Namespace();
        try {

            ns.prefix = GeoserverPopulator.NAMESPACE;
            ns.uri = "http://www.oskari.org";
            geoserver.createNamespace(ns);
            LOG.info("Added namespace:", ns);
        } catch (FeignException ex) {
            LOG.error(ex, "Error adding namespace");
        }

        final String storeName = MODULE_NAME;
        try {
            DBDatastore ds = new DBDatastore();
            ds.name = storeName;

            DatasourceHelper helper = DatasourceHelper.getInstance();
            ConnectionInfo info = helper.getPropsForDS(MODULE_NAME);

            ds.connectionParameters.user = info.user;
            ds.connectionParameters.passwd = info.pass;
            ds.connectionParameters.host = info.getHost();
            ds.connectionParameters.port = info.getPort();
            ds.connectionParameters.database = info.getDBName();
            // !! in 2.5.2 namespace = NS PREFIX, in 2.7.1 it needs to be the NS URI!!
            ds.connectionParameters.namespace = ns.uri;
            ds.addEntry("Loose bbox", "true");
            ds.addEntry("Primary key metadata table", "gt_pk_metadata_table");
            //System.out.println(mapper.writeValueAsString(ds));
            geoserver.createDBDatastore(ds, GeoserverPopulator.NAMESPACE);
            LOG.info("Added store:", ds);
        } catch (FeignException ex) {
            LOG.error(ex, "Error adding store");
        }

        // for data modification (WFS) - data
        try {
            FeatureType featureData = new FeatureType();
            featureData.enabled = true;
            featureData.name = "vuser_layer_data";
            GeoserverPopulator.resolveCRS(featureData, srs);

            geoserver.createFeatureType(featureData, GeoserverPopulator.NAMESPACE, storeName);
            LOG.info("Added featuretype:", featureData);
        } catch (FeignException ex) {
            LOG.error(ex, "Error adding featuretype vuser_layer_data");
        }

        // for viewing (WMS) - combination for data/style
        FeatureType featureStyledData = new FeatureType();
        try {
            featureStyledData.enabled = true;
            featureStyledData.name = "user_layer_data_style";
            GeoserverPopulator.resolveCRS(featureStyledData, srs);

            geoserver.createFeatureType(featureStyledData, GeoserverPopulator.NAMESPACE, storeName);
            LOG.info("Added featuretype:", featureStyledData);
        } catch (FeignException ex) {
            LOG.error(ex, "Error adding featuretype user_layer_data_style");
        }
    }
}

@@ -2744,605 +2744,8 @@
    %7D
-%0A%0A        // for viewing (WMS) - combination for data/style%0A        FeatureType featureStyledData = new FeatureType();%0A        try %7B%0A            featureStyledData.enabled = true;%0A            featureStyledData.name = %22user_layer_data_style%22;%0A            GeoserverPopulator.resolveCRS(featureStyledData, srs);%0A%0A            geoserver.createFeatureType(featureStyledData, GeoserverPopulator.NAMESPACE, storeName);%0A            LOG.info(%22Added featuretype:%22, featureStyledData);%0A        %7D catch (FeignException ex) %7B%0A            LOG.error(ex, %22Error adding featuretype user_layer_data_style%22);%0A        %7D
 %0A   

Remove user_layer_data_style layer setup