ingo@96: package de.intevation.flys.client.server;
ingo@96:
ingo@96: import java.util.ArrayList;
ingo@96: import java.util.Date;
ingo@96: import java.util.List;
ingo@96:
ingo@96: import javax.xml.xpath.XPathConstants;
ingo@96:
ingo@96: import org.w3c.dom.Document;
ingo@587: import org.w3c.dom.Element;
ingo@96: import org.w3c.dom.NodeList;
ingo@96:
ingo@1367: import org.apache.log4j.Logger;
ingo@1367:
ingo@96: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
ingo@96:
ingo@96: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@96: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@96:
ingo@96: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@96: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@96: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@96:
ingo@96: import de.intevation.flys.client.shared.model.Collection;
ingo@96: import de.intevation.flys.client.shared.model.DefaultCollection;
ingo@96: import de.intevation.flys.client.client.services.UserCollectionsService;
ingo@96:
ingo@96:
ingo@96: /**
ingo@96: * This service returns a list of collections owned by a specified user.
sascha@615: * NOTE: The Collections returned by this service provide no information
ingo@96: * about the CollectionItems or OutputModes of the Collection. You need to fetch
ingo@96: * these information explicitly using another service.
ingo@96: *
ingo@96: * @author Ingo Weinzierl
ingo@96: */
ingo@96: public class UserCollectionsServiceImpl
ingo@96: extends RemoteServiceServlet
ingo@96: implements UserCollectionsService
ingo@96: {
ingo@1367: private static final Logger logger = Logger.getLogger(
ingo@1367: UserCollectionsServiceImpl.class);
ingo@1367:
ingo@1367:
ingo@1493: public Collection[] getUserCollections(String locale, String userid) {
ingo@1367: logger.info("UserCollectionsServiceImpl.getUserCollections");
ingo@96:
ingo@1493: String serverUrl = getServletContext().getInitParameter("server-url");
ingo@229: HttpClient client = new HttpClientImpl(serverUrl, locale);
ingo@96:
ingo@96: try {
ingo@96: Document result = client.listUserCollections(userid);
ingo@96:
ingo@96: NodeList list = (NodeList) XMLUtils.xpath(
ingo@96: result,
ingo@96: "/art:artifact-collections/art:artifact-collection",
ingo@96: XPathConstants.NODESET,
ingo@96: ArtifactNamespaceContext.INSTANCE);
ingo@96:
ingo@96: if (list == null || list.getLength() == 0) {
ingo@1367: logger.debug("No collection found for user: " + userid);
ingo@96: return null;
ingo@96: }
ingo@96:
ingo@96: int num = list.getLength();
ingo@96:
ingo@96: List all = new ArrayList(num);
ingo@96:
ingo@96: for (int i = 0; i < num; i++) {
ingo@587: Collection c = createCollection((Element) list.item(i));
ingo@96:
ingo@96: if (c != null) {
ingo@96: all.add(c);
ingo@96: }
ingo@96: }
ingo@96:
ingo@1367: logger.debug("User has " + all.size() + " collections.");
ingo@96:
ingo@96: return (Collection[]) all.toArray(new Collection[all.size()]);
ingo@96: }
ingo@96: catch (ConnectionException ce) {
ingo@1367: logger.error(ce, ce);
ingo@96: }
ingo@96:
ingo@1367: logger.debug("No user collections found.");
ingo@96: return null;
ingo@96: }
ingo@96:
ingo@96:
ingo@96: /**
ingo@96: * Extracts a SimpleCollection from node.
ingo@96: *
ingo@96: * @param node Contains information about a collection.
ingo@96: *
ingo@96: * @return a list of Simplecollections.
ingo@96: */
ingo@587: protected Collection createCollection(Element node) {
ingo@587: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@96:
ingo@587: String creationStr = node.getAttributeNS(uri, "creation");
ingo@587: String name = node.getAttributeNS(uri, "name");
ingo@587: String uuid = node.getAttributeNS(uri, "uuid");
ingo@587: String ttlStr = node.getAttributeNS(uri, "ttl");
ingo@96:
ingo@587: if (uuid != null && ttlStr != null) {
ingo@587: try {
ingo@587: long time = Long.parseLong(creationStr);
ingo@587: long ttl = Long.parseLong(ttlStr);
ingo@587: return new DefaultCollection(uuid, ttl, name, new Date(time));
ingo@587: }
ingo@587: catch (NumberFormatException nfe) {
ingo@1367: logger.warn("Error while parsing collection attributes.");
ingo@587: return null;
ingo@587: }
ingo@96: }
ingo@96:
ingo@1367: logger.warn("Found an invalid Collection.");
ingo@96: return null;
ingo@96: }
ingo@96: }
ingo@96: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :