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@96: import org.w3c.dom.Node; ingo@96: import org.w3c.dom.NodeList; ingo@96: 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. ingo@96: * 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@96: public Collection[] getUserCollections(String serverUrl, String userid) { ingo@96: System.out.println("UserCollectionsServiceImpl.getUserCollections"); ingo@96: ingo@96: HttpClient client = new HttpClientImpl(serverUrl); 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@96: System.out.println("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@96: Collection c = createCollection(list.item(i)); ingo@96: ingo@96: if (c != null) { ingo@96: all.add(c); ingo@96: } ingo@96: } ingo@96: ingo@96: System.out.println("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@96: System.err.println(ce.getLocalizedMessage()); ingo@96: } ingo@96: ingo@96: System.err.println("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@96: protected Collection createCollection(Node node) { ingo@96: String creationStr = XMLUtils.xpathString( ingo@96: node, "@art:creation", ArtifactNamespaceContext.INSTANCE); ingo@96: ingo@96: String name = XMLUtils.xpathString( ingo@96: node, "@art:name", ArtifactNamespaceContext.INSTANCE); ingo@96: ingo@96: String uuid = XMLUtils.xpathString( ingo@96: node, "@art:uuid", ArtifactNamespaceContext.INSTANCE); ingo@96: ingo@96: if (uuid != null) { ingo@96: long time = Long.parseLong(creationStr); ingo@96: return new DefaultCollection(uuid, name, new Date(time)); ingo@96: } ingo@96: ingo@96: System.err.println("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 :