comparison artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/salix/DistanceOnlyPartHistoricalSelect.java @ 9246:c08d5cfa4981

some hibernate queries on bedheigts for salix
author gernotbelger
date Thu, 12 Jul 2018 11:15:42 +0200
parents
children 1ec3b891ab02
comparison
equal deleted inserted replaced
9245:f5cff8708531 9246:c08d5cfa4981
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;
26 import org.w3c.dom.Element;
27
28 public class DistanceOnlyPartHistoricalSelect extends DistanceOnlySelect {
29
30 private static final long serialVersionUID = 1L;
31 private static Logger log = Logger.getLogger(DistanceOnlyPartHistoricalSelect.class);
32
33 List<BedHeight> bhs = null;
34
35 @Override
36 protected String getUIProvider() {
37 return "distance_only_part_historical_panel";
38 }
39
40 @Override
41 protected String getTitle(final CallContext context) {
42 // REMARK: that is how it should be: return Resources.getMsg(context.getMeta(), getID());
43 return Resources.getMsg(context.getMeta(), "state.title.distance_part_state");
44 }
45
46 @Override
47 protected void appendItems(final Artifact artifact, final ElementCreator creator, final String name, final CallContext context, final Element select) {
48 final String datakey = "bedheights_for_part";
49
50 try {
51 if (datakey.equals(name)) {
52 makeDataSourceYearEpoch(artifact, creator, select, context, getBedheights(artifact)); // ist nur n test
53 } else if (name.equals("ld_from_part")) {
54
55 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
56 final double lowerSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[0];
57 final double lowerKm = access.getLowerKm() > lowerSoundings ? access.getLowerKm() : lowerSoundings;
58
59 creator.addAttr(select, "type", "options", true);
60
61 final Element item = creator.create("item");
62 creator.addAttr(item, "label", "from_test", true);
63 creator.addAttr(item, "value", String.valueOf(lowerKm), true);
64
65 select.appendChild(item);
66 }
67
68 else if (name.equals("ld_to_part")) {
69 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
70 final double upperSoundings = this.getLowerUpperKmRange(getBedheights(artifact))[1];
71 final double upperKm = access.getUpperKm() < upperSoundings ? access.getUpperKm() : upperSoundings;
72
73 creator.addAttr(select, "type", "options", true);
74
75 final Element item = creator.create("item");
76 creator.addAttr(item, "label", "to_test", true);
77 creator.addAttr(item, "value", String.valueOf(upperKm), true);
78
79 select.appendChild(item);
80
81 }
82 }
83 catch (
84
85 final IllegalArgumentException iae) {
86 iae.printStackTrace();
87 }
88 }
89
90 private List<BedHeight> getBedheights(final Artifact artifact) {
91 if (this.bhs == null) {
92 final SalixLineAccess access = new SalixLineAccess((UINFOArtifact) artifact);
93 final Integer year = access.getYear();
94
95 final Integer epoch = access.getEpoch();
96 final boolean isEpoch = epoch == null ? false : true;
97 this.bhs = BedHeight.getBedHeightYearEpoch(isEpoch, isEpoch ? epoch : year, access.getRiver(), access.getLowerKm(), access.getUpperKm());
98 }
99 return this.bhs;
100
101 }
102
103 private static final void makeDataSourceYearEpoch(final Artifact artifact, final ElementCreator creator, final Element select, final CallContext context,
104 final List<BedHeight> bedheights) {
105
106 final AddTableDataHelper helper = new AddTableDataHelper(creator, select, "year", context.getMeta());
107
108 helper.addColumn(0, "from_to", "100", "year", "INTEGER", "LEFT", null);
109 helper.addColumn(1, "description", "500", "uinfo.salix.soundings", "STRING", "LEFT", null);
110
111 final TreeMap<String, String> bedHeightSorted = new TreeMap<>();
112 final double min = Double.MAX_VALUE;
113 final double max = -Double.MAX_VALUE;
114
115 for (final BedHeight bh : bedheights) {
116 final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh);
117 final Double from = range.getA().doubleValue(); // NullPointer check??
118 final Double to = range.getB().doubleValue();
119
120 bedHeightSorted.put(bh.getDescription(), String.valueOf(from) + " - " + String.valueOf(to));
121 }
122 final Iterator<String> iterator = bedHeightSorted.keySet().iterator();
123 while (iterator.hasNext()) {
124 final String descr = iterator.next();
125 final String fromTo = bedHeightSorted.get(descr);
126 final Map<String, String> row = new HashMap<>();
127 row.put("from_to", String.valueOf(fromTo));
128 row.put("description", descr);
129 helper.addRow(row);
130 }
131
132 helper.submitMapToXml();
133 }
134
135 private double[] getLowerUpperKmRange(final List<BedHeight> bedheights) {
136 double min = Double.MAX_VALUE;
137 double max = -Double.MAX_VALUE;
138
139 for (final BedHeight bh : bedheights) {
140 final org.dive4elements.river.model.Range range = BedHeight.getRangeFromBedHeights(bh);
141 try {
142 final Double from = range.getA().doubleValue(); // NullPointer check?? -> try catch
143 final Double to = range.getB().doubleValue();
144
145 final double upper = to > from ? to : from;
146 final double lower = from < to ? from : to;
147 if (upper > max)
148 max = upper;
149
150 if (lower < min)
151 min = lower;
152 }
153 catch (final Exception e) {
154 e.printStackTrace();
155 }
156
157 }
158 return new double[] { min, max };
159 }
160
161 }

http://dive4elements.wald.intevation.org