view flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java @ 1925:01f2473e362d

Fixed flys/411 (Oracle max expressions for annotations query). flys-artifacts/trunk@3301 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 22 Nov 2011 14:00:55 +0000
parents 71139016cd0f
children 007a8f5ed9f1
line wrap: on
line source
package de.intevation.flys.artifacts;

import java.util.ArrayList;
import java.util.List;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

import org.w3c.dom.Document;

import org.apache.log4j.Logger;

import org.hibernate.impl.SessionFactoryImpl;

import com.vividsolutions.jts.geom.Envelope;

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactFactory;
import de.intevation.artifacts.CallMeta;

import de.intevation.artifactdatabase.data.DefaultStateData;
import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.State;

import de.intevation.artifacts.common.ArtifactNamespaceContext;
import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.backend.SessionFactoryProvider;

import de.intevation.flys.artifacts.states.DefaultState;
import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
import de.intevation.flys.utils.FLYSUtils;


public abstract class WMSDBArtifact extends StaticFLYSArtifact {

    private static final Logger logger = Logger.getLogger(WMSDBArtifact.class);

    public static final String XPATH_IDS = "/art:action/art:ids/@value";

    public static final Pattern DB_URL_PATTERN =
        Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");


    @Override
    public void setup(
        String          identifier,
        ArtifactFactory factory,
        Object          context,
        CallMeta        callMeta,
        Document        data)
    {
        logger.debug("WMSDBArtifact.setup");

        super.setup(identifier, factory, context, callMeta, data);

        String ids = XMLUtils.xpathString(
            data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE);

        if (ids != null && ids.length() > 0) {
            addData("ids", new DefaultStateData("ids", null, null, ids));
        }
        else {
            throw new IllegalArgumentException("No attribute 'ids' found!");
        }

        List<Facet> fs = new ArrayList<Facet>();

        WMSDBState state = (WMSDBState) getCurrentState(context);
        state.computeInit(this, hash(), context, callMeta, fs);

        if (!fs.isEmpty()) {
            facets.put(getCurrentStateId(), fs);
        }
    }


    @Override
    protected void initialize(
        Artifact artifact,
        Object   context,
        CallMeta callMeta)
    {
        // do nothing
    }


    /**
     * Get a list containing the one and only State.
     * @param  context ignored.
     * @return list with one and only state.
     */
    @Override
    protected List<State> getStates(Object context) {
        ArrayList<State> states = new ArrayList<State>();
        states.add(getCurrentState(context));

        return states;
    }



    public static abstract class WMSDBState extends DefaultState {
        private static final Logger logger = Logger.getLogger(WMSDBState.class);

        protected WMSDBArtifact artifact;

        public WMSDBState(WMSDBArtifact artifact) {
            this.artifact = artifact;
        }

        @Override
        public Object computeInit(
            FLYSArtifact artifact,
            String       hash,
            Object       context,
            CallMeta     meta,
            List<Facet>  facets
        ) {
            logger.debug("WMSDBState.computeInit");

            String type = getFacetType();

            WMSDBLayerFacet facet = new WMSDBLayerFacet(
                0,
                type,
                getTitle(meta),
                ComputeType.INIT,
                getID(), hash,
                getUrl());

            String name = type + "-" + artifact.identifier();

            facet.addLayer(name);
            facet.setExtent(getExtent());
            facet.setSrid(getSrid());
            facet.setData(getDataString());
            facet.setFilter(getFilter());
            facet.setGeometryType(getGeometryType());
            facet.setConnection(getConnection());
            facet.setConnectionType(getConnectionType());
            facet.setLabelItem(getLabelItem());

            facets.add(facet);

            return null;
        }

        /**
         * This method returns a connection string for databases used by
         * Mapserver's Mapfile.
         *
         * @return A connection string for Mapserver.
         */
        protected String getConnection() {
            SessionFactoryImpl sf = (SessionFactoryImpl)
                SessionFactoryProvider.getSessionFactory();

            String user = SessionFactoryProvider.getUser(sf);
            String pass = SessionFactoryProvider.getPass(sf);
            String url  = SessionFactoryProvider.getURL(sf);

            logger.debug("Parse connection url: " + url);

            Matcher m = DB_URL_PATTERN.matcher(url);
            if (!m.matches()) {
                logger.warn("Could not parse Connection string.");
                return null;
            }

            logger.debug("Groups for connection string: " + m.groupCount());
            int groups = m.groupCount();

            for (int i = 0; i <= m.groupCount(); i++) {
                logger.debug("Group " + i + ": " + m.group(i));
            }

            String connection = null;

            if (FLYSUtils.isUsingOracle()) {
                if (groups < 3) {
                    logger.warn("Could only partially parse connection string.");
                    return null;
                }

                String host = m.group(2);
                String port = m.group(3);

                connection = user + "/" + pass + "@" + host;
            }
            else {
                if (groups < 4) {
                    logger.warn("Could only partially parse connection string.");
                    return null;
                }

                String host = m.group(2);
                String port = m.group(3);
                String db   = m.group(4);

                StringBuilder sb = new StringBuilder();
                sb.append("dbname=" + db);
                sb.append("host='" + host + "'");
                sb.append("port=" + port);
                sb.append("password='" + pass + "'");
                sb.append("sslmode=disable");

                connection = sb.toString();
            }

            logger.debug("Created connection: '" + connection + "'");

            return connection;
        }

        protected String getConnectionType() {
            return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
        }

        protected String getLabelItem() {
            return null;
        }

        protected abstract String getFacetType();

        protected abstract String getTitle(CallMeta meta);

        protected abstract String getUrl();

        protected abstract String getSrid();

        protected abstract Envelope getExtent();

        protected abstract String getFilter();

        protected abstract String getDataString();

        protected abstract String getGeometryType();
    } // end of WMSDBState
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org