ingo@99: package de.intevation.flys.client.server;
ingo@99:
ingo@99: import java.util.ArrayList;
ingo@99: import java.util.List;
ingo@99:
ingo@99: import javax.xml.xpath.XPathConstants;
ingo@99:
ingo@99: import org.w3c.dom.Document;
ingo@99: import org.w3c.dom.Node;
ingo@99: import org.w3c.dom.NodeList;
ingo@99:
ingo@99: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
ingo@99:
ingo@99: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@99: import de.intevation.artifacts.common.utils.ClientProtocolUtils;
ingo@99: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@99:
ingo@99: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@99: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@99: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@99: import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
ingo@99:
ingo@215: import de.intevation.flys.client.shared.exceptions.ServerException;
ingo@99: import de.intevation.flys.client.shared.model.Collection;
ingo@99: import de.intevation.flys.client.shared.model.CollectionItem;
ingo@99: import de.intevation.flys.client.shared.model.DefaultCollection;
ingo@99: import de.intevation.flys.client.shared.model.DefaultCollectionItem;
ingo@270: import de.intevation.flys.client.shared.model.DefaultFacet;
ingo@99: import de.intevation.flys.client.shared.model.DefaultOutputMode;
ingo@270: import de.intevation.flys.client.shared.model.ExportMode;
ingo@270: import de.intevation.flys.client.shared.model.Facet;
ingo@99: import de.intevation.flys.client.shared.model.OutputMode;
ingo@99: import de.intevation.flys.client.client.services.DescribeCollectionService;
ingo@99:
ingo@99:
ingo@99: /**
ingo@99: * This service implements a method that queries the DESCRIBE document of a
ingo@99: * specific collection and returns a Collection object with the information of
ingo@99: * the document.
ingo@99: *
ingo@99: * @author Ingo Weinzierl
ingo@99: */
ingo@99: public class DescribeCollectionServiceImpl
ingo@99: extends RemoteServiceServlet
ingo@99: implements DescribeCollectionService
ingo@99: {
ingo@215: /** The error message key that is thrown if an error occured while
ingo@215: * describe() a Collection.*/
ingo@215: public static final String ERROR_DESCRIBE_COLLECTION =
ingo@215: "error_describe_collection";
ingo@215:
ingo@270: public static final String XPATH_FACETS = "art:facets/art:facet";
ingo@270:
ingo@215:
ingo@229: public Collection describe(String uuid, String serverUrl, String locale)
ingo@215: throws ServerException
ingo@215: {
ingo@99: System.out.println("DescribeCollectionServiceImpl.describe");
ingo@99:
ingo@99: Document describe = ClientProtocolUtils.newDescribeCollectionDocument(
ingo@99: uuid);
ingo@99:
ingo@229: HttpClient client = new HttpClientImpl(serverUrl, locale);
ingo@99:
ingo@99: try {
ingo@99: Document response = (Document) client.doCollectionAction(
ingo@99: describe, uuid, new DocumentResponseHandler());
ingo@99:
ingo@99: Collection c = parseCollection(response);
ingo@99:
ingo@99: if (c == null) {
ingo@215: throw new ServerException(ERROR_DESCRIBE_COLLECTION);
ingo@99: }
ingo@99:
ingo@99: return c;
ingo@99: }
ingo@99: catch (ConnectionException ce) {
ingo@99: System.err.println(ce.getLocalizedMessage());
ingo@99: }
ingo@99:
ingo@215: throw new ServerException(ERROR_DESCRIBE_COLLECTION);
ingo@99: }
ingo@99:
ingo@99:
ingo@99: /**
ingo@99: * This method takes the DESCRIBE document of the Collections describe()
ingo@99: * operation and extracts the information about the collection itself and
ingo@99: * the collection items.
ingo@99: *
ingo@99: * @param description The DESCRIBE document of the Collections describe()
ingo@99: * operation.
ingo@99: *
ingo@99: * @return a Collection with CollectionItems.
ingo@99: */
ingo@99: protected Collection parseCollection(Document description) {
ingo@99: System.out.println("AddArtifactServiceImpl.parseCollection");
ingo@99:
ingo@99: if (description == null) {
ingo@99: System.err.println("The DESCRIBE of the Collection is null!");
ingo@99: return null;
ingo@99: }
ingo@99:
ingo@99: String uuid = XMLUtils.xpathString(
ingo@99: description,
ingo@99: "art:artifact-collection/@art:uuid",
ingo@99: ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: if (uuid == null || uuid.equals("")) {
ingo@99: System.err.println("Found an invalid Collection!");
ingo@99: return null;
ingo@99: }
ingo@99:
ingo@99: Collection c = new DefaultCollection(uuid);
ingo@99:
ingo@99: NodeList items = (NodeList) XMLUtils.xpath(
ingo@99: description,
ingo@99: "art:artifact-collection/art:artifacts/art:artifact",
ingo@99: XPathConstants.NODESET,
ingo@99: ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: if (items == null || items.getLength() == 0) {
ingo@99: System.out.println("No collection item found for this collection.");
ingo@99:
ingo@99: return c;
ingo@99: }
ingo@99:
ingo@99: int size = items.getLength();
ingo@99:
ingo@99: for (int i = 0; i < size; i++) {
ingo@99: CollectionItem item = parseCollectionItem(items.item(i));
ingo@99:
ingo@99: if (item != null) {
ingo@99: c.addItem(item);
ingo@99: }
ingo@99: }
ingo@99:
ingo@99: System.out.println(
ingo@99: "Found " + c.getItemLength() + " collection items " +
ingo@99: "for the Collection '" + c.identifier() + "'.");
ingo@99:
ingo@99: return c;
ingo@99: }
ingo@99:
ingo@99:
ingo@99: /**
ingo@99: * This method extracts the CollectionItem from node with its output
ingo@99: * modes. The output modes are parsed using the parseOutputModes() method.
ingo@99: *
ingo@99: * @param node A node that contains information about a CollectionItem.
ingo@99: *
ingo@99: * @return a CollectionItem.
ingo@99: */
ingo@99: protected CollectionItem parseCollectionItem(Node node) {
ingo@99: System.out.println("AddArtifactServiceImpl.parseCollectionItem");
ingo@99:
ingo@99: if (node == null) {
ingo@99: System.err.println("The node for parsing CollectionItem is null!");
ingo@99: return null;
ingo@99: }
ingo@99:
ingo@99: String uuid = XMLUtils.xpathString(
ingo@99: node, "@art:uuid", ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: String hash = XMLUtils.xpathString(
ingo@99: node, "@art:hash", ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: if (uuid == null || uuid.equals("")) {
ingo@99: System.err.println("Found an invalid CollectionItem!");
ingo@99: }
ingo@99:
ingo@99: Node outputmodes = (Node) XMLUtils.xpath(
ingo@99: node,
ingo@99: "art:outputmodes",
ingo@99: XPathConstants.NODE,
ingo@99: ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: List modes = parseOutputModes(outputmodes);
ingo@99:
ingo@99: return new DefaultCollectionItem(uuid, hash, modes);
ingo@99: }
ingo@99:
ingo@99:
ingo@99: /**
ingo@99: * This method extracts the OutputModes available for a specific
ingo@99: * CollectionItem and returns them as list.
ingo@99: *
ingo@99: * @param node The root node of the outputmodes list.
ingo@99: *
ingo@99: * @return a list of OutputModes.
ingo@99: */
ingo@99: protected List parseOutputModes(Node node) {
ingo@99: System.out.println("AddArtifactServiceImpl.parseOutputModes");
ingo@99:
ingo@99: if (node == null) {
ingo@99: System.err.println("The node for parsing OutputModes is null!");
ingo@99: return null;
ingo@99: }
ingo@99:
ingo@99: NodeList list = (NodeList) XMLUtils.xpath(
ingo@99: node,
ingo@99: "art:output",
ingo@99: XPathConstants.NODESET,
ingo@99: ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: if (list == null || list.getLength() == 0) {
ingo@99: System.err.println("No outputmode nodes found!");
ingo@99: return null;
ingo@99: }
ingo@99:
ingo@99: int size = list.getLength();
ingo@99:
ingo@99: List modes = new ArrayList(size);
ingo@99:
ingo@99: for (int i = 0; i < size; i++) {
ingo@99: Node tmp = list.item(i);
ingo@99: String name = XMLUtils.xpathString(
ingo@99: tmp, "@art:name", ArtifactNamespaceContext.INSTANCE);
ingo@99: String desc = XMLUtils.xpathString(
ingo@99: tmp, "@art:description", ArtifactNamespaceContext.INSTANCE);
ingo@99: String mime = XMLUtils.xpathString(
ingo@99: tmp, "@art:mime-type", ArtifactNamespaceContext.INSTANCE);
ingo@99:
ingo@99: if (name == null || name.equals("")) {
ingo@99: System.err.println("Found an invalid output mode.");
ingo@99:
ingo@99: continue;
ingo@99: }
ingo@99:
ingo@270: OutputMode outmode = null;
ingo@270: List fs = extractFacets(tmp);
ingo@99:
ingo@270: if (name.indexOf("export") > -1) {
ingo@270: outmode = new ExportMode(name, desc, mime, fs);
ingo@270: }
ingo@270: else {
ingo@270: outmode = new DefaultOutputMode(name, desc, mime, fs);
ingo@270: }
ingo@270:
ingo@270: modes.add(outmode);
ingo@99: }
ingo@99:
ingo@99: return modes;
ingo@99: }
ingo@270:
ingo@270:
ingo@270: protected static List extractFacets(Node outmode) {
ingo@270: System.out.println("ArtifactDescriptionFactory - extractFacets()");
ingo@270:
ingo@270: NodeList facetList = (NodeList) XMLUtils.xpath(
ingo@270: outmode,
ingo@270: XPATH_FACETS,
ingo@270: XPathConstants.NODESET,
ingo@270: ArtifactNamespaceContext.INSTANCE);
ingo@270:
ingo@270: int num = facetList != null ? facetList.getLength() : 0;
ingo@270:
ingo@270: List facets = new ArrayList(num);
ingo@270:
ingo@270: for (int i = 0; i < num; i++) {
ingo@270: Node facetNode = facetList.item(i);
ingo@270:
ingo@270: String name = XMLUtils.xpathString(
ingo@270: facetNode, "@art:name", ArtifactNamespaceContext.INSTANCE);
ingo@270:
ingo@270: if (name != null && name.length() > 0) {
ingo@270: facets.add(new DefaultFacet(name));
ingo@270: }
ingo@270: }
ingo@270:
ingo@270: return facets;
ingo@270: }
ingo@99: }
ingo@99: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :