ingo@1805: package de.intevation.flys.artifacts;
ingo@1805: 
ingo@1805: import com.vividsolutions.jts.geom.Envelope;
ingo@1805: 
ingo@1805: import de.intevation.artifactdatabase.data.DefaultStateData;
ingo@1805: import de.intevation.artifactdatabase.state.Facet;
ingo@1805: import de.intevation.artifactdatabase.state.State;
christian@4654: import de.intevation.artifacts.Artifact;
christian@4654: import de.intevation.artifacts.ArtifactFactory;
christian@4654: import de.intevation.artifacts.CallMeta;
christian@4654: import de.intevation.artifacts.common.utils.FileTools;
christian@4654: import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet;
ingo@2683: import de.intevation.flys.artifacts.resources.Resources;
ingo@1805: import de.intevation.flys.artifacts.states.DefaultState;
ingo@1854: import de.intevation.flys.utils.FLYSUtils;
rrenkert@4841: import de.intevation.flys.utils.MapUtils;
christian@4654: 
christian@4654: import java.io.File;
christian@4654: import java.util.ArrayList;
christian@4654: import java.util.List;
christian@4654: 
christian@4654: import org.apache.log4j.Logger;
christian@4654: import org.w3c.dom.Document;
ingo@1805: 
ingo@1805: 
ingo@1805: public abstract class WMSDBArtifact extends StaticFLYSArtifact {
ingo@1805: 
ingo@1805:     private static final Logger logger = Logger.getLogger(WMSDBArtifact.class);
ingo@1805: 
ingo@1805:     @Override
ingo@1805:     public void setup(
ingo@1805:         String          identifier,
ingo@1805:         ArtifactFactory factory,
ingo@1805:         Object          context,
ingo@1805:         CallMeta        callMeta,
ingo@1805:         Document        data)
ingo@1805:     {
ingo@1805:         logger.debug("WMSDBArtifact.setup");
ingo@1805: 
ingo@1805:         super.setup(identifier, factory, context, callMeta, data);
ingo@1805: 
felix@2741:         String ids = getDatacageIDValue(data);
ingo@1805: 
ingo@1805:         if (ids != null && ids.length() > 0) {
ingo@1805:             addData("ids", new DefaultStateData("ids", null, null, ids));
ingo@1805:         }
ingo@1805:         else {
ingo@1805:             throw new IllegalArgumentException("No attribute 'ids' found!");
ingo@1805:         }
ingo@1805: 
ingo@1805:         List<Facet> fs = new ArrayList<Facet>();
ingo@1805: 
ingo@1805:         WMSDBState state = (WMSDBState) getCurrentState(context);
ingo@1805:         state.computeInit(this, hash(), context, callMeta, fs);
ingo@1805: 
ingo@1805:         if (!fs.isEmpty()) {
bjoern@4497:             addFacets(getCurrentStateId(), fs);
ingo@1805:         }
ingo@1805:     }
ingo@1805: 
ingo@1805: 
ingo@1805:     @Override
ingo@1805:     protected void initialize(
ingo@1805:         Artifact artifact,
ingo@1805:         Object   context,
ingo@1805:         CallMeta callMeta)
ingo@1805:     {
ingo@1805:         // do nothing
ingo@1805:     }
ingo@1805: 
ingo@1805: 
ingo@3019:     @Override
ingo@3019:     protected State getState(Object context, String stateID) {
ingo@3019:         return getCurrentState(context);
ingo@3019:     }
ingo@3019: 
ingo@3019: 
ingo@1805:     /**
ingo@1805:      * Get a list containing the one and only State.
ingo@1805:      * @param  context ignored.
ingo@1805:      * @return list with one and only state.
ingo@1805:      */
ingo@1805:     @Override
ingo@1805:     protected List<State> getStates(Object context) {
ingo@1805:         ArrayList<State> states = new ArrayList<State>();
ingo@1805:         states.add(getCurrentState(context));
ingo@1805: 
ingo@1805:         return states;
ingo@1805:     }
ingo@1805: 
ingo@1805: 
ingo@1805: 
ingo@1805:     public static abstract class WMSDBState extends DefaultState {
ingo@1805:         private static final Logger logger = Logger.getLogger(WMSDBState.class);
ingo@1805: 
raimund@2089:         protected FLYSArtifact artifact;
ingo@1805: 
ingo@2672:         protected String name;
ingo@2683:         protected int    riverId;
ingo@2672: 
ingo@2672: 
raimund@2082:         public WMSDBState() {}
raimund@2082: 
raimund@2089:         public WMSDBState(FLYSArtifact artifact) {
ingo@1805:             this.artifact = artifact;
ingo@2672:             this.name     = null;
ingo@2683:             this.riverId  = 0;
ingo@1805:         }
ingo@1805: 
ingo@1805:         @Override
ingo@1805:         public Object computeInit(
ingo@1805:             FLYSArtifact artifact,
ingo@1805:             String       hash,
ingo@1805:             Object       context,
ingo@1805:             CallMeta     meta,
ingo@1805:             List<Facet>  facets
ingo@1805:         ) {
ingo@1805:             logger.debug("WMSDBState.computeInit");
ingo@1805: 
ingo@1805:             String type = getFacetType();
ingo@1805: 
ingo@1805:             WMSDBLayerFacet facet = new WMSDBLayerFacet(
ingo@1805:                 0,
ingo@1805:                 type,
ingo@1805:                 getTitle(meta),
ingo@1805:                 ComputeType.INIT,
ingo@1805:                 getID(), hash,
ingo@1805:                 getUrl());
ingo@1805: 
christian@4864:             facet.addLayer(getLayer());
ingo@1805:             facet.setExtent(getExtent());
ingo@3918:             facet.setOriginalExtent(getExtent(true));
ingo@1805:             facet.setSrid(getSrid());
ingo@1805:             facet.setData(getDataString());
ingo@1805:             facet.setFilter(getFilter());
ingo@1816:             facet.setGeometryType(getGeometryType());
rrenkert@4841:             facet.setConnection(MapUtils.getConnection());
rrenkert@4841:             facet.setConnectionType(MapUtils.getConnectionType());
ingo@1876:             facet.setLabelItem(getLabelItem());
ingo@1805: 
ingo@1805:             facets.add(facet);
ingo@1805: 
ingo@1805:             return null;
ingo@1805:         }
ingo@1805: 
ingo@1876:         protected String getLabelItem() {
ingo@1876:             return null;
ingo@1876:         }
ingo@1876: 
ingo@2683:         public int getRiverId() {
ingo@2683:             if (riverId == 0) {
aheinecke@5403:                 String rid = getIdPart(0);
ingo@2683: 
ingo@2683:                 try {
aheinecke@5403:                     riverId = Integer.parseInt(rid);
ingo@2683:                 }
ingo@2683:                 catch (NumberFormatException nfe) {
aheinecke@5403:                     logger.error("Cannot parse river id from '" +
aheinecke@5403:                             artifact.getDataAsString("ids") + "'");
ingo@2683:                 }
ingo@2683:             }
ingo@2683:             return riverId;
ingo@2683:         }
ingo@2683: 
christian@4864:         protected String getLayer() {
christian@4864:             String type = getFacetType();
christian@4864:             String name = type + "-" + artifact.identifier();
christian@4864:             return name;
christian@4864:         }
christian@4864: 
ingo@2672:         /**
ingo@2672:          * Returns the name of the WMS layer. This method extracts the name
ingo@2672:          * from 'ids' data string. It is expected, that the 'ids' string is
ingo@2672:          * seperated by ';' and that the name is placed at index 1.
ingo@2672:          *
ingo@2672:          * @return the name of the WMS layer.
ingo@2672:          */
ingo@2672:         public String getName() {
ingo@2672:             if (name == null) {
aheinecke@5403:                 name = getIdPart(1);
ingo@2672:             }
ingo@2672: 
ingo@2672:             return name;
ingo@2672:         }
ingo@2672: 
aheinecke@5403:         /**
aheinecke@5403:          * Returns a part of the ID string. This method splits the
aheinecke@5403:          * 'ids' data string. It is expected, that the 'ids' string is
aheinecke@5403:          * seperated by ';'.
aheinecke@5403:          *
aheinecke@5403:          * @param number the position of the id data string
aheinecke@5403:          *
aheinecke@5403:          * @return the part of the id string at position number.
aheinecke@5403:          *         Null if number was out of bounds.
aheinecke@5403:          */
aheinecke@5403:         public String getIdPart(int number) {
aheinecke@5403:             String ids = artifact.getDataAsString("ids");
aheinecke@5403: 
aheinecke@5403:             String parts[] = ids != null ? ids.split(";") : null;
aheinecke@5403: 
aheinecke@5403:             if (parts != null && parts.length >= number + 1) {
aheinecke@5403:                 return parts[number];
aheinecke@5403:             }
aheinecke@5403:             return null;
aheinecke@5403:         }
aheinecke@5403: 
ingo@2683: 
ingo@2683:         /**
ingo@2683:          * Returns the name of the layer (returned by getName()) or the layer
ingo@2683:          * type if the name is empty. The layer type is created by an i18n
ingo@2683:          * string of getFacetType().
ingo@2683:          *
ingo@2683:          * @param meta A CallMeta used for i18n.
ingo@2683:          *
ingo@2683:          * @return the name of the layer or its type if name is empty.
ingo@2683:          */
ingo@2683:         protected String getTitle(CallMeta meta) {
ingo@2683:             String name = getName();
ingo@2683: 
ingo@2683:             return name != null && name.length() > 0
ingo@2683:                 ? name
ingo@2683:                 : Resources.getMsg(
ingo@2683:                     meta,
ingo@2683:                     getFacetType(),
ingo@2683:                     getFacetType());
ingo@2683:         }
ingo@2683: 
ingo@2683: 
ingo@3019:         @Override
ingo@3019:         public void endOfLife(Artifact owner, Object context) {
ingo@3019:             logger.info("Destroy WMSDBState: " + getID());
ingo@3019: 
christian@4656:             String p = FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR);
ingo@3019:             File dir = new File(p, owner.identifier());
ingo@3019: 
ingo@3019:             if (dir != null && dir.exists()) {
ingo@3019:                 logger.debug("Try to delete directory '" + dir + "'");
ingo@3019: 
ingo@3019:                 FileTools.deleteRecursive(dir);
ingo@3019:             }
ingo@3019:         }
sascha@3923: 
ingo@3918:         /**
ingo@3918:          * This method returns the extent of a DB layer in the projection of the
ingo@3918:          * database.
sascha@3923:          *
ingo@3918:          * @return the extent of the DB layer in the projection of the database.
ingo@3918:          */
ingo@3918:         protected Envelope getExtent() {
ingo@3918:             return getExtent(false);
ingo@3918:         }
ingo@3019: 
ingo@3019: 
ingo@1805:         protected abstract String getFacetType();
ingo@1805: 
ingo@1805:         protected abstract String getUrl();
ingo@1805: 
ingo@1805:         protected abstract String getSrid();
ingo@1805: 
ingo@3918:         /**
ingo@3918:          * This method returns the extent of the DB layer. The projection of the
ingo@3918:          * extent depends on the <i>reproject</i> parameter. If reproject is set,
ingo@3918:          * the extent is reprojected into the original projection which is
ingo@3918:          * specified in the configuration. Otherwise, the projection of the
ingo@3918:          * database is used.
sascha@3923:          *
ingo@3918:          * @param reproject True, to reproject the extent into the projection
ingo@3918:          * specified in the configuration.
sascha@3923:          *
ingo@3918:          * @return the extent of the database layer.
ingo@3918:          */
ingo@3918:         protected abstract Envelope getExtent(boolean reproject);
ingo@1805: 
ingo@1805:         protected abstract String getFilter();
ingo@1805: 
ingo@1805:         protected abstract String getDataString();
ingo@1816: 
ingo@1816:         protected abstract String getGeometryType();
ingo@1805:     } // end of WMSDBState
ingo@1805: }
ingo@1805: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :