raimund@1600: package de.intevation.flys.client.server;
raimund@1600:
raimund@1600: import java.util.ArrayList;
raimund@1600: import java.util.List;
raimund@1600:
raimund@1600: import org.w3c.dom.Document;
raimund@1600: import org.w3c.dom.Element;
raimund@1600: import org.w3c.dom.NodeList;
raimund@1600:
raimund@1600: import org.apache.log4j.Logger;
raimund@1600:
raimund@1600: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
raimund@1600:
raimund@1600: import de.intevation.artifacts.common.ArtifactNamespaceContext;
raimund@1600: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@1600:
raimund@1600: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
raimund@1600: import de.intevation.artifacts.httpclient.http.HttpClient;
raimund@1600: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
raimund@1600:
raimund@1600: import de.intevation.flys.client.shared.exceptions.ServerException;
raimund@1600: import de.intevation.flys.client.client.services.DischargeInfoService;
raimund@1600: import de.intevation.flys.client.shared.model.DischargeInfoObject;
raimund@1600: import de.intevation.flys.client.shared.model.DischargeInfoObjectImpl;
raimund@1600:
raimund@1600:
raimund@1600: /**
raimund@1600: * @author Raimund Renkert
raimund@1600: */
raimund@1600: public class DischargeInfoServiceImpl
raimund@1600: extends RemoteServiceServlet
raimund@1600: implements DischargeInfoService
raimund@1600: {
raimund@1600: private static final Logger logger =
raimund@1600: Logger.getLogger(DischargeInfoServiceImpl.class);
raimund@1600:
raimund@1600: public static final String ERROR_NO_DISCHARGEINFO_FOUND =
raimund@1600: "error_no_dischargeinfo_found";
raimund@1600:
raimund@1600: public static final String XPATH_DISTANCES = "art:discharges/art:discharge";
raimund@1600:
raimund@1600:
raimund@1600: public DischargeInfoObject[] getDischargeInfo(
raimund@1600: String locale,
raimund@1600: long gauge)
raimund@1600: throws ServerException
raimund@1600: {
raimund@1600: logger.info("DichargeInfoServiceImpl.getDischargeInfo");
raimund@1600:
raimund@1600: String url = getServletContext().getInitParameter("server-url");
raimund@1600:
raimund@1600: Document doc = XMLUtils.newDocument();
raimund@1600:
raimund@1600: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
raimund@1600: doc,
raimund@1600: ArtifactNamespaceContext.NAMESPACE_URI,
raimund@1600: ArtifactNamespaceContext.NAMESPACE_PREFIX);
raimund@1600:
raimund@1600: Element gaugeEl = ec.create("gauge");
raimund@1600: gaugeEl.setTextContent(String.valueOf(gauge));
raimund@1600:
raimund@1600: doc.appendChild(gaugeEl);
raimund@1600:
raimund@1600: HttpClient client = new HttpClientImpl(url, locale);
raimund@1600:
raimund@1600: try {
raimund@1600: Document result = client.callService(url, "dischargeinfo", doc);
raimund@1600:
raimund@1600: logger.debug("Extract discharge info objects now.");
raimund@1600: DischargeInfoObject[] objects = extractDischargeInfoObjects(result);
raimund@1600:
raimund@1600: if (objects != null && objects.length > 0) {
raimund@1600: return objects;
raimund@1600: }
raimund@1600: }
raimund@1600: catch (ConnectionException ce) {
raimund@1600: logger.error(ce, ce);
raimund@1600: }
raimund@1600:
raimund@1600: throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND);
raimund@1600: }
raimund@1600:
raimund@1600: protected DischargeInfoObject[] extractDischargeInfoObjects(
raimund@1600: Document result
raimund@1600: )
raimund@1600: throws ServerException {
raimund@1600: NodeList list = result.getElementsByTagName("discharge");
raimund@1600:
raimund@1600: if (list == null || list.getLength() == 0) {
raimund@1600: logger.warn("No discharge info found.");
raimund@1600: throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND);
raimund@1600: }
raimund@1600:
raimund@1600: int num = list.getLength();
raimund@1600: logger.debug("Response contains " + num + " objects.");
raimund@1600:
raimund@1600: List objects =
raimund@1600: new ArrayList(num);
raimund@1600:
raimund@1600: for (int i = 0; i < num; i++) {
raimund@1600: DischargeInfoObject obj = buildDischargeInfoObject(
raimund@1600: (Element)list.item(i));
raimund@1600:
raimund@1600: if (obj != null) {
raimund@1600: objects.add(obj);
raimund@1600: }
raimund@1600: }
raimund@1600:
raimund@1600: logger.debug("Retrieved " + objects.size() + " discharges.");
raimund@1600:
raimund@1600: return (DischargeInfoObject[])
raimund@1600: objects.toArray(new DischargeInfoObject[num]);
raimund@1600:
raimund@1600: }
raimund@1600:
raimund@1600: protected DischargeInfoObject buildDischargeInfoObject(Element node) {
raimund@1600:
raimund@1600: String desc = node.getAttribute("description").trim();
raimund@1600: String start = node.getAttribute("start").trim();
raimund@1600: String end = node.getAttribute("end").trim();
raimund@1600:
raimund@1600: if (start.length() > 0 && end.length() > 0) {
raimund@1600: try {
raimund@1600: Integer startYear = new Integer(start);
raimund@1600: Integer endYear = new Integer(end);
raimund@1600: return new DischargeInfoObjectImpl(desc, startYear, endYear);
raimund@1600: }
raimund@1600: catch (NumberFormatException nfe) {
raimund@1600: logger.warn(nfe.getLocalizedMessage());
raimund@1600: }
raimund@1600: }
raimund@1600:
raimund@1600: logger.warn("Invalid distance info object found.");
raimund@1600:
raimund@1600: return null;
raimund@1600: }
raimund@1600: }
raimund@1600: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :