Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/UserCollectionsServiceImpl.java @ 96:261a2ee7d9bb
Added a service that returns the collections of a specified user.
flys-client/trunk@1609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 29 Mar 2011 13:49:04 +0000 |
parents | |
children | 924da6695800 |
comparison
equal
deleted
inserted
replaced
95:e2abb6b9dc7e | 96:261a2ee7d9bb |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Date; | |
5 import java.util.List; | |
6 | |
7 import javax.xml.xpath.XPathConstants; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Node; | |
11 import org.w3c.dom.NodeList; | |
12 | |
13 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
14 | |
15 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
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.model.Collection; | |
23 import de.intevation.flys.client.shared.model.DefaultCollection; | |
24 import de.intevation.flys.client.client.services.UserCollectionsService; | |
25 | |
26 | |
27 /** | |
28 * This service returns a list of collections owned by a specified user. | |
29 * <b>NOTE:</b> The Collections returned by this service provide no information | |
30 * about the CollectionItems or OutputModes of the Collection. You need to fetch | |
31 * these information explicitly using another service. | |
32 * | |
33 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
34 */ | |
35 public class UserCollectionsServiceImpl | |
36 extends RemoteServiceServlet | |
37 implements UserCollectionsService | |
38 { | |
39 public Collection[] getUserCollections(String serverUrl, String userid) { | |
40 System.out.println("UserCollectionsServiceImpl.getUserCollections"); | |
41 | |
42 HttpClient client = new HttpClientImpl(serverUrl); | |
43 | |
44 try { | |
45 Document result = client.listUserCollections(userid); | |
46 | |
47 NodeList list = (NodeList) XMLUtils.xpath( | |
48 result, | |
49 "/art:artifact-collections/art:artifact-collection", | |
50 XPathConstants.NODESET, | |
51 ArtifactNamespaceContext.INSTANCE); | |
52 | |
53 if (list == null || list.getLength() == 0) { | |
54 System.out.println("No collection found for user: " + userid); | |
55 return null; | |
56 } | |
57 | |
58 int num = list.getLength(); | |
59 | |
60 List<Collection> all = new ArrayList<Collection>(num); | |
61 | |
62 for (int i = 0; i < num; i++) { | |
63 Collection c = createCollection(list.item(i)); | |
64 | |
65 if (c != null) { | |
66 all.add(c); | |
67 } | |
68 } | |
69 | |
70 System.out.println("User has " + all.size() + " collections."); | |
71 | |
72 return (Collection[]) all.toArray(new Collection[all.size()]); | |
73 } | |
74 catch (ConnectionException ce) { | |
75 System.err.println(ce.getLocalizedMessage()); | |
76 } | |
77 | |
78 System.err.println("No user collections found."); | |
79 return null; | |
80 } | |
81 | |
82 | |
83 /** | |
84 * Extracts a SimpleCollection from <i>node</i>. | |
85 * | |
86 * @param node Contains information about a collection. | |
87 * | |
88 * @return a list of Simplecollections. | |
89 */ | |
90 protected Collection createCollection(Node node) { | |
91 String creationStr = XMLUtils.xpathString( | |
92 node, "@art:creation", ArtifactNamespaceContext.INSTANCE); | |
93 | |
94 String name = XMLUtils.xpathString( | |
95 node, "@art:name", ArtifactNamespaceContext.INSTANCE); | |
96 | |
97 String uuid = XMLUtils.xpathString( | |
98 node, "@art:uuid", ArtifactNamespaceContext.INSTANCE); | |
99 | |
100 if (uuid != null) { | |
101 long time = Long.parseLong(creationStr); | |
102 return new DefaultCollection(uuid, name, new Date(time)); | |
103 } | |
104 | |
105 System.err.println("Found an invalid Collection."); | |
106 return null; | |
107 } | |
108 } | |
109 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |