teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; raimund@2540: raimund@2540: import com.google.gwt.user.server.rpc.RemoteServiceServlet; raimund@2540: teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; sascha@2911: teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; raimund@2540: teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; raimund@2540: teichmann@5835: import org.dive4elements.river.client.client.services.ThemeListingService; sascha@2911: teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException; teichmann@5835: teichmann@5835: import org.dive4elements.river.client.shared.model.Style; sascha@2911: sascha@2911: import java.util.HashMap; sascha@2911: import java.util.Map; sascha@2911: sascha@2911: import javax.xml.xpath.XPathConstants; sascha@2911: sascha@2911: import org.apache.log4j.Logger; sascha@2911: sascha@2911: import org.w3c.dom.Document; sascha@2911: import org.w3c.dom.Element; sascha@2911: import org.w3c.dom.NodeList; raimund@2540: raimund@2540: /** raimund@2540: * This interface provides a method to list themes filtered by name. raimund@2540: * raimund@2540: * @author Raimund Renkert raimund@2540: */ raimund@2540: public class ThemeListingServiceImpl raimund@2540: extends RemoteServiceServlet raimund@2540: implements ThemeListingService raimund@2540: { teichmann@8203: private static final Logger log = raimund@2540: Logger.getLogger(ThemeListingServiceImpl.class); raimund@2540: raimund@2540: raimund@2540: private static final String XPATH_THEME_GROUPS = "/themes/themegroup"; raimund@2540: /** The error message key that is thrown if an error occured while reading raimund@2540: * the supported rivers from server.*/ raimund@2540: public static final String ERROR_NO_GROUPS_FOUND = "error_no_groups_found"; raimund@2540: raimund@2540: raimund@2540: public Map list(String locale, String name) raimund@2540: throws ServerException raimund@2540: { raimund@2540: String url = getServletContext().getInitParameter("server-url"); raimund@2540: raimund@2540: Document doc = XMLUtils.newDocument(); raimund@2540: raimund@2540: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( raimund@2540: doc, raimund@2540: null, raimund@2540: null); raimund@2540: raimund@2540: Element e = ec.create("theme"); raimund@2540: ec.addAttr(e, "name", name); raimund@2540: doc.appendChild(e); raimund@2540: HttpClient client = new HttpClientImpl(url, locale); raimund@2540: raimund@2540: try { raimund@2540: Document res = client.callService(url, "themelisting", doc); raimund@2540: raimund@2540: NodeList themeGroups = (NodeList) XMLUtils.xpath( raimund@2540: res, raimund@2540: XPATH_THEME_GROUPS, raimund@2540: XPathConstants.NODESET, raimund@2540: null); raimund@2540: raimund@2540: if (themeGroups == null || themeGroups.getLength() == 0) { raimund@2540: throw new ServerException(ERROR_NO_GROUPS_FOUND); raimund@2540: } raimund@2540: raimund@2540: int count = themeGroups.getLength(); raimund@2540: raimund@2540: Map theStyles = new HashMap(count); raimund@2540: raimund@2540: for (int i = 0; i < count; i++) { raimund@2540: Element tmp = (Element)themeGroups.item(i); raimund@2540: raimund@2540: String groupName = tmp.getAttribute("name"); raimund@2540: NodeList theTheme = (NodeList) XMLUtils.xpath( raimund@2540: tmp, raimund@2540: "theme", raimund@2540: XPathConstants.NODESET, raimund@2540: null); raimund@2540: raimund@2540: for (int j = 0; j < theTheme.getLength(); j++) { raimund@2540: Element elem = (Element) theTheme.item(j); ingo@2908: theStyles.put(groupName, StyleHelper.getStyle(elem)); raimund@2540: } raimund@2540: } raimund@2540: raimund@2540: return theStyles; raimund@2540: } raimund@2540: catch (ConnectionException ce) { teichmann@8203: log.error(ce, ce); raimund@2540: } raimund@2540: raimund@2540: throw new ServerException(ERROR_NO_GROUPS_FOUND); raimund@2540: } raimund@2540: } raimund@2540: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :