sascha@2489: package de.intevation.flys.client.server; sascha@2489: sascha@2489: import com.google.gwt.user.server.rpc.RemoteServiceServlet; sascha@2489: sascha@2493: import de.intevation.artifacts.common.utils.XMLUtils; 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.FixEvent; raimund@2505: import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector; raimund@2505: sascha@2915: import de.intevation.flys.client.shared.model.FixingsOverviewInfo; sascha@2915: sascha@2493: import java.io.IOException; sascha@2493: import java.io.InputStream; sascha@2493: sascha@2915: import java.util.ArrayList; sascha@2915: import java.util.List; sascha@2915: sascha@2915: import javax.xml.xpath.XPathConstants; sascha@2915: sascha@2493: import org.apache.log4j.Logger; sascha@2493: sascha@2915: import org.w3c.dom.Document; sascha@2915: import org.w3c.dom.Element; sascha@2915: import org.w3c.dom.NodeList; sascha@2915: 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: raimund@2505: sascha@2489: @Override raimund@2505: public FixingsOverviewInfo generateOverview( raimund@2505: String locale, sascha@2489: String uuid, raimund@2505: String filter, sascha@3376: boolean checkboxes, sascha@3376: 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@2916: boolean debug = log.isDebugEnabled(); sascha@2916: sascha@2916: if (debug) { sascha@2916: log.debug("JSON filter: ------------------"); sascha@2916: log.debug(filter); sascha@2916: } sascha@2916: 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: sascha@2916: if (debug) { sascha@2916: log.debug("XML filter: ------------------"); sascha@2916: log.debug(XMLUtils.toString(filterDoc)); sascha@2916: } sascha@2916: sascha@2493: try { sascha@2493: String url = getServletContext().getInitParameter("server-url"); sascha@2493: HttpClient client = new HttpClientImpl(url, locale); sascha@2921: Document resultDoc = sascha@2493: client.callService(url, SERVICE_NAME, filterDoc); sascha@2916: sascha@2916: if (debug) { sascha@2916: log.debug("Result XML: -----------"); sascha@2916: log.debug(XMLUtils.toString(resultDoc)); sascha@2916: } sascha@2916: sascha@3376: FixingsOverviewInfo i = getInfo( sascha@3376: locale, 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( sascha@3376: String locale, raimund@2505: Document doc, sascha@2915: String uuid, sascha@2915: boolean checkboxes, sascha@2915: String callback raimund@2505: ) { sascha@3376: // TODO: Find a more general solution. sascha@3376: locale = locale == null || locale.toLowerCase().startsWith("de") sascha@3376: ? "de" sascha@3376: : "en"; sascha@3376: sascha@2915: InputStream transform = getServletContext() sascha@2915: .getResourceAsStream(XSL_TRANSFORM); raimund@2505: sascha@2915: if (transform == null) { sascha@2915: log.warn("transform not found"); sascha@2915: return null; sascha@2915: } sascha@2915: sascha@2915: String result = null; sascha@2915: try { sascha@2915: XSLTransformer xformer = new XSLTransformer(); sascha@3376: xformer.addParameter("locale", locale); sascha@2915: xformer.addParameter("project-uuid", uuid); sascha@2915: xformer.addParameter( sascha@2915: "render-checkboxes", sascha@2915: checkboxes ? Boolean.TRUE : Boolean.FALSE); sascha@2915: xformer.addParameter("callback", callback); sascha@2915: result = xformer.transform(doc, transform); sascha@2915: } sascha@2915: finally { sascha@2915: try { transform.close(); } sascha@2915: catch (IOException ioe) {} sascha@2915: } sascha@2915: sascha@2916: if (log.isDebugEnabled()) { sascha@2916: log.debug("--------------------------------------"); sascha@2916: log.debug(result); sascha@2916: log.debug("--------------------------------------"); sascha@2916: } sascha@2916: sascha@2915: int rid = -1; raimund@2505: double from = -1; sascha@2915: double to = -1; sascha@2915: sascha@2915: String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null); sascha@2915: String river = XMLUtils.xpathString(doc, XPATH_RIVER, null); raimund@2505: String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null); sascha@2915: String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null); sascha@2915: raimund@2505: try { sascha@2915: rid = Integer.parseInt(rid_str); sascha@2915: from = Double.parseDouble(from_str); sascha@2915: to = Double.parseDouble(to_str); 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( sascha@3376: rid, sascha@3376: river, sascha@3376: from, sascha@3376: to, sascha@3376: fixEvents, sascha@3376: result); raimund@2505: } raimund@2505: raimund@2505: raimund@2505: protected List getFixEvents(Document doc) { sascha@2916: List list = new ArrayList(); sascha@2916: 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!"); sascha@2916: return list; raimund@2505: } raimund@2505: sascha@2915: for (int i = 0, E = events.getLength(); i < E; i++) { sascha@2915: Element n = (Element)events.item(i); raimund@2505: List sectors = getSectors(n); sascha@2915: String cid = n.getAttribute("cid"); sascha@2915: String date = n.getAttribute("date");; sascha@2915: String name = n.getAttribute("description"); sascha@2915: list.add(new FixEvent( cid, date, name, sectors)); raimund@2505: } raimund@2505: return list; raimund@2505: } raimund@2505: sascha@2915: protected List getSectors(Element event) { sascha@2915: NodeList sectors = event.getElementsByTagName("sector"); sascha@2915: sascha@2915: if (sectors.getLength() == 0) { raimund@2505: log.warn("No Sectors in Event!"); raimund@2505: return null; raimund@2505: } raimund@2505: sascha@2915: List list = new ArrayList(); sascha@2915: for (int i = 0, S = sectors.getLength(); i < S; i++) { sascha@2915: Element n = (Element)sectors.item(i); sascha@2915: int cls = -1; raimund@2505: double from = -1; sascha@2915: double to = -1; sascha@2915: String cls_str = n.getAttribute("class"); sascha@2915: String from_str = n.getAttribute("from"); sascha@2915: String to_str = n.getAttribute("to"); raimund@2505: try { sascha@2915: cls = Integer.parseInt(cls_str); sascha@2915: from = Double.parseDouble(from_str); sascha@2915: to = Double.parseDouble(to_str); raimund@2505: } raimund@2505: catch(NumberFormatException nfe) { raimund@2505: log.warn(nfe, nfe); raimund@2505: } sascha@2915: list.add(new Sector(cls, from, 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 :