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