Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/AnnotationArtifact.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/AnnotationArtifact.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.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 org.dive4elements.artifacts.Artifact; | |
12 import org.dive4elements.artifacts.ArtifactFactory; | |
13 import org.dive4elements.artifacts.ArtifactNamespaceContext; | |
14 import org.dive4elements.artifacts.CallContext; | |
15 import org.dive4elements.artifacts.CallMeta; | |
16 | |
17 import org.dive4elements.artifactdatabase.ProtocolUtils; | |
18 import org.dive4elements.artifactdatabase.state.Facet; | |
19 import org.dive4elements.artifactdatabase.state.FacetActivity; | |
20 import org.dive4elements.artifactdatabase.state.Output; | |
21 import org.dive4elements.artifactdatabase.state.State; | |
22 import org.dive4elements.artifactdatabase.state.StateEngine; | |
23 | |
24 import org.dive4elements.river.artifacts.model.FacetTypes; | |
25 | |
26 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
27 | |
28 import org.dive4elements.river.artifacts.states.DefaultState; | |
29 import org.dive4elements.river.artifacts.context.FLYSContext; | |
30 | |
31 import org.dive4elements.river.utils.FLYSUtils; | |
32 | |
33 /** | |
34 * Artifact to access names of Points Of Interest along a segment of a river. | |
35 */ | |
36 public class AnnotationArtifact | |
37 extends StaticFLYSArtifact | |
38 implements FacetTypes { | |
39 | |
40 /** The logger for this class. */ | |
41 private static Logger logger = Logger.getLogger(AnnotationArtifact.class); | |
42 | |
43 /** The name of the artifact. */ | |
44 public static final String ARTIFACT_NAME = "annotation"; | |
45 | |
46 // Let Annotations enter life inactively if in Fix Analysis LS setting. | |
47 static { | |
48 FacetActivity.Registry.getInstance().register( | |
49 "annotation", | |
50 new FacetActivity() { | |
51 @Override | |
52 public Boolean isInitialActive( | |
53 Artifact artifact, | |
54 Facet facet, | |
55 String output | |
56 ) { | |
57 if (output.contains(FacetTypes.ChartType.FLSC.toString())) { | |
58 // Longitudinal section chart | |
59 String name = facet.getName(); | |
60 | |
61 if (name.contains(FacetTypes.LONGITUDINAL_ANNOTATION)) { | |
62 return Boolean.FALSE; | |
63 } | |
64 } | |
65 | |
66 return Boolean.TRUE; | |
67 } | |
68 }); | |
69 } | |
70 | |
71 | |
72 @Override | |
73 public void setup( | |
74 String identifier, | |
75 ArtifactFactory factory, | |
76 Object context, | |
77 CallMeta callMeta, | |
78 Document data) | |
79 { | |
80 logger.debug("AnnotationArtifact.setup"); | |
81 String filter = StaticFLYSArtifact.getDatacageIDValue(data); | |
82 String[] splits = filter.split(":"); | |
83 if (splits.length > 1) { | |
84 addStringData("nameFilter", splits[1]); | |
85 } | |
86 super.setup(identifier, factory, context, callMeta, data); | |
87 } | |
88 | |
89 | |
90 /** Get river, setup Facets. */ | |
91 @Override | |
92 protected void initialize(Artifact artifact, Object context, | |
93 CallMeta meta) { | |
94 logger.debug("AnnotationArtifact.initialize, id: " | |
95 + artifact.identifier()); | |
96 | |
97 FLYSArtifact flys = (FLYSArtifact) artifact; | |
98 // TODO issue880: make annotations available _per type_ | |
99 importData(flys, "river"); | |
100 | |
101 List<Facet> fs = new ArrayList<Facet>(); | |
102 | |
103 // TODO Add CallMeta (duplicate TODO in RiverAxisArtifact.java). | |
104 DefaultState state = (DefaultState) getCurrentState(context); | |
105 state.computeInit(this, hash(), context, meta, fs); | |
106 | |
107 if (!fs.isEmpty()) { | |
108 logger.debug("Facets to add in AnnotationsArtifact.initialize ."); | |
109 addFacets(getCurrentStateId(), fs); | |
110 } | |
111 else { | |
112 logger.debug("No facets to add in AnnotationsArtifact.initialize ."); | |
113 } | |
114 } | |
115 | |
116 | |
117 /** Shortcut to nameFilter-data (TODO: move to respective Access). */ | |
118 public String getFilterName() { | |
119 return getDataAsString("nameFilter"); | |
120 } | |
121 | |
122 | |
123 public double[] getDistance() { | |
124 /** TODO In initialize(), access maximal range of river (via | |
125 * AnnotationFactory) instead of overriding getDistance, | |
126 * important for diagram generation. */ | |
127 return new double[] {0f, 1000f}; | |
128 } | |
129 | |
130 | |
131 /** | |
132 * Create the description of this AnnotationArtifact-instance. | |
133 * | |
134 * @param data Some data. | |
135 * @param context The CallContext. | |
136 * | |
137 * @return the description of this artifact. | |
138 */ | |
139 @Override | |
140 public Document describe(Document data, CallContext context) { | |
141 logger.debug("Describe: the current state is: " + getCurrentStateId()); | |
142 | |
143 if (logger.isDebugEnabled()) { | |
144 dumpArtifact(); | |
145 } | |
146 | |
147 Document description = XMLUtils.newDocument(); | |
148 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
149 description, | |
150 ArtifactNamespaceContext.NAMESPACE_URI, | |
151 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
152 | |
153 Element root = ProtocolUtils.createRootNode(creator); | |
154 description.appendChild(root); | |
155 | |
156 State current = getCurrentState(context); | |
157 | |
158 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); | |
159 ProtocolUtils.appendState(creator, root, current); | |
160 | |
161 Element name = ProtocolUtils.createArtNode( | |
162 creator, "name", | |
163 new String[] { "value" }, | |
164 new String[] { getName() }); | |
165 | |
166 Element outs = ProtocolUtils.createArtNode( | |
167 creator, "outputmodes", null, null); | |
168 appendOutputModes(description, outs, context); | |
169 | |
170 root.appendChild(name); | |
171 root.appendChild(outs); | |
172 | |
173 return description; | |
174 } | |
175 | |
176 | |
177 /** | |
178 * Returns the name of the concrete artifact. | |
179 * | |
180 * @return the name of the concrete artifact. | |
181 */ | |
182 public String getName() { | |
183 return ARTIFACT_NAME; | |
184 } | |
185 | |
186 | |
187 /** | |
188 * Append outputmode elements to given document. | |
189 * | |
190 * @param doc Document to add outputmodes to. | |
191 * @param outs Element to add outputmode elements to. | |
192 * @param context The given CallContext (mostly for internationalization). | |
193 */ | |
194 //@Override | |
195 protected void appendOutputModes( | |
196 Document doc, | |
197 Element outs, | |
198 CallContext context) | |
199 { | |
200 List<String> stateIds = getPreviousStateIds(); | |
201 | |
202 FLYSContext flysContext = FLYSUtils.getFlysContext(context); | |
203 StateEngine engine = (StateEngine) flysContext.get( | |
204 FLYSContext.STATE_ENGINE_KEY); | |
205 | |
206 for (String stateId: stateIds) { | |
207 logger.debug("Append output modes for state: " + stateId); | |
208 DefaultState state = (DefaultState) engine.getState(stateId); | |
209 | |
210 List<Output> list = state.getOutputs(); | |
211 if (list == null || list.isEmpty()) { | |
212 logger.debug("-> No output modes for this state."); | |
213 continue; | |
214 } | |
215 | |
216 List<Facet> fs = getFacets(stateId); | |
217 if (fs == null || fs.isEmpty()) { | |
218 logger.debug("No facets found."); | |
219 continue; | |
220 } | |
221 | |
222 logger.debug("Found " + fs.size() + " facets in previous states."); | |
223 | |
224 List<Output> generated = generateOutputs(list, fs); | |
225 | |
226 ProtocolUtils.appendOutputModes(doc, outs, generated); | |
227 } | |
228 | |
229 try { | |
230 DefaultState cur = (DefaultState) getCurrentState(context); | |
231 if (cur.validate(this)) { | |
232 List<Output> list = cur.getOutputs(); | |
233 if (list != null && list.size() > 0) { | |
234 logger.debug( | |
235 "Append output modes for state: " + cur.getID()); | |
236 | |
237 List<Facet> fs = getFacets(cur.getID()); | |
238 if (fs != null && fs.size() > 0) { | |
239 List<Output> generated = generateOutputs(list, fs); | |
240 | |
241 logger.debug("Found " + fs.size() + " current facets."); | |
242 if (!generated.isEmpty()) { | |
243 ProtocolUtils.appendOutputModes( | |
244 doc, outs, generated); | |
245 } | |
246 else{ | |
247 logger.debug("Cannot append output to generated document."); | |
248 } | |
249 } | |
250 else { | |
251 logger.debug("No facets found for the current state."); | |
252 } | |
253 } | |
254 } | |
255 } | |
256 catch (IllegalArgumentException iae) { | |
257 // state is not valid, so we do not append its outputs. | |
258 } | |
259 } | |
260 } | |
261 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |