Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/CrossSectionArtifact.java @ 3814:8083f6384023
merged flys-artifacts/pre2.6-2012-01-04
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:56 +0200 |
parents | cd9bcdcf6597 |
children | f021080cb409 |
comparison
equal
deleted
inserted
replaced
1491:2a00f4849738 | 3814:8083f6384023 |
---|---|
1 package de.intevation.flys.artifacts; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import org.w3c.dom.Document; | |
9 | |
10 import de.intevation.artifacts.Artifact; | |
11 import de.intevation.artifacts.ArtifactFactory; | |
12 import de.intevation.artifacts.CallMeta; | |
13 | |
14 import de.intevation.flys.artifacts.model.CrossSectionFacet; | |
15 | |
16 import de.intevation.flys.model.CrossSection; | |
17 import de.intevation.flys.model.CrossSectionLine; | |
18 import de.intevation.flys.artifacts.model.CrossSectionFactory; | |
19 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
20 import de.intevation.artifacts.common.utils.XMLUtils; | |
21 | |
22 import de.intevation.flys.artifacts.states.StaticState; | |
23 | |
24 import de.intevation.artifactdatabase.state.Facet; | |
25 import de.intevation.artifactdatabase.state.State; | |
26 | |
27 import de.intevation.flys.utils.FLYSUtils; | |
28 | |
29 | |
30 /** | |
31 * Artifact describing a cross-section. | |
32 */ | |
33 public class CrossSectionArtifact extends StaticFLYSArtifact { | |
34 | |
35 /** Access ids of doc. */ | |
36 public static final String XPATH_IDS = "/art:action/art:ids/@value"; | |
37 | |
38 /** Name of Artifact. */ | |
39 public static final String CS_ARTIFACT_NAME = "cross_section"; | |
40 | |
41 /** Name of state. */ | |
42 public static final String STATIC_STATE_NAME = "state.cross_section"; | |
43 | |
44 /** Name of data item keeping the position. */ | |
45 public static final String DATA_KM = "cross_section.km"; | |
46 | |
47 /** Name of data item keeping the database id of this c.s.. */ | |
48 public static final String DATA_DBID = "cross_section.dbid"; | |
49 | |
50 /** Name of data item flagging whether we think that we are master. */ | |
51 public static final String DATA_IS_MASTER = "cross_section.master?"; | |
52 | |
53 /** Name of data item flagging whether we are the newest. */ | |
54 public static final String DATA_IS_NEWEST = "cross_section.newest?"; | |
55 | |
56 /** Own logger. */ | |
57 private static final Logger logger = | |
58 Logger.getLogger(CrossSectionArtifact.class); | |
59 | |
60 | |
61 /** Return given name. */ | |
62 @Override | |
63 public String getName() { | |
64 return CS_ARTIFACT_NAME; | |
65 } | |
66 | |
67 | |
68 /** Store ids, create a CrossSectionFacet. */ | |
69 @Override | |
70 public void setup( | |
71 String identifier, | |
72 ArtifactFactory factory, | |
73 Object context, | |
74 CallMeta callMeta, | |
75 Document data) | |
76 { | |
77 logger.info("CrossSectionArtifact.setup"); | |
78 | |
79 super.setup(identifier, factory, context, callMeta, data); | |
80 | |
81 String ids = XMLUtils.xpathString( | |
82 data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE); | |
83 | |
84 if (ids != null && ids.length() > 0) { | |
85 addStringData(DATA_DBID, ids); | |
86 logger.debug("CrossSectionArtifacts db-id: " + ids); | |
87 } | |
88 else { | |
89 throw new IllegalArgumentException("No attribute 'ids' found!"); | |
90 } | |
91 | |
92 List<Facet> fs = new ArrayList<Facet>(); | |
93 CrossSection cs = CrossSectionFactory.getCrossSection(Integer.valueOf(ids)); | |
94 CrossSectionLine csl = cs.getLines().get(0); | |
95 // Find min-km of cross sections, than set DATA_KM to min(DATA_KM, minCross). | |
96 if (csl != null) { | |
97 double masterKm = Double.valueOf(getDataAsString(DATA_KM)); | |
98 if (masterKm < csl.getKm().doubleValue()) { | |
99 addStringData(DATA_KM, csl.getKm().toString()); | |
100 } | |
101 } | |
102 fs.add(new CrossSectionFacet(0, cs.getDescription())); | |
103 | |
104 // Find out if we are newest. | |
105 boolean isNewest = CrossSectionFactory.isNewest(cs); | |
106 String newString = (isNewest) ? "1" : "0"; | |
107 addStringData(DATA_IS_NEWEST, newString); | |
108 addStringData(DATA_IS_MASTER, newString); | |
109 | |
110 StaticState state = (StaticState) getCurrentState(context); | |
111 | |
112 if (!fs.isEmpty()) { | |
113 facets.put(getCurrentStateId(), fs); | |
114 } | |
115 } | |
116 | |
117 | |
118 /** Copy km where master-artifact "starts". */ | |
119 @Override | |
120 protected void initialize( | |
121 Artifact artifact, | |
122 Object context, | |
123 CallMeta callMeta) | |
124 { | |
125 WINFOArtifact winfo = (WINFOArtifact) artifact; | |
126 double[] range = FLYSUtils.getKmRange(winfo); | |
127 double min = 0.0f; | |
128 if (range != null && range.length > 0) { | |
129 min = range[0]; | |
130 } | |
131 this.addStringData(DATA_KM, Double.toString(min)); | |
132 } | |
133 | |
134 | |
135 /** | |
136 * Create and return a new StaticState with charting output. | |
137 */ | |
138 @Override | |
139 public State getCurrentState(Object cc) { | |
140 final List<Facet> fs = facets.get(getCurrentStateId()); | |
141 | |
142 StaticState state = new StaticState(STATIC_STATE_NAME) { | |
143 @Override | |
144 public Object staticCompute(List<Facet> facets) { | |
145 if (facets != null) { | |
146 facets.addAll(fs); | |
147 } | |
148 return null; | |
149 } | |
150 }; | |
151 | |
152 state.addDefaultChartOutput("cross_section", fs); | |
153 | |
154 return state; | |
155 } | |
156 | |
157 | |
158 /** | |
159 * Get a list containing the one and only State. | |
160 * @param context ignored. | |
161 * @return list with one and only state. | |
162 */ | |
163 @Override | |
164 protected List<State> getStates(Object context) { | |
165 ArrayList<State> states = new ArrayList<State>(); | |
166 states.add(getCurrentState(context)); | |
167 | |
168 return states; | |
169 } | |
170 | |
171 // TODO all data access needs proper caching. | |
172 | |
173 /** | |
174 * Get a DataItem casted to int (0 if fails). | |
175 */ | |
176 public int getDataAsIntNull(String dataName) { | |
177 String val = getDataAsString(dataName); | |
178 try { | |
179 return Integer.valueOf(val); | |
180 } | |
181 catch (NumberFormatException e) { | |
182 logger.warn("Could not get data " + dataName + " as int", e); | |
183 return 0; | |
184 } | |
185 } | |
186 | |
187 | |
188 /** Returns database-id of cross-section (from data). */ | |
189 protected int getDBID() { | |
190 return getDataAsIntNull(DATA_DBID); | |
191 } | |
192 | |
193 | |
194 /** | |
195 * Return position (km) from data, 0 if not found. | |
196 */ | |
197 protected double getKm() { | |
198 String val = getDataAsString(DATA_KM); | |
199 try { | |
200 return Double.valueOf(val); | |
201 } | |
202 catch (NumberFormatException e) { | |
203 logger.warn("Could not get data " + DATA_KM + " as double", e); | |
204 return 0; | |
205 } | |
206 } | |
207 | |
208 | |
209 /** Returns true if artifact is set to be a "master" (other facets will | |
210 * refer to this). */ | |
211 public boolean isMaster() { | |
212 return !getDataAsString(DATA_IS_MASTER).equals("0"); | |
213 } | |
214 | |
215 | |
216 /** | |
217 * Get points of Profile of cross section at given kilometer. | |
218 * | |
219 * @return an array holding coordinates of points of profile ( | |
220 * in the form {{x1, x2} {y1, y2}} ). | |
221 */ | |
222 public double [][] getCrossSectionData() { | |
223 logger.info("getCrossSectionData() for cross_section.km " | |
224 + getDataAsString(DATA_KM)); | |
225 CrossSectionLine line = searchCrossSectionLine(); | |
226 | |
227 return line != null | |
228 ? line.fetchCrossSectionProfile() | |
229 : null; | |
230 } | |
231 | |
232 | |
233 /** | |
234 * Get CrossSectionLine spatially closest to what is specified in the data | |
235 * "cross_section.km". | |
236 * | |
237 * @return CrossSectionLine closest to "cross_section.km". | |
238 */ | |
239 public CrossSectionLine searchCrossSectionLine() { | |
240 double wishKM = getKm(); | |
241 | |
242 CrossSection crossSection = CrossSectionFactory.getCrossSection(getDBID()); | |
243 logger.debug("dbid " + getDBID() + " : " + crossSection); | |
244 List<CrossSectionLine> crossSectionLines = | |
245 crossSection.getLines(); | |
246 | |
247 // Get the cross section closest to requested km. | |
248 // Naive, linear approach. | |
249 CrossSectionLine oldLine = crossSectionLines.get(0); | |
250 double oldDiff = Math.abs(wishKM - oldLine.getKm().doubleValue()); | |
251 for (CrossSectionLine line: crossSectionLines) { | |
252 double diff = Math.abs(wishKM - line.getKm().doubleValue()); | |
253 if (diff > oldDiff) { | |
254 break; | |
255 } | |
256 oldDiff = diff; | |
257 oldLine = line; | |
258 } | |
259 return oldLine; | |
260 } | |
261 | |
262 | |
263 /** | |
264 * Determines Facets initial disposition regarding activity (think of | |
265 * selection in Client ThemeList GUI). This will be checked one time | |
266 * when the facet enters a collections describe document. | |
267 * | |
268 * @param outputName Ignored. | |
269 * @param facetName Ignored. | |
270 * @param index Ignored. | |
271 * @return 0 if not active | |
272 */ | |
273 @Override | |
274 public int getInitialFacetActivity(String outputName, String facetName, int index) { | |
275 return (getDataAsString(DATA_IS_NEWEST) != null | |
276 && getDataAsString(DATA_IS_NEWEST).equals("1")) ? 1 : 0; | |
277 } | |
278 } | |
279 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |