comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/ChartArtifact.java @ 2113:d109e7702995

Initial implementation for module 'new chart'. flys-artifacts/trunk@3677 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 13 Jan 2012 12:19:53 +0000
parents
children f021080cb409
comparison
equal deleted inserted replaced
2112:2d0b22602fc2 2113:d109e7702995
1 package de.intevation.flys.artifacts;
2
3 import org.apache.log4j.Logger;
4
5 import java.util.List;
6
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10
11 import de.intevation.artifacts.Artifact;
12
13 import de.intevation.artifacts.ArtifactFactory;
14 import de.intevation.artifacts.CallMeta;
15 import de.intevation.artifacts.CallContext;
16
17 import de.intevation.artifacts.common.utils.XMLUtils;
18 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
19 import de.intevation.artifacts.common.ArtifactNamespaceContext;
20
21 import de.intevation.artifactdatabase.ProtocolUtils;
22 import de.intevation.artifactdatabase.state.Facet;
23 import de.intevation.artifactdatabase.state.State;
24 import de.intevation.artifactdatabase.state.StateEngine;
25 import de.intevation.artifactdatabase.state.Output;
26 import de.intevation.artifactdatabase.transition.TransitionEngine;
27
28 import de.intevation.flys.model.River;
29 import de.intevation.flys.utils.FLYSUtils;
30
31 import de.intevation.flys.artifacts.states.DefaultState;
32 import de.intevation.flys.artifacts.context.FLYSContext;
33 import de.intevation.flys.artifacts.resources.Resources;
34
35
36 public class ChartArtifact extends FLYSArtifact {
37
38 private static final Logger logger =
39 Logger.getLogger(ChartArtifact.class);
40
41 @Override
42 public void setup(
43 String identifier,
44 ArtifactFactory factory,
45 Object context,
46 CallMeta callmeta,
47 Document data)
48 {
49 logger.debug("ChartArtifact.setup");
50 this.identifier = identifier;
51 name = "new_chart";
52
53 List<State> states = getStates(context);
54
55 setCurrentState(states.get(0));
56 }
57
58
59 @Override
60 public Document describe(Document data, CallContext context) {
61 logger.debug("Describe: the current state is: " + getCurrentStateId());
62
63 if (logger.isDebugEnabled()) {
64 dumpArtifact();
65 }
66
67 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
68
69 StateEngine stateEngine = (StateEngine) flysContext.get(
70 FLYSContext.STATE_ENGINE_KEY);
71
72 TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
73 FLYSContext.TRANSITION_ENGINE_KEY);
74
75 List<State> reachable = transitionEngine.getReachableStates(
76 this, getCurrentState(context), stateEngine);
77
78 Document description = XMLUtils.newDocument();
79 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
80 description,
81 ArtifactNamespaceContext.NAMESPACE_URI,
82 ArtifactNamespaceContext.NAMESPACE_PREFIX);
83
84 Element root = ProtocolUtils.createRootNode(creator);
85 description.appendChild(root);
86
87 State current = getCurrentState(context);
88
89 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
90 ProtocolUtils.appendState(creator, root, current);
91 ProtocolUtils.appendReachableStates(creator, root, reachable);
92
93 appendBackgroundActivity(creator, root, context);
94
95 Element name = ProtocolUtils.createArtNode(
96 creator, "name",
97 new String[] { "value" },
98 new String[] { getName() });
99
100 Element ui = ProtocolUtils.createArtNode(
101 creator, "ui", null, null);
102
103 Element staticUI = ProtocolUtils.createArtNode(
104 creator, "static", null, null);
105
106 Element outs = ProtocolUtils.createArtNode(
107 creator, "outputmodes", null, null);
108 appendOutputModes(description, outs, context, identifier());
109
110 appendStaticUI(description, staticUI, context, identifier());
111
112 Element dynamic = current.describe(
113 this,
114 description,
115 root,
116 context,
117 identifier());
118
119 if (dynamic != null) {
120 ui.appendChild(dynamic);
121 }
122
123 ui.appendChild(staticUI);
124
125 root.appendChild(name);
126 root.appendChild(ui);
127 root.appendChild(outs);
128
129 return description;
130
131 }
132
133
134 protected static void appendBackgroundActivity(
135 ElementCreator cr,
136 Element root,
137 CallContext context
138 ) {
139 Element inBackground = cr.create("background-processing");
140 root.appendChild(inBackground);
141
142 cr.addAttr(
143 inBackground,
144 "value",
145 String.valueOf(context.isInBackground()),
146 true);
147 }
148
149
150 /**
151 * Append output mode nodes to a document.
152 */
153 protected void appendOutputModes(
154 Document doc,
155 Element outs,
156 CallContext context,
157 String uuid)
158 {
159 List<String> stateIds = getPreviousStateIds();
160
161 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
162 doc,
163 ArtifactNamespaceContext.NAMESPACE_URI,
164 ArtifactNamespaceContext.NAMESPACE_PREFIX);
165
166 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
167 StateEngine engine = (StateEngine) flysContext.get(
168 FLYSContext.STATE_ENGINE_KEY);
169
170 for (String stateId: stateIds) {
171 logger.debug("Append output modes for state: " + stateId);
172 DefaultState state = (DefaultState) engine.getState(stateId);
173
174 List<Output> list = state.getOutputs();
175 if (list == null || list.size() == 0) {
176 logger.debug("-> No output modes for this state.");
177 continue;
178 }
179
180 List<Facet> fs = facets.get(stateId);
181
182 if (fs == null || fs.size() == 0) {
183 logger.debug("No facets for previous state found.");
184 continue;
185 }
186
187 logger.debug("Found " + fs.size() + " facets in previous states.");
188
189 List<Output> generated = generateOutputs(list, fs);
190
191 ProtocolUtils.appendOutputModes(doc, outs, generated);
192 }
193
194 try {
195 DefaultState cur = (DefaultState) getCurrentState(context);
196 if (cur.validate(this)) {
197 List<Output> list = cur.getOutputs();
198 if (list != null && list.size() > 0) {
199 logger.debug(
200 "Append output modes for current state: " + cur.getID());
201
202 List<Facet> fs = facets.get(cur.getID());
203
204 if (fs != null && fs.size() > 0) {
205 List<Output> generated = generateOutputs(list, fs);
206
207 logger.debug("Found " + fs.size() + " current facets.");
208 if (!generated.isEmpty()) {
209 ProtocolUtils.appendOutputModes(
210 doc, outs, generated);
211 }
212 }
213 else {
214 logger.debug("No facets found for the current state.");
215 }
216 }
217 }
218 }
219 catch (IllegalArgumentException iae) {
220 // state is not valid, so we do not append its outputs.
221 }
222 }
223
224
225 /**
226 * This method appends the static data - that has already been inserted by
227 * the user - to the static node of the DESCRIBE document.
228 *
229 * @param doc The document.
230 * @param ui The root node.
231 * @param context The CallContext.
232 * @param uuid The identifier of the artifact.
233 */
234 protected void appendStaticUI(
235 Document doc,
236 Node ui,
237 CallContext context,
238 String uuid)
239 {
240 List<String> stateIds = getPreviousStateIds();
241
242 logger.debug("previous states: " + stateIds);
243 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
244 StateEngine engine = (StateEngine) flysContext.get(
245 FLYSContext.STATE_ENGINE_KEY);
246
247 for (String stateId: stateIds) {
248 logger.debug("Append static data for state: " + stateId);
249 DefaultState state = (DefaultState) engine.getState(stateId);
250
251 ui.appendChild(state.describeStatic(this, doc, ui, context, uuid));
252 }
253 }
254
255
256 public static class ChartState extends DefaultState {
257
258 public static final String FIELD_MODE = "chart_type";
259
260 public static final String DURATION_CURVE =
261 "chart.new.durationcurve";
262
263 public static final String COMPUTED_DISCHARGE_CURVE =
264 "chart.new.computeddischargecurve";
265
266 public static final String DISCHARGE_LONGITUDINAL_CURVE =
267 "chart.new.discharge_longitudinal_section";
268
269 public static final String W_DIFFERENCES =
270 "chart.new.w_differences";
271
272 public static final String WATERLEVEL =
273 "chart.new.waterlevel";
274
275 public static final String[] CHARTS = {
276 COMPUTED_DISCHARGE_CURVE,
277 DURATION_CURVE,
278 DISCHARGE_LONGITUDINAL_CURVE,
279 W_DIFFERENCES,
280 WATERLEVEL };
281
282
283
284 @Override
285 public Object computeAdvance(
286 FLYSArtifact artifact,
287 String hash,
288 CallContext context,
289 List<Facet> facets,
290 Object old)
291 {
292 logger.debug("ChartState.computeAdvance");
293
294
295 return null;
296 }
297
298
299 @Override
300 protected Element[] createItems(
301 XMLUtils.ElementCreator cr,
302 Artifact artifact,
303 String name,
304 CallContext context)
305 {
306 CallMeta meta = context.getMeta();
307 Element[] charts = new Element[CHARTS.length];
308
309 int i = 0;
310
311 for (String chart: CHARTS) {
312 charts[i++] = createItem(
313 cr, new String[] {
314 Resources.getMsg(meta, chart, chart),
315 chart
316 });
317 }
318
319 return charts;
320 }
321
322
323 }
324 }

http://dive4elements.wald.intevation.org