comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.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 65b32220f197
children 5642a83420f2
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
1 package de.intevation.flys.artifacts.states;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import de.intevation.artifactdatabase.state.Facet;
9 import de.intevation.artifactdatabase.data.StateData;
10
11 import de.intevation.artifacts.CallContext;
12 import de.intevation.artifacts.Artifact;
13 import de.intevation.flys.artifacts.FLYSArtifact;
14 import de.intevation.flys.artifacts.StaticWKmsArtifact;
15 import de.intevation.flys.artifacts.WINFOArtifact;
16 import de.intevation.flys.artifacts.ChartArtifact;
17
18 import de.intevation.flys.artifacts.math.WKmsOperation;
19
20 import de.intevation.flys.artifacts.model.CalculationResult;
21 import de.intevation.flys.artifacts.model.DataFacet;
22 import de.intevation.flys.artifacts.model.DifferenceCurveFacet;
23 import de.intevation.flys.artifacts.model.EmptyFacet;
24
25 import de.intevation.flys.artifacts.model.FacetTypes;
26 import de.intevation.flys.artifacts.model.WKms;
27 import de.intevation.flys.artifacts.model.WQKms;
28
29 import de.intevation.flys.utils.FLYSUtils;
30 import de.intevation.flys.utils.StringUtil;
31
32
33 public class WDifferencesState
34 extends DefaultState
35 implements FacetTypes
36 {
37 /** The logger that is used in this state. */
38 private static Logger logger = Logger.getLogger(WDifferencesState.class);
39
40
41 public WDifferencesState() {
42 }
43
44
45 /** Specify to display nothing (this is kind of a "final" state). */
46 @Override
47 protected String getUIProvider() {
48 return "noinput";
49 }
50
51
52 @Override
53 public boolean validate(Artifact artifact)
54 throws IllegalArgumentException
55 {
56 FLYSArtifact flys = (FLYSArtifact) artifact;
57
58 StateData data = flys.getData("diffids");
59
60 if (data == null) {
61 throw new IllegalArgumentException("diffids is empty");
62 }
63
64 // TODO: Also validate format.
65
66 return true;
67 }
68
69
70 protected WKms getWKms(String mingle, CallContext context) {
71 String[] def = mingle.split(";");
72 String uuid = def[0];
73 String name = def[1];
74 int idx = Integer.parseInt(def[2]);
75
76 if (name.startsWith("staticwkms")) {
77 StaticWKmsArtifact staticWKms =
78 (StaticWKmsArtifact) FLYSUtils.getArtifact(
79 uuid,
80 context);
81 logger.debug("WDifferencesState obtain data from StaticWKms");
82 WKms wkms = staticWKms.getWKms(idx);
83 if (wkms == null)
84 logger.error("No WKms from artifact.");
85 return wkms;
86 }
87
88 WINFOArtifact flys = (WINFOArtifact) FLYSUtils.getArtifact(
89 uuid,
90 context);
91
92 if (flys == null) {
93 logger.warn("One of the artifacts (1) for diff calculation could not be loaded");
94 return null;
95 }
96 else{
97 WQKms[] wqkms = (WQKms[]) flys.getWaterlevelData().
98 getData();
99 if (wqkms == null)
100 logger.warn("not waterlevels in artifact");
101 else if (wqkms.length < idx)
102 logger.warn("not enough waterlevels in artifact");
103 return wqkms[idx];
104 }
105 }
106
107
108 /**
109 * Return CalculationResult with Array of WKms that are difference of
110 * Waterlevels. Add respective facets (DifferencesCurveFacet, DataFacet).
111 */
112 @Override
113 public Object computeAdvance(
114 FLYSArtifact artifact,
115 String hash,
116 CallContext context,
117 List<Facet> facets,
118 Object old
119 ) {
120 if (artifact instanceof ChartArtifact) {
121 ChartArtifact chart = (ChartArtifact)artifact;
122 facets.add(new EmptyFacet());
123 return null;
124 }
125 WINFOArtifact winfo = (WINFOArtifact) artifact;
126 String id = getID();
127
128 // Load the Artifacts/facets that we want to subtract and display.
129 // Expected format is:
130 // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....]
131 String diffids = winfo.getDataAsString("diffids");
132 logger.debug("WDifferencesState has: " + diffids);
133 String datas[] = diffids.split("#");
134
135 // Validate the Data-Strings.
136 for (String s: datas) {
137 if (!WaterlevelSelectState.isValueValid(winfo.getDataAsString("diffids"))) {
138 // TODO: escalate.
139 }
140 }
141
142 if (datas.length < 2) {
143 // TODO crash with style
144 }
145
146 List<WKms> wkmss = new ArrayList<WKms>();
147
148 for(int i = 0; i < datas.length; i+=2) {
149 // e.g.:
150 // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1
151 WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]),
152 context);
153 WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]),
154 context);
155
156 String facetName = "diff ()";
157
158 if (minuendWKms != null && subtrahendWKms != null) {
159 facetName = StringUtil.wWrap(minuendWKms.getName())
160 + " - " + StringUtil.wWrap(subtrahendWKms.getName());
161 WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms,
162 subtrahendWKms);
163 wkms.setName(facetName);
164 wkmss.add(wkms);
165 logger.debug("WKMSSubtraction happened");
166 }
167 if (facets != null) {
168 facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName,
169 ComputeType.ADVANCE, id, hash));
170 }
171 }
172
173 if (facets != null) {
174 facets.add(new DataFacet(CSV, "CSV data"));
175 facets.add(new DataFacet(PDF, "PDF data"));
176 logger.debug("Adding facets in WDifferencesState.");
177 }
178 else {
179 logger.debug("Not adding facets in WDifferencesState.");
180 }
181
182 // TODO Evaluate whether null is okay as reports.
183 WKms[] diffs = wkmss.toArray(new WKms[wkmss.size()]);
184 CalculationResult result = new CalculationResult(diffs, null);
185 return result;
186 }
187 }
188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org