# HG changeset patch # User Tom Gottfried # Date 1365148562 -7200 # Node ID 2a058dff966625e3f2bc4e167d3e0320fadeea17 # Parent f12837bc510fe519cff30be237935e31331e0123# Parent 42692d6868e685452ecfe120be63e3c01a5ed002 merge diff -r f12837bc510f -r 2a058dff9666 flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java Fri Apr 05 09:55:33 2013 +0200 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java Fri Apr 05 09:56:02 2013 +0200 @@ -1,6 +1,8 @@ package de.intevation.flys.client.server; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import javax.xml.xpath.XPathConstants; @@ -84,7 +86,7 @@ logger.debug("Extract wq info objects now."); WQInfoObject[] objects = extractWQInfoObjects(result); - if (objects != null && objects.length > 0) { + if (objects.length > 0) { return objects; } } @@ -118,8 +120,12 @@ throw new ServerException(ERROR_NO_WQINFO_FOUND); } + boolean debug = logger.isDebugEnabled(); + int num = list.getLength(); - logger.debug("Response contains " + num + " objects."); + if (debug) { + logger.debug("Response contains " + num + " objects."); + } List objects = new ArrayList(num); @@ -132,12 +138,35 @@ } } - logger.debug("Retrieved " + objects.size() + " wq values"); + if (debug) { + logger.debug("Retrieved " + objects.size() + " wq values"); + } - return (WQInfoObject[]) - objects.toArray(new WQInfoObject[num]); + WQInfoObject [] array = (WQInfoObject[]) + objects.toArray(new WQInfoObject[objects.size()]); + + Arrays.sort(array, WQ_INFO_OBJECT_CMP); + + return array; } + public static final Comparator WQ_INFO_OBJECT_CMP = + new Comparator() { + @Override + public int compare(WQInfoObject a, WQInfoObject b) { + + // Descending by type: Qs before Ds + int cmp = a.getType().compareTo(b.getType()); + if (cmp < 0) return +1; + if (cmp > 0) return -1; + + // Ascending by value + double diff = a.getValue() - b.getValue(); + if (diff < 0d) return -1; + if (diff > 0d) return +1; + return 0; + } + }; /** * Extracts information for a single wq info object and intializes an @@ -148,6 +177,8 @@ * @return a valid WQInfoObject. */ protected WQInfoObject buildWQInfoObject(Node node) { + + // TODO: Replace this expensive XPaths with simpler use of DOM. String name = XMLUtils.xpathString( node, "@name", ArtifactNamespaceContext.INSTANCE);