comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.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 721298eeb694
children 102050c4fc00
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
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 /**
71 * Access the data (wkms).
72 */
73 protected WKms getWKms(String mingle, CallContext context) {
74 String[] def = mingle.split(";");
75 String uuid = def[0];
76 String name = def[1];
77 int idx = Integer.parseInt(def[2]);
78
79 if (name.startsWith("staticwkms")) {
80 StaticWKmsArtifact staticWKms =
81 (StaticWKmsArtifact) FLYSUtils.getArtifact(
82 uuid,
83 context);
84 logger.debug("WDifferencesState obtain data from StaticWKms");
85 WKms wkms = staticWKms.getWKms(idx);
86 if (wkms == null)
87 logger.error("No WKms from artifact.");
88 return wkms;
89 }
90
91 WINFOArtifact flys = (WINFOArtifact) FLYSUtils.getArtifact(
92 uuid,
93 context);
94
95 if (flys == null) {
96 logger.warn("One of the artifacts (1) for diff calculation could not be loaded");
97 return null;
98 }
99 else{
100 WQKms[] wqkms = (WQKms[]) flys.getWaterlevelData().
101 getData();
102 if (wqkms == null)
103 logger.warn("not waterlevels in artifact");
104 else if (wqkms.length < idx)
105 logger.warn("not enough waterlevels in artifact");
106 return wqkms[idx];
107 }
108 }
109
110
111 /**
112 * Return CalculationResult with Array of WKms that are difference of
113 * Waterlevels. Add respective facets (DifferencesCurveFacet, DataFacet).
114 */
115 @Override
116 public Object computeAdvance(
117 FLYSArtifact artifact,
118 String hash,
119 CallContext context,
120 List<Facet> facets,
121 Object old
122 ) {
123 if (artifact instanceof ChartArtifact) {
124 ChartArtifact chart = (ChartArtifact)artifact;
125 facets.add(new EmptyFacet());
126 return null;
127 }
128 WINFOArtifact winfo = (WINFOArtifact) artifact;
129 String id = getID();
130
131 // Load the Artifacts/facets that we want to subtract and display.
132 // Expected format is:
133 // [42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;0]#[1231f2-....]
134 String diffids = winfo.getDataAsString("diffids");
135 logger.debug("WDifferencesState has: " + diffids);
136 String datas[] = diffids.split("#");
137
138 // Validate the Data-Strings.
139 for (String s: datas) {
140 if (!WaterlevelSelectState.isValueValid(winfo.getDataAsString("diffids"))) {
141 // TODO: escalate.
142 }
143 }
144
145 if (datas.length < 2) {
146 // TODO crash with style
147 }
148
149 List<WKms> wkmss = new ArrayList<WKms>();
150
151 for(int i = 0; i < datas.length; i+=2) {
152 // e.g.:
153 // 42537f1e-3522-42ef-8968-635b03d8e9c6;longitudinal_section.w;1
154 WKms minuendWKms = getWKms(StringUtil.unbracket(datas[i+0]),
155 context);
156 WKms subtrahendWKms = getWKms(StringUtil.unbracket(datas[i+1]),
157 context);
158
159 String facetName = "diff ()";
160
161 if (minuendWKms != null && subtrahendWKms != null) {
162 facetName = StringUtil.wWrap(minuendWKms.getName())
163 + " - " + StringUtil.wWrap(subtrahendWKms.getName());
164 WKms wkms = WKmsOperation.SUBTRACTION.operate(minuendWKms,
165 subtrahendWKms);
166 wkms.setName(facetName);
167 wkmss.add(wkms);
168 logger.debug("WKMSSubtraction happened");
169 }
170 if (facets != null) {
171 facets.add(new DifferenceCurveFacet(i/2, W_DIFFERENCES, facetName,
172 ComputeType.ADVANCE, id, hash));
173 }
174 }
175
176 if (facets != null) {
177 facets.add(new DataFacet(CSV, "CSV data"));
178 facets.add(new DataFacet(PDF, "PDF data"));
179 logger.debug("Adding facets in WDifferencesState.");
180 }
181 else {
182 logger.debug("Not adding facets in WDifferencesState.");
183 }
184
185 // TODO Evaluate whether null is okay as reports.
186 WKms[] diffs = wkmss.toArray(new WKms[wkmss.size()]);
187 CalculationResult result = new CalculationResult(diffs, null);
188 return result;
189 }
190 }
191 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org