ingo@530: package de.intevation.flys.client.server;
ingo@530:
ingo@905: import java.util.ArrayList;
ingo@905: import java.util.HashMap;
ingo@530: import java.util.List;
ingo@530: import java.util.Map;
ingo@804: import java.util.Set;
ingo@530:
ingo@905: import javax.xml.xpath.XPathConstants;
ingo@905:
ingo@530: import org.w3c.dom.Document;
ingo@530: import org.w3c.dom.Element;
ingo@905: import org.w3c.dom.NamedNodeMap;
ingo@905: import org.w3c.dom.Node;
ingo@905: import org.w3c.dom.NodeList;
ingo@530:
ingo@530: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@905: import de.intevation.artifacts.common.utils.ClientProtocolUtils;
ingo@530: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@530: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@530:
ingo@905: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@905: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@905: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@905: import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
ingo@905:
ingo@905: import de.intevation.flys.client.shared.exceptions.ServerException;
ingo@905: import de.intevation.flys.client.shared.model.Artifact;
ingo@804: import de.intevation.flys.client.shared.model.AttributedTheme;
ingo@905: import de.intevation.flys.client.shared.model.ChartMode;
ingo@530: import de.intevation.flys.client.shared.model.Collection;
ingo@905: import de.intevation.flys.client.shared.model.CollectionItem;
ingo@905: import de.intevation.flys.client.shared.model.DefaultCollection;
ingo@905: import de.intevation.flys.client.shared.model.DefaultCollectionItem;
ingo@905: import de.intevation.flys.client.shared.model.DefaultFacet;
ingo@905: import de.intevation.flys.client.shared.model.ExportMode;
ingo@905: import de.intevation.flys.client.shared.model.Facet;
ingo@905: import de.intevation.flys.client.shared.model.MapMode;
ingo@530: import de.intevation.flys.client.shared.model.OutputMode;
ingo@905: import de.intevation.flys.client.shared.model.ReportMode;
ingo@809: import de.intevation.flys.client.shared.model.Recommendation;
ingo@530: import de.intevation.flys.client.shared.model.Theme;
ingo@530: import de.intevation.flys.client.shared.model.ThemeList;
ingo@530:
ingo@530:
ingo@530: /**
ingo@530: * @author Ingo Weinzierl
ingo@530: */
ingo@530: public class CollectionHelper {
ingo@530:
ingo@905: public static final String ERROR_ADD_ARTIFACT = "error_add_artifact";
ingo@905:
felix@1332: public static final String ERROR_REMOVE_ARTIFACT = "error_remove_artifact";
felix@1332:
ingo@905: public static final String XPATH_FACETS = "art:facets/art:facet";
ingo@905:
ingo@905: public static final String XPATH_LOADED_RECOMMENDATIONS =
ingo@905: "/art:artifact-collection/art:attribute/art:loaded-recommendations/art:recommendation";
ingo@905:
ingo@905:
ingo@530: public static Document createAttribute(Collection collection) {
ingo@530: System.out.println("CollectionHelper.createAttribute");
ingo@530:
ingo@530: Document doc = XMLUtils.newDocument();
ingo@530:
ingo@530: ElementCreator cr = new ElementCreator(
ingo@530: doc,
ingo@530: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@530: ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@530:
ingo@530: Element attr = cr.create("attribute");
ingo@530:
ingo@530: doc.appendChild(attr);
ingo@530:
ingo@530: Map tmpOuts = collection.getOutputModes();
ingo@530:
ingo@530: Element outs = createOutputElements(cr, collection, tmpOuts);
ingo@809: Element recs = createRecommendationsElements(cr, collection);
ingo@530:
ingo@530: if (outs != null) {
ingo@530: attr.appendChild(outs);
ingo@530: }
ingo@530:
ingo@809: if (recs != null) {
ingo@809: attr.appendChild(recs);
ingo@809: }
ingo@809:
ingo@530: return doc;
ingo@530: }
ingo@530:
ingo@530:
ingo@530: /**
ingo@530: * Creates a whole block with art:output nodes.
ingo@530: *
ingo@530: * @param cr The ElementCreator used to create new elements.
ingo@530: * @param c The collection.
ingo@530: * @param modes The OutputModes that should be included.
ingo@530: *
ingo@530: * @return an element with output modes.
ingo@530: */
ingo@530: protected static Element createOutputElements(
ingo@530: ElementCreator cr,
ingo@530: Collection c,
ingo@530: Map mmodes)
ingo@530: {
ingo@530: System.out.println("CollectionHelper.createOutputElements");
ingo@530:
ingo@530: java.util.Collection modes = mmodes != null
ingo@530: ? mmodes.values()
ingo@530: : null;
ingo@530:
ingo@530: if (modes == null || modes.size() == 0) {
ingo@530: System.err.println("Collection has no modes: " + c.identifier());
ingo@530: return null;
ingo@530: }
ingo@530:
ingo@530: Element outs = cr.create("outputs");
ingo@530:
ingo@530: for (OutputMode mode: modes) {
ingo@530: Element out = createOutputElement(cr, c, mode);
ingo@530:
ingo@530: if (out != null) {
ingo@530: outs.appendChild(out);
ingo@530: }
ingo@530: }
ingo@530:
ingo@530: return outs;
ingo@530: }
ingo@530:
ingo@530:
ingo@530: /**
ingo@530: * Create a node art:output that further consist of art:theme nodes.
ingo@530: *
ingo@530: * @param cr The ElementCreator used to create new elements.
ingo@530: * @param c The collection.
ingo@530: * @param mode The OutputMode.
ingo@530: *
ingo@530: * @return an element that represents an output mode with its themes.
ingo@530: */
ingo@530: protected static Element createOutputElement(
ingo@530: ElementCreator cr,
ingo@530: Collection collection,
ingo@530: OutputMode mode)
ingo@530: {
ingo@530: System.out.println("CollectionHelper.createOutputElement");
ingo@530:
ingo@530: Element out = cr.create("output");
ingo@530: cr.addAttr(out, "name", mode.getName(), false);
ingo@530:
ingo@530: ThemeList themeList = collection.getThemeList(mode.getName());
ingo@530: List themes = themeList != null ? themeList.getThemes() : null;
ingo@530:
ingo@530: if (themes == null || themes.size() == 0) {
ingo@530: System.err.println("No themes for output mode: " + mode.getName());
ingo@530: return null;
ingo@530: }
ingo@530:
ingo@530: for (Theme theme: themes) {
ingo@530: Element t = createThemeElement(cr, collection, theme);
ingo@530:
ingo@530: if (t != null) {
ingo@530: out.appendChild(t);
ingo@530: }
ingo@530: }
ingo@530:
ingo@530: return out;
ingo@530: }
ingo@530:
ingo@530:
ingo@530: /**
ingo@530: * Creates a theme node art:theme that represents a curve in a chart or map.
ingo@530: *
ingo@530: * @param cr The ElementCreator used to create new elements.
ingo@530: * @param collection The collection.
ingo@530: * @param theme The theme whose attributes should be written to an element.
ingo@530: *
ingo@530: * @return an element that contains the informtion of the given theme.
ingo@530: */
ingo@530: protected static Element createThemeElement(
ingo@530: ElementCreator cr,
ingo@530: Collection collection,
ingo@530: Theme theme)
ingo@530: {
ingo@530: if (theme == null) {
ingo@530: return null;
ingo@530: }
ingo@530:
ingo@804: Element t = cr.create("facet");
ingo@530:
ingo@804: if (theme instanceof AttributedTheme) {
ingo@804: AttributedTheme at = (AttributedTheme) theme;
ingo@804: Set keys = at.getKeys();
ingo@804:
ingo@804: for (String key: keys) {
ingo@804: cr.addAttr(t, key, at.getAttr(key), true);
ingo@804: }
ingo@804: }
ingo@804: else {
ingo@804: cr.addAttr(t, "active", Integer.toString(theme.getActive()), true);
ingo@804: cr.addAttr(t, "artifact", theme.getArtifact(), true);
ingo@804: cr.addAttr(t, "facet", theme.getFacet(), true);
ingo@804: cr.addAttr(t, "pos", Integer.toString(theme.getPosition()), true);
ingo@804: cr.addAttr(t, "index", Integer.toString(theme.getIndex()), true);
ingo@804: cr.addAttr(t, "description", theme.getDescription(), true);
ingo@1342: cr.addAttr(t, "visible", Integer.toString(theme.getVisible()), true);
ingo@804: }
ingo@530:
ingo@530: return t;
ingo@530: }
ingo@809:
ingo@809:
ingo@809: /**
ingo@809: * Creates a whole block with art:loaded-recommendations nodes.
ingo@809: *
ingo@809: * @param cr The ElementCreator used to create new elements.
ingo@809: * @param c The collection.
ingo@809: *
ingo@809: * @return an element with loaded recommendations.
ingo@809: */
ingo@809: protected static Element createRecommendationsElements(
ingo@809: ElementCreator cr,
ingo@809: Collection c)
ingo@809: {
ingo@809: System.out.println("CollectionHelper.createRecommendationsElements");
ingo@809:
ingo@809: List rs = c.getRecommendations();
ingo@809:
ingo@809: if (rs == null || rs.size() == 0) {
ingo@809: System.err.println("Collection did not load recommendations: " +
ingo@809: c.identifier());
ingo@809: return null;
ingo@809: }
ingo@809:
ingo@809: Element loaded = cr.create("loaded-recommendations");
ingo@809:
ingo@809: for (Recommendation r: rs) {
ingo@809: Element recommendation = createRecommendationElement(cr, c, r);
ingo@809:
ingo@809: if (recommendation != null) {
ingo@809: loaded.appendChild(recommendation);
ingo@809: }
ingo@809: }
ingo@809:
ingo@809: return loaded;
ingo@809: }
ingo@809:
ingo@809:
ingo@809: /**
ingo@809: * Create a node art:recommended.
ingo@809: *
ingo@809: * @param cr The ElementCreator used to create new elements.
ingo@809: * @param c The collection.
ingo@809: * @param r The Recommendation.
ingo@809: *
ingo@809: * @return an element that represents an output mode with its themes.
ingo@809: */
ingo@809: protected static Element createRecommendationElement(
ingo@809: ElementCreator cr,
ingo@809: Collection c,
ingo@809: Recommendation r)
ingo@809: {
ingo@809: System.out.println("CollectionHelper.createRecommendationElement");
ingo@809:
ingo@809: Element recommendation = cr.create("recommendation");
ingo@809: cr.addAttr(recommendation, "factory", r.getFactory(), true);
sascha@836: cr.addAttr(recommendation, "ids", r.getIDs(), true);
ingo@809:
ingo@809: return recommendation;
ingo@809: }
ingo@905:
ingo@905:
ingo@905: /**
felix@1318: * Take the DESCRIBE document of the Collections describe()
ingo@905: * operation and extracts the information about the collection itself and
ingo@905: * the collection items.
ingo@905: *
ingo@905: * @param description The DESCRIBE document of the Collections describe()
ingo@905: * operation.
ingo@905: *
ingo@905: * @return a Collection with CollectionItems.
ingo@905: */
ingo@905: public static Collection parseCollection(Document description) {
ingo@905: System.out.println("AddArtifactServiceImpl.parseCollection");
ingo@905:
ingo@905: if (description == null) {
ingo@905: System.err.println("The DESCRIBE of the Collection is null!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: String uuid = XMLUtils.xpathString(
ingo@905: description,
ingo@905: "art:artifact-collection/@art:uuid",
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: String ttlStr = XMLUtils.xpathString(
ingo@905: description,
ingo@905: "art:artifact-collection/@art:ttl",
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: String name = XMLUtils.xpathString(
ingo@905: description,
ingo@905: "art:artifact-collection/@art:name",
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
felix@1318: if (uuid.length() == 0) {
felix@1318: System.err.println("Found an invalid (zero length uuid) Collection!");
felix@1318: return null;
felix@1318: }
felix@1318:
felix@1318: if (ttlStr.length() == 0) {
felix@1318: System.err.println("Found an invalid Collectioni (zero length ttl)!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: long ttl = -1;
ingo@905: try {
ingo@905: ttl = Long.valueOf(ttlStr);
ingo@905: }
ingo@905: catch (NumberFormatException nfe) {
ingo@905: // do nothing
ingo@905: }
ingo@905:
ingo@905: Map themeList = parseThemeLists(description);
ingo@905: List recommended = parseRecommendations(description);
ingo@905:
ingo@905: name = (name != null && name.length() > 0) ? name : uuid;
ingo@905:
ingo@905: Collection c = !themeList.isEmpty()
ingo@905: ? new DefaultCollection(uuid, ttl, name, recommended, themeList)
ingo@905: : new DefaultCollection(uuid, ttl, name, recommended);
ingo@905:
ingo@905: NodeList items = (NodeList) XMLUtils.xpath(
ingo@905: description,
ingo@905: "art:artifact-collection/art:artifacts/art:artifact",
ingo@905: XPathConstants.NODESET,
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: if (items == null || items.getLength() == 0) {
ingo@905: System.out.println("No collection item found for this collection.");
ingo@905:
ingo@905: return c;
ingo@905: }
ingo@905:
ingo@905: int size = items.getLength();
ingo@905:
ingo@905: for (int i = 0; i < size; i++) {
ingo@905: CollectionItem item = parseCollectionItem(
ingo@905: (Element)items.item(i),
ingo@905: i == 0);
ingo@905:
ingo@905: if (item != null) {
ingo@905: c.addItem(item);
ingo@905: }
ingo@905: }
ingo@905:
ingo@905: System.out.println(
ingo@905: "Found " + c.getItemLength() + " collection items " +
ingo@905: "for the Collection '" + c.identifier() + "'.");
ingo@905:
ingo@905: return c;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: protected static Map parseThemeLists(Document desc) {
ingo@905: System.out.println("DescribeCollectionServiceImpl.parseThemeLists");
ingo@905:
ingo@905: NodeList lists = (NodeList) XMLUtils.xpath(
ingo@905: desc,
ingo@905: "/art:artifact-collection/art:attribute/art:outputs/art:output",
ingo@905: XPathConstants.NODESET,
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: int num = lists != null ? lists.getLength() : 0;
ingo@905:
ingo@905: Map themeList = new HashMap(num);
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: for (int i = 0; i < num; i++) {
ingo@905: Element node = (Element)lists.item(i);
ingo@905:
ingo@905: String outName = node.getAttribute("name");
ingo@905:
ingo@905: if (outName.length() > 0) {
ingo@905: ThemeList list = parseThemeList(node);
ingo@905: if (list.getThemeCount() > 0) {
ingo@905: themeList.put(outName, list);
ingo@905: }
ingo@905: }
ingo@905: }
ingo@905:
ingo@905: return themeList;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: protected static ThemeList parseThemeList(Element node) {
ingo@905: System.out.println("DescribeCollectionServiceImpl.parseThemeList");
ingo@905:
ingo@905: NodeList themes = node.getElementsByTagNameNS(
ingo@905: ArtifactNamespaceContext.NAMESPACE_URI,
ingo@905: "facet");
ingo@905:
ingo@905: int num = themes != null ? themes.getLength() : 0;
ingo@905:
ingo@905: List themeList = new ArrayList(num);
ingo@905:
ingo@905: for (int i = 0; i < num; i++) {
ingo@905: Theme theme = parseTheme((Element)themes.item(i));
ingo@905:
ingo@905: if (theme != null) {
ingo@905: themeList.add(theme);
ingo@905: }
ingo@905: }
ingo@905:
ingo@905: return new ThemeList(themeList);
ingo@905: }
ingo@905:
ingo@905:
ingo@905: protected static Theme parseTheme(Element ele) {
ingo@905: System.out.println("DescribeCollectionServiceImpl.parseTheme");
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: NamedNodeMap attrMap = ele.getAttributes();
ingo@905: int attrNum = attrMap != null ? attrMap.getLength() : 0;
ingo@905:
ingo@905: AttributedTheme t = new AttributedTheme();
ingo@905:
ingo@905: for (int i = 0; i < attrNum; i++) {
ingo@905: Node attr = attrMap.item(i);
ingo@905:
ingo@905: String prefix = attr.getPrefix();
ingo@905: String name = attr.getNodeName().replace(prefix + ":", "");
ingo@905: String value = attr.getNodeValue();
ingo@905:
ingo@905: t.addAttr(name, value);
ingo@905: }
ingo@905:
ingo@905: return t;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: /**
ingo@905: * This method extracts the CollectionItem from node with its output
ingo@905: * modes. The output modes are parsed using the parseOutputModes() method.
ingo@905: *
ingo@905: * @param node A node that contains information about a CollectionItem.
ingo@905: *
ingo@905: * @return a CollectionItem.
ingo@905: */
ingo@905: protected static CollectionItem parseCollectionItem(
ingo@905: Element node,
ingo@905: boolean outs
ingo@905: ) {
ingo@905: System.out.println("AddArtifactServiceImpl.parseCollectionItem");
ingo@905:
ingo@905: if (node == null) {
ingo@905: System.err.println("The node for parsing CollectionItem is null!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: String uuid = node.getAttributeNS(uri, "uuid");
ingo@905: String hash = node.getAttributeNS(uri, "hash");
ingo@905:
ingo@905: if (uuid == null || uuid.length() == 0) {
ingo@905: System.err.println("Found an invalid CollectionItem!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: List modes = new ArrayList();
ingo@905:
ingo@905: if (outs) {
ingo@905: NodeList outputmodes = node.getElementsByTagNameNS(
ingo@905: uri, "outputmodes");
ingo@905:
ingo@905: if (outputmodes.getLength() < 1) {
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: Element om = (Element)outputmodes.item(0);
ingo@905:
ingo@905: modes = parseOutputModes(om);
ingo@905: }
ingo@905:
ingo@905: return new DefaultCollectionItem(uuid, hash, modes);
ingo@905: }
ingo@905:
ingo@905:
ingo@905: /**
ingo@905: * This method extracts the OutputModes available for a specific
ingo@905: * CollectionItem and returns them as list.
ingo@905: *
ingo@905: * @param node The root node of the outputmodes list.
ingo@905: *
ingo@905: * @return a list of OutputModes.
ingo@905: */
ingo@905: protected static List parseOutputModes(Element node) {
ingo@905: System.out.println("AddArtifactServiceImpl.parseOutputModes");
ingo@905:
ingo@905: if (node == null) {
ingo@905: System.err.println("The node for parsing OutputModes is null!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: NodeList list = node.getElementsByTagNameNS(uri, "output");
ingo@905:
ingo@905: int size = list.getLength();
ingo@905:
ingo@905: if (size == 0) {
ingo@905: System.err.println("No outputmode nodes found!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: List modes = new ArrayList(size);
ingo@905:
ingo@905: for (int i = 0; i < size; i++) {
ingo@905: Element tmp = (Element)list.item(i);
ingo@905:
ingo@905: String name = tmp.getAttributeNS(uri, "name");
ingo@905: String desc = tmp.getAttributeNS(uri, "description");
ingo@905: String mime = tmp.getAttributeNS(uri, "mime-type");
ingo@905: String type = tmp.getAttributeNS(uri, "type");
ingo@905:
ingo@905: if (name.length() == 0) {
ingo@905: System.err.println("Found an invalid output mode.");
ingo@905: continue;
ingo@905: }
ingo@905:
ingo@905: OutputMode outmode = null;
ingo@905: List fs = extractFacets(tmp);
ingo@905:
ingo@905: if (type.equals("export")) {
ingo@905: outmode = new ExportMode(name, desc, mime, fs);
ingo@905: }
ingo@905: else if (type.equals("report")) {
ingo@905: outmode = new ReportMode(name, desc, mime, fs);
ingo@905: }
ingo@905: else if (type.equals("chart")){
ingo@905: outmode = new ChartMode(name, desc, mime, fs);
ingo@905: }
ingo@905: else if (type.equals("map")){
ingo@905: outmode = new MapMode(name, desc, mime, fs);
ingo@905: }
ingo@905: else {
ingo@905: System.err.println("Broken Output mode without type found.");
ingo@905: continue;
ingo@905: }
ingo@905:
ingo@905: modes.add(outmode);
ingo@905: }
ingo@905:
ingo@905: return modes;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: protected static List extractFacets(Element outmode) {
ingo@905: System.out.println("DescribeCollectionServiceImpl - extractFacets()");
ingo@905:
ingo@905: NodeList facetList = (NodeList) XMLUtils.xpath(
ingo@905: outmode,
ingo@905: XPATH_FACETS,
ingo@905: XPathConstants.NODESET,
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: int num = facetList != null ? facetList.getLength() : 0;
ingo@905:
ingo@905: List facets = new ArrayList(num);
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: for (int i = 0; i < num; i++) {
ingo@905: Element facetEl = (Element) facetList.item(i);
ingo@905:
ingo@905: String name = facetEl.getAttributeNS(uri, "name");
ingo@905: String desc = facetEl.getAttributeNS(uri, "description");
ingo@905: String index = facetEl.getAttributeNS(uri, "index");
ingo@905:
ingo@905: if (name != null && name.length() > 0 && index != null) {
ingo@905: facets.add(new DefaultFacet(name, Integer.valueOf(index),desc));
ingo@905: }
ingo@905: }
ingo@905:
ingo@905: return facets;
ingo@905: }
ingo@905:
ingo@905:
ingo@905: public static List parseRecommendations(Document doc) {
ingo@905: System.out.println("DescribeCollectionServiceImpl.parseRecommendations");
ingo@905:
ingo@905: NodeList list = (NodeList) XMLUtils.xpath(
ingo@905: doc,
ingo@905: XPATH_LOADED_RECOMMENDATIONS,
ingo@905: XPathConstants.NODESET,
ingo@905: ArtifactNamespaceContext.INSTANCE);
ingo@905:
ingo@905: int num = list != null ? list.getLength() : 0;
ingo@905:
ingo@905: List recs = new ArrayList(num);
ingo@905:
ingo@905: String uri = ArtifactNamespaceContext.NAMESPACE_URI;
ingo@905:
ingo@905: for (int i = 0; i < num; i++) {
ingo@905: Element rec = (Element) list.item(i);
ingo@905:
ingo@905: String factory = rec.getAttributeNS(uri, "factory");
ingo@905: String dbids = rec.getAttributeNS(uri, "ids");
ingo@905:
ingo@905: if (factory != null && factory.length() > 0) {
ingo@905: recs.add(new Recommendation(factory, dbids));
ingo@905: }
ingo@905: }
ingo@905:
ingo@905: return recs;
ingo@905: }
ingo@905:
ingo@905:
felix@1318: /**
felix@1318: * Add an artifact to a collection.
felix@1318: * @param collection Collection to add artifact to.
felix@1318: * @param artifact Artifact to add to collection
felix@1318: */
ingo@905: public static Collection addArtifact(
ingo@905: Collection collection,
ingo@905: Artifact artifact,
ingo@905: String url,
ingo@905: String locale)
ingo@905: throws ServerException
ingo@905: {
ingo@905: System.out.println("Collection.addArtifact");
ingo@905:
ingo@905: if (collection == null) {
ingo@905: System.err.println("The given Collection is null!");
ingo@905: return null;
ingo@905: }
ingo@905:
ingo@905: Document add = ClientProtocolUtils.newAddArtifactDocument(
ingo@905: artifact.getUuid(), null);
ingo@905:
ingo@905: HttpClient client = new HttpClientImpl(url, locale);
ingo@905:
ingo@905: try {
ingo@905: System.out.println("Do HTTP request now.");
ingo@905:
ingo@905: Document response = (Document) client.doCollectionAction(
ingo@905: add, collection.identifier(), new DocumentResponseHandler());
ingo@905:
ingo@905: System.out.println(
ingo@905: "Finished HTTP request successfully. Parse Collection now.");
ingo@905:
ingo@905: Collection c = CollectionHelper.parseCollection(response);
ingo@905:
ingo@905: if (c == null) {
ingo@905: throw new ServerException(ERROR_ADD_ARTIFACT);
ingo@905: }
ingo@905:
ingo@905: return c;
ingo@905: }
ingo@905: catch (ConnectionException ce) {
ingo@905: System.err.println(ce.getLocalizedMessage());
ingo@905: }
ingo@905: catch (Exception e) {
ingo@905: e.printStackTrace();
ingo@905: }
ingo@905:
ingo@905: throw new ServerException(ERROR_ADD_ARTIFACT);
ingo@905: }
felix@1320:
felix@1320:
felix@1320: /**
felix@1320: * Remove an artifact from a collection.
felix@1320: * @param collection Collection to remove artifact to.
felix@1320: * @param artifact Artifact to add to collection
felix@1320: */
felix@1320: public static Collection removeArtifact(
felix@1320: Collection collection,
felix@1320: String artifactId,
felix@1320: String url,
felix@1320: String locale)
felix@1320: throws ServerException
felix@1320: {
felix@1320: System.out.println("Collection.removeArtifact");
felix@1320:
felix@1320: if (collection == null) {
felix@1320: System.err.println("The given Collection is null!");
felix@1320: return null;
felix@1320: }
felix@1320:
felix@1320: Document remove = ClientProtocolUtils.newRemoveArtifactDocument(
felix@1320: artifactId);
felix@1320:
felix@1320: HttpClient client = new HttpClientImpl(url, locale);
felix@1320:
felix@1320: try {
felix@1320: System.out.println("Do HTTP request now.");
felix@1320:
felix@1320: Document response = (Document) client.doCollectionAction(
felix@1320: remove, collection.identifier(), new DocumentResponseHandler());
felix@1320:
felix@1320: System.out.println(
felix@1320: "Finished HTTP request successfully. Parse Collection now.");
felix@1320: System.out.println(XMLUtils.toString(response));
felix@1320:
felix@1320: Collection c = CollectionHelper.parseCollection(response);
felix@1320:
felix@1320: if (c == null) {
felix@1332: throw new ServerException(ERROR_REMOVE_ARTIFACT);
felix@1320: }
felix@1320:
felix@1320: return c;
felix@1320: }
felix@1320: catch (ConnectionException ce) {
felix@1320: System.err.println(ce.getLocalizedMessage());
felix@1320: }
felix@1320: catch (Exception e) {
felix@1320: e.printStackTrace();
felix@1320: }
felix@1332: throw new ServerException(ERROR_REMOVE_ARTIFACT);
felix@1320: }
ingo@530: }
ingo@530: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :