comparison flys-client/src/main/java/de/intevation/flys/client/server/AddArtifactServiceImpl.java @ 69:4bdb18e5f484

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

http://dive4elements.wald.intevation.org