comparison flys-client/src/main/java/de/intevation/flys/client/server/DescribeCollectionServiceImpl.java @ 99:5c3d685546a6

Added a new service to describe collections. flys-client/trunk@1613 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 30 Mar 2011 07:35:59 +0000
parents
children f7967d12ce6e
comparison
equal deleted inserted replaced
98:83bf2fb17b7c 99:5c3d685546a6
1 package de.intevation.flys.client.server;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
13
14 import de.intevation.artifacts.common.ArtifactNamespaceContext;
15 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
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 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
22
23 import de.intevation.flys.client.shared.model.Artifact;
24 import de.intevation.flys.client.shared.model.Collection;
25 import de.intevation.flys.client.shared.model.CollectionItem;
26 import de.intevation.flys.client.shared.model.DefaultCollection;
27 import de.intevation.flys.client.shared.model.DefaultCollectionItem;
28 import de.intevation.flys.client.shared.model.DefaultOutputMode;
29 import de.intevation.flys.client.shared.model.OutputMode;
30 import de.intevation.flys.client.client.services.DescribeCollectionService;
31
32
33 /**
34 * This service implements a method that queries the DESCRIBE document of a
35 * specific collection and returns a Collection object with the information of
36 * the document.
37 *
38 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
39 */
40 public class DescribeCollectionServiceImpl
41 extends RemoteServiceServlet
42 implements DescribeCollectionService
43 {
44 public Collection describe(String uuid, String serverUrl) {
45 System.out.println("DescribeCollectionServiceImpl.describe");
46
47 Document describe = ClientProtocolUtils.newDescribeCollectionDocument(
48 uuid);
49
50 HttpClient client = new HttpClientImpl(serverUrl);
51
52 try {
53 Document response = (Document) client.doCollectionAction(
54 describe, uuid, new DocumentResponseHandler());
55
56 Collection c = parseCollection(response);
57
58 if (c == null) {
59 throw new NullPointerException("No collection returned.");
60 }
61
62 return c;
63 }
64 catch (ConnectionException ce) {
65 System.err.println(ce.getLocalizedMessage());
66 }
67
68 return null;
69 }
70
71
72 /**
73 * This method takes the DESCRIBE document of the Collections describe()
74 * operation and extracts the information about the collection itself and
75 * the collection items.
76 *
77 * @param description The DESCRIBE document of the Collections describe()
78 * operation.
79 *
80 * @return a Collection with CollectionItems.
81 */
82 protected Collection parseCollection(Document description) {
83 System.out.println("AddArtifactServiceImpl.parseCollection");
84
85 if (description == null) {
86 System.err.println("The DESCRIBE of the Collection is null!");
87 return null;
88 }
89
90 String uuid = XMLUtils.xpathString(
91 description,
92 "art:artifact-collection/@art:uuid",
93 ArtifactNamespaceContext.INSTANCE);
94
95 if (uuid == null || uuid.equals("")) {
96 System.err.println("Found an invalid Collection!");
97 return null;
98 }
99
100 Collection c = new DefaultCollection(uuid);
101
102 NodeList items = (NodeList) XMLUtils.xpath(
103 description,
104 "art:artifact-collection/art:artifacts/art:artifact",
105 XPathConstants.NODESET,
106 ArtifactNamespaceContext.INSTANCE);
107
108 if (items == null || items.getLength() == 0) {
109 System.out.println("No collection item found for this collection.");
110
111 return c;
112 }
113
114 int size = items.getLength();
115
116 for (int i = 0; i < size; i++) {
117 CollectionItem item = parseCollectionItem(items.item(i));
118
119 if (item != null) {
120 c.addItem(item);
121 }
122 }
123
124 System.out.println(
125 "Found " + c.getItemLength() + " collection items " +
126 "for the Collection '" + c.identifier() + "'.");
127
128 return c;
129 }
130
131
132 /**
133 * This method extracts the CollectionItem from <i>node</i> with its output
134 * modes. The output modes are parsed using the parseOutputModes() method.
135 *
136 * @param node A node that contains information about a CollectionItem.
137 *
138 * @return a CollectionItem.
139 */
140 protected CollectionItem parseCollectionItem(Node node) {
141 System.out.println("AddArtifactServiceImpl.parseCollectionItem");
142
143 if (node == null) {
144 System.err.println("The node for parsing CollectionItem is null!");
145 return null;
146 }
147
148 String uuid = XMLUtils.xpathString(
149 node, "@art:uuid", ArtifactNamespaceContext.INSTANCE);
150
151 String hash = XMLUtils.xpathString(
152 node, "@art:hash", ArtifactNamespaceContext.INSTANCE);
153
154 if (uuid == null || uuid.equals("")) {
155 System.err.println("Found an invalid CollectionItem!");
156 }
157
158 Node outputmodes = (Node) XMLUtils.xpath(
159 node,
160 "art:outputmodes",
161 XPathConstants.NODE,
162 ArtifactNamespaceContext.INSTANCE);
163
164 List<OutputMode> modes = parseOutputModes(outputmodes);
165
166 return new DefaultCollectionItem(uuid, hash, modes);
167 }
168
169
170 /**
171 * This method extracts the OutputModes available for a specific
172 * CollectionItem and returns them as list.
173 *
174 * @param node The root node of the outputmodes list.
175 *
176 * @return a list of OutputModes.
177 */
178 protected List<OutputMode> parseOutputModes(Node node) {
179 System.out.println("AddArtifactServiceImpl.parseOutputModes");
180
181 if (node == null) {
182 System.err.println("The node for parsing OutputModes is null!");
183 return null;
184 }
185
186 NodeList list = (NodeList) XMLUtils.xpath(
187 node,
188 "art:output",
189 XPathConstants.NODESET,
190 ArtifactNamespaceContext.INSTANCE);
191
192 if (list == null || list.getLength() == 0) {
193 System.err.println("No outputmode nodes found!");
194 return null;
195 }
196
197 int size = list.getLength();
198
199 List<OutputMode> modes = new ArrayList<OutputMode>(size);
200
201 for (int i = 0; i < size; i++) {
202 Node tmp = list.item(i);
203 String name = XMLUtils.xpathString(
204 tmp, "@art:name", ArtifactNamespaceContext.INSTANCE);
205 String desc = XMLUtils.xpathString(
206 tmp, "@art:description", ArtifactNamespaceContext.INSTANCE);
207 String mime = XMLUtils.xpathString(
208 tmp, "@art:mime-type", ArtifactNamespaceContext.INSTANCE);
209
210 if (name == null || name.equals("")) {
211 System.err.println("Found an invalid output mode.");
212
213 continue;
214 }
215
216 // TODO Parse Facets
217
218 modes.add(new DefaultOutputMode(name, desc, mime));
219 }
220
221 return modes;
222 }
223 }
224 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org