comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MapArtifact.java @ 2089:0da8874bd378

Added initial state to map artifact to be able to advance and step back. The map artifact overrides describe() to have the complete UI information in the describe response document. flys-artifacts/trunk@3613 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 06 Jan 2012 12:02:10 +0000
parents 007a8f5ed9f1
children 60e3bf470c5b
comparison
equal deleted inserted replaced
2088:701658081f4f 2089:0da8874bd378
3 import org.apache.log4j.Logger; 3 import org.apache.log4j.Logger;
4 4
5 import java.util.List; 5 import java.util.List;
6 6
7 import org.w3c.dom.Document; 7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
8 10
9 import de.intevation.artifacts.ArtifactFactory; 11 import de.intevation.artifacts.ArtifactFactory;
10 import de.intevation.artifacts.CallMeta; 12 import de.intevation.artifacts.CallMeta;
11 import de.intevation.artifacts.CallContext; 13 import de.intevation.artifacts.CallContext;
12 14
13 import de.intevation.artifactdatabase.state.DefaultOutput; 15 import de.intevation.artifacts.common.utils.XMLUtils;
16 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
17 import de.intevation.artifacts.common.ArtifactNamespaceContext;
18
19 import de.intevation.artifactdatabase.ProtocolUtils;
14 import de.intevation.artifactdatabase.state.Facet; 20 import de.intevation.artifactdatabase.state.Facet;
15 import de.intevation.artifactdatabase.state.State; 21 import de.intevation.artifactdatabase.state.State;
16 22 import de.intevation.artifactdatabase.state.StateEngine;
23 import de.intevation.artifactdatabase.state.Output;
24 import de.intevation.artifactdatabase.transition.TransitionEngine;
25
26 import de.intevation.flys.model.River;
27 import de.intevation.flys.utils.FLYSUtils;
28
29 import de.intevation.flys.artifacts.RiverAxisArtifact.RiverAxisState;
30 import de.intevation.flys.artifacts.states.DefaultState;
31 import de.intevation.flys.artifacts.context.FLYSContext;
17 import de.intevation.flys.artifacts.model.WMSDBLayerFacet; 32 import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
18 33
19 import de.intevation.flys.model.River; 34
20 35 public class MapArtifact extends FLYSArtifact {
21 import de.intevation.flys.artifacts.model.FacetTypes;
22 import de.intevation.flys.artifacts.model.RiverFactory;
23 import de.intevation.flys.artifacts.resources.Resources;
24 import de.intevation.flys.utils.FLYSUtils;
25 import de.intevation.flys.utils.GeometryUtils;
26
27
28 public class MapArtifact extends RiverAxisArtifact {
29 36
30 private static final Logger logger = 37 private static final Logger logger =
31 Logger.getLogger(MapArtifact.class); 38 Logger.getLogger(MapArtifact.class);
32 39
33 @Override 40 @Override
38 CallMeta callmeta, 45 CallMeta callmeta,
39 Document data) 46 Document data)
40 { 47 {
41 logger.debug("MapArtifact.setup"); 48 logger.debug("MapArtifact.setup");
42 this.identifier = identifier; 49 this.identifier = identifier;
43 } 50 name = "map";
51
52 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
53
54 List<State> states = getStates(context);
55
56 setCurrentState(states.get(0));
57 }
58
44 59
45 @Override 60 @Override
46 public State getCurrentState(Object cc) { 61 public Document describe(Document data, CallContext context) {
47 logger.debug("MapArtifact.getCurrentState"); 62 logger.debug("Describe: the current state is: " + getCurrentStateId());
48 63
49 State state = new MapState(this); 64 if (logger.isDebugEnabled()) {
50 List<Facet> fs = facets.get(getCurrentStateId()); 65 dumpArtifact();
51 66 }
52 DefaultOutput o = new DefaultOutput( 67
53 "floodmap", 68 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
54 "floodmap", 69
55 "image/png", 70 StateEngine stateEngine = (StateEngine) flysContext.get(
56 fs, 71 FLYSContext.STATE_ENGINE_KEY);
57 "map"); 72
58 73 TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
59 state.getOutputs().add(o); 74 FLYSContext.TRANSITION_ENGINE_KEY);
60 75
61 return state; 76 List<State> reachable = transitionEngine.getReachableStates(
62 } 77 this, getCurrentState(context), stateEngine);
63 78
64 79 Document description = XMLUtils.newDocument();
65 @Override 80 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
66 public String getCurrentStateId() { 81 description,
67 return "state.map.new"; 82 ArtifactNamespaceContext.NAMESPACE_URI,
83 ArtifactNamespaceContext.NAMESPACE_PREFIX);
84
85 Element root = ProtocolUtils.createRootNode(creator);
86 description.appendChild(root);
87
88 State current = getCurrentState(context);
89
90 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
91 ProtocolUtils.appendState(creator, root, current);
92 ProtocolUtils.appendReachableStates(creator, root, reachable);
93
94 appendBackgroundActivity(creator, root, context);
95
96 Element name = ProtocolUtils.createArtNode(
97 creator, "name",
98 new String[] { "value" },
99 new String[] { getName() });
100
101 Element ui = ProtocolUtils.createArtNode(
102 creator, "ui", null, null);
103
104 Element staticUI = ProtocolUtils.createArtNode(
105 creator, "static", null, null);
106
107 Element outs = ProtocolUtils.createArtNode(
108 creator, "outputmodes", null, null);
109 appendOutputModes(description, outs, context, identifier());
110
111 appendStaticUI(description, staticUI, context, identifier());
112
113 Element dynamic = current.describe(
114 this,
115 description,
116 root,
117 context,
118 identifier());
119
120 if (dynamic != null) {
121 ui.appendChild(dynamic);
122 }
123
124 ui.appendChild(staticUI);
125
126 root.appendChild(name);
127 root.appendChild(ui);
128 root.appendChild(outs);
129
130 return description;
131
132 }
133
134
135 protected static void appendBackgroundActivity(
136 ElementCreator cr,
137 Element root,
138 CallContext context
139 ) {
140 Element inBackground = cr.create("background-processing");
141 root.appendChild(inBackground);
142
143 cr.addAttr(
144 inBackground,
145 "value",
146 String.valueOf(context.isInBackground()),
147 true);
148 }
149
150
151 /**
152 * Append output mode nodes to a document.
153 */
154 protected void appendOutputModes(
155 Document doc,
156 Element outs,
157 CallContext context,
158 String uuid)
159 {
160 List<String> stateIds = getPreviousStateIds();
161
162 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
163 doc,
164 ArtifactNamespaceContext.NAMESPACE_URI,
165 ArtifactNamespaceContext.NAMESPACE_PREFIX);
166
167 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
168 StateEngine engine = (StateEngine) flysContext.get(
169 FLYSContext.STATE_ENGINE_KEY);
170
171 for (String stateId: stateIds) {
172 logger.debug("Append output modes for state: " + stateId);
173 DefaultState state = (DefaultState) engine.getState(stateId);
174
175 List<Output> list = state.getOutputs();
176 if (list == null || list.size() == 0) {
177 logger.debug("-> No output modes for this state.");
178 continue;
179 }
180
181 List<Facet> fs = facets.get(stateId);
182
183 if (fs == null || fs.size() == 0) {
184 logger.debug("No facets for previous state found.");
185 continue;
186 }
187
188 logger.debug("Found " + fs.size() + " facets in previous states.");
189
190 List<Output> generated = generateOutputs(list, fs);
191
192 ProtocolUtils.appendOutputModes(doc, outs, generated);
193 }
194
195 try {
196 DefaultState cur = (DefaultState) getCurrentState(context);
197 if (cur.validate(this)) {
198 List<Output> list = cur.getOutputs();
199 if (list != null && list.size() > 0) {
200 logger.debug(
201 "Append output modes for current state: " + cur.getID());
202
203 List<Facet> fs = facets.get(cur.getID());
204
205 if (fs != null && fs.size() > 0) {
206 List<Output> generated = generateOutputs(list, fs);
207
208 logger.debug("Found " + fs.size() + " current facets.");
209 if (!generated.isEmpty()) {
210 ProtocolUtils.appendOutputModes(
211 doc, outs, generated);
212 }
213 }
214 else {
215 logger.debug("No facets found for the current state.");
216 }
217 }
218 }
219 }
220 catch (IllegalArgumentException iae) {
221 // state is not valid, so we do not append its outputs.
222 }
223 }
224
225
226 /**
227 * This method appends the static data - that has already been inserted by
228 * the user - to the static node of the DESCRIBE document.
229 *
230 * @param doc The document.
231 * @param ui The root node.
232 * @param context The CallContext.
233 * @param uuid The identifier of the artifact.
234 */
235 protected void appendStaticUI(
236 Document doc,
237 Node ui,
238 CallContext context,
239 String uuid)
240 {
241 List<String> stateIds = getPreviousStateIds();
242
243 logger.debug("previous states: " + stateIds);
244 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
245 StateEngine engine = (StateEngine) flysContext.get(
246 FLYSContext.STATE_ENGINE_KEY);
247
248 for (String stateId: stateIds) {
249 logger.debug("Append static data for state: " + stateId);
250 DefaultState state = (DefaultState) engine.getState(stateId);
251
252 ui.appendChild(state.describeStatic(this, doc, ui, context, uuid));
253 }
68 } 254 }
69 255
70 256
71 public static class MapState extends RiverAxisState { 257 public static class MapState extends RiverAxisState {
72 258
73 public MapState() {
74 }
75
76 public MapState(MapArtifact artifact) {
77 super(artifact);
78 this.id = "state.map.new";
79 }
80
81 @Override 259 @Override
82 public Object computeInit( 260 public Object computeAdvance(
83 FLYSArtifact artifact,
84 String hash,
85 Object context,
86 CallMeta meta,
87 List<Facet> facets)
88 {
89 return null;
90 }
91
92 @Override
93 public Object computeFeed(
94 FLYSArtifact artifact, 261 FLYSArtifact artifact,
95 String hash, 262 String hash,
96 CallContext context, 263 CallContext context,
97 List<Facet> facets, 264 List<Facet> facets,
98 Object old) 265 Object old)
99 { 266 {
100 logger.debug("MapState.computeFeed"); 267 logger.debug("MapState.computeAdvance");
268
269 this.artifact = artifact;
101 270
102 String type = getFacetType(); 271 String type = getFacetType();
103 272
104 WMSDBLayerFacet facet = new WMSDBLayerFacet( 273 WMSDBLayerFacet facet = new WMSDBLayerFacet(
105 0, 274 0,
106 type, 275 type,
107 getTitle(context.getMeta()), 276 getTitle(context.getMeta()),
108 ComputeType.FEED, 277 ComputeType.ADVANCE,
109 getID(), hash, 278 getID(), hash,
110 getUrl()); 279 getUrl());
111 280
112 String name = type + "-" + artifact.identifier(); 281 String name = type + "-" + artifact.identifier();
113 282

http://dive4elements.wald.intevation.org