From: Richard Millet Date: Mon, 3 Aug 2009 23:43:36 +0000 (+0000) Subject: cspace-271: Adding common relation-related classes. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=a56f4ad5769605aef0113a41943625bd395b85ee;p=tmp%2Fjakarta-migration.git cspace-271: Adding common relation-related classes. --- diff --git a/services/common/build.xml b/services/common/build.xml deleted file mode 100644 index 144607a67..000000000 --- a/services/common/build.xml +++ /dev/null @@ -1,128 +0,0 @@ - - - - collectionspace services common - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/services/common/src/main/java/org/collectionspace/services/RelationJAXBSchema.java b/services/common/src/main/java/org/collectionspace/services/RelationJAXBSchema.java index 215aa46c2..66a1a20d5 100644 --- a/services/common/src/main/java/org/collectionspace/services/RelationJAXBSchema.java +++ b/services/common/src/main/java/org/collectionspace/services/RelationJAXBSchema.java @@ -4,22 +4,35 @@ package org.collectionspace.services; /** - * @author remillet + * @author Richard Millet * */ public interface RelationJAXBSchema { // The Nuxeo root element name for the relation entity. + /** The Constant REL_ROOT_ELEM_NAME. */ final static String REL_ROOT_ELEM_NAME = "relationtype"; // Need to fix conflict between the Nuxeo XSD and the JAX-B XSD for the "relation" entity + /** The Constant DOCUMENT_ID_1. */ final static String DOCUMENT_ID_1 = "documentId1"; + + /** The Constant DOCUMENT_TYPE_1. */ final static String DOCUMENT_TYPE_1 = "documentType1"; + + /** The Constant DOCUMENT_ID_2. */ final static String DOCUMENT_ID_2 = "documentId2"; + + /** The Constant DOCUMENT_TYPE_2. */ final static String DOCUMENT_TYPE_2 = "documentType2"; + + /** The Constant RELATIONSHIP_TYPE. */ final static String RELATIONSHIP_TYPE = "relationshipType"; + /** The Constant ENUM_RELATIONSHIP_TYPE_ASSOC. */ final static String ENUM_RELATIONSHIP_TYPE_ASSOC = "association"; + + /** The Constant ENUM_RELATIONSHIP_TYPE_CONTAINS. */ final static String ENUM_RELATIONSHIP_TYPE_CONTAINS = "contains"; } diff --git a/services/common/src/main/java/org/collectionspace/services/RelationListItemJAXBSchema.java b/services/common/src/main/java/org/collectionspace/services/RelationListItemJAXBSchema.java index a8203f93b..6212c7cc0 100644 --- a/services/common/src/main/java/org/collectionspace/services/RelationListItemJAXBSchema.java +++ b/services/common/src/main/java/org/collectionspace/services/RelationListItemJAXBSchema.java @@ -1,6 +1,13 @@ package org.collectionspace.services; +/** + * The Interface RelationListItemJAXBSchema. + */ public interface RelationListItemJAXBSchema { + + /** The Constant CSID. */ final static String CSID = "csid"; + + /** The Constant URI. */ final static String URI = "url"; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/RelationUtils.java b/services/common/src/main/java/org/collectionspace/services/common/RelationUtils.java new file mode 100644 index 000000000..d49babaf0 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/RelationUtils.java @@ -0,0 +1,34 @@ +/** + * RelationUtils.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services.common; + +/** + * The Interface RelationUtils. + */ +public interface RelationUtils { + +} diff --git a/services/common/src/main/java/org/collectionspace/services/common/RelationsManager.java b/services/common/src/main/java/org/collectionspace/services/common/RelationsManager.java new file mode 100644 index 000000000..c381db51e --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/RelationsManager.java @@ -0,0 +1,56 @@ +/** + * RelationsManager.java + * + * {Purpose of This Class} + * + * {Other Notes Relating to This Class (Optional)} + * + * $LastChangedBy: $ + * $LastChangedRevision: $ + * $LastChangedDate: $ + * + * This document is a part of the source code and related artifacts + * for CollectionSpace, an open source collections management system + * for museums and related institutions: + * + * http://www.collectionspace.org + * http://wiki.collectionspace.org + * + * Copyright © 2009 {Contributing Institution} + * + * Licensed under the Educational Community License (ECL), Version 2.0. + * You may not use this file except in compliance with this License. + * + * You may obtain a copy of the ECL 2.0 License at + * https://source.collectionspace.org/collection-space/LICENSE.txt + */ +package org.collectionspace.services.common; + +/** + * The Class RelationsManager. + */ +public class RelationsManager { + + /** The relation utils. */ + private RelationUtils relationUtils = null; + + // FIXME: Add singleton patter here. + /** + * Sets the utils. + * + * @param utils the new utils + */ + public void setUtils(RelationUtils utils) { + relationUtils = utils; + } + + /** + * Gets the relation utils. + * + * @return the relation utils + */ + public RelationUtils getRelationUtils() { + return relationUtils; + } + +} diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java index deb8112ce..89bbd7c55 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java @@ -3,29 +3,155 @@ */ package org.collectionspace.services.nuxeo; +import java.io.IOException; + +import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.nuxeo.client.rest.NuxeoRESTClient; +import org.collectionspace.services.nuxeo.client.java.NuxeoConnector; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.dom.DOMDocument; +import org.dom4j.dom.DOMDocumentFactory; + +import org.w3c.dom.DOMException; +import org.w3c.dom.Element; + +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.DocumentRef; +import org.nuxeo.ecm.core.api.IdRef; +import org.nuxeo.ecm.core.api.ClientException; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.client.NuxeoClient; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author remillet - * + * */ public abstract class CollectionSpaceServiceNuxeoImpl { - //replace host if not running on localhost - //static String CS_NUXEO_HOST = "173.45.234.217"; - static String CS_NUXEO_HOST = "localhost"; - static String CS_NUXEO_URI = "http://" + CS_NUXEO_HOST + ":8080/nuxeo"; - protected Logger logger = LoggerFactory.getLogger(CollectionSpaceServiceNuxeoImpl.class); + // replace host if not running on localhost + // static String CS_NUXEO_HOST = "173.45.234.217"; + static String CS_NUXEO_HOST = "localhost"; + static String CS_NUXEO_URI = "http://" + CS_NUXEO_HOST + ":8080/nuxeo"; + + protected Logger logger = LoggerFactory + .getLogger(CollectionSpaceServiceNuxeoImpl.class); + + public NuxeoRESTClient getClient() { + NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI); + + nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC); + nxClient.setBasicAuthentication("Administrator", "Administrator"); + + return nxClient; + } + +// FIXME: Replace this method after integration of the relation code + protected RepositoryInstance getRepositorySession() throws Exception { + // FIXME: is it possible to reuse repository session? + // Authentication failures happen while trying to reuse the session + NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance(); + return nuxeoConnector.getRepositorySession(); + } + + protected Document deleteDocument(RepositoryInstance repoSession, String csid) throws DocumentException, + IOException { + Document result = null; + + try { + repoSession = getRepositorySession(); + DocumentRef relDocumentRef = new IdRef(csid); + repoSession.removeDocument(relDocumentRef); + repoSession.save(); + result = new DOMDocument(); // set to non-null to indicate success + } catch (Exception e) { + e.printStackTrace(); + } + + return result; + } + + protected Document browseWorkspace(RepositoryInstance repoSession, + String workspaceName) { + + DOMDocumentFactory domfactory = new DOMDocumentFactory(); + DOMDocument result = (DOMDocument) domfactory.createDocument(); + + try { + repoSession = getRepositorySession(); + DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession, + workspaceName); + + Element current = result.createElement("document"); + try { + current.setAttribute("title", workspaceModel.getTitle()); + } catch (Exception e) { + e.printStackTrace(); + } + current.setAttribute("type", workspaceModel.getType()); + current.setAttribute("id", workspaceModel.getId()); + current.setAttribute("name", workspaceModel.getName()); + current.setAttribute("url", getRelURL(workspaceName, workspaceModel.getRef().toString())); + result.setRootElement((org.dom4j.Element) current); + + if (workspaceModel.isFolder()) { + // Element childrenElem = result.createElement("children"); + // root.appendChild(childrenElem); + + DocumentModelList children = null; + try { + children = repoSession.getChildren(workspaceModel.getRef()); + } catch (ClientException e) { + e.printStackTrace(); + } - public NuxeoRESTClient getClient() { - NuxeoRESTClient nxClient = new NuxeoRESTClient(CS_NUXEO_URI); + for (DocumentModel child : children) { + Element el = result.createElement("document"); + try { + el.setAttribute("title", child.getTitle()); + } catch (DOMException e) { + e.printStackTrace(); + } catch (ClientException e) { + e.printStackTrace(); + } + el.setAttribute("type", child.getType()); + el.setAttribute("id", child.getId()); + el.setAttribute("name", child.getName()); + el.setAttribute("url", getRelURL(workspaceName, child.getRef() + .toString())); + current.appendChild(el); + } + } - nxClient.setAuthType(NuxeoRESTClient.AUTH_TYPE_BASIC); - nxClient.setBasicAuthentication("Administrator", "Administrator"); + } catch (Exception e) { + e.printStackTrace(); + } + + if (logger.isDebugEnabled() == true) { + System.out.println(result.asXML()); + } - return nxClient; - } + return result; + } + protected void releaseRepositorySession(RepositoryInstance repoSession) { + try { + // release session + NuxeoConnector nuxeoConnector = NuxeoConnector.getInstance(); + nuxeoConnector.releaseRepositorySession(repoSession); + } catch (Exception e) { + logger.error("Could not close the repository session", e); + // no need to throw this service specific exception + } + } + + private static String getRelURL(String repo, String uuid) { + return '/' + repo + '/' + uuid; + } } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/RelationUtilsNuxeoImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/RelationUtilsNuxeoImpl.java new file mode 100644 index 000000000..2a355df05 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/RelationUtilsNuxeoImpl.java @@ -0,0 +1,154 @@ +package org.collectionspace.services.nuxeo; + +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.collectionspace.services.RelationJAXBSchema; +import org.collectionspace.services.common.RelationUtils; +import org.collectionspace.services.relation.Relation; +import org.collectionspace.services.relation.RelationshipType; +import org.collectionspace.services.nuxeo.util.NuxeoUtils; +import org.collectionspace.services.common.repository.DocumentException; + +//import org.dom4j.DocumentException; +import org.nuxeo.ecm.core.api.ClientException; +import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.model.DocumentPart; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RelationUtilsNuxeoImpl implements RelationUtils { + + static public String CS_RELATION_SERVICE_NAME = "relations"; + + final public static String REL_NUXEO_DOCTYPE = "Relation"; + final public static String REL_NUXEO_SCHEMA_NAME = "relation"; + final public static String REL_NUXEO_SCHEMA_ROOT_ELEMENT = "relationtype"; + final public static String REL_NUXEO_DC_TITLE = "CollectionSpace-Relation"; + + private static Logger logger = LoggerFactory + .getLogger(RelationUtilsNuxeoImpl.class); + + + static public void fillRelationFromDocModel(Relation relation, DocumentModel relDocModel) + throws ClientException { + String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/"; + Object valueObject = null; + + valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot + + RelationJAXBSchema.DOCUMENT_ID_1); + relation.setDocumentId1((String) valueObject); + + valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot + + RelationJAXBSchema.DOCUMENT_TYPE_1); + relation.setDocumentType1((String) valueObject); + + valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot + + RelationJAXBSchema.DOCUMENT_ID_2); + relation.setDocumentId2((String) valueObject); + + valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot + + RelationJAXBSchema.DOCUMENT_TYPE_1); + relation.setDocumentType2((String) valueObject); + + valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot + + RelationJAXBSchema.RELATIONSHIP_TYPE); + relation.setRelationshipType(RelationshipType + .fromValue((String) valueObject)); + + if (logger.isDebugEnabled() == true) { + System.out.println("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"); + System.out.println(relation.toString()); + System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); + } + } + + static public void fillDocModelFromRelation(Relation p, DocumentModel relDocModel) + throws Exception { + + // set the DublinCore title (this works) + relDocModel.setPropertyValue("dublincore:title", "default title"); + + // // set value for element + // try { + // relDocModel.setProperty("relation", "/relationtype/documentId1", + // "docId1"); + // } catch (Exception x) { + // x.printStackTrace(); + // } + + String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/"; + if (p.getDocumentId1() != null) { + String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1; + relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p + .getDocumentId1()); + } + if (p.getDocumentType1() != null) { + String property = xpathRoot + RelationJAXBSchema.DOCUMENT_TYPE_1; + relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p + .getDocumentType1()); + } + if (p.getDocumentId2() != null) { + String property = xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2; + relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p + .getDocumentId2()); + } + if (p.getDocumentType2() != null) { + String property = xpathRoot + "/" + + RelationJAXBSchema.DOCUMENT_TYPE_2; + relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p + .getDocumentType2()); + } + + if (p.getRelationshipType() != null) { + String property = xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE; + relDocModel.setProperty(REL_NUXEO_SCHEMA_NAME, property, p + .getRelationshipType().value()); + } + } + + static public void printDocumentModel(DocumentModel documentModel) { + System.out.println(documentModel); + } + + static private void describeDocumentModel(DocumentModel docModel) throws Exception { + String[] schemas = docModel.getDeclaredSchemas(); + for (int i = 0; schemas != null && i < schemas.length; i++) { + System.out.println("Schema-" + i + "=" + schemas[i]); + } + + DocumentPart[] parts = docModel.getParts(); + Map propertyValues = null; + for (int i = 0; parts != null && i < parts.length; i++) { + System.out.println("Part-" + i + " name =" + parts[i].getName()); + System.out.println("Part-" + i + " path =" + parts[i].getPath()); + System.out.println("Part-" + i + " schema =" + parts[i].getSchema().getName()); + propertyValues = parts[i].exportValues(); + } + + } + + static public List getRelationships(RepositoryInstance repoSession) + throws DocumentException, IOException, ClientException { + List result = new ArrayList(); + + DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel( + repoSession, CS_RELATION_SERVICE_NAME); + DocumentModelList children = repoSession.getChildren(relationWorkspace + .getRef()); + Relation relation = null; + for (DocumentModel child : children) { + relation = new Relation(); + fillRelationFromDocModel(relation, child); + result.add(relation); + } + + return result; + } + +} diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java index cc4cf4733..35fc56675 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/util/NuxeoUtils.java @@ -20,22 +20,30 @@ package org.collectionspace.services.nuxeo.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; + import org.collectionspace.services.common.repository.DocumentException; +import org.collectionspace.services.common.ServiceMain; + import org.dom4j.Document; import org.dom4j.io.SAXReader; + import org.nuxeo.ecm.core.api.DocumentModel; +import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.repository.RepositoryInstance; +import org.nuxeo.ecm.core.api.DocumentRef; +import org.nuxeo.ecm.core.api.IdRef; import org.nuxeo.ecm.core.io.DocumentPipe; import org.nuxeo.ecm.core.io.DocumentReader; import org.nuxeo.ecm.core.io.DocumentWriter; import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl; import org.nuxeo.ecm.core.io.impl.plugins.SingleDocumentReader; import org.nuxeo.ecm.core.io.impl.plugins.XMLDocumentWriter; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * Various utilities related to Nuxeo + * Various utilities related to Nuxeo API */ public class NuxeoUtils { @@ -95,4 +103,43 @@ public class NuxeoUtils { } return doc; } + +public static Document getDocument(RepositoryInstance repoSession, String csid) + throws DocumentException { + Document result = null; + + DocumentModel docModel = getDocumentModel(repoSession, csid); + result = getDocument(repoSession, docModel); + + return result; + } + + public static DocumentModel getWorkspaceModel( + RepositoryInstance repoSession, String workspaceName) + throws DocumentException, IOException, ClientException { + DocumentModel result = null; + + String workspaceUUID = ServiceMain.getInstance().getWorkspaceId( + workspaceName); + DocumentRef workspaceRef = new IdRef(workspaceUUID); + result = repoSession.getDocument(workspaceRef); + + return result; + } + + public static DocumentModel getDocumentModel( + RepositoryInstance repoSession, String csid) + throws DocumentException { + DocumentModel result = null; + + try { + DocumentRef documentRef = new IdRef(csid); + result = repoSession.getDocument(documentRef); + } catch (ClientException e) { + e.printStackTrace(); + } + + return result; + } + }