comparison flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java @ 5567:42692d6868e6

Order Qs before Ds and values ascending
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Apr 2013 09:54:07 +0200
parents e70ff0a600a3
children 4b281c7046c8
comparison
equal deleted inserted replaced
5566:adb6c0f14810 5567:42692d6868e6
1 package de.intevation.flys.client.server; 1 package de.intevation.flys.client.server;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
4 import java.util.List; 6 import java.util.List;
5 7
6 import javax.xml.xpath.XPathConstants; 8 import javax.xml.xpath.XPathConstants;
7 9
8 import org.w3c.dom.Document; 10 import org.w3c.dom.Document;
82 Document result = client.callService(url, "mainvalues", doc); 84 Document result = client.callService(url, "mainvalues", doc);
83 85
84 logger.debug("Extract wq info objects now."); 86 logger.debug("Extract wq info objects now.");
85 WQInfoObject[] objects = extractWQInfoObjects(result); 87 WQInfoObject[] objects = extractWQInfoObjects(result);
86 88
87 if (objects != null && objects.length > 0) { 89 if (objects.length > 0) {
88 return objects; 90 return objects;
89 } 91 }
90 } 92 }
91 catch (ConnectionException ce) { 93 catch (ConnectionException ce) {
92 logger.error(ce, ce); 94 logger.error(ce, ce);
116 logger.warn("No wq info found."); 118 logger.warn("No wq info found.");
117 119
118 throw new ServerException(ERROR_NO_WQINFO_FOUND); 120 throw new ServerException(ERROR_NO_WQINFO_FOUND);
119 } 121 }
120 122
123 boolean debug = logger.isDebugEnabled();
124
121 int num = list.getLength(); 125 int num = list.getLength();
122 logger.debug("Response contains " + num + " objects."); 126 if (debug) {
127 logger.debug("Response contains " + num + " objects.");
128 }
123 129
124 List<WQInfoObject> objects = 130 List<WQInfoObject> objects =
125 new ArrayList<WQInfoObject>(num); 131 new ArrayList<WQInfoObject>(num);
126 132
127 for (int i = 0; i < num; i++) { 133 for (int i = 0; i < num; i++) {
130 if (obj != null) { 136 if (obj != null) {
131 objects.add(obj); 137 objects.add(obj);
132 } 138 }
133 } 139 }
134 140
135 logger.debug("Retrieved " + objects.size() + " wq values"); 141 if (debug) {
136 142 logger.debug("Retrieved " + objects.size() + " wq values");
137 return (WQInfoObject[]) 143 }
138 objects.toArray(new WQInfoObject[num]); 144
145 WQInfoObject [] array = (WQInfoObject[])
146 objects.toArray(new WQInfoObject[objects.size()]);
147
148 Arrays.sort(array, WQ_INFO_OBJECT_CMP);
149
150 return array;
139 } 151 }
140 152
153 public static final Comparator<WQInfoObject> WQ_INFO_OBJECT_CMP =
154 new Comparator<WQInfoObject>() {
155 @Override
156 public int compare(WQInfoObject a, WQInfoObject b) {
157
158 // Descending by type: Qs before Ds
159 int cmp = a.getType().compareTo(b.getType());
160 if (cmp < 0) return +1;
161 if (cmp > 0) return -1;
162
163 // Ascending by value
164 double diff = a.getValue() - b.getValue();
165 if (diff < 0d) return -1;
166 if (diff > 0d) return +1;
167 return 0;
168 }
169 };
141 170
142 /** 171 /**
143 * Extracts information for a single wq info object and intializes an 172 * Extracts information for a single wq info object and intializes an
144 * WQInfoObject with them. 173 * WQInfoObject with them.
145 * 174 *
146 * @param node The node that contains the information. 175 * @param node The node that contains the information.
147 * 176 *
148 * @return a valid WQInfoObject. 177 * @return a valid WQInfoObject.
149 */ 178 */
150 protected WQInfoObject buildWQInfoObject(Node node) { 179 protected WQInfoObject buildWQInfoObject(Node node) {
180
181 // TODO: Replace this expensive XPaths with simpler use of DOM.
151 String name = XMLUtils.xpathString( 182 String name = XMLUtils.xpathString(
152 node, "@name", ArtifactNamespaceContext.INSTANCE); 183 node, "@name", ArtifactNamespaceContext.INSTANCE);
153 184
154 String type = XMLUtils.xpathString( 185 String type = XMLUtils.xpathString(
155 node, "@type", ArtifactNamespaceContext.INSTANCE); 186 node, "@type", ArtifactNamespaceContext.INSTANCE);

http://dive4elements.wald.intevation.org