raimund@245: package de.intevation.flys.client.server;
raimund@245:
raimund@245: import java.util.ArrayList;
teichmann@5567: import java.util.Arrays;
teichmann@5567: import java.util.Comparator;
raimund@245: import java.util.List;
raimund@245:
raimund@245: import javax.xml.xpath.XPathConstants;
raimund@245:
raimund@245: import org.w3c.dom.Document;
raimund@245: import org.w3c.dom.Element;
teichmann@5596: import org.w3c.dom.Node;
raimund@245: import org.w3c.dom.NodeList;
raimund@245:
ingo@1367: import org.apache.log4j.Logger;
ingo@1367:
raimund@245: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
raimund@245:
raimund@245: import de.intevation.artifacts.common.ArtifactNamespaceContext;
raimund@245: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@245:
raimund@245: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
raimund@245: import de.intevation.artifacts.httpclient.http.HttpClient;
raimund@245: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
raimund@245:
raimund@245: import de.intevation.flys.client.shared.exceptions.ServerException;
raimund@245: import de.intevation.flys.client.client.services.WQInfoService;
raimund@245: import de.intevation.flys.client.shared.model.WQInfoObject;
raimund@245: import de.intevation.flys.client.shared.model.WQInfoObjectImpl;
raimund@245:
raimund@245:
raimund@245: /**
raimund@245: * @author Ingo Weinzierl
raimund@245: */
raimund@245: public class WQInfoServiceImpl
raimund@245: extends RemoteServiceServlet
raimund@245: implements WQInfoService
raimund@245: {
ingo@1367: private static final Logger logger =
ingo@1367: Logger.getLogger(WQInfoServiceImpl.class);
ingo@1367:
raimund@245: public static final String ERROR_NO_WQINFO_FOUND =
raimund@245: "error_no_wqinfo_found";
raimund@245:
raimund@245: public static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue";
raimund@245:
raimund@245:
raimund@245: public WQInfoObject[] getWQInfo(
raimund@245: String locale,
raimund@245: String river,
raimund@245: double from,
raimund@245: double to)
raimund@245: throws ServerException
raimund@245: {
ingo@1367: logger.info("WQInfoServiceImpl.getWQInfo");
raimund@245:
raimund@1425: String url = getServletContext().getInitParameter("server-url");
raimund@1425:
raimund@245: Document doc = XMLUtils.newDocument();
raimund@245:
raimund@245: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
raimund@245: doc,
raimund@245: ArtifactNamespaceContext.NAMESPACE_URI,
raimund@245: ArtifactNamespaceContext.NAMESPACE_PREFIX);
raimund@245:
raimund@245: Element mainvalues = ec.create("mainvalues");
raimund@245: Element riverEl = ec.create("river");
raimund@245: Element startEl = ec.create("start");
raimund@245: Element endEl = ec.create("end");
raimund@245:
raimund@245: riverEl.setTextContent(river);
raimund@245: startEl.setTextContent(Double.valueOf(from).toString());
raimund@245: endEl.setTextContent(Double.valueOf(to).toString());
raimund@245:
raimund@245: mainvalues.appendChild(riverEl);
raimund@245: mainvalues.appendChild(startEl);
raimund@245: mainvalues.appendChild(endEl);
raimund@245:
raimund@245: doc.appendChild(mainvalues);
raimund@245:
raimund@245: HttpClient client = new HttpClientImpl(url, locale);
raimund@245:
raimund@245: try {
raimund@245: Document result = client.callService(url, "mainvalues", doc);
raimund@245:
ingo@1367: logger.debug("Extract wq info objects now.");
raimund@245: WQInfoObject[] objects = extractWQInfoObjects(result);
raimund@245:
teichmann@5567: if (objects.length > 0) {
raimund@245: return objects;
raimund@245: }
raimund@245: }
raimund@245: catch (ConnectionException ce) {
ingo@1367: logger.error(ce, ce);
raimund@245: }
raimund@245:
raimund@245: throw new ServerException(ERROR_NO_WQINFO_FOUND);
raimund@245: }
raimund@245:
raimund@245:
raimund@245: /**
raimund@245: * Extracts all wq info objects from result document.
raimund@245: *
raimund@245: * @param result The document retrieved by the server.
raimund@245: *
raimund@245: * @return a list of WQInfoObjects.
raimund@245: */
raimund@245: protected WQInfoObject[] extractWQInfoObjects(Document result)
raimund@245: throws ServerException
raimund@245: {
raimund@245: NodeList list = (NodeList) XMLUtils.xpath(
raimund@245: result,
raimund@245: XPATH_WQS,
raimund@245: XPathConstants.NODESET,
raimund@245: ArtifactNamespaceContext.INSTANCE);
raimund@245:
raimund@245: if (list == null || list.getLength() == 0) {
ingo@1367: logger.warn("No wq info found.");
raimund@245:
raimund@245: throw new ServerException(ERROR_NO_WQINFO_FOUND);
raimund@245: }
raimund@245:
teichmann@5567: boolean debug = logger.isDebugEnabled();
teichmann@5567:
raimund@245: int num = list.getLength();
teichmann@5567: if (debug) {
teichmann@5567: logger.debug("Response contains " + num + " objects.");
teichmann@5567: }
raimund@245:
raimund@245: List objects =
raimund@245: new ArrayList(num);
raimund@245:
raimund@245: for (int i = 0; i < num; i++) {
teichmann@5596: WQInfoObject obj = buildWQInfoObject(list.item(i));
raimund@245:
raimund@245: if (obj != null) {
raimund@245: objects.add(obj);
raimund@245: }
raimund@245: }
raimund@245:
teichmann@5567: if (debug) {
teichmann@5567: logger.debug("Retrieved " + objects.size() + " wq values");
teichmann@5567: }
raimund@245:
teichmann@5567: WQInfoObject [] array = (WQInfoObject[])
teichmann@5567: objects.toArray(new WQInfoObject[objects.size()]);
teichmann@5567:
teichmann@5567: Arrays.sort(array, WQ_INFO_OBJECT_CMP);
teichmann@5567:
teichmann@5567: return array;
raimund@245: }
raimund@245:
teichmann@5567: public static final Comparator WQ_INFO_OBJECT_CMP =
teichmann@5567: new Comparator() {
teichmann@5567: @Override
teichmann@5567: public int compare(WQInfoObject a, WQInfoObject b) {
teichmann@5567:
teichmann@5567: // Descending by type: Qs before Ds
teichmann@5567: int cmp = a.getType().compareTo(b.getType());
teichmann@5567: if (cmp < 0) return +1;
teichmann@5567: if (cmp > 0) return -1;
teichmann@5567:
teichmann@5567: // Ascending by value
teichmann@5567: double diff = a.getValue() - b.getValue();
teichmann@5567: if (diff < 0d) return -1;
teichmann@5567: if (diff > 0d) return +1;
teichmann@5567: return 0;
teichmann@5567: }
teichmann@5567: };
raimund@245:
raimund@245: /**
raimund@245: * Extracts information for a single wq info object and intializes an
raimund@245: * WQInfoObject with them.
raimund@245: *
raimund@245: * @param node The node that contains the information.
raimund@245: *
raimund@245: * @return a valid WQInfoObject.
raimund@245: */
teichmann@5596: protected static WQInfoObject buildWQInfoObject(Node node) {
raimund@245:
teichmann@5596: String name = XMLUtils.xpathString(
teichmann@5596: node, "@name", ArtifactNamespaceContext.INSTANCE);
raimund@245:
teichmann@5596: String type = XMLUtils.xpathString(
teichmann@5596: node, "@type", ArtifactNamespaceContext.INSTANCE);
teichmann@5596:
teichmann@5596: String value = XMLUtils.xpathString(
teichmann@5596: node, "@value", ArtifactNamespaceContext.INSTANCE);
teichmann@5596:
teichmann@5596: if (name != null && type != null) {
raimund@245: try {
raimund@245: return new WQInfoObjectImpl(
raimund@245: name,
raimund@245: type,
raimund@245: new Double(value));
raimund@245: }
raimund@245: catch (NumberFormatException nfe) {
ingo@1367: logger.warn(nfe.getLocalizedMessage());
raimund@245: }
raimund@245: }
raimund@245:
ingo@1367: logger.warn("Invalid wq info object found.");
raimund@245:
raimund@245: return null;
raimund@245: }
raimund@245: }
raimund@245: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :