comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 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
children abd2ab6dff4f
comparison
equal deleted inserted replaced
1025:02c327ffbad7 1026:a39511688679
1 package de.intevation.flys.artifacts;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9
10 import org.apache.log4j.Logger;
11
12 import org.hibernate.Session;
13
14 import net.sf.ehcache.Cache;
15
16 import de.intevation.artifacts.Artifact;
17 import de.intevation.artifacts.ArtifactNamespaceContext;
18 import de.intevation.artifacts.CallContext;
19 import de.intevation.artifacts.CallMeta;
20
21 import de.intevation.artifactdatabase.ProtocolUtils;
22 import de.intevation.artifactdatabase.state.Facet;
23 import de.intevation.artifactdatabase.state.Output;
24 import de.intevation.artifactdatabase.state.State;
25 import de.intevation.artifactdatabase.state.StateEngine;
26
27 import de.intevation.artifacts.common.utils.XMLUtils;
28
29 import de.intevation.flys.artifacts.states.DefaultState;
30 import de.intevation.flys.artifacts.context.FLYSContext;
31
32 import de.intevation.flys.backend.SessionHolder;
33
34 import de.intevation.flys.artifacts.cache.CacheFactory;
35
36 import de.intevation.flys.model.Annotation;
37 import de.intevation.flys.artifacts.model.AnnotationsFactory;
38
39
40 /**
41 * Artifact to access names of Points Of Interest along a segment of a river.
42 */
43 public class AnnotationArtifact extends StaticFLYSArtifact {
44
45 /** The logger for this class. */
46 private static Logger logger = Logger.getLogger(WINFOArtifact.class);
47
48 /** The name of the artifact. */
49 public static final String ARTIFACT_NAME = "annotation";
50
51 public static final String CACHE_NAME = "service-distanceinfo";
52
53
54 @Override
55 protected void initialize(Artifact artifact, Object context, CallMeta meta) {
56 logger.debug("Initialize internal state of AnnotationArtifact with: " + artifact.identifier());
57
58 FLYSArtifact flys = (FLYSArtifact) artifact;
59 addData("river", flys.getData("river"));
60
61 List<Facet> fs = new ArrayList<Facet>();
62
63 // TODO Add CallMeta (duplicate TODO in RiverAxisArtifact.java).
64 DefaultState state = (DefaultState) getCurrentState(context);
65 state.computeInit(this, hash(), context, meta, fs);
66
67 if (!fs.isEmpty()) {
68 logger.debug("Facets to add in AnnotationsArtifact.initialize .");
69 facets.put(getCurrentStateId(), fs);
70 }
71 else {
72 logger.debug("No facets to add in AnnotationsArtifact.initialize .");
73 }
74 }
75
76
77 @Override
78 public double[] getDistance() {
79 /** TODO In initialize(), access maximal range of river instead of
80 * overriding getDistance, important for diagram generation. */
81 return new double[] {0f, 1000f};
82 }
83
84
85 /**
86 * Create the description of this AnnotationArtifact-instance.
87 *
88 * @param data Some data.
89 * @param context The CallContext.
90 *
91 * @return the description of this artifact.
92 */
93 public Document describe(Document data, CallContext context) {
94 logger.debug("Describe: the current state is: " + getCurrentStateId());
95
96 if (logger.isDebugEnabled()) {
97 dumpArtifact();
98 }
99
100 FLYSContext flysContext = getFlysContext(context);
101 StateEngine stateEngine = (StateEngine) flysContext.get(
102 FLYSContext.STATE_ENGINE_KEY);
103
104 Document description = XMLUtils.newDocument();
105 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
106 description,
107 ArtifactNamespaceContext.NAMESPACE_URI,
108 ArtifactNamespaceContext.NAMESPACE_PREFIX);
109
110 Element root = ProtocolUtils.createRootNode(creator);
111 description.appendChild(root);
112
113 State current = getCurrentState(context);
114
115 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
116 ProtocolUtils.appendState(creator, root, current);
117
118 Element name = ProtocolUtils.createArtNode(
119 creator, "name",
120 new String[] { "value" },
121 new String[] { getName() });
122
123 Element outs = ProtocolUtils.createArtNode(
124 creator, "outputmodes", null, null);
125 appendOutputModes(description, outs, context);
126
127 root.appendChild(name);
128 root.appendChild(outs);
129
130 return description;
131 }
132
133
134 /**
135 * Returns the name of the concrete artifact.
136 *
137 * @return the name of the concrete artifact.
138 */
139 public String getName() {
140 return ARTIFACT_NAME;
141 }
142
143
144 /**
145 * Append outputmode elements to given document.
146 *
147 * @param doc Document to add outputmodes to.
148 * @param outs Element to add outputmode elements to.
149 * @param context The given CallContext (mostly for internationalization).
150 */
151 protected void appendOutputModes(
152 Document doc,
153 Element outs,
154 CallContext context)
155 {
156 List<String> stateIds = getPreviousStateIds();
157
158 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
159 doc,
160 ArtifactNamespaceContext.NAMESPACE_URI,
161 ArtifactNamespaceContext.NAMESPACE_PREFIX);
162
163 FLYSContext flysContext = getFlysContext(context);
164 StateEngine engine = (StateEngine) flysContext.get(
165 FLYSContext.STATE_ENGINE_KEY);
166
167 for (String stateId: stateIds) {
168 logger.debug("Append output modes for state: " + stateId);
169 DefaultState state = (DefaultState) engine.getState(stateId);
170
171 List<Output> list = state.getOutputs();
172 if (list == null || list.size() == 0) {
173 logger.debug("-> No output modes for this state.");
174 continue;
175 }
176
177 List<Facet> fs = facets.get(stateId);
178 if (fs == null || fs.size() == 0) {
179 logger.debug("No facets found.");
180 continue;
181 }
182
183 logger.debug("Found " + fs.size() + " facets in previous states.");
184
185 List<Output> generated = generateOutputs(list, fs);
186
187 ProtocolUtils.appendOutputModes(doc, outs, generated);
188 }
189
190 try {
191 DefaultState cur = (DefaultState) getCurrentState(context);
192 if (cur.validate(this, context)) {
193 List<Output> list = cur.getOutputs();
194 if (list != null && list.size() > 0) {
195 logger.debug(
196 "Append output modes for state: " + cur.getID());
197
198 List<Facet> fs = facets.get(cur.getID());
199 if (fs != null && fs.size() > 0) {
200 List<Output> generated = generateOutputs(list, fs);
201
202 logger.debug("Found " + fs.size() + " current facets.");
203 if (!generated.isEmpty()) {
204 ProtocolUtils.appendOutputModes(
205 doc, outs, generated);
206 }
207 else{
208 logger.debug("Cannot append output to generated document.");
209 }
210 }
211 else {
212 logger.debug("No facets found for the current state.");
213 }
214 }
215 }
216 }
217 catch (IllegalArgumentException iae) {
218 // state is not valid, so we do not append its outputs.
219 }
220 }
221
222
223 /**
224 * Get Annotations for Points (opposed to segments) in river in range.
225 *
226 * @return list of Annotations.
227 */
228 public List<Annotation> getAnnotations() {
229 // TODO Use Cache of DistanceInfoService.
230 // TODO Use given river.
231 // TODO Query Points (opposed to segments) only.
232 String river = "Saar";
233 logger.debug("Search annotations for river: " /*+ river*/);
234 //Cache cache = CacheFactory.getCache(CACHE_NAME);
235
236 List<Annotation> annotations = new ArrayList<Annotation>();
237
238 //if (cache == null) {
239 // logger.debug("No cache configured for distance info.");
240 //return getUncached(river, filter);
241 //}
242 Session session = SessionHolder.acquire();
243 try {
244 Iterator<Annotation> iter =
245 AnnotationsFactory.getAnnotationsIterator(river);
246 while (iter.hasNext()) {
247 Annotation a = iter.next();
248 logger.debug("Annotation: " + a.getPosition().getValue());
249 annotations.add(a);
250 }
251 } finally {session.close(); SessionHolder.release();}
252 return annotations;
253 }
254 }
255 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org