sascha@2489: package de.intevation.flys.client.server; sascha@2489: raimund@2505: import java.util.List; raimund@2505: import java.util.ArrayList; raimund@2505: raimund@2505: import org.w3c.dom.Document; raimund@2505: import org.w3c.dom.Node; raimund@2505: import org.w3c.dom.NodeList; raimund@2505: raimund@2505: import javax.xml.xpath.XPathConstants; raimund@2505: sascha@2489: import com.google.gwt.user.server.rpc.RemoteServiceServlet; sascha@2489: sascha@2493: import de.intevation.artifacts.common.utils.XMLUtils; raimund@2505: sascha@2493: import de.intevation.artifacts.common.utils.XSLTransformer; sascha@2493: sascha@2493: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; sascha@2493: sascha@2493: import de.intevation.artifacts.httpclient.http.HttpClient; sascha@2493: import de.intevation.artifacts.httpclient.http.HttpClientImpl; sascha@2493: sascha@2489: import de.intevation.flys.client.client.services.FixingsOverviewService; sascha@2489: sascha@2489: import de.intevation.flys.client.shared.exceptions.ServerException; sascha@2489: raimund@2505: import de.intevation.flys.client.shared.model.FixingsOverviewInfo; raimund@2505: import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent; raimund@2505: import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector; raimund@2505: sascha@2493: import java.io.IOException; sascha@2493: import java.io.InputStream; sascha@2493: sascha@2493: import org.apache.log4j.Logger; sascha@2493: sascha@2489: public class FixingsOverviewServiceImpl sascha@2489: extends RemoteServiceServlet sascha@2489: implements FixingsOverviewService sascha@2489: { sascha@2493: private static final Logger log = sascha@2493: Logger.getLogger(FixingsOverviewServiceImpl.class); sascha@2493: sascha@2493: public static final String SERVICE_NAME = "fixings-overview"; sascha@2493: sascha@2493: public static final String XSL_TRANSFORM = sascha@2493: "/WEB-INF/stylesheets/fixoverview2html.xsl"; sascha@2493: raimund@2505: protected static final String XPATH_RID = "/fixings/river/@rid"; raimund@2505: protected static final String XPATH_RIVER = "/fixings/river/@name"; raimund@2505: protected static final String XPATH_RFROM = "/fixings/river/@from"; raimund@2505: protected static final String XPATH_RTO = "/fixings/river/@to"; raimund@2505: raimund@2505: protected static final String XPATH_EVENT = "/fixings/events/event"; raimund@2505: protected static final String XPATH_SECTORS = "sector"; raimund@2505: raimund@2505: protected static final String XPATH_CID = "@cid"; raimund@2505: protected static final String XPATH_DATE = "@date"; raimund@2536: protected static final String XPATH_DESCRIPTION = "@description"; raimund@2505: protected static final String XPATH_CLS = "@class"; raimund@2505: protected static final String XPATH_FROM = "@from"; raimund@2505: protected static final String XPATH_TO = "@to"; raimund@2505: sascha@2489: @Override raimund@2505: public FixingsOverviewInfo generateOverview( raimund@2505: String locale, sascha@2489: String uuid, raimund@2505: String filter, raimund@2505: boolean checkboxes, raimund@2505: String callback raimund@2505: ) sascha@2489: throws ServerException sascha@2489: { sascha@2493: log.info("FixingsOverviewServiceImpl.doGet"); sascha@2493: sascha@2493: if (filter == null || filter.length() == 0) { sascha@2493: log.warn("Missing 'filter' parameter."); raimund@2505: return null; sascha@2493: } sascha@2493: sascha@2493: Document filterDoc = XMLUtils.jsonToXML(filter); sascha@2493: sascha@2493: if (filterDoc == null) { sascha@2493: log.warn("Creating filter document failed."); raimund@2505: return null; sascha@2493: } sascha@2493: raimund@2505: /* filterDoc = XMLUtils.newDocument(); raimund@2505: ElementCreator ec = new ElementCreator(filterDoc, null, null); raimund@2505: Element f = ec.create("fixings"); raimund@2505: Element r = ec.create("river"); raimund@2505: r.setAttribute("name", "Saar"); raimund@2505: f.appendChild(r); raimund@2505: filterDoc.appendChild(f); raimund@2505: */ sascha@2493: try { sascha@2493: String url = getServletContext().getInitParameter("server-url"); sascha@2493: HttpClient client = new HttpClientImpl(url, locale); sascha@2493: Document resultDoc = sascha@2493: client.callService(url, SERVICE_NAME, filterDoc); raimund@2505: FixingsOverviewInfo i = getInfo(resultDoc, uuid, checkboxes, callback); raimund@2505: return i; sascha@2493: } sascha@2493: catch (ConnectionException ce) { sascha@2493: log.error(ce); sascha@2493: } raimund@2505: return null; raimund@2505: } raimund@2505: raimund@2505: raimund@2505: protected FixingsOverviewInfo getInfo( raimund@2505: Document doc, raimund@2505: String uuid, raimund@2505: boolean checkboxes, raimund@2505: String callback raimund@2505: ) { raimund@2505: InputStream transform = raimund@2505: getServletContext().getResourceAsStream(XSL_TRANSFORM); raimund@2505: XSLTransformer xformer = new XSLTransformer(); raimund@2505: xformer.addParameter("project-uuid", uuid); raimund@2505: xformer.addParameter( raimund@2505: "render-checkboxes", checkboxes ? Boolean.TRUE : Boolean.FALSE); raimund@2505: xformer.addParameter("callback", callback); raimund@2505: String result = xformer.transform(doc, transform); raimund@2505: raimund@2505: try { transform.close(); } raimund@2505: catch (IOException ioe) {} raimund@2505: raimund@2505: int rid = -1; raimund@2505: double from = -1; raimund@2505: double to = -1; raimund@2505: String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null); raimund@2505: String river = XMLUtils.xpathString(doc, XPATH_RIVER, null); raimund@2505: String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null); raimund@2505: String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null); raimund@2505: try { raimund@2505: rid = Integer.valueOf(rid_str).intValue(); raimund@2505: from = Double.valueOf(from_str).doubleValue(); raimund@2505: to = Double.valueOf(to_str).doubleValue(); raimund@2505: } raimund@2505: catch(NumberFormatException nfe) { raimund@2505: log.warn(nfe, nfe); sascha@2493: } sascha@2493: raimund@2505: List fixEvents = getFixEvents(doc); raimund@2505: return new FixingsOverviewInfo( raimund@2505: rid, raimund@2505: river, raimund@2505: from, raimund@2505: to, raimund@2505: fixEvents, raimund@2505: result); raimund@2505: } raimund@2505: raimund@2505: raimund@2505: protected List getFixEvents(Document doc) { raimund@2505: NodeList events = (NodeList) XMLUtils.xpath( raimund@2505: doc, raimund@2505: XPATH_EVENT, raimund@2505: XPathConstants.NODESET, raimund@2505: null); raimund@2505: raimund@2505: if (events == null || events.getLength() == 0) { raimund@2505: log.warn("No events in Overview!"); raimund@2505: return null; raimund@2505: } raimund@2505: raimund@2505: List list = raimund@2505: new ArrayList(); raimund@2505: for (int i = 0; i < events.getLength(); i++) { raimund@2505: Node n = events.item(i); raimund@2505: List sectors = getSectors(n); raimund@2505: String cid = XMLUtils.xpathString(n, XPATH_CID, null); raimund@2505: log.debug("'" + cid + "'"); raimund@2505: String date = XMLUtils.xpathString(n, XPATH_DATE, null); raimund@2536: String name = XMLUtils.xpathString(n, XPATH_DESCRIPTION, null); raimund@2505: list.add(new FixEvent( raimund@2505: cid, raimund@2505: date, raimund@2505: name, raimund@2505: sectors)); raimund@2505: } raimund@2505: return list; raimund@2505: } raimund@2505: raimund@2505: protected List getSectors(Node event) { raimund@2505: NodeList sectors = (NodeList) XMLUtils.xpath( raimund@2505: event, raimund@2505: XPATH_SECTORS, raimund@2505: XPathConstants.NODESET, raimund@2505: null); raimund@2505: if (sectors == null || sectors.getLength() == 0) { raimund@2505: log.warn("No Sectors in Event!"); raimund@2505: return null; raimund@2505: } raimund@2505: raimund@2505: List list = raimund@2505: new ArrayList(); raimund@2505: for (int i = 0; i < sectors.getLength(); i++) { raimund@2505: Node n = sectors.item(i); raimund@2505: int cls = -1; raimund@2505: double from = -1; raimund@2505: double to = -1; raimund@2505: String cls_str = XMLUtils.xpathString(n, XPATH_CLS, null); raimund@2505: String from_str = XMLUtils.xpathString(n, XPATH_FROM, null); raimund@2505: String to_str = XMLUtils.xpathString(n, XPATH_TO, null); raimund@2505: try { raimund@2505: cls = Integer.valueOf(cls_str).intValue(); raimund@2505: from = Double.valueOf(from_str).doubleValue(); raimund@2505: to = Double.valueOf(to_str).doubleValue(); raimund@2505: } raimund@2505: catch(NumberFormatException nfe) { raimund@2505: log.warn(nfe, nfe); raimund@2505: } raimund@2505: list.add(new Sector( raimund@2505: cls, raimund@2505: from, raimund@2505: to)); raimund@2505: } raimund@2505: return list; sascha@2489: } sascha@2489: } sascha@2489: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :