comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 1190:f514894ec2fd

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

http://dive4elements.wald.intevation.org