ingo@69: package de.intevation.flys.client.server;
ingo@69:
ingo@69: import java.util.ArrayList;
ingo@69: import java.util.List;
ingo@69:
ingo@69: import javax.xml.xpath.XPathConstants;
ingo@69:
ingo@69: import org.w3c.dom.Document;
ingo@69: import org.w3c.dom.Node;
ingo@69: import org.w3c.dom.NodeList;
ingo@69:
ingo@69: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
ingo@69:
ingo@69: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@69: import de.intevation.artifacts.common.utils.ClientProtocolUtils;
ingo@69: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@69:
ingo@69: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@69: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@69: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@69: import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
ingo@69:
ingo@69: import de.intevation.flys.client.shared.model.Artifact;
ingo@69: import de.intevation.flys.client.shared.model.Collection;
ingo@69: import de.intevation.flys.client.shared.model.CollectionItem;
ingo@69: import de.intevation.flys.client.shared.model.DefaultCollection;
ingo@69: import de.intevation.flys.client.shared.model.DefaultCollectionItem;
ingo@69: import de.intevation.flys.client.shared.model.DefaultOutputMode;
ingo@69: import de.intevation.flys.client.shared.model.OutputMode;
ingo@69: import de.intevation.flys.client.client.services.AddArtifactService;
ingo@69:
ingo@69:
ingo@69: /**
ingo@69: * @author Ingo Weinzierl
ingo@69: */
ingo@69: public class AddArtifactServiceImpl
ingo@69: extends RemoteServiceServlet
ingo@69: implements AddArtifactService
ingo@69: {
ingo@69: public Collection add(Collection collection, Artifact artifact, String url) {
ingo@69: System.out.println("AddArtifactServiceImpl - add()");
ingo@69:
ingo@69: Document add = ClientProtocolUtils.newAddArtifactDocument(
ingo@69: artifact.getUuid(), null);
ingo@69:
ingo@69: HttpClient client = new HttpClientImpl(url);
ingo@69:
ingo@69: try {
ingo@69: Document response = (Document) client.doCollectionAction(
ingo@69: add, collection.identifier(), new DocumentResponseHandler());
ingo@69:
ingo@69: Collection c = parseCollection(response);
ingo@69:
ingo@69: if (c == null) {
ingo@69: throw new NullPointerException("No collection returned.");
ingo@69: }
ingo@69:
ingo@69: return c;
ingo@69: }
ingo@69: catch (ConnectionException ce) {
ingo@69: System.err.println(ce.getLocalizedMessage());
ingo@69: }
ingo@69:
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69:
ingo@69: /**
ingo@69: * This method takes the DESCRIBE document of the Collections describe()
ingo@69: * operation and extracts the information about the collection itself and
ingo@69: * the collection items.
ingo@69: *
ingo@69: * @param description The DESCRIBE document of the Collections describe()
ingo@69: * operation.
ingo@69: *
ingo@69: * @return a Collection with CollectionItems.
ingo@69: */
ingo@69: protected Collection parseCollection(Document description) {
ingo@69: System.out.println("AddArtifactServiceImpl.parseCollection");
ingo@69:
ingo@69: if (description == null) {
ingo@69: System.err.println("The DESCRIBE of the Collection is null!");
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69: String uuid = XMLUtils.xpathString(
ingo@69: description,
ingo@69: "art:artifact-collection/@art:uuid",
ingo@69: ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@69: if (uuid == null || uuid.equals("")) {
ingo@69: System.err.println("Found an invalid Collection!");
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69: Collection c = new DefaultCollection(uuid);
ingo@69:
ingo@69: NodeList items = (NodeList) XMLUtils.xpath(
ingo@69: description,
ingo@69: "art:artifact-collection/art:artifacts/art:artifact",
ingo@69: XPathConstants.NODESET,
ingo@69: ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@69: if (items == null || items.getLength() == 0) {
ingo@69: System.out.println("No collection item found for this collection.");
ingo@69:
ingo@69: return c;
ingo@69: }
ingo@69:
ingo@69: int size = items.getLength();
ingo@69:
ingo@69: for (int i = 0; i < size; i++) {
ingo@69: CollectionItem item = parseCollectionItem(items.item(i));
ingo@69:
ingo@69: if (item != null) {
ingo@69: c.addItem(item);
ingo@69: }
ingo@69: }
ingo@69:
ingo@69: System.out.println(
ingo@69: "Found " + c.getItemLength() + " collection items " +
ingo@69: "for the Collection '" + c.identifier() + "'.");
ingo@69:
ingo@69: return c;
ingo@69: }
ingo@69:
ingo@69:
ingo@69: /**
ingo@69: * This method extracts the CollectionItem from node with its output
ingo@69: * modes. The output modes are parsed using the parseOutputModes() method.
ingo@69: *
ingo@69: * @param node A node that contains information about a CollectionItem.
ingo@69: *
ingo@69: * @return a CollectionItem.
ingo@69: */
ingo@69: protected CollectionItem parseCollectionItem(Node node) {
ingo@69: System.out.println("AddArtifactServiceImpl.parseCollectionItem");
ingo@69:
ingo@69: if (node == null) {
ingo@69: System.err.println("The node for parsing CollectionItem is null!");
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69: String uuid = XMLUtils.xpathString(
ingo@69: node, "@art:uuid", ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@87: String hash = XMLUtils.xpathString(
ingo@87: node, "@art:hash", ArtifactNamespaceContext.INSTANCE);
ingo@87:
ingo@69: if (uuid == null || uuid.equals("")) {
ingo@69: System.err.println("Found an invalid CollectionItem!");
ingo@69: }
ingo@69:
ingo@69: Node outputmodes = (Node) XMLUtils.xpath(
ingo@69: node,
ingo@69: "art:outputmodes",
ingo@69: XPathConstants.NODE,
ingo@69: ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@69: List modes = parseOutputModes(outputmodes);
ingo@69:
ingo@87: return new DefaultCollectionItem(uuid, hash, modes);
ingo@69: }
ingo@69:
ingo@69:
ingo@69: /**
ingo@69: * This method extracts the OutputModes available for a specific
ingo@69: * CollectionItem and returns them as list.
ingo@69: *
ingo@69: * @param node The root node of the outputmodes list.
ingo@69: *
ingo@69: * @return a list of OutputModes.
ingo@69: */
ingo@69: protected List parseOutputModes(Node node) {
ingo@69: System.out.println("AddArtifactServiceImpl.parseOutputModes");
ingo@69:
ingo@69: if (node == null) {
ingo@69: System.err.println("The node for parsing OutputModes is null!");
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69: NodeList list = (NodeList) XMLUtils.xpath(
ingo@69: node,
ingo@69: "art:output",
ingo@69: XPathConstants.NODESET,
ingo@69: ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@69: if (list == null || list.getLength() == 0) {
ingo@69: System.err.println("No outputmode nodes found!");
ingo@69: return null;
ingo@69: }
ingo@69:
ingo@69: int size = list.getLength();
ingo@69:
ingo@69: List modes = new ArrayList(size);
ingo@69:
ingo@69: for (int i = 0; i < size; i++) {
ingo@69: Node tmp = list.item(i);
ingo@69: String name = XMLUtils.xpathString(
ingo@69: tmp, "@art:name", ArtifactNamespaceContext.INSTANCE);
ingo@69: String desc = XMLUtils.xpathString(
ingo@69: tmp, "@art:description", ArtifactNamespaceContext.INSTANCE);
ingo@69: String mime = XMLUtils.xpathString(
ingo@69: tmp, "@art:mime-type", ArtifactNamespaceContext.INSTANCE);
ingo@69:
ingo@69: if (name == null || name.equals("")) {
ingo@69: System.err.println("Found an invalid output mode.");
ingo@69:
ingo@69: continue;
ingo@69: }
ingo@69:
ingo@69: // TODO Parse Facets
ingo@69:
ingo@69: modes.add(new DefaultOutputMode(name, desc, mime));
ingo@69: }
ingo@69:
ingo@69: return modes;
ingo@69: }
ingo@69: }
ingo@69: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :