Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/DistanceOnlyPartHistoricalSelect.java @ 9247:1ec3b891ab02
salix.historical.distance_only_part detail work
author | gernotbelger |
---|---|
date | Thu, 12 Jul 2018 12:12:01 +0200 |
parents | c08d5cfa4981 |
children | f48a8dc78529 |
rev | line source |
---|---|
9246 | 1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.artifacts.uinfo.salix; | |
10 | |
11 import java.util.HashMap; | |
12 import java.util.Iterator; | |
13 import java.util.List; | |
14 import java.util.Map; | |
15 import java.util.TreeMap; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 import org.dive4elements.artifacts.Artifact; | |
19 import org.dive4elements.artifacts.CallContext; | |
20 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
21 import org.dive4elements.river.artifacts.resources.Resources; | |
22 import org.dive4elements.river.artifacts.states.AddTableDataHelper; | |
23 import org.dive4elements.river.artifacts.states.DistanceOnlySelect; | |
24 import org.dive4elements.river.artifacts.uinfo.UINFOArtifact; | |
25 import org.dive4elements.river.model.BedHeight; | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
26 import org.dive4elements.river.model.River; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
27 import org.dive4elements.river.utils.Formatter; |
9246 | 28 import org.w3c.dom.Element; |
29 | |
30 public class DistanceOnlyPartHistoricalSelect extends DistanceOnlySelect { | |
31 | |
32 private static final long serialVersionUID = 1L; | |
33 private static Logger log = Logger.getLogger(DistanceOnlyPartHistoricalSelect.class); | |
34 | |
35 List<BedHeight> bhs = null; | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
36 Integer epoch = null; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
37 Integer year = null; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
38 Double lowerKm = null; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
39 Double upperKm = null; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
40 River river = null; |
9246 | 41 |
42 @Override | |
43 protected String getUIProvider() { | |
44 return "distance_only_part_historical_panel"; | |
45 } | |
46 | |
47 @Override | |
48 protected String getTitle(final CallContext context) { | |
49 // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID()); | |
50 return Resources.getMsg(context.getMeta(), "state.title.distance_part_state"); | |
51 } | |
52 | |
53 @Override | |
54 protected void appendItems(final Artifact artifact, final ElementCreator creator, final String name, final CallContext context, final Element select) { | |
55 final String datakey = "bedheights_for_part"; | |
56 | |
57 try { | |
58 if (datakey.equals(name)) { | |
59 makeDataSourceYearEpoch(artifact, creator, select, context, getBedheights(artifact)); // ist nur n test | |
60 } else if (name.equals("ld_from_part")) { | |
61 | |
62 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact); | |
63 final double lowerSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[0]; | |
64 final double lowerKm = access.getLowerKm() > lowerSoundings ? access.getLowerKm() : lowerSoundings; | |
65 | |
66 creator.addAttr(select, "type", "options", true); | |
67 | |
68 final Element item = creator.create("item"); | |
69 creator.addAttr(item, "label", "from_test", true); | |
70 creator.addAttr(item, "value", String.valueOf(lowerKm), true); | |
71 | |
72 select.appendChild(item); | |
73 } | |
74 | |
75 else if (name.equals("ld_to_part")) { | |
76 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact); | |
77 final double upperSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[1]; | |
78 final double upperKm = access.getUpperKm() < upperSoundings ? access.getUpperKm() : upperSoundings; | |
79 | |
80 creator.addAttr(select, "type", "options", true); | |
81 | |
82 final Element item = creator.create("item"); | |
83 creator.addAttr(item, "label", "to_test", true); | |
84 creator.addAttr(item, "value", String.valueOf(upperKm), true); | |
85 | |
86 select.appendChild(item); | |
87 | |
88 } | |
89 } | |
90 catch ( | |
91 | |
92 final IllegalArgumentException iae) { | |
93 iae.printStackTrace(); | |
94 } | |
95 } | |
96 | |
97 private List<BedHeight> getBedheights(final Artifact artifact) { | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
98 |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
99 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact); // improved lazy-loading |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
100 final Integer year = access.getYear(); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
101 final Integer epoch = access.getEpoch(); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
102 final River river = access.getRiver(); // comparable? workflow does not allow return and change anyway... |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
103 final Double lower = access.getLowerKm(); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
104 final Double upper = access.getUpperKm(); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
105 if (!(this.year == year && this.epoch == epoch && this.river == river && this.lowerKm == lower && this.upperKm == upper)) { |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
106 this.bhs = null; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
107 } |
9246 | 108 if (this.bhs == null) { |
109 final boolean isEpoch = epoch == null ? false : true; | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
110 this.bhs = BedHeight.getBedHeightYearEpoch(isEpoch, isEpoch ? epoch : year, river, lower, upper); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
111 this.year = year; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
112 this.epoch = epoch; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
113 this.river = river; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
114 this.lowerKm = lower; |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
115 this.upperKm = upper; |
9246 | 116 } |
117 return this.bhs; | |
118 | |
119 } | |
120 | |
121 private static final void makeDataSourceYearEpoch(final Artifact artifact, final ElementCreator creator, final Element select, final CallContext context, | |
122 final List<BedHeight> bedheights) { | |
123 | |
124 final AddTableDataHelper helper = new AddTableDataHelper(creator, select, "year", context.getMeta()); | |
125 | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
126 // TODO: probably aggregating results, no sound-row, output as single row |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
127 |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
128 int year = 0; // hässlich, aber kommt vermutlich eh bald weg |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
129 if (bedheights != null && bedheights.size() > 0) |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
130 year = bedheights.get(0).getYear(); // das jahr/epoche ist immer dasselbe |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
131 |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
132 helper.addColumn(0, "year", "60", "year", "INTEGER", "LEFT", null); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
133 helper.addColumn(1, "range", "130", "state.uinfo.salix.historical.km_range_part", "STRING", "LEFT", null); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
134 helper.addColumn(2, "description", "500", "uinfo.salix.sounding", "STRING", "LEFT", null); |
9246 | 135 |
136 final TreeMap<String, String> bedHeightSorted = new TreeMap<>(); | |
137 final double min = Double.MAX_VALUE; | |
138 final double max = -Double.MAX_VALUE; | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
139 final java.text.NumberFormat formatter = Formatter.getCalculationKm(context.getMeta()); |
9246 | 140 |
141 for (final BedHeight bh : bedheights) { | |
142 final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh); | |
143 final Double from = range.getA().doubleValue(); // NullPointer check?? | |
144 final Double to = range.getB().doubleValue(); | |
145 | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
146 bedHeightSorted.put(bh.getDescription(), formatter.format(from) + " - " + formatter.format(to)); |
9246 | 147 } |
148 final Iterator<String> iterator = bedHeightSorted.keySet().iterator(); | |
149 while (iterator.hasNext()) { | |
150 final String descr = iterator.next(); | |
151 final String fromTo = bedHeightSorted.get(descr); | |
152 final Map<String, String> row = new HashMap<>(); | |
9247
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
153 row.put("year", String.valueOf(year)); |
1ec3b891ab02
salix.historical.distance_only_part detail work
gernotbelger
parents:
9246
diff
changeset
|
154 row.put("range", fromTo); |
9246 | 155 row.put("description", descr); |
156 helper.addRow(row); | |
157 } | |
158 | |
159 helper.submitMapToXml(); | |
160 } | |
161 | |
162 private double[] getLowerUpperKmRange(final List<BedHeight> bedheights) { | |
163 double min = Double.MAX_VALUE; | |
164 double max = -Double.MAX_VALUE; | |
165 | |
166 for (final BedHeight bh : bedheights) { | |
167 final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh); | |
168 try { | |
169 final Double from = range.getA().doubleValue(); // NullPointer check?? -> try catch | |
170 final Double to = range.getB().doubleValue(); | |
171 | |
172 final double upper = to > from ? to : from; | |
173 final double lower = from < to ? from : to; | |
174 if (upper > max) | |
175 max = upper; | |
176 | |
177 if (lower < min) | |
178 min = lower; | |
179 } | |
180 catch (final Exception e) { | |
181 e.printStackTrace(); | |
182 } | |
183 | |
184 } | |
185 return new double[] { min, max }; | |
186 } | |
187 | |
188 } |