# HG changeset patch # User Raimund Renkert # Date 1328544082 0 # Node ID 67468c90ca68b8507b792fee34e7fc4a63c80a00 # Parent 4ba6f1fb3a03bf267d22d604285bfc2b43a7721b Added service to request discharge infos at a specific gauge. flys-client/trunk@3938 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/ChangeLog --- a/flys-client/ChangeLog Mon Feb 06 15:14:25 2012 +0000 +++ b/flys-client/ChangeLog Mon Feb 06 16:01:22 2012 +0000 @@ -1,3 +1,25 @@ +2012-02-06 Raimund Renkert + + Added service to request discharge infos at a specific gauge. + + * src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java, + src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java: + New. Interfaces for new service. + + * src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java: + New. Service implementation. + + * src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java: + New. Service to request the discharge infos and provide the data via servlet + to a data source object. + + * src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java, + src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java: + New. Interface and implementation for objects containing discharge infos. + + * src/main/webapp/WEB-INF/web.xml: + Added entries for discharge info servlets and servlet mappings. + 2012-02-06 Sascha L. Teichmann * src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties, diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoService.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,26 @@ +package de.intevation.flys.client.client.services; + +import com.google.gwt.user.client.rpc.RemoteService; +import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.shared.model.DischargeInfoObject; + +/** + * This service is used to fetch a list of DischargeInfoObjects from artifact + * server for a specific gauge. + * + * @author Raimund Renkert + */ +@RemoteServiceRelativePath("dischargeinfo") +public interface DischargeInfoService extends RemoteService { + + /** + * This method returns a list of DischargeInfoObjects for a specific gauge. + */ + DischargeInfoObject[] getDischargeInfo( + String locale, + long gauge) + throws ServerException; +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/client/services/DischargeInfoServiceAsync.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,18 @@ +package de.intevation.flys.client.client.services; + +import com.google.gwt.user.client.rpc.AsyncCallback; + +import de.intevation.flys.client.shared.model.DischargeInfoObject; + + +/** + * @author Raimund Renkert + */ +public interface DischargeInfoServiceAsync { + + void getDischargeInfo( + String locale, + long gauge, + AsyncCallback cb); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,138 @@ +package de.intevation.flys.client.server; + +import java.util.ArrayList; +import java.util.List; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import org.apache.log4j.Logger; + +import com.google.gwt.user.server.rpc.RemoteServiceServlet; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.client.services.DischargeInfoService; +import de.intevation.flys.client.shared.model.DischargeInfoObject; +import de.intevation.flys.client.shared.model.DischargeInfoObjectImpl; + + +/** + * @author Raimund Renkert + */ +public class DischargeInfoServiceImpl +extends RemoteServiceServlet +implements DischargeInfoService +{ + private static final Logger logger = + Logger.getLogger(DischargeInfoServiceImpl.class); + + public static final String ERROR_NO_DISCHARGEINFO_FOUND = + "error_no_dischargeinfo_found"; + + public static final String XPATH_DISTANCES = "art:discharges/art:discharge"; + + + public DischargeInfoObject[] getDischargeInfo( + String locale, + long gauge) + throws ServerException + { + logger.info("DichargeInfoServiceImpl.getDischargeInfo"); + + String url = getServletContext().getInitParameter("server-url"); + + Document doc = XMLUtils.newDocument(); + + XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element gaugeEl = ec.create("gauge"); + gaugeEl.setTextContent(String.valueOf(gauge)); + + doc.appendChild(gaugeEl); + + HttpClient client = new HttpClientImpl(url, locale); + + try { + Document result = client.callService(url, "dischargeinfo", doc); + + logger.debug("Extract discharge info objects now."); + DischargeInfoObject[] objects = extractDischargeInfoObjects(result); + + if (objects != null && objects.length > 0) { + return objects; + } + } + catch (ConnectionException ce) { + logger.error(ce, ce); + } + + throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); + } + + protected DischargeInfoObject[] extractDischargeInfoObjects( + Document result + ) + throws ServerException { + NodeList list = result.getElementsByTagName("discharge"); + + if (list == null || list.getLength() == 0) { + logger.warn("No discharge info found."); + throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); + } + + int num = list.getLength(); + logger.debug("Response contains " + num + " objects."); + + List objects = + new ArrayList(num); + + for (int i = 0; i < num; i++) { + DischargeInfoObject obj = buildDischargeInfoObject( + (Element)list.item(i)); + + if (obj != null) { + objects.add(obj); + } + } + + logger.debug("Retrieved " + objects.size() + " discharges."); + + return (DischargeInfoObject[]) + objects.toArray(new DischargeInfoObject[num]); + + } + + protected DischargeInfoObject buildDischargeInfoObject(Element node) { + + String desc = node.getAttribute("description").trim(); + String start = node.getAttribute("start").trim(); + String end = node.getAttribute("end").trim(); + + if (start.length() > 0 && end.length() > 0) { + try { + Integer startYear = new Integer(start); + Integer endYear = new Integer(end); + return new DischargeInfoObjectImpl(desc, startYear, endYear); + } + catch (NumberFormatException nfe) { + logger.warn(nfe.getLocalizedMessage()); + } + } + + logger.warn("Invalid distance info object found."); + + return null; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoXML.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,82 @@ +package de.intevation.flys.client.server; + +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.apache.log4j.Logger; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; +import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; + + +/** + * @author Raimund Renkert + */ +public class DischargeInfoXML +extends HttpServlet +{ + private static final Logger logger = Logger.getLogger(DischargeInfoXML.class); + + + public static final String ERROR_NO_DISTANCEINFO_FOUND = + "error_no_dischargeinfo_found"; + + + public void doGet(HttpServletRequest req, HttpServletResponse resp) { + logger.info("DischargeInfoXML.doGet"); + + String url = getServletContext().getInitParameter("server-url"); + + String gauge = req.getParameter("gauge"); + + Document doc = XMLUtils.newDocument(); + + XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( + doc, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + + Element gaugeEl = ec.create("gauge"); + gaugeEl.setTextContent(gauge); + + doc.appendChild(gaugeEl); + + HttpClient client = new HttpClientImpl(url); + + try { + InputStream in = (InputStream) client.callService( + url, "dischargeinfo", doc, new StreamResponseHandler()); + + OutputStream out = resp.getOutputStream(); + + byte[] b = new byte[4096]; + int i; + while ((i = in.read(b)) >= 0) { + out.write(b, 0, i); + } + + out.flush(); + out.close(); + } + catch (ConnectionException ce) { + logger.error(ce, ce); + } + catch (IOException ioe) { + logger.error(ioe, ioe); + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObject.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,17 @@ +package de.intevation.flys.client.shared.model; + +import java.io.Serializable; + + +/** + * @author Raimund Renkert + */ +public interface DischargeInfoObject extends Serializable { + + String getDescription(); + + Integer getStartYear(); + + Integer getEndYear(); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DischargeInfoObjectImpl.java Mon Feb 06 16:01:22 2012 +0000 @@ -0,0 +1,44 @@ +package de.intevation.flys.client.shared.model; + + +/** + * @author Raimund Renkert + */ +public class DischargeInfoObjectImpl implements DischargeInfoObject { + + protected String description; + + protected Integer startYear; + + protected Integer endYear; + + + public DischargeInfoObjectImpl() { + } + + + public DischargeInfoObjectImpl( + String description, + Integer startYear, + Integer endYear + ) { + this.description = description; + this.startYear = startYear; + this.endYear = endYear; + } + + public String getDescription() { + return description; + } + + + public Integer getStartYear() { + return startYear; + } + + + public Integer getEndYear() { + return endYear; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 4ba6f1fb3a03 -r 67468c90ca68 flys-client/src/main/webapp/WEB-INF/web.xml --- a/flys-client/src/main/webapp/WEB-INF/web.xml Mon Feb 06 15:14:25 2012 +0000 +++ b/flys-client/src/main/webapp/WEB-INF/web.xml Mon Feb 06 16:01:22 2012 +0000 @@ -7,7 +7,7 @@ server-url - http://localhost:8181 + http://localhost:8182 @@ -164,6 +164,26 @@ + dischargeinfo + de.intevation.flys.client.server.DischargeInfoServiceImpl + + + + dischargeinfo + /flys/dischargeinfo + + + + DischargeInfoXML + de.intevation.flys.client.server.DischargeInfoXML + + + + DischargeInfoXML + /flys/dischargeinfoxml + + + meta-data de.intevation.flys.client.server.MetaDataServiceImpl