comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 3806:881fcd01e056

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

http://dive4elements.wald.intevation.org