raimund@2540: package de.intevation.flys.client.server;
raimund@2540:
raimund@2540: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
raimund@2540:
raimund@2540: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@2540:
raimund@2540: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
sascha@2911:
raimund@2540: import de.intevation.artifacts.httpclient.http.HttpClient;
raimund@2540: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
raimund@2540:
raimund@2540: import de.intevation.flys.client.client.services.ThemeListingService;
raimund@2540:
sascha@2911: import de.intevation.flys.client.shared.exceptions.ServerException;
sascha@2911:
sascha@2911: import de.intevation.flys.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: {
raimund@2540: private static final Logger logger =
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) {
raimund@2540: logger.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 :