comparison flys-client/src/main/java/de/intevation/flys/client/server/ThemeListingServiceImpl.java @ 2540:e75b15818435

Added a style chooser to the style editor to provide predefined styles and implemented a service to request theme styles. flys-client/trunk@4471 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 23 May 2012 08:59:49 +0000
parents
children cd068ff7966d
comparison
equal deleted inserted replaced
2539:a9cdd62aa73e 2540:e75b15818435
1 package de.intevation.flys.client.server;
2
3 import java.util.Map;
4 import java.util.HashMap;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.NodeList;
10 import org.w3c.dom.Element;
11
12 import org.apache.log4j.Logger;
13
14 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
15
16 import de.intevation.artifacts.common.utils.XMLUtils;
17
18 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
19 import de.intevation.artifacts.httpclient.http.HttpClient;
20 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
21
22 import de.intevation.flys.client.shared.exceptions.ServerException;
23 import de.intevation.flys.client.shared.model.Style;
24 import de.intevation.flys.client.shared.model.StyleSetting;
25 import de.intevation.flys.client.client.services.ThemeListingService;
26
27
28 /**
29 * This interface provides a method to list themes filtered by name.
30 *
31 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
32 */
33 public class ThemeListingServiceImpl
34 extends RemoteServiceServlet
35 implements ThemeListingService
36 {
37 private static final Logger logger =
38 Logger.getLogger(ThemeListingServiceImpl.class);
39
40
41 private static final String XPATH_THEME_GROUPS = "/themes/themegroup";
42 /** The error message key that is thrown if an error occured while reading
43 * the supported rivers from server.*/
44 public static final String ERROR_NO_GROUPS_FOUND = "error_no_groups_found";
45
46
47 public Map<String, Style> list(String locale, String name)
48 throws ServerException
49 {
50 String url = getServletContext().getInitParameter("server-url");
51
52 Document doc = XMLUtils.newDocument();
53
54 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
55 doc,
56 null,
57 null);
58
59 Element e = ec.create("theme");
60 ec.addAttr(e, "name", name);
61 doc.appendChild(e);
62 HttpClient client = new HttpClientImpl(url, locale);
63
64 try {
65 Document res = client.callService(url, "themelisting", doc);
66
67 NodeList themeGroups = (NodeList) XMLUtils.xpath(
68 res,
69 XPATH_THEME_GROUPS,
70 XPathConstants.NODESET,
71 null);
72
73 if (themeGroups == null || themeGroups.getLength() == 0) {
74 throw new ServerException(ERROR_NO_GROUPS_FOUND);
75 }
76
77 int count = themeGroups.getLength();
78
79 Map<String, Style> theStyles = new HashMap<String, Style>(count);
80
81 for (int i = 0; i < count; i++) {
82 Element tmp = (Element)themeGroups.item(i);
83
84 String groupName = tmp.getAttribute("name");
85 NodeList theTheme = (NodeList) XMLUtils.xpath(
86 tmp,
87 "theme",
88 XPathConstants.NODESET,
89 null);
90
91 for (int j = 0; j < theTheme.getLength(); j++) {
92 Element elem = (Element) theTheme.item(j);
93 theStyles.put(groupName, getStyle(elem));
94 }
95 }
96
97 return theStyles;
98 }
99 catch (ConnectionException ce) {
100 logger.error(ce, ce);
101 }
102
103 throw new ServerException(ERROR_NO_GROUPS_FOUND);
104 }
105
106
107 protected Style getStyle (Element element) {
108 if (!element.getTagName().equals("theme")) {
109 return null;
110 }
111
112 NodeList list = element.getElementsByTagName("field");
113 Style style = new Style();
114
115 style.setName (element.getAttribute("name"));
116 style.setFacet (element.getAttribute("facet"));
117
118 try {
119 int ndx = Integer.parseInt(element.getAttribute("index"));
120 style.setIndex (ndx);
121 }
122 catch(NumberFormatException nfe) {
123 return null;
124 }
125
126 for(int i = 0; i < list.getLength(); i++) {
127 Element e = (Element) list.item(i);
128 StyleSetting set = new StyleSetting (
129 e.getAttribute("name"),
130 e.getAttribute("default"),
131 e.getAttribute("display"),
132 e.getAttribute("hints"),
133 e.getAttribute("type"));
134 style.appendStyleSetting(set);
135 }
136 return style;
137 }
138 }
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org