comparison flys-client/src/main/java/org/dive4elements/river/client/server/UserCollectionsServiceImpl.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/UserCollectionsServiceImpl.java@d35a0bc153fa
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
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.Element;
11 import org.w3c.dom.NodeList;
12
13 import org.apache.log4j.Logger;
14
15 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
16
17 import de.intevation.artifacts.common.ArtifactNamespaceContext;
18 import de.intevation.artifacts.common.utils.XMLUtils;
19
20 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
21 import de.intevation.artifacts.httpclient.http.HttpClient;
22 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
23
24 import de.intevation.flys.client.shared.model.Collection;
25 import de.intevation.flys.client.shared.model.DefaultCollection;
26 import de.intevation.flys.client.client.services.UserCollectionsService;
27
28
29 /**
30 * This service returns a list of collections owned by a specified user.
31 * <b>NOTE:</b> The Collections returned by this service provide no information
32 * about the CollectionItems or OutputModes of the Collection. You need to fetch
33 * these information explicitly using another service.
34 *
35 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
36 */
37 public class UserCollectionsServiceImpl
38 extends RemoteServiceServlet
39 implements UserCollectionsService
40 {
41 private static final Logger logger = Logger.getLogger(
42 UserCollectionsServiceImpl.class);
43
44
45 public Collection[] getUserCollections(String locale, String userid) {
46 logger.info("UserCollectionsServiceImpl.getUserCollections");
47
48 String serverUrl = getServletContext().getInitParameter("server-url");
49 HttpClient client = new HttpClientImpl(serverUrl, locale);
50
51 try {
52 Document result = client.listUserCollections(userid);
53
54 NodeList list = (NodeList) XMLUtils.xpath(
55 result,
56 "/art:artifact-collections/art:artifact-collection",
57 XPathConstants.NODESET,
58 ArtifactNamespaceContext.INSTANCE);
59
60 if (list == null || list.getLength() == 0) {
61 logger.debug("No collection found for user: " + userid);
62 return null;
63 }
64
65 int num = list.getLength();
66
67 List<Collection> all = new ArrayList<Collection>(num);
68
69 for (int i = 0; i < num; i++) {
70 Collection c = createCollection((Element) list.item(i));
71
72 if (c != null) {
73 all.add(c);
74 }
75 }
76
77 logger.debug("User has " + all.size() + " collections.");
78
79 return all.toArray(new Collection[all.size()]);
80 }
81 catch (ConnectionException ce) {
82 logger.error(ce, ce);
83 }
84
85 logger.debug("No user collections found.");
86 return null;
87 }
88
89
90 /**
91 * Extracts a SimpleCollection from <i>node</i>.
92 *
93 * @param node Contains information about a collection.
94 *
95 * @return a list of Simplecollections.
96 */
97 protected Collection createCollection(Element node) {
98 String uri = ArtifactNamespaceContext.NAMESPACE_URI;
99
100 String creationStr = node.getAttributeNS(uri, "creation");
101 String name = node.getAttributeNS(uri, "name");
102 String uuid = node.getAttributeNS(uri, "uuid");
103 String ttlStr = node.getAttributeNS(uri, "ttl");
104
105 if (!uuid.isEmpty() && !ttlStr.isEmpty() && !creationStr.isEmpty()) {
106 try {
107 long time = Long.parseLong(creationStr);
108 long ttl = Long.parseLong(ttlStr);
109 return new DefaultCollection(uuid, ttl, name, new Date(time));
110 }
111 catch (NumberFormatException nfe) {
112 logger.warn("Error while parsing collection attributes.");
113 return null;
114 }
115 }
116
117 logger.warn("Found an invalid Collection.");
118 return null;
119 }
120 }
121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org