comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/MainValuesService.java @ 3728:d03e65378b9f

Removed obsolete code. Removed NPE as regular flow control. flys-artifacts/trunk@5401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 08 Sep 2012 13:36:06 +0000
parents ad54896ec369
children 0c217de0d84b
comparison
equal deleted inserted replaced
3727:b81f328da582 3728:d03e65378b9f
19 import de.intevation.flys.model.MainValueType; 19 import de.intevation.flys.model.MainValueType;
20 import de.intevation.flys.model.NamedMainValue; 20 import de.intevation.flys.model.NamedMainValue;
21 import de.intevation.flys.model.Range; 21 import de.intevation.flys.model.Range;
22 import de.intevation.flys.model.River; 22 import de.intevation.flys.model.River;
23 23
24 import de.intevation.flys.artifacts.model.MainValuesFactory;
25 import de.intevation.flys.artifacts.model.RiverFactory; 24 import de.intevation.flys.artifacts.model.RiverFactory;
26 25
27 26
28 /** 27 /**
29 * This service returns the main values of a river's gauge based on the start 28 * This service returns the main values of a river's gauge based on the start
50 * The default constructor. 49 * The default constructor.
51 */ 50 */
52 public MainValuesService() { 51 public MainValuesService() {
53 } 52 }
54 53
54 private static final Document error(String msg) {
55 logger.debug(msg);
56 return XMLUtils.newDocument();
57 }
58
55 59
56 @Override 60 @Override
57 public Document doProcess( 61 public Document doProcess(
58 Document data, 62 Document data,
59 GlobalContext context, 63 GlobalContext context,
60 CallMeta callMeta 64 CallMeta callMeta
61 ) { 65 ) {
62 logger.debug("MainValuesService.process"); 66 logger.debug("MainValuesService.process");
63 67
64 try { 68 River river = getRequestedRiver(data);
65 River river = getRequestedRiver(data); 69 if (river == null) {
66 double[] minmax = getRequestedStartEnd(data, river); 70 return error("no river found.");
67 Gauge gauge = river.determineGauge(minmax[0], minmax[1]); 71 }
68 72
69 logger.debug("Found gauge: " + gauge.getName()); 73 double[] minmax = getRequestedStartEnd(data, river);
70 74 Gauge gauge = river.determineGauge(minmax[0], minmax[1]);
71 List<MainValue> mainValues = getMainValues(river, gauge); 75
72 76 if (gauge == null) {
73 return buildDocument(river, gauge, mainValues, context); 77 return error("no gauge found.");
74 } 78 }
75 catch (NullPointerException npe) { 79
76 logger.error("Could not process the request."); 80 List<MainValue> mainValues = getMainValues(river, gauge);
77 logger.error(npe, npe); 81
78 82 return buildDocument(river, gauge, mainValues, context);
79 return XMLUtils.newDocument();
80 }
81 } 83 }
82 84
83 85
84 /** 86 /**
85 * This method extracts the river from the incoming request. If no river 87 * This method extracts the river from the incoming request. If no river
96 logger.debug("MainValuesService.getRiver"); 98 logger.debug("MainValuesService.getRiver");
97 99
98 String riverStr = XMLUtils.xpathString( 100 String riverStr = XMLUtils.xpathString(
99 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE); 101 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE);
100 102
101 if (riverStr == null || riverStr.trim().length() == 0) { 103 return riverStr != null && (riverStr = riverStr.trim()).length() > 0
102 throw new NullPointerException("No river found in the request."); 104 ? RiverFactory.getRiver(riverStr)
103 } 105 : null;
104
105 River river = RiverFactory.getRiver(riverStr);
106
107 if (river == null) {
108 throw new NullPointerException("No such river found: " + riverStr);
109 }
110
111 return river;
112 } 106 }
113 107
114 108
115 /** 109 /**
116 * This method extracts the start and end point from incoming request 110 * This method extracts the start and end point from incoming request
130 data, XPATH_START, ArtifactNamespaceContext.INSTANCE); 124 data, XPATH_START, ArtifactNamespaceContext.INSTANCE);
131 125
132 String endStr = XMLUtils.xpathString( 126 String endStr = XMLUtils.xpathString(
133 data, XPATH_END, ArtifactNamespaceContext.INSTANCE); 127 data, XPATH_END, ArtifactNamespaceContext.INSTANCE);
134 128
129 if (startStr == null || endStr == null) {
130 return river.determineMinMaxDistance();
131 }
132
135 try { 133 try {
136 double start = Double.parseDouble(startStr); 134 double start = Double.parseDouble(startStr);
137 double end = Double.parseDouble(endStr); 135 double end = Double.parseDouble(endStr);
138 136
139 logger.debug("Found start: " + start); 137 if (logger.isDebugEnabled()) {
140 logger.debug("Found end: " + end); 138 logger.debug("Found start: " + start);
139 logger.debug("Found end: " + end);
140 }
141 141
142 return new double[] { start, end }; 142 return new double[] { start, end };
143 } 143 }
144 catch (NumberFormatException nfe) { 144 catch (NumberFormatException nfe) {
145 logger.warn(nfe, nfe); 145 logger.warn(nfe, nfe);
146
147 return river.determineMinMaxDistance(); 146 return river.determineMinMaxDistance();
148 } 147 }
149 } 148 }
150 149
151 150
157 * @param gauge The gauge. 156 * @param gauge The gauge.
158 * 157 *
159 * @return a document that includes the main values of the specified river 158 * @return a document that includes the main values of the specified river
160 * at the specified gauge. 159 * at the specified gauge.
161 */ 160 */
162 protected List<MainValue> getMainValues(River river, Gauge gauge) 161 protected List<MainValue> getMainValues(River river, Gauge gauge) {
163 throws NullPointerException 162
164 {
165 if (logger.isDebugEnabled()) { 163 if (logger.isDebugEnabled()) {
166 logger.debug("MainValuesService.buildMainValues"); 164 logger.debug("MainValuesService.buildMainValues");
167 logger.debug("River: " + river.getName()); 165 logger.debug("River: " + river.getName());
168 logger.debug("Gauge: " + gauge.getName()); 166 logger.debug("Gauge: " + gauge.getName());
169 } 167 }
170 168
171 List<MainValue> mainValues = MainValuesFactory.getMainValues(gauge); 169 List<MainValue> mainValues = gauge.fetchMainValues();
172 170
173 if (mainValues == null || mainValues.isEmpty()) { 171 if (logger.isDebugEnabled()) {
174 throw new NullPointerException("No main values found."); 172 logger.debug(mainValues.size() + " main values found.");
175 } 173 }
176
177 logger.debug(mainValues.size() + " main values found.");
178 174
179 return mainValues; 175 return mainValues;
180 } 176 }
181 177
182 178

http://dive4elements.wald.intevation.org