Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 5642a83420f2 |
children | b1912514e0f5 |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.w3c.dom.Document; | |
7 import org.w3c.dom.Element; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import de.intevation.artifacts.Artifact; | |
12 import de.intevation.artifacts.ArtifactNamespaceContext; | |
13 import de.intevation.artifacts.CallContext; | |
14 import de.intevation.artifacts.CallMeta; | |
15 | |
16 import de.intevation.artifactdatabase.ProtocolUtils; | |
17 import de.intevation.artifactdatabase.state.Facet; | |
18 import de.intevation.artifactdatabase.state.Output; | |
19 import de.intevation.artifactdatabase.state.State; | |
20 import de.intevation.artifactdatabase.state.StateEngine; | |
21 | |
22 import de.intevation.artifacts.common.utils.XMLUtils; | |
23 | |
24 import de.intevation.flys.artifacts.states.DefaultState; | |
25 import de.intevation.flys.artifacts.context.FLYSContext; | |
26 | |
27 import de.intevation.flys.utils.FLYSUtils; | |
28 | |
29 /** | |
30 * Artifact to access names of Points Of Interest along a segment of a river. | |
31 */ | |
32 public class AnnotationArtifact extends StaticFLYSArtifact { | |
33 | |
34 /** The logger for this class. */ | |
35 private static Logger logger = Logger.getLogger(AnnotationArtifact.class); | |
36 | |
37 /** The name of the artifact. */ | |
38 public static final String ARTIFACT_NAME = "annotation"; | |
39 | |
40 /** Get river, setup Facets. */ | |
41 @Override | |
42 protected void initialize(Artifact artifact, Object context, | |
43 CallMeta meta) { | |
44 logger.debug("AnnotationArtifact.initialize, id: " | |
45 + artifact.identifier()); | |
46 | |
47 FLYSArtifact flys = (FLYSArtifact) artifact; | |
48 importData(flys, "river"); | |
49 | |
50 List<Facet> fs = new ArrayList<Facet>(); | |
51 | |
52 // TODO Add CallMeta (duplicate TODO in RiverAxisArtifact.java). | |
53 DefaultState state = (DefaultState) getCurrentState(context); | |
54 state.computeInit(this, hash(), context, meta, fs); | |
55 | |
56 if (!fs.isEmpty()) { | |
57 logger.debug("Facets to add in AnnotationsArtifact.initialize ."); | |
58 facets.put(getCurrentStateId(), fs); | |
59 } | |
60 else { | |
61 logger.debug("No facets to add in AnnotationsArtifact.initialize ."); | |
62 } | |
63 } | |
64 | |
65 | |
66 public double[] getDistance() { | |
67 /** TODO In initialize(), access maximal range of river (via | |
68 * AnnotationFactory) instead of overriding getDistance, | |
69 * important for diagram generation. */ | |
70 return new double[] {0f, 1000f}; | |
71 } | |
72 | |
73 | |
74 /** | |
75 * Create the description of this AnnotationArtifact-instance. | |
76 * | |
77 * @param data Some data. | |
78 * @param context The CallContext. | |
79 * | |
80 * @return the description of this artifact. | |
81 */ | |
82 @Override | |
83 public Document describe(Document data, CallContext context) { | |
84 logger.debug("Describe: the current state is: " + getCurrentStateId()); | |
85 | |
86 if (logger.isDebugEnabled()) { | |
87 dumpArtifact(); | |
88 } | |
89 | |
90 FLYSContext flysContext = FLYSUtils.getFlysContext(context); | |
91 StateEngine stateEngine = (StateEngine) flysContext.get( | |
92 FLYSContext.STATE_ENGINE_KEY); | |
93 | |
94 Document description = XMLUtils.newDocument(); | |
95 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
96 description, | |
97 ArtifactNamespaceContext.NAMESPACE_URI, | |
98 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
99 | |
100 Element root = ProtocolUtils.createRootNode(creator); | |
101 description.appendChild(root); | |
102 | |
103 State current = getCurrentState(context); | |
104 | |
105 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); | |
106 ProtocolUtils.appendState(creator, root, current); | |
107 | |
108 Element name = ProtocolUtils.createArtNode( | |
109 creator, "name", | |
110 new String[] { "value" }, | |
111 new String[] { getName() }); | |
112 | |
113 Element outs = ProtocolUtils.createArtNode( | |
114 creator, "outputmodes", null, null); | |
115 appendOutputModes(description, outs, context); | |
116 | |
117 root.appendChild(name); | |
118 root.appendChild(outs); | |
119 | |
120 return description; | |
121 } | |
122 | |
123 | |
124 /** | |
125 * Returns the name of the concrete artifact. | |
126 * | |
127 * @return the name of the concrete artifact. | |
128 */ | |
129 public String getName() { | |
130 return ARTIFACT_NAME; | |
131 } | |
132 | |
133 | |
134 /** | |
135 * Append outputmode elements to given document. | |
136 * | |
137 * @param doc Document to add outputmodes to. | |
138 * @param outs Element to add outputmode elements to. | |
139 * @param context The given CallContext (mostly for internationalization). | |
140 */ | |
141 //@Override | |
142 protected void appendOutputModes( | |
143 Document doc, | |
144 Element outs, | |
145 CallContext context) | |
146 { | |
147 List<String> stateIds = getPreviousStateIds(); | |
148 | |
149 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
150 doc, | |
151 ArtifactNamespaceContext.NAMESPACE_URI, | |
152 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
153 | |
154 FLYSContext flysContext = FLYSUtils.getFlysContext(context); | |
155 StateEngine engine = (StateEngine) flysContext.get( | |
156 FLYSContext.STATE_ENGINE_KEY); | |
157 | |
158 for (String stateId: stateIds) { | |
159 logger.debug("Append output modes for state: " + stateId); | |
160 DefaultState state = (DefaultState) engine.getState(stateId); | |
161 | |
162 List<Output> list = state.getOutputs(); | |
163 if (list == null || list.size() == 0) { | |
164 logger.debug("-> No output modes for this state."); | |
165 continue; | |
166 } | |
167 | |
168 List<Facet> fs = facets.get(stateId); | |
169 if (fs == null || fs.size() == 0) { | |
170 logger.debug("No facets found."); | |
171 continue; | |
172 } | |
173 | |
174 logger.debug("Found " + fs.size() + " facets in previous states."); | |
175 | |
176 List<Output> generated = generateOutputs(list, fs); | |
177 | |
178 ProtocolUtils.appendOutputModes(doc, outs, generated); | |
179 } | |
180 | |
181 try { | |
182 DefaultState cur = (DefaultState) getCurrentState(context); | |
183 if (cur.validate(this)) { | |
184 List<Output> list = cur.getOutputs(); | |
185 if (list != null && list.size() > 0) { | |
186 logger.debug( | |
187 "Append output modes for state: " + cur.getID()); | |
188 | |
189 List<Facet> fs = facets.get(cur.getID()); | |
190 if (fs != null && fs.size() > 0) { | |
191 List<Output> generated = generateOutputs(list, fs); | |
192 | |
193 logger.debug("Found " + fs.size() + " current facets."); | |
194 if (!generated.isEmpty()) { | |
195 ProtocolUtils.appendOutputModes( | |
196 doc, outs, generated); | |
197 } | |
198 else{ | |
199 logger.debug("Cannot append output to generated document."); | |
200 } | |
201 } | |
202 else { | |
203 logger.debug("No facets found for the current state."); | |
204 } | |
205 } | |
206 } | |
207 } | |
208 catch (IllegalArgumentException iae) { | |
209 // state is not valid, so we do not append its outputs. | |
210 } | |
211 } | |
212 } | |
213 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |