comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MINFOArtifact.java @ 2793:6310b1582f2d

merged flys-artifacts/2.7
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:30 +0200
parents b60751cfdd6c
children 2f922be407ea
comparison
equal deleted inserted replaced
2548:ada02bbd3b7f 2793:6310b1582f2d
1 package de.intevation.flys.artifacts;
2
3 import de.intevation.artifactdatabase.ProtocolUtils;
4
5 import de.intevation.artifactdatabase.state.Output;
6 import de.intevation.artifactdatabase.state.State;
7 import de.intevation.artifactdatabase.state.StateEngine;
8
9 import de.intevation.artifactdatabase.transition.TransitionEngine;
10
11 import de.intevation.artifacts.CallContext;
12 import de.intevation.artifacts.Message;
13
14 import de.intevation.artifacts.common.ArtifactNamespaceContext;
15
16 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
17
18 import de.intevation.artifacts.common.utils.XMLUtils;
19
20 import de.intevation.flys.artifacts.context.FLYSContext;
21
22 import de.intevation.flys.artifacts.model.CalculationMessage;
23 import de.intevation.flys.artifacts.model.FacetTypes;
24
25 import de.intevation.flys.artifacts.states.DefaultState;
26 import de.intevation.flys.artifacts.states.SoundingsSelect;
27
28 import de.intevation.flys.utils.FLYSUtils;
29
30 import gnu.trove.TIntArrayList;
31
32 import java.util.LinkedList;
33 import java.util.List;
34
35 import org.apache.log4j.Logger;
36
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
40
41 /**
42 * The default MINFO artifact.
43 *
44 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
45 */
46 public class MINFOArtifact
47 extends FLYSArtifact
48 implements FacetTypes {
49
50 /** The logger for this class. */
51 private static Logger logger = Logger.getLogger(MINFOArtifact.class);
52
53 /** The name of the artifact. */
54 public static final String ARTIFACT_NAME = "minfo";
55
56 /** XPath */
57 public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static";
58
59
60 /**
61 * The default constructor.
62 */
63 public MINFOArtifact() {
64 }
65
66
67 /**
68 * This method returns a description of this artifact.
69 *
70 * @param data Some data.
71 * @param context The CallContext.
72 *
73 * @return the description of this artifact.
74 */
75 public Document describe(Document data, CallContext context) {
76 logger.debug("Describe: the current state is: " + getCurrentStateId());
77
78 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
79 StateEngine stateEngine = (StateEngine) flysContext.get(
80 FLYSContext.STATE_ENGINE_KEY);
81
82 TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
83 FLYSContext.TRANSITION_ENGINE_KEY);
84
85 List<State> reachable = transitionEngine.getReachableStates(
86 this, getCurrentState(context), stateEngine);
87
88 Document description = XMLUtils.newDocument();
89 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
90 description,
91 ArtifactNamespaceContext.NAMESPACE_URI,
92 ArtifactNamespaceContext.NAMESPACE_PREFIX);
93
94 Element root = ProtocolUtils.createRootNode(creator);
95 description.appendChild(root);
96
97 State current = getCurrentState(context);
98
99 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
100 ProtocolUtils.appendState(creator, root, current);
101 ProtocolUtils.appendReachableStates(creator, root, reachable);
102
103 appendBackgroundActivity(creator, root, context);
104
105 Element name = ProtocolUtils.createArtNode(
106 creator, "name",
107 new String[] { "value" },
108 new String[] { getName() });
109
110 Element ui = ProtocolUtils.createArtNode(
111 creator, "ui", null, null);
112
113 Element staticUI = ProtocolUtils.createArtNode(
114 creator, "static", null, null);
115
116 Element outs = ProtocolUtils.createArtNode(
117 creator, "outputmodes", null, null);
118 appendOutputModes(description, outs, context, identifier());
119
120 appendStaticUI(description, staticUI, context, identifier());
121
122 Element dynamic = current.describe(
123 this,
124 description,
125 root,
126 context,
127 identifier());
128
129 if (dynamic != null) {
130 ui.appendChild(dynamic);
131 }
132
133 ui.appendChild(staticUI);
134
135 root.appendChild(name);
136 root.appendChild(ui);
137 root.appendChild(outs);
138
139 return description;
140 }
141
142
143 /**
144 * Returns the name of the concrete artifact.
145 *
146 * @return the name of the concrete artifact.
147 */
148 public String getName() {
149 return ARTIFACT_NAME;
150 }
151
152
153 protected static void appendBackgroundActivity(
154 ElementCreator cr,
155 Element root,
156 CallContext context
157 ) {
158 Element inBackground = cr.create("background-processing");
159 root.appendChild(inBackground);
160
161 cr.addAttr(
162 inBackground,
163 "value",
164 String.valueOf(context.isInBackground()),
165 true);
166
167 LinkedList<Message> messages = context.getBackgroundMessages();
168
169 if (messages == null) {
170 return;
171 }
172
173 CalculationMessage message = (CalculationMessage) messages.getLast();
174 cr.addAttr(
175 inBackground,
176 "steps",
177 String.valueOf(message.getSteps()),
178 true);
179
180 cr.addAttr(
181 inBackground,
182 "currentStep",
183 String.valueOf(message.getCurrentStep()),
184 true);
185
186 inBackground.setTextContent(message.getMessage());
187 }
188
189
190 /**
191 * Append output mode nodes to a document.
192 */
193 protected void appendOutputModes(
194 Document doc,
195 Element outs,
196 CallContext context,
197 String uuid)
198 {
199 List<Output> generated = getOutputs(context);
200 logger.debug("This Artifact has " + generated.size() + " Outputs.");
201
202 ProtocolUtils.appendOutputModes(doc, outs, generated);
203 }
204
205
206 /**
207 * This method appends the static data - that has already been inserted by
208 * the user - to the static node of the DESCRIBE document.
209 *
210 * @param doc The document.
211 * @param ui The root node.
212 * @param context The CallContext.
213 * @param uuid The identifier of the artifact.
214 */
215 protected void appendStaticUI(
216 Document doc,
217 Node ui,
218 CallContext context,
219 String uuid)
220 {
221 List<String> stateIds = getPreviousStateIds();
222
223 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
224 StateEngine engine = (StateEngine) flysContext.get(
225 FLYSContext.STATE_ENGINE_KEY);
226
227 for (String stateId: stateIds) {
228 logger.debug("Append static data for state: " + stateId);
229 DefaultState state = (DefaultState) engine.getState(stateId);
230
231 ui.appendChild(state.describeStatic(this, doc, ui, context, uuid));
232 }
233 }
234
235
236 /**
237 * Determines Facets initial disposition regarding activity (think of
238 * selection in Client ThemeList GUI). This will be checked one time
239 * when the facet enters a collections describe document.
240 *
241 * @param facetName name of the facet.
242 * @param index index of the facet.
243 * @return 0 if not active
244 */
245 @Override
246 public int getInitialFacetActivity(String outputName, String facetName, int index) {
247 logger.warn("MINFOArtifact.getInitialFacetActivity: not implemented!");
248 return 1;
249 }
250
251
252 public int[] getMainChannels() {
253 String data = getDataAsString("main.channel");
254
255 if (data == null) {
256 logger.warn("No 'main.channel' parameter specified!");
257 return null;
258 }
259
260 return FLYSUtils.intArrayFromString(data);
261 }
262
263
264 public int[] getTotalChannels() {
265 String data = getDataAsString("total.channel");
266
267 if (data == null) {
268 logger.warn("No 'total.channel' parameter specified!");
269 return null;
270 }
271
272 return FLYSUtils.intArrayFromString(data);
273 }
274
275
276 public int[] getBedHeightSingleIDs() {
277 String data = getDataAsString("soundings");
278
279 if (data == null) {
280 logger.warn("No 'soundings' parameter specified!");
281 return null;
282 }
283
284 String[] parts = data.split(";");
285
286 TIntArrayList ids = new TIntArrayList();
287
288 for (String part: parts) {
289 if (part.indexOf(SoundingsSelect.PREFIX_SINGLE) >= 0) {
290 String tmp = part.replace(SoundingsSelect.PREFIX_SINGLE, "");
291
292 try {
293 ids.add(Integer.parseInt(tmp));
294 }
295 catch (NumberFormatException nfe) {
296 logger.warn("Cannot parse int from string: '" + tmp + "'");
297 }
298 }
299 }
300
301 return ids.toNativeArray();
302 }
303
304
305 public int[] getBedHeightEpochIDs() {
306 String data = getDataAsString("soundings");
307
308 if (data == null) {
309 logger.warn("No 'soundings' parameter specified!");
310 return null;
311 }
312
313 String[] parts = data.split(";");
314
315 TIntArrayList ids = new TIntArrayList();
316
317 for (String part: parts) {
318 if (part.indexOf(SoundingsSelect.PREFIX_EPOCH) >= 0) {
319 String tmp = part.replace(SoundingsSelect.PREFIX_EPOCH, "");
320
321 try {
322 ids.add(Integer.parseInt(tmp));
323 }
324 catch (NumberFormatException nfe) {
325 logger.warn("Cannot parse int from string: '" + tmp + "'");
326 }
327 }
328 }
329
330 return ids.toNativeArray();
331 }
332 }
333 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org