Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MapArtifact.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 | 60e3bf470c5b |
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.ArtifactFactory; | |
12 import de.intevation.artifacts.CallMeta; | |
13 import de.intevation.artifacts.CallContext; | |
14 | |
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; | |
20 import de.intevation.artifactdatabase.state.Facet; | |
21 import de.intevation.artifactdatabase.state.State; | |
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; | |
32 import de.intevation.flys.artifacts.model.WMSDBLayerFacet; | |
33 | |
34 | |
35 public class MapArtifact extends FLYSArtifact { | |
36 | |
37 private static final Logger logger = | |
38 Logger.getLogger(MapArtifact.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("MapArtifact.setup"); | |
49 this.identifier = identifier; | |
50 name = "new_map"; | |
51 | |
52 FLYSContext flysContext = FLYSUtils.getFlysContext(context); | |
53 | |
54 List<State> states = getStates(context); | |
55 | |
56 setCurrentState(states.get(0)); | |
57 } | |
58 | |
59 | |
60 @Override | |
61 public Document describe(Document data, CallContext context) { | |
62 logger.debug("Describe: the current state is: " + getCurrentStateId()); | |
63 | |
64 if (logger.isDebugEnabled()) { | |
65 dumpArtifact(); | |
66 } | |
67 | |
68 FLYSContext flysContext = FLYSUtils.getFlysContext(context); | |
69 | |
70 StateEngine stateEngine = (StateEngine) flysContext.get( | |
71 FLYSContext.STATE_ENGINE_KEY); | |
72 | |
73 TransitionEngine transitionEngine = (TransitionEngine) flysContext.get( | |
74 FLYSContext.TRANSITION_ENGINE_KEY); | |
75 | |
76 List<State> reachable = transitionEngine.getReachableStates( | |
77 this, getCurrentState(context), stateEngine); | |
78 | |
79 Document description = XMLUtils.newDocument(); | |
80 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
81 description, | |
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 } | |
254 } | |
255 | |
256 | |
257 public static class MapState extends RiverAxisState { | |
258 | |
259 @Override | |
260 public Object computeAdvance( | |
261 FLYSArtifact artifact, | |
262 String hash, | |
263 CallContext context, | |
264 List<Facet> facets, | |
265 Object old) | |
266 { | |
267 logger.debug("MapState.computeAdvance"); | |
268 | |
269 this.artifact = artifact; | |
270 | |
271 String type = getFacetType(); | |
272 | |
273 WMSDBLayerFacet facet = new WMSDBLayerFacet( | |
274 0, | |
275 type, | |
276 getTitle(context.getMeta()), | |
277 ComputeType.ADVANCE, | |
278 getID(), hash, | |
279 getUrl()); | |
280 | |
281 String name = type + "-" + artifact.identifier(); | |
282 | |
283 facet.addLayer(name); | |
284 facet.setExtent(getExtent()); | |
285 facet.setSrid(getSrid()); | |
286 facet.setData(getDataString()); | |
287 facet.setFilter(getFilter()); | |
288 facet.setGeometryType(getGeometryType()); | |
289 facet.setConnection(getConnection()); | |
290 facet.setConnectionType(getConnectionType()); | |
291 facet.setLabelItem(getLabelItem()); | |
292 | |
293 facets.add(facet); | |
294 | |
295 return null; | |
296 } | |
297 | |
298 @Override | |
299 public int getRiverId() { | |
300 River r = FLYSUtils.getRiver(artifact); | |
301 int riverId = r.getId(); | |
302 | |
303 return riverId; | |
304 } | |
305 } | |
306 } |