changeset 1026:a39511688679

Added initial version of AnnotationArtifact and friends. flys-artifacts/trunk@2486 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 11 Aug 2011 12:24:31 +0000
parents 02c327ffbad7
children a60d2a46f0a8
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/AnnotationRiverState.java
diffstat 4 files changed, 372 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Aug 10 14:57:12 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Aug 11 12:24:31 2011 +0000
@@ -1,3 +1,12 @@
+2011-08-11  Felix Wolfsteller <felix.wolfsteller@intevation.de>
+
+	Added demo- implementation of a AnnotationArtifact and its Facet.
+
+	* src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java,
+	  src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java,
+	  src/main/java/de/intevation/flys/artifacts/states/AnnotationRiverState.java:
+	  New. Initial version of an AnnotationArtifact and its State and Facet.
+
 2011-08-10  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* doc/conf/datacage.sql: Extended schema for artifacts and collections
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Thu Aug 11 12:24:31 2011 +0000
@@ -0,0 +1,255 @@
+package de.intevation.flys.artifacts;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.Session;
+
+import net.sf.ehcache.Cache;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifactdatabase.ProtocolUtils;
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.artifactdatabase.state.Output;
+import de.intevation.artifactdatabase.state.State;
+import de.intevation.artifactdatabase.state.StateEngine;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+import de.intevation.flys.artifacts.states.DefaultState;
+import de.intevation.flys.artifacts.context.FLYSContext;
+
+import de.intevation.flys.backend.SessionHolder;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.model.Annotation;
+import de.intevation.flys.artifacts.model.AnnotationsFactory;
+
+
+/**
+ * Artifact to access names of Points Of Interest along a segment of a river.
+ */
+public class AnnotationArtifact extends StaticFLYSArtifact {
+
+    /** The logger for this class. */
+    private static Logger logger = Logger.getLogger(WINFOArtifact.class);
+
+    /** The name of the artifact. */
+    public static final String ARTIFACT_NAME = "annotation";
+
+    public static final String CACHE_NAME = "service-distanceinfo";
+
+
+    @Override
+    protected void initialize(Artifact artifact, Object context, CallMeta meta) {
+        logger.debug("Initialize internal state of AnnotationArtifact with: " + artifact.identifier());
+
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+        addData("river", flys.getData("river"));
+
+        List<Facet> fs = new ArrayList<Facet>();
+
+        // TODO Add CallMeta (duplicate TODO in RiverAxisArtifact.java).
+        DefaultState state = (DefaultState) getCurrentState(context);
+        state.computeInit(this, hash(), context, meta, fs);
+
+        if (!fs.isEmpty()) {
+            logger.debug("Facets to add in AnnotationsArtifact.initialize .");
+            facets.put(getCurrentStateId(), fs);
+        }
+        else {
+            logger.debug("No facets to add in AnnotationsArtifact.initialize .");
+        }
+    }
+
+
+    @Override
+    public double[] getDistance() {
+        /** TODO In initialize(), access maximal range of river instead of
+         *       overriding getDistance, important for diagram generation. */
+        return new double[] {0f, 1000f};
+    }
+
+
+    /**
+     * Create the description of this AnnotationArtifact-instance.
+     *
+     * @param data Some data.
+     * @param context The CallContext.
+     *
+     * @return the description of this artifact.
+     */
+    public Document describe(Document data, CallContext context) {
+        logger.debug("Describe: the current state is: " + getCurrentStateId());
+
+        if (logger.isDebugEnabled()) {
+            dumpArtifact();
+        }
+
+        FLYSContext flysContext = getFlysContext(context);
+        StateEngine stateEngine = (StateEngine) flysContext.get(
+            FLYSContext.STATE_ENGINE_KEY);
+
+        Document description            = XMLUtils.newDocument();
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
+            description,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element root = ProtocolUtils.createRootNode(creator);
+        description.appendChild(root);
+
+        State current = getCurrentState(context);
+
+        ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
+        ProtocolUtils.appendState(creator, root, current);
+
+        Element name = ProtocolUtils.createArtNode(
+            creator, "name",
+            new String[] { "value" },
+            new String[] { getName() });
+
+        Element outs = ProtocolUtils.createArtNode(
+            creator, "outputmodes", null, null);
+        appendOutputModes(description, outs, context);
+
+        root.appendChild(name);
+        root.appendChild(outs);
+
+        return description;
+    }
+
+
+    /**
+     * Returns the name of the concrete artifact.
+     *
+     * @return the name of the concrete artifact.
+     */
+    public String getName() {
+        return ARTIFACT_NAME;
+    }
+
+
+    /**
+     * Append outputmode elements to given document.
+     *
+     * @param doc Document to add outputmodes to.
+     * @param outs Element to add outputmode elements to.
+     * @param context The given CallContext (mostly for internationalization).
+     */
+    protected void appendOutputModes(
+        Document    doc,
+        Element     outs,
+        CallContext context)
+    {
+        List<String> stateIds = getPreviousStateIds();
+
+        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        FLYSContext flysContext = getFlysContext(context);
+        StateEngine engine      = (StateEngine) flysContext.get(
+            FLYSContext.STATE_ENGINE_KEY);
+
+        for (String stateId: stateIds) {
+            logger.debug("Append output modes for state: " + stateId);
+            DefaultState state = (DefaultState) engine.getState(stateId);
+
+            List<Output> list = state.getOutputs();
+            if (list == null || list.size() == 0) {
+                logger.debug("-> No output modes for this state.");
+                continue;
+            }
+
+            List<Facet>  fs = facets.get(stateId);
+            if (fs == null || fs.size() == 0) {
+                logger.debug("No facets found.");
+                continue;
+            }
+
+            logger.debug("Found " + fs.size() + " facets in previous states.");
+
+            List<Output> generated = generateOutputs(list, fs);
+
+            ProtocolUtils.appendOutputModes(doc, outs, generated);
+        }
+
+        try {
+            DefaultState cur = (DefaultState) getCurrentState(context);
+            if (cur.validate(this, context)) {
+                List<Output> list = cur.getOutputs();
+                if (list != null && list.size() > 0) {
+                    logger.debug(
+                        "Append output modes for state: " + cur.getID());
+
+                    List<Facet>  fs = facets.get(cur.getID());
+                    if (fs != null && fs.size() > 0) {
+                        List<Output> generated = generateOutputs(list, fs);
+
+                        logger.debug("Found " + fs.size() + " current facets.");
+                        if (!generated.isEmpty()) {
+                            ProtocolUtils.appendOutputModes(
+                                doc, outs, generated);
+                        }
+                        else{
+                            logger.debug("Cannot append output to generated document.");
+                        }
+                    }
+                    else {
+                        logger.debug("No facets found for the current state.");
+                    }
+                }
+            }
+        }
+        catch (IllegalArgumentException iae) {
+            // state is not valid, so we do not append its outputs.
+        }
+    } 
+
+
+    /**
+     * Get Annotations for Points (opposed to segments) in river in range.
+     *
+     * @return list of Annotations.
+     */
+    public List<Annotation> getAnnotations() {
+        // TODO Use Cache of DistanceInfoService.
+        // TODO Use given river.
+        // TODO Query Points (opposed to segments) only.
+        String river = "Saar";
+        logger.debug("Search annotations for river: " /*+ river*/);
+        //Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        List<Annotation> annotations = new ArrayList<Annotation>();
+
+        //if (cache == null) {
+        //    logger.debug("No cache configured for distance info.");
+            //return getUncached(river, filter);
+        //}
+        Session session = SessionHolder.acquire();
+        try {
+            Iterator<Annotation> iter =
+                AnnotationsFactory.getAnnotationsIterator(river);
+            while (iter.hasNext()) {
+                Annotation a = iter.next();
+                logger.debug("Annotation: " + a.getPosition().getValue());
+                annotations.add(a);
+            }
+        } finally {session.close(); SessionHolder.release();}
+        return annotations;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java	Thu Aug 11 12:24:31 2011 +0000
@@ -0,0 +1,45 @@
+package de.intevation.flys.artifacts.model;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.AnnotationArtifact;
+
+import de.intevation.artifactdatabase.state.DefaultFacet;
+
+/**
+ * Facet to access Annotations of a river.
+ */
+public class AnnotationFacet
+extends      DefaultFacet
+{
+    /** Logger for this class. */
+    private static final Logger logger = Logger.getLogger(AnnotationFacet.class);
+
+
+    /**
+     * Trivial Constructor for a AnnotationFacet.
+     *
+     * @param index       Database-Index to use.
+     * @param name        Name (~type) of Facet.
+     * @param description Description of Facet.
+     */
+    public AnnotationFacet(int index, String name, String description) {
+        super(index, name, description);
+    }
+
+
+    /**
+     * Get List of Annotations for river from Artifact.
+     *
+     * @param artifact (Annotation-)Artifact to query for list of Annotations.
+     * @param context  Ignored.
+     */
+    @Override
+    public Object getData(Artifact artifact, CallContext context) {
+        AnnotationArtifact annotationArtifact  = (AnnotationArtifact) artifact;
+        return annotationArtifact.getAnnotations();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/AnnotationRiverState.java	Thu Aug 11 12:24:31 2011 +0000
@@ -0,0 +1,63 @@
+package de.intevation.flys.artifacts.states;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.CallMeta;
+
+import de.intevation.artifactdatabase.state.Facet;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.flys.artifacts.model.AnnotationFacet;
+import de.intevation.flys.artifacts.model.FacetTypes;
+
+import de.intevation.flys.artifacts.resources.Resources;
+
+
+/**
+ * The only state for an AnnotationArtifact (River is known).
+ */
+public class AnnotationRiverState
+extends      DefaultState
+implements   FacetTypes
+{
+    /** Developer-centric description of facet. */
+    public static final String I18N_DESCRIPTION = "facet.longitudinal_section.annotations";
+
+    /** The logger that is used in this state. */
+    private static final Logger logger = Logger.getLogger(AnnotationRiverState.class);
+
+
+    /**
+     * Add an AnnotationFacet to list of Facets.
+     *
+     * @param artifact Ignored.
+     * @param hash Ignored.
+     * @param context Ignored.
+     * @param meta CallMeta to be used for internationalization.
+     * @param facets List to add AnnotationFacet to.
+     *
+     * @return null.
+     */
+    @Override
+    public Object computeInit(
+        FLYSArtifact artifact,
+        String       hash,
+        Object       context,
+        CallMeta     meta,
+        List<Facet>  facets
+    ) {
+        logger.debug("AnnotationRiverState.computeInit()");
+
+        AnnotationFacet facet = new AnnotationFacet(
+            0,
+            LONGITUDINAL_ANNOTATION,
+            Resources.getMsg(meta, I18N_DESCRIPTION, I18N_DESCRIPTION));
+        facets.add(facet);
+
+        return null;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org