Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/CollectionItemAttributeServiceImpl.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/CollectionItemAttributeServiceImpl.java@b296d435fc69 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import org.w3c.dom.Document; | |
4 import org.w3c.dom.Element; | |
5 import org.w3c.dom.NodeList; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
10 | |
11 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
12 import de.intevation.artifacts.httpclient.http.HttpClient; | |
13 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
14 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; | |
15 | |
16 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
17 import de.intevation.artifacts.common.utils.XMLUtils; | |
18 | |
19 import de.intevation.flys.client.shared.exceptions.ServerException; | |
20 import de.intevation.flys.client.client.services.CollectionItemAttributeService; | |
21 | |
22 import de.intevation.flys.client.shared.model.Collection; | |
23 import de.intevation.flys.client.shared.model.CollectionItemAttribute; | |
24 import de.intevation.flys.client.shared.model.Style; | |
25 import de.intevation.flys.client.shared.model.StyleSetting; | |
26 | |
27 | |
28 /** | |
29 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
30 */ | |
31 public class CollectionItemAttributeServiceImpl | |
32 extends RemoteServiceServlet | |
33 implements CollectionItemAttributeService | |
34 { | |
35 private static final Logger logger = | |
36 Logger.getLogger(CollectionItemAttributeServiceImpl.class); | |
37 | |
38 | |
39 public static final String XPATH_RESULT = "/art:result/text()"; | |
40 | |
41 public static final String OPERATION_FAILURE = "FAILED"; | |
42 | |
43 public static final String ERROR_NO_STYLES_FOUND = | |
44 "error_no_theme_styles_found"; | |
45 | |
46 | |
47 public CollectionItemAttribute getCollectionItemAttribute( | |
48 Collection collection, | |
49 String artifact, | |
50 String locale) | |
51 throws ServerException | |
52 { | |
53 logger.info( | |
54 "CollectionItemAttributeServiceImpl.getCollectionItemAttribute"); | |
55 | |
56 String url = getServletContext().getInitParameter("server-url"); | |
57 | |
58 Document requestDoc = XMLUtils.newDocument(); | |
59 | |
60 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | |
61 requestDoc, | |
62 ArtifactNamespaceContext.NAMESPACE_URI, | |
63 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
64 | |
65 Element action = ec.create("action"); | |
66 | |
67 Element type = ec.create("type"); | |
68 ec.addAttr(type, "name", "getitemattribute", false); | |
69 | |
70 Element art = ec.create("artifact"); | |
71 ec.addAttr(art, "uuid", artifact, false); | |
72 | |
73 type.appendChild(art); | |
74 action.appendChild(type); | |
75 requestDoc.appendChild (action); | |
76 | |
77 try { | |
78 HttpClient client = new HttpClientImpl(url, locale); | |
79 Document res = (Document) client.doCollectionAction( | |
80 requestDoc, | |
81 collection.identifier(), | |
82 new DocumentResponseHandler()); | |
83 return readXML (res, artifact); | |
84 } | |
85 catch (ConnectionException ce) { | |
86 logger.error(ce, ce); | |
87 } | |
88 | |
89 throw new ServerException(ERROR_NO_STYLES_FOUND); | |
90 } | |
91 | |
92 | |
93 public void setCollectionItemAttribute( | |
94 Collection collection, | |
95 String artifact, | |
96 String locale, | |
97 CollectionItemAttribute attributes) | |
98 throws ServerException | |
99 { | |
100 logger.info( | |
101 "CollectionItemAttributeServiceImpl.setCollectionItemAttribute"); | |
102 | |
103 String url = getServletContext().getInitParameter("server-url"); | |
104 | |
105 Document doc = writeXML(attributes, artifact); | |
106 | |
107 try { | |
108 HttpClient client = new HttpClientImpl(url, locale); | |
109 Document res = (Document) client.doCollectionAction( | |
110 doc, | |
111 collection.identifier(), | |
112 new DocumentResponseHandler()); | |
113 | |
114 return; | |
115 } | |
116 catch (ConnectionException ce) { | |
117 logger.error(ce, ce); | |
118 throw new ServerException(ce.getLocalizedMessage()); | |
119 } | |
120 } | |
121 | |
122 protected CollectionItemAttribute readXML(Document doc, String artifact) | |
123 throws ServerException | |
124 { | |
125 CollectionItemAttribute cia = new CollectionItemAttribute(); | |
126 cia.setArtifact(artifact); | |
127 | |
128 Element root = doc.getDocumentElement(); | |
129 NodeList themes = root.getElementsByTagName("art:themes"); | |
130 | |
131 if (themes == null || themes.getLength() == 0) { | |
132 throw new ServerException(ERROR_NO_STYLES_FOUND); | |
133 } | |
134 | |
135 Element e = (Element) themes.item(0); | |
136 NodeList items = e.getElementsByTagName("theme"); | |
137 | |
138 for (int i = 0; i < items.getLength(); i++) { | |
139 Style s = StyleHelper.getStyle ((Element) items.item(i)); | |
140 if(s == null) { | |
141 throw new ServerException(ERROR_NO_STYLES_FOUND); | |
142 } | |
143 else { | |
144 cia.appendStyle(s); | |
145 } | |
146 } | |
147 | |
148 return cia; | |
149 } | |
150 | |
151 | |
152 protected Document writeXML ( | |
153 CollectionItemAttribute attributes, | |
154 String artifact) | |
155 { | |
156 Document styles = XMLUtils.newDocument(); | |
157 | |
158 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | |
159 styles, | |
160 ArtifactNamespaceContext.NAMESPACE_URI, | |
161 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
162 Element action = ec.create("action"); | |
163 Element type = ec.create("type"); | |
164 type.setAttribute("name", "setitemattribute"); | |
165 Element art = ec.create("artifact"); | |
166 art.setAttribute("uuid", artifact); | |
167 Element attr = ec.create("attribute"); | |
168 Element themes = ec.create("themes"); | |
169 action.appendChild(type); | |
170 type.appendChild(art); | |
171 art.appendChild(attr); | |
172 attr.appendChild(themes); | |
173 | |
174 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( | |
175 styles, | |
176 "", | |
177 ""); | |
178 | |
179 for (int i = 0; i < attributes.getNumStyles(); i++) { | |
180 Style s = attributes.getStyle(i); | |
181 Element theme = creator.create("theme"); | |
182 theme.setAttribute("name", s.getName()); | |
183 theme.setAttribute("facet", s.getFacet()); | |
184 theme.setAttribute("index", String.valueOf(s.getIndex())); | |
185 for (int j = 0; j < s.getNumSettings(); j++) { | |
186 StyleSetting set = s.getSetting(j); | |
187 Element field = creator.create("field"); | |
188 field.setAttribute("name", set.getName()); | |
189 field.setAttribute("display", set.getDisplayName()); | |
190 field.setAttribute("default", set.getDefaultValue()); | |
191 field.setAttribute("hints", set.getHints()); | |
192 field.setAttribute("type", set.getType()); | |
193 field.setAttribute("hidden", String.valueOf(set.isHidden())); | |
194 theme.appendChild(field); | |
195 } | |
196 themes.appendChild(theme); | |
197 } | |
198 styles.appendChild(action); | |
199 return styles; | |
200 } | |
201 } | |
202 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |