]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-332: A working snapshot set of Relation manager sources before checking in...
authorRichard Millet <richard.millet@berkeley.edu>
Fri, 7 Aug 2009 19:22:55 +0000 (19:22 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Fri, 7 Aug 2009 19:22:55 +0000 (19:22 +0000)
services/JaxRsServiceProvider/pom.xml
services/common/src/main/java/org/collectionspace/services/RelationJAXBSchema.java
services/common/src/main/java/org/collectionspace/services/RelationListItemJAXBSchema.java
services/common/src/main/java/org/collectionspace/services/common/RelationUtils.java
services/common/src/main/java/org/collectionspace/services/common/RelationsManager.java
services/common/src/main/java/org/collectionspace/services/nuxeo/CollectionSpaceServiceNuxeoImpl.java
services/common/src/main/java/org/collectionspace/services/nuxeo/RelationUtilsNuxeoImpl.java
services/common/src/main/resources/relation.xsd
services/relation/service/src/main/java/org/collectionspace/services/RelationResource.java
services/relation/service/src/main/java/org/collectionspace/services/RelationService.java
services/relation/service/src/main/java/org/collectionspace/services/RelationServiceNuxeoImpl.java

index a3edaf3219cb2f37bc2aef524ba67f20e9122bb5..df50b22736140f5fb3282bc63abf175f430e5e7e 100644 (file)
                     <port>8180</port>\r
                 </configuration>\r
                 <executions>\r
-                    <execution>\r
+<!--                    <execution>\r
                         <id>jboss-undeploy</id>\r
                         <goals>\r
                             <goal>undeploy</goal>\r
                                 <param>${project.build.directory}/${project.build.finalName}.war</param>\r
                             </fileNames>\r
                         </configuration>\r
-                    </execution>\r
+                    </execution> -->\r
                 </executions>\r
             </plugin>\r
 \r
index 66a1a20d58d1b5ff4fd40f3b87873a66ddd4f4f5..74c58e3c58c906247c50bf03c099a21c82e758f7 100644 (file)
@@ -1,11 +1,33 @@
-/**\r
- * \r
+/**    \r
+ * RelationJAXBSchema.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
  */\r
 package org.collectionspace.services;\r
 \r
 /**\r
- * @author Richard Millet\r
- *\r
+ * The Interface RelationJAXBSchema.\r
  */\r
 public interface RelationJAXBSchema {\r
        \r
@@ -14,6 +36,9 @@ public interface RelationJAXBSchema {
        final static String REL_ROOT_ELEM_NAME = "relationtype";\r
        // Need to fix conflict between the Nuxeo XSD and the JAX-B XSD for the "relation" entity\r
        \r
+       /** The Constant CSID. */\r
+       final static String CSID = "csid";\r
+       \r
        /** The Constant DOCUMENT_ID_1. */\r
        final static String DOCUMENT_ID_1 = "documentId1";\r
        \r
@@ -29,11 +54,19 @@ public interface RelationJAXBSchema {
        /** The Constant RELATIONSHIP_TYPE. */\r
        final static String RELATIONSHIP_TYPE = "relationshipType";\r
        \r
+       /*\r
+        * Relation Types/Predicates Enumerations\r
+        */\r
+       \r
        /** The Constant ENUM_RELATIONSHIP_TYPE_ASSOC. */\r
-       final static String ENUM_RELATIONSHIP_TYPE_ASSOC = "association";\r
+       final static String ENUM_REL_TYPE_ASSOC = "association";\r
        \r
        /** The Constant ENUM_RELATIONSHIP_TYPE_CONTAINS. */\r
-       final static String ENUM_RELATIONSHIP_TYPE_CONTAINS = "contains";\r
+       final static String ENUM_REL_TYPE_CONTAINS = "contains";\r
+       \r
+       /** The Constant ENUM_RELATIONSHIP_TYPE_COLLECTIONOBJECT_INTAKE. */\r
+       final static String ENUM_REL_TYPE_COLLECTIONOBJECT_INTAKE = "collectionobject-intake";\r
+       \r
 }\r
 \r
 \r
index 6212c7cc0de27a2cbcbf13feea3477ed6576a5ae..7e500529cae7ede1390f29e6e8798a5b79249bd4 100644 (file)
@@ -1,3 +1,29 @@
+/**    \r
+ * RelationListItemJAXBSchema.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services;\r
 \r
 /**\r
@@ -5,6 +31,9 @@ package org.collectionspace.services;
  */\r
 public interface RelationListItemJAXBSchema {\r
        \r
+       /** The Constant REL_ROOT_ELEM_NAME. */\r
+       final static String REL_ROOT_ELEM_NAME = "relation-list";\r
+       \r
        /** The Constant CSID. */\r
        final static String CSID = "csid";\r
        \r
index d49babaf0446794675ffe4d4f0f34757671a288b..cc4797ae80c2f78499f0c623dbb23b1fee1b74de 100644 (file)
  */\r
 package org.collectionspace.services.common;\r
 \r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import org.collectionspace.services.common.repository.DocumentException;\r
+import org.collectionspace.services.relation.Relation;\r
+import org.dom4j.Document;\r
+\r
 /**\r
  * The Interface RelationUtils.\r
  */\r
 public interface RelationUtils {\r
 \r
-}\r
+       /**\r
+        * Gets the relationships.\r
+        * \r
+        * @param repoSession the repo session\r
+        * \r
+        * @return the relationships\r
+        * \r
+        * @throws DocumentException the document exception\r
+        */\r
+       public List<Relation> getRelationships(Object repoSession)\r
+                       throws DocumentException;\r
+\r
+       /**\r
+        * Gets the relationships for the entity corresponding to the CSID=csid.\r
+        * The csid refers to either the subject *OR* the object.\r
+        * \r
+        * @param nuxeoRepoSession the nuxeo repo session\r
+        * @param csid the csid\r
+        * \r
+        * @return the relationships\r
+        * \r
+        * @throws DocumentException the document exception\r
+        */\r
+       public List<Relation> getRelationships(Object nuxeoRepoSession, String csid)\r
+               throws DocumentException;\r
+       \r
+       /**\r
+        * Gets the relationships.\r
+        * \r
+        * @param repoSession the repo session\r
+        * @param subjectCsid the subject csid\r
+        * @param relationType the relation type\r
+        * @param objectCsid the object csid\r
+        * \r
+        * @return the relationships\r
+        * \r
+        * @throws DocumentException the document exception\r
+        */\r
+       public List<Relation> getRelationships(Object repoSession,\r
+                       String subjectCsid, \r
+                       String relationType, \r
+                       String objectCsid)\r
+               throws DocumentException;\r
+\r
+       /**\r
+        * Creates the relationship.\r
+        * \r
+        * @param repoSession the repo session\r
+        * @param subjectCsid the subject csid\r
+        * @param predicate the predicate\r
+        * @param objectCsid the object csid\r
+        * \r
+        * @return the relation\r
+        * \r
+        * @throws DocumentException the document exception\r
+        */\r
+       public Relation createRelationship(Object repoSession, String subjectCsid,\r
+                       String predicate, String objectCsid) throws DocumentException;\r
+}
\ No newline at end of file
index c381db51e751430e7557369d05e8a99001e0fd83..85135af6415e00a9e2a9d6558fb86e7bce722ec1 100644 (file)
  */\r
 package org.collectionspace.services.common;\r
 \r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import org.collectionspace.services.common.repository.DocumentException;\r
+import org.collectionspace.services.nuxeo.RelationUtilsNuxeoImpl;\r
+import org.collectionspace.services.relation.Relation;\r
+import org.dom4j.Document;\r
+\r
 /**\r
  * The Class RelationsManager.\r
+ * \r
+ * This class loosely uses the RDF terms subject, predicate, object to describe\r
+ * relationships between entity objects.  For example, if a CollectionObject entity named\r
+ * CO#1 is related to a Intake entity named IN#1 then the corresponding\r
+ * RDF-like term would be: (Subject) CO#1 (Predicate) has-intake (Object) IN#1.\r
+ * \r
+ * Many of the methods below, refer to RDF-like terms such as "Subject" and "Object" and\r
+ * "Predicate."\r
+ *  \r
  */\r
 public class RelationsManager {\r
-       \r
+\r
        /** The relation utils. */\r
-       private RelationUtils relationUtils = null;\r
-       \r
-       // FIXME: Add singleton patter here.\r
+       static private RelationUtils relationUtils = new RelationUtilsNuxeoImpl();\r
+\r
+       /**\r
+        * Gets the ALL relationships in the system.\r
+        * \r
+        * @param repoSession\r
+        *            the repo session\r
+        * \r
+        * @return the relationships\r
+        * \r
+        * @throws DocumentException\r
+        *             the document exception\r
+        */\r
+       static public List<Relation> getRelationships(Object repoSession)\r
+                       throws DocumentException {\r
+               return relationUtils.getRelationships(repoSession);\r
+       }\r
+               \r
        /**\r
-        * Sets the utils.\r
+        * Gets the relationships.  Null values act as wild card and match everything.\r
+        * \r
+        * @param repoSession the repo session\r
+        * @param subjectCsid the subject csid\r
+        * @param predicate the predicate\r
+        * @param objectCsid the object csid\r
+        * \r
+        * @return the relationships\r
         * \r
-        * @param utils the new utils\r
+        * @throws DocumentException the document exception\r
         */\r
-       public void setUtils(RelationUtils utils) {\r
-               relationUtils = utils;\r
+       static public List<Relation> getRelationships(Object repoSession, String subjectCsid, String predicate, String objectCsid)\r
+                       throws DocumentException {\r
+               return relationUtils.getRelationships(repoSession, subjectCsid, predicate, objectCsid);\r
        }\r
        \r
        /**\r
-        * Gets the relation utils.\r
+        * Gets the relationships for the entity corresponding to the CSID=csid.\r
+        * The csid refers to either the subject OR the object\r
+        * \r
+        * @param repoSession\r
+        *            the repo session\r
+        * @param csid\r
+        *            the csid\r
+        * \r
+        * @return the relationships\r
+        * \r
+        * @throws DocumentException\r
+        *             the document exception\r
+        */\r
+       static public List<Relation> getRelationships(Object repoSession,\r
+                       String csid) throws DocumentException {\r
+               return relationUtils.getRelationships(repoSession, csid);\r
+       }\r
+               \r
+       /**\r
+        * Creates the relationship.\r
+        * \r
+        * @param repoSession the repo session\r
+        * @param subjectCsid the subject csid\r
+        * @param predicate the predicate\r
+        * @param objectCsid the object csid\r
+        * \r
+        * @return the relation\r
         * \r
-        * @return the relation utils\r
+        * @throws DocumentException the document exception\r
         */\r
-       public RelationUtils getRelationUtils() {\r
-               return relationUtils;\r
+       static public Relation createRelationship(Object repoSession,\r
+                       String subjectCsid,\r
+                       String predicate,\r
+                       String objectCsid) throws DocumentException {\r
+               return relationUtils.createRelationship(repoSession, subjectCsid,\r
+                               predicate, objectCsid);\r
        }\r
 \r
 }\r
index 89bbd7c55b0cfd6a91bfc7a76550a1aed9935a2f..bcad5ee344b092ca740a60359a964a18dc29ae46 100644 (file)
@@ -6,6 +6,7 @@ package org.collectionspace.services.nuxeo;
 import java.io.IOException;\r
 \r
 import org.collectionspace.services.common.ServiceMain;\r
+import org.collectionspace.services.common.repository.DocumentNotFoundException;\r
 import org.collectionspace.services.nuxeo.client.rest.NuxeoRESTClient;\r
 import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;\r
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
@@ -60,16 +61,22 @@ public abstract class CollectionSpaceServiceNuxeoImpl {
                return nuxeoConnector.getRepositorySession();\r
        }\r
                        \r
-       protected Document deleteDocument(RepositoryInstance repoSession, String csid) throws DocumentException,\r
-                       IOException {\r
+       protected Document deleteDocument(RepositoryInstance repoSession, String csid)\r
+                       throws DocumentException, IOException {\r
                Document result = null;\r
 \r
                try {\r
                        repoSession = getRepositorySession();\r
                        DocumentRef relDocumentRef = new IdRef(csid);\r
-                       repoSession.removeDocument(relDocumentRef);\r
-                       repoSession.save();\r
-                       result = new DOMDocument(); // set to non-null to indicate success\r
+                       \r
+            try{\r
+                repoSession.removeDocument(relDocumentRef);\r
+            }catch(ClientException ce){\r
+                String msg = "could not find document to delete with id=" + csid;\r
+                logger.error(msg, ce);\r
+                throw new DocumentNotFoundException(msg, ce);\r
+            }\r
+            repoSession.save();                        \r
                } catch (Exception e) {\r
                        e.printStackTrace();\r
                }\r
@@ -77,7 +84,7 @@ public abstract class CollectionSpaceServiceNuxeoImpl {
                return result;\r
        }\r
        \r
-       protected Document browseWorkspace(RepositoryInstance repoSession,\r
+       protected Document listWorkspaceContent(RepositoryInstance repoSession,\r
                        String workspaceName) {\r
 \r
                DOMDocumentFactory domfactory = new DOMDocumentFactory();\r
index 2a355df05f88959889c156f1af5346fd00038920..b94c549ad52628da45177d830a108974893d8c3f 100644 (file)
@@ -1,3 +1,29 @@
+/**    \r
+ * RelationUtilsNuxeoImpl.java\r
+ *\r
+ * {Purpose of This Class}\r
+ *\r
+ * {Other Notes Relating to This Class (Optional)}\r
+ *\r
+ * $LastChangedBy: $\r
+ * $LastChangedRevision: $\r
+ * $LastChangedDate: $\r
+ *\r
+ * This document is a part of the source code and related artifacts\r
+ * for CollectionSpace, an open source collections management system\r
+ * for museums and related institutions:\r
+ *\r
+ * http://www.collectionspace.org\r
+ * http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009 {Contributing Institution}\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0.\r
+ * You may not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ */\r
 package org.collectionspace.services.nuxeo;\r
 \r
 import java.io.IOException;\r
@@ -7,13 +33,22 @@ import java.util.List;
 import java.util.Map;\r
 \r
 import org.collectionspace.services.RelationJAXBSchema;\r
+import org.collectionspace.services.RelationListItemJAXBSchema;\r
+\r
 import org.collectionspace.services.common.RelationUtils;\r
 import org.collectionspace.services.relation.Relation;\r
 import org.collectionspace.services.relation.RelationshipType;\r
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
 import org.collectionspace.services.common.repository.DocumentException;\r
 \r
+import org.dom4j.Document;\r
+import org.dom4j.Element;\r
+import org.dom4j.dom.DOMElement;\r
+import org.dom4j.dom.DOMDocument;\r
+import org.dom4j.dom.DOMDocumentFactory;\r
 //import org.dom4j.DocumentException;\r
+\r
+import org.nuxeo.common.utils.IdUtils;\r
 import org.nuxeo.ecm.core.api.ClientException;\r
 import org.nuxeo.ecm.core.api.DocumentModel;\r
 import org.nuxeo.ecm.core.api.DocumentModelList;\r
@@ -21,25 +56,50 @@ import org.nuxeo.ecm.core.api.model.DocumentPart;
 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
+//import org.w3c.dom.DOMException;\r
+//import org.w3c.dom.Element;\r
 \r
+/**\r
+ * The Class RelationUtilsNuxeoImpl.\r
+ */\r
 public class RelationUtilsNuxeoImpl implements RelationUtils {\r
        \r
+       /** The C s_ relatio n_ servic e_ name. */\r
        static public String CS_RELATION_SERVICE_NAME = "relations";\r
+       static public String CS_EMPTY_STRING = "";\r
        \r
+       /** The Constant REL_NUXEO_DOCTYPE. */\r
        final public static String REL_NUXEO_DOCTYPE = "Relation";\r
+       \r
+       /** The Constant REL_NUXEO_SCHEMA_NAME. */\r
        final public static String REL_NUXEO_SCHEMA_NAME = "relation";\r
+       \r
+       /** The Constant REL_NUXEO_SCHEMA_ROOT_ELEMENT. */\r
        final public static String REL_NUXEO_SCHEMA_ROOT_ELEMENT = "relationtype";\r
+       \r
+       /** The Constant REL_NUXEO_DC_TITLE. */\r
        final public static String REL_NUXEO_DC_TITLE = "CollectionSpace-Relation";\r
        \r
+       /** The logger. */\r
        private static Logger logger = LoggerFactory\r
        .getLogger(RelationUtilsNuxeoImpl.class);       \r
 \r
 \r
+       /**\r
+        * Fill relation from doc model.\r
+        * \r
+        * @param relation the relation\r
+        * @param relDocModel the rel doc model\r
+        * \r
+        * @throws ClientException the client exception\r
+        */\r
        static public void fillRelationFromDocModel(Relation relation, DocumentModel relDocModel)\r
                        throws ClientException {\r
                String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
                Object valueObject = null;\r
 \r
+               relation.setCsid(relDocModel.getId());\r
+\r
                valueObject = relDocModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
                                + RelationJAXBSchema.DOCUMENT_ID_1);\r
                relation.setDocumentId1((String) valueObject);\r
@@ -68,6 +128,14 @@ public class RelationUtilsNuxeoImpl implements RelationUtils {
                }\r
        }\r
 \r
+       /**\r
+        * Fill doc model from relation.\r
+        * \r
+        * @param p the p\r
+        * @param relDocModel the rel doc model\r
+        * \r
+        * @throws Exception the exception\r
+        */\r
        static public void fillDocModelFromRelation(Relation p, DocumentModel relDocModel)\r
                        throws Exception {\r
 \r
@@ -112,10 +180,22 @@ public class RelationUtilsNuxeoImpl implements RelationUtils {
                }\r
        }\r
        \r
+       /**\r
+        * Prints the document model.\r
+        * \r
+        * @param documentModel the document model\r
+        */\r
        static public void printDocumentModel(DocumentModel documentModel) {\r
                System.out.println(documentModel);\r
        }\r
        \r
+       /**\r
+        * Describe document model.\r
+        * \r
+        * @param docModel the doc model\r
+        * \r
+        * @throws Exception the exception\r
+        */\r
        static private void describeDocumentModel(DocumentModel docModel) throws Exception {\r
                String[] schemas = docModel.getDeclaredSchemas();\r
                for (int i = 0; schemas != null && i < schemas.length; i++) {\r
@@ -133,22 +213,324 @@ public class RelationUtilsNuxeoImpl implements RelationUtils {
 \r
        }\r
 \r
-       static public List<Relation> getRelationships(RepositoryInstance repoSession)\r
-                       throws DocumentException, IOException, ClientException {\r
-               List<Relation> result = new ArrayList<Relation>();\r
+       /**\r
+        * Creates the relationship.\r
+        * \r
+        * @param nuxeoRepoSession the nuxeo repo session\r
+        * @param newRelation the new relation\r
+        * \r
+        * @return the document model\r
+        * \r
+        * @throws DocumentException the document exception\r
+        */\r
+       static public DocumentModel createRelationship(Object nuxeoRepoSession, Relation newRelation)\r
+                       throws DocumentException {\r
+               DocumentModel result = null;\r
+               RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
+               \r
+               try {\r
+                       // get the Nuxeo 'Relations' workspace\r
+                       DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,\r
+                                       CS_RELATION_SERVICE_NAME);\r
+                       \r
+               String docType = REL_NUXEO_DOCTYPE;\r
+               String id = IdUtils.generateId("New " + docType);\r
+               \r
+               //create document model\r
+               String workspacePath = workspaceModel.getPathAsString();            \r
+               DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType);\r
+       \r
+               newRelation.setCsid(newRelDocModel.getId());\r
+               fillDocModelFromRelation(newRelation, newRelDocModel);\r
+               \r
+               //create document with the new DocumentModel\r
+               result = repoSession.createDocument(newRelDocModel);\r
+               repoSession.save();\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+                       throw new DocumentException(e);\r
+               }\r
+               \r
+               return result;\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.RelationUtils#createRelationship(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)\r
+        */\r
+       public Relation createRelationship(Object nuxeoRepoSession, String subjectCsid, String predicate,\r
+                       String objectCsid) throws DocumentException {\r
+               Relation result = null;\r
+               RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
+               \r
+        Relation temp = new Relation();\r
+        temp.setDocumentId1(subjectCsid);\r
+        temp.setRelationshipType(null);\r
+        temp.setDocumentId2(objectCsid);\r
+        createRelationship(repoSession, temp);\r
+               \r
+               return result;\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object)\r
+        */\r
+       public List<Relation> getRelationships(Object nuxeoRepoSession)\r
+                       throws DocumentException {\r
+               List<Relation> result = null;\r
+               RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
+\r
+               try {\r
+                       DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
+                                       repoSession, CS_RELATION_SERVICE_NAME);\r
+                       DocumentModelList children = repoSession.getChildren(relationWorkspace\r
+                                       .getRef());\r
+                       \r
+                       result = new ArrayList<Relation>();\r
+                       Relation relation = null;\r
+                       for (DocumentModel child : children) {\r
+                               relation = new Relation();\r
+                               fillRelationFromDocModel(relation, child);\r
+                               result.add(relation);\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+                       throw new DocumentException(e);\r
+               }\r
 \r
-               DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
-                               repoSession, CS_RELATION_SERVICE_NAME);\r
-               DocumentModelList children = repoSession.getChildren(relationWorkspace\r
-                               .getRef());\r
-               Relation relation = null;\r
-               for (DocumentModel child : children) {\r
-                       relation = new Relation();\r
-                       fillRelationFromDocModel(relation, child);\r
-                       result.add(relation);\r
+               return result;\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String)\r
+        */\r
+       public List<Relation> getRelationships(Object nuxeoRepoSession, String csid)\r
+                       throws DocumentException {\r
+               List<Relation> result = null;\r
+               RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
+                               \r
+               try {\r
+                       DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
+                                       repoSession, CS_RELATION_SERVICE_NAME);\r
+                       DocumentModelList children = repoSession.getChildren(relationWorkspace\r
+                                       .getRef());\r
+                       \r
+                       result = new ArrayList<Relation>();\r
+                       Relation relation = null;\r
+                       for (DocumentModel child : children) {\r
+                               if ((isSubjectOfRelation(csid, child) == true) ||\r
+                                               (isObjectOfRelation(csid, child) == true)) {\r
+                                       relation = new Relation();\r
+                                       fillRelationFromDocModel(relation, child);\r
+                                       result.add(relation);\r
+                               }\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+                       throw new DocumentException(e);\r
+               }\r
+               \r
+               return result;\r
+       }\r
+       \r
+       \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.common.RelationUtils#getRelationships(java.lang.Object, java.lang.String, java.lang.String, java.lang.String)\r
+        */\r
+       public List<Relation> getRelationships(Object nuxeoRepoSession,\r
+                       String subjectCsid, \r
+                       String predicate, \r
+                       String objectCsid) throws DocumentException {\r
+               List<Relation> result = null;\r
+               RepositoryInstance repoSession = (RepositoryInstance)nuxeoRepoSession;\r
+               \r
+               try {\r
+                       DocumentModel relationWorkspace = NuxeoUtils.getWorkspaceModel(\r
+                                       repoSession, CS_RELATION_SERVICE_NAME);\r
+                       DocumentModelList children = repoSession.getChildren(relationWorkspace\r
+                                       .getRef());\r
+                       \r
+                       result = new ArrayList<Relation>();\r
+                       Relation relation = null;\r
+                       for (DocumentModel child : children) {\r
+                               if (isQueryMatch(child, subjectCsid, predicate, objectCsid) == true) {\r
+                                       relation = new Relation();\r
+                                       fillRelationFromDocModel(relation, child);\r
+                                       result.add(relation);                   }\r
+                       }\r
+               } catch (Exception e) {\r
+                       e.printStackTrace();\r
+                       throw new DocumentException(e);\r
                }\r
+               \r
+               return result;\r
+       }\r
+               \r
+       /**\r
+        * Checks if is subject of relation.\r
+        * \r
+        * @param csid the csid\r
+        * @param documentModel the document model\r
+        * \r
+        * @return true, if is subject of relation\r
+        * \r
+        * @throws ClientException the client exception\r
+        */\r
+       private boolean isSubjectOfRelation(String csid, DocumentModel documentModel)\r
+                       throws ClientException {\r
+               boolean result = false;\r
+               String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
 \r
+               Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME, xpathRoot\r
+                               + RelationJAXBSchema.DOCUMENT_ID_1);\r
+               if (valueObject != null && csid != null) {\r
+                       String subjectID = (String) valueObject;\r
+                       result = subjectID.equals(csid);\r
+               }\r
+               \r
                return result;\r
        }\r
 \r
+       /**\r
+        * Checks if is object of relation.\r
+        * \r
+        * @param csid the csid\r
+        * @param documentModel the document model\r
+        * \r
+        * @return true, if is object of relation\r
+        * \r
+        * @throws ClientException the client exception\r
+        */\r
+       private boolean isObjectOfRelation(String csid, DocumentModel documentModel)\r
+                       throws ClientException {\r
+               boolean result = false;\r
+               String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
+\r
+               Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
+                               xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);\r
+               if (valueObject != null  && csid != null) {\r
+                       String subjectID = (String) valueObject;\r
+                       result = subjectID.equals(csid);\r
+               }\r
+\r
+               return result;\r
+       }\r
+       \r
+       private boolean isPredicateOfRelation(String predicate,\r
+                       DocumentModel documentModel) throws ClientException {\r
+               boolean result = false;\r
+               String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
+\r
+               Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
+                               xpathRoot + RelationJAXBSchema.RELATIONSHIP_TYPE);\r
+               if (valueObject != null  && predicate != null) {\r
+                       String relationType = (String) valueObject;\r
+                       result = predicate.equalsIgnoreCase(relationType);\r
+               }\r
+\r
+               return result;\r
+       }\r
+\r
+       /**\r
+        * Gets the object ID from of the (Subject-Predicate-Object) relationship.\r
+        * \r
+        * @param csid the ID of the 'Subject' (Subject-Predicate-Object)\r
+        * @param documentModel represents the relation entry.\r
+        * \r
+        * @return the object csid of the relationship\r
+        * \r
+        * @throws ClientException the client exception\r
+        */\r
+       private String getObjectFromSubject(String csid, DocumentModel documentModel)\r
+                       throws ClientException {\r
+               String result = null;\r
+               String xpathRoot = "/" + REL_NUXEO_SCHEMA_ROOT_ELEMENT + "/";\r
+\r
+               Object valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
+                               xpathRoot + RelationJAXBSchema.DOCUMENT_ID_1);\r
+               if (valueObject != null) {\r
+                       String subjectID = (String) valueObject;\r
+                       if (subjectID.equals(csid) == true) {\r
+                               valueObject = documentModel.getProperty(REL_NUXEO_SCHEMA_NAME,\r
+                                               xpathRoot + RelationJAXBSchema.DOCUMENT_ID_2);\r
+                               if (valueObject != null) {\r
+                                       result = (String) valueObject;\r
+                               }\r
+                       }\r
+               }\r
+\r
+               return result;\r
+       }\r
+       \r
+       static public Document getDocument(List<Relation> relationList)\r
+                       throws DocumentException {\r
+               DOMDocumentFactory domfactory = new DOMDocumentFactory();\r
+               DOMDocument result = (DOMDocument) domfactory.createDocument();\r
+\r
+               try {\r
+                       // setup the root element\r
+                       DOMElement root = (DOMElement) result\r
+                                       .createElement(RelationListItemJAXBSchema.REL_ROOT_ELEM_NAME);\r
+                       result.setRootElement((org.dom4j.Element) root);\r
+\r
+                       // populate the document with child elements\r
+                       for (Relation child : relationList) {\r
+                               DOMElement el = (DOMElement) result.createElement(RelationJAXBSchema.REL_ROOT_ELEM_NAME);\r
+                               el.setAttribute(RelationListItemJAXBSchema.CSID, child\r
+                                               .getCsid());\r
+                               el.setAttribute(RelationListItemJAXBSchema.URI, getRelURL(\r
+                                               CS_RELATION_SERVICE_NAME, child.getCsid()));\r
+\r
+                               if (logger.isDebugEnabled() == true) {\r
+                                       System.out.println(el.asXML());\r
+                               }\r
+                               \r
+                               root.appendChild(el);\r
+                       }\r
+               } catch (Exception e) {\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
+               }\r
+\r
+               if (logger.isDebugEnabled() == true) {\r
+                       System.out.println(result.asXML());\r
+               }\r
+\r
+               return result;\r
+       }\r
+       \r
+       private boolean isQueryMatch(DocumentModel documentModel,\r
+                       String subjectCsid,\r
+                       String predicate,\r
+                       String objectCsid) throws ClientException {\r
+               boolean result = true;\r
+               \r
+               block: {\r
+                       if (subjectCsid != null) {\r
+                               if (isSubjectOfRelation(subjectCsid, documentModel) == false) {\r
+                                       result = false;\r
+                                       break block;\r
+                               }\r
+                       }\r
+                       if (predicate != null) {\r
+                               if (isPredicateOfRelation(predicate, documentModel) == false) {\r
+                                       result = false;\r
+                                       break block;\r
+                               }\r
+                       }\r
+                       if (objectCsid != null) {\r
+                               if (isObjectOfRelation(objectCsid, documentModel) == false) {\r
+                                       result = false;\r
+                                       break block;\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               return result;\r
+       }\r
+    private static String getRelURL(String repo, String uuid) {\r
+        return '/' + repo + '/' + uuid;\r
+    }  \r
+       \r
 }\r
index 0aea0663435aeb727d3a3c14d93be71c9773508c..4c65bc96d91073a0769ae0e69cc7eca5e68da247 100644 (file)
@@ -42,6 +42,8 @@
             <xs:enumeration value="association"/>\r
             <!-- document with id as document-id-1 contains one or more document with id as document-id-2 -->\r
             <xs:enumeration value="contains"/>\r
+            <!-- document with id as document-id-1 contains one or more document with id as document-id-2 -->\r
+            <xs:enumeration value="collectionobject-intake"/>\r
         </xs:restriction>\r
     </xs:simpleType>\r
 \r
index 0cb9d5408cf263b4b6954f0284f24e4ef7d8c7e9..31ff7b4b469b29361fee4b17ce50f90e3ff9fabf 100644 (file)
@@ -49,8 +49,13 @@ import javax.xml.bind.Marshaller;
 import org.collectionspace.services.RelationService;
 import org.collectionspace.services.relation.*;
 import org.collectionspace.services.relation.RelationList.*;
+
 import org.collectionspace.services.RelationJAXBSchema;
+import org.collectionspace.services.RelationListItemJAXBSchema;
+
 import org.collectionspace.services.common.ServiceMain;
+import org.collectionspace.services.common.repository.DocumentException;
+import org.collectionspace.services.common.repository.DocumentNotFoundException;
 
 import org.dom4j.Document;
 import org.dom4j.Element;
@@ -67,8 +72,7 @@ import org.slf4j.LoggerFactory;
 public class RelationResource {
 
        /** The logger. */
-       final Logger logger = LoggerFactory
-                       .getLogger(RelationResource.class);
+       final Logger logger = LoggerFactory.getLogger(RelationResource.class);
 
        // This should be a DI wired by a container like Spring, Seam, or EJB3
        /** The Constant service. */
@@ -82,81 +86,264 @@ public class RelationResource {
        }
 
        /**
-        * Gets the relation list.
+        * Returns a list of *all* relation entities.
         * 
-        * @param ui the ui
+        * @param ui
+        *            the ui
         * 
         * @return the relation list
         */
        @GET
        public RelationList getRelationList(@Context UriInfo ui) {
-               
-               URI absoluteURI = ui.getAbsolutePath();
-               String uriString = absoluteURI.toString();
-               
-               RelationList p = new RelationList();
-               try {
-                       Document document = service.getRelationList();
-                       Element root = document.getRootElement();
+               RelationList relationList = this.getRequestedRelationList(ui, null,
+                               null, null);
 
-                       // debug
-                       System.err.println(document.asXML());
+               return relationList;
+       }
 
-                       List<RelationList.RelationListItem> list = p
-                                       .getRelationListItem();
-                       for (Iterator i = root.elementIterator(); i.hasNext();) {
-                               Element element = (Element) i.next();
-                               // debug
-                               System.err.println();
-                               element.asXML();
+       /**
+        * Gets a list of relations with the subject=
+        * 
+        * @param ui
+        *            the ui
+        * @param subjectCsid
+        *            the subject == subjectCsid
+        * 
+        * @return the relation list_ s
+        */
+       @GET
+       @Path("subject/{subjectCsid}")
+       public RelationList getRelationList_S(@Context UriInfo ui,
+                       @PathParam("subjectCsid") String subjectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui,
+                               subjectCsid, null, null);
 
-                               // set the Relation list item entity elements
-                               RelationListItem pli = new RelationListItem();
-                               pli.setUri(element.attributeValue("url"));
-                               pli.setCsid(element.attributeValue("id"));
-                               list.add(pli);
-                       }
+               return relationList;
+       }
 
-               } catch (Exception e) {
-                       e.printStackTrace();
-               }
+       /**
+        * Gets a list of relations with predicate == predicate.
+        * 
+        * @param ui
+        *            the ui
+        * @param predicate
+        *            the predicate
+        * 
+        * @return the relation list of type
+        */
+       @GET
+       @Path("type/{predicate}")
+       public RelationList getRelationList_P(@Context UriInfo ui,
+                       @PathParam("predicate") String predicate) {
+               RelationList relationList = this.getRequestedRelationList(ui, null,
+                               predicate, null);
+
+               return relationList;
+       }
 
-               return p;
+       /**
+        * Gets a list of relations with object == objectCsid
+        * 
+        * @param ui
+        *            the ui
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list_ o
+        */
+       @GET
+       @Path("object/{objectCsid}")
+       public RelationList getRelationList_O(@Context UriInfo ui,
+                       @PathParam("objectCsid") String objectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui, null,
+                               null, objectCsid);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with predicate == predicate *and* subject ==
+        * subjectCsid
+        * 
+        * @param ui
+        *            the ui
+        * @param predicate
+        *            the predicate
+        * @param subjectCsid
+        *            the subject subjectCsid
+        * 
+        * @return the relation list of type with subject
+        */
+       @GET
+       @Path("type/{predicate}/subject/{subjectCsid}")
+       public RelationList getRelationList_PS(@Context UriInfo ui,
+                       @PathParam("predicate") String predicate,
+                       @PathParam("subjectCsid") String subjectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui,
+                               subjectCsid, predicate, null);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with subject == subjectCsid *and* predicate ==
+        * predicate
+        * 
+        * @param ui
+        *            the ui
+        * @param subjectCsid
+        *            the subject csid
+        * @param predicate
+        *            the predicate
+        * 
+        * @return the relation list_ sp
+        */
+       @GET
+       @Path("subject/{subjectCsid}/type/{predicate}")
+       public RelationList getRelationList_SP(@Context UriInfo ui,
+                       @PathParam("subjectCsid") String subjectCsid,
+                       @PathParam("predicate") String predicate) {
+               RelationList relationList = this.getRequestedRelationList(ui,
+                               subjectCsid, predicate, null);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with predicate == predicate *and* object ==
+        * objectCsid
+        * 
+        * @param ui
+        *            the ui
+        * @param predicate
+        *            the predicate
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list of type with object
+        */
+       @GET
+       @Path("type/{predicate}/object/{objectCsid}")
+       public RelationList getRelationList_PO(@Context UriInfo ui,
+                       @PathParam("predicate") String predicate,
+                       @PathParam("objectCsid") String objectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui, null,
+                               predicate, objectCsid);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with object == objectCsid *and* predicate ==
+        * predicate
+        * 
+        * @param ui
+        *            the ui
+        * @param objectCsid
+        *            the object csid
+        * @param predicate
+        *            the predicate
+        * 
+        * @return the relation list_ op
+        */
+       @GET
+       @Path("object/{objectCsid}/type/{predicate}")
+       public RelationList getRelationList_OP(@Context UriInfo ui,
+                       @PathParam("objectCsid") String objectCsid,
+                       @PathParam("predicate") String predicate) {
+               RelationList relationList = this.getRequestedRelationList(ui, null,
+                               predicate, objectCsid);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with predicate == predicate *and* subject ==
+        * subjectCsid *and* object == objectCsid
+        * 
+        * @param ui
+        *            the ui
+        * @param predicate
+        *            the predicate
+        * @param subjectCsid
+        *            the subject csid
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list
+        */
+       @GET
+       @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}")
+       public RelationList getRelationList_PSO(@Context UriInfo ui,
+                       @PathParam("predicate") String predicate,
+                       @PathParam("subjectCsid") String subjectCsid,
+                       @PathParam("objectCsid") String objectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui,
+                               predicate, subjectCsid, objectCsid);
+
+               return relationList;
+       }
+
+       /**
+        * Gets a list of relations with subject == subjectCsid *and* predicate ==
+        * predicate *and* object == objectCsid
+        * 
+        * @param ui
+        *            the ui
+        * @param subjectCsid
+        *            the subject csid
+        * @param predicate
+        *            the predicate
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list_ spo
+        */
+       @GET
+       @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}")
+       public RelationList getRelationList_SPO(@Context UriInfo ui,
+                       @PathParam("subjectCsid") String subjectCsid,
+                       @PathParam("predicate") String predicate,
+                       @PathParam("objectCsid") String objectCsid) {
+               RelationList relationList = this.getRequestedRelationList(ui,
+                               subjectCsid, predicate, objectCsid);
+
+               return relationList;
        }
 
        /**
         * Creates the relation.
         * 
-        * @param ui the ui
-        * @param co the co
+        * @param ui
+        *            the ui
+        * @param co
+        *            the co
         * 
         * @return the response
         */
        @POST
        public Response createRelation(@Context UriInfo ui, Relation co) {
                String csid = null;
-               
+
                try {
                        Document document = service.postRelation(co);
                        Element root = document.getRootElement();
                        csid = root.attributeValue("id");
                        co.setCsid(csid);
                } catch (Exception e) {
-                       Response response = Response.status(Response.Status.NOT_FOUND)
-                                       .entity("Create failed").type("text/plain").build();
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in createRelation", e);
+                       }
+                       Response response = Response.status(
+                                       Response.Status.INTERNAL_SERVER_ERROR).entity(
+                                       "Index failed").type("text/plain").build();
                        throw new WebApplicationException(response);
                }
 
-               //debug
-               verbose("createRelation: ", co);
-               
                UriBuilder uriBuilder = ui.getAbsolutePathBuilder();
                uriBuilder.path(csid);
                URI uri = uriBuilder.build();
-               
-               //debug
-               System.out.println(uri.toString());
-               
+
                Response response = Response.created(uri).build();
                return response;
        }
@@ -164,7 +351,8 @@ public class RelationResource {
        /**
         * Gets the relation.
         * 
-        * @param csid the csid
+        * @param csid
+        *            the csid
         * 
         * @return the relation
         */
@@ -193,8 +381,9 @@ public class RelationResource {
                                        Iterator<Element> relIter = schemaElement
                                                        .elementIterator(RelationJAXBSchema.REL_ROOT_ELEM_NAME);
                                        Iterator<Element> relIter2 = schemaElement
-                                       .elementIterator("rel:" + RelationJAXBSchema.REL_ROOT_ELEM_NAME);
-                                       
+                                                       .elementIterator("rel:"
+                                                                       + RelationJAXBSchema.REL_ROOT_ELEM_NAME);
+
                                        while (relIter.hasNext()) {
                                                Element relElement = relIter.next();
 
@@ -229,10 +418,12 @@ public class RelationResource {
                                }
                        }
                } catch (Exception e) {
-                       e.printStackTrace();
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in getRelation", e);
+                       }
                        Response response = Response.status(
-                                       Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed")
-                                       .type("text/plain").build();
+                                       Response.Status.INTERNAL_SERVER_ERROR).entity(
+                                       "Index failed").type("text/plain").build();
                        throw new WebApplicationException(response);
                }
                if (co == null) {
@@ -243,7 +434,10 @@ public class RelationResource {
                                        .build();
                        throw new WebApplicationException(response);
                }
-               verbose("getRelation: ", co);
+
+               if (logger.isDebugEnabled() == true) {
+                       verbose("getRelation: ", co);
+               }
 
                return co;
        }
@@ -251,17 +445,21 @@ public class RelationResource {
        /**
         * Update relation.
         * 
-        * @param csid the csid
-        * @param theUpdate the the update
+        * @param csid
+        *            the csid
+        * @param theUpdate
+        *            the the update
         * 
         * @return the relation
         */
        @PUT
        @Path("{csid}")
-       public Relation updateRelation(
-                       @PathParam("csid") String csid, Relation theUpdate) {
+       public Relation updateRelation(@PathParam("csid") String csid,
+                       Relation theUpdate) {
 
-               verbose("updateRelation with input: ", theUpdate);
+               if (logger.isDebugEnabled() == true) {
+                       verbose("updateRelation with input: ", theUpdate);
+               }
 
                String status = null;
                try {
@@ -276,9 +474,12 @@ public class RelationResource {
                                }
                        }
                } catch (Exception e) {
-                       // FIXME: NOT_FOUND?
-                       Response response = Response.status(Response.Status.NOT_FOUND)
-                                       .entity("Update failed ").type("text/plain").build();
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in updateRelation", e);
+                       }
+                       Response response = Response.status(
+                                       Response.Status.INTERNAL_SERVER_ERROR).entity(
+                                       "Index failed").type("text/plain").build();
                        throw new WebApplicationException(response);
                }
 
@@ -288,7 +489,8 @@ public class RelationResource {
        /**
         * Delete relation.
         * 
-        * @param csid the csid
+        * @param csid
+        *            the csid
         */
        @DELETE
        @Path("{csid}")
@@ -296,30 +498,133 @@ public class RelationResource {
 
                verbose("deleteRelation with csid=" + csid);
                try {
-                       
-                       Document document = service.deleteRelation(csid);
+                       service.deleteRelation(csid);
+               } catch (DocumentNotFoundException dnfe) {
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("caught exception in deleteRelation", dnfe);
+                       }
+                       Response response = Response.status(Response.Status.NOT_FOUND)
+                                       .entity("Delete failed on Relation csid=" + csid)
+                                       .type("text/plain").build();
+                       throw new WebApplicationException(response);
+               } catch (Exception e) {
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in deleteRelation", e);
+                       }
+                       Response response = Response.status(
+                                       Response.Status.INTERNAL_SERVER_ERROR).entity(
+                                       "Index failed").type("text/plain").build();
+                       throw new WebApplicationException(response);
+               }
+       }
+
+       /*
+        * Private Methods
+        */
+
+       /**
+        * Gets the relation list common.
+        * 
+        * @param ui
+        *            the ui
+        * @param subjectCsid
+        *            the subject csid
+        * @param predicate
+        *            the predicate
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list common
+        * 
+        * @throws WebApplicationException
+        *             the web application exception
+        */
+       private RelationList getRequestedRelationList(@Context UriInfo ui,
+                       String subjectCsid, String predicate, String objectCsid)
+                       throws WebApplicationException {
+
+               URI absoluteURI = ui.getAbsolutePath();
+               String uriString = absoluteURI.toString();
+
+               RelationList relationList = null;
+               try {
+                       relationList = this.getRelationList(subjectCsid, predicate,
+                                       objectCsid);
+               } catch (Exception e) {
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in getRelationList", e);
+                       }
+                       Response response = Response.status(
+                                       Response.Status.INTERNAL_SERVER_ERROR).entity(
+                                       "Index failed").type("text/plain").build();
+                       throw new WebApplicationException(response);
+               }
+
+               return relationList;
+       }
+
+       /**
+        * Gets the relation list.
+        * 
+        * @param subjectCsid
+        *            the subject csid
+        * @param predicate
+        *            the predicate
+        * @param objectCsid
+        *            the object csid
+        * 
+        * @return the relation list
+        * 
+        * @throws DocumentException
+        *             the document exception
+        */
+       private RelationList getRelationList(String subjectCsid, String predicate,
+                       String objectCsid) throws DocumentException {
+               RelationList relationList = new RelationList();
+               try {
+                       Document document = service.getRelationList(subjectCsid, predicate,
+                                       objectCsid);
+                       if (logger.isDebugEnabled() == true) {
+                               System.err.println(document.asXML());
+                       }
+
                        Element root = document.getRootElement();
+                       List<RelationList.RelationListItem> list = relationList
+                                       .getRelationListItem();
+                       Element node = null;
                        for (Iterator i = root.elementIterator(); i.hasNext();) {
-                               Element element = (Element) i.next();
-                               if ("docRef".equals(element.getName())) {
-                                       String status = (String) element.getData();
-                                       verbose("deleteRelationt response: " + status);
+                               node = (Element) i.next();
+                               if (logger.isDebugEnabled() == true) {
+                                       System.out.println();
+                                       node.asXML();
                                }
+
+                               // set the Relation list item entity elements
+                               RelationListItem listItem = new RelationListItem();
+                               listItem.setUri(node
+                                               .attributeValue(RelationListItemJAXBSchema.URI));
+                               listItem.setCsid(node
+                                               .attributeValue(RelationListItemJAXBSchema.CSID));
+                               list.add(listItem);
                        }
+
                } catch (Exception e) {
-                       // FIXME: NOT_FOUND?
-                       Response response = Response.status(Response.Status.NOT_FOUND)
-                                       .entity("Delete failed ").type("text/plain").build();
-                       throw new WebApplicationException(response);
+                       if (logger.isDebugEnabled()) {
+                               logger.debug("Caught exception in getRelationListOfType", e);
+                       }
+                       throw new DocumentException(e);
                }
 
+               return relationList;
        }
-       
+
        /**
         * Verbose.
         * 
-        * @param msg the msg
-        * @param co the co
+        * @param msg
+        *            the msg
+        * @param co
+        *            the co
         */
        private void verbose(String msg, Relation co) {
                try {
@@ -331,13 +636,14 @@ public class RelationResource {
                        m.marshal(co, System.out);
                } catch (Exception e) {
                        e.printStackTrace();
-               }               
+               }
        }
 
        /**
         * Verbose.
         * 
-        * @param msg the msg
+        * @param msg
+        *            the msg
         */
        private void verbose(String msg) {
                System.out.println("RelationResource. " + msg);
index f9f857b56d1981cb1af36f7940e8c285a82a1538..1b7d98913ab73af7829705b724a8930c9e3ff49c 100644 (file)
@@ -28,7 +28,9 @@ package org.collectionspace.services;
 \r
 import java.io.IOException;\r
 import org.dom4j.Document;\r
-import org.dom4j.DocumentException;\r
+//import org.dom4j.DocumentException;\r
+import org.collectionspace.services.common.repository.DocumentException;\r
+\r
 \r
 import org.collectionspace.services.relation.Relation;\r
 \r
@@ -79,6 +81,22 @@ public interface RelationService {
         * @throws IOException Signals that an I/O exception has occurred.\r
         */\r
        Document getRelationList() throws DocumentException, IOException;\r
+       \r
+       /**\r
+        * Gets the relation list of type.\r
+        * \r
+        * @param subjectCsid the subject csid\r
+        * @param predicate the predicate\r
+        * @param objectCsid the object csid\r
+        * \r
+        * @return the relation list of type\r
+        * \r
+        * @throws DocumentException the document exception\r
+        * @throws IOException Signals that an I/O exception has occurred.\r
+        */\r
+       Document getRelationList(String subjectCsid,\r
+                       String predicate, String objectCsid)\r
+               throws DocumentException, IOException;\r
 \r
        // Update\r
        /**\r
index 95821949efe8def3fb1ce1426967cb1314ab8a38..1b729f3ef60c3a3cb3db939194f970d9a222da98 100644 (file)
@@ -32,10 +32,11 @@ import java.io.IOException;
 //import java.util.ArrayList;\r
 //import java.util.Arrays;\r
 //import java.util.HashMap;\r
-//import java.util.List;\r
+import java.util.List;\r
 //import java.util.Map;\r
 //import java.util.Iterator;\r
 \r
+import org.collectionspace.services.common.repository.DocumentException;\r
 //import org.collectionspace.services.nuxeo.NuxeoRESTClient;\r
 import org.collectionspace.services.nuxeo.CollectionSpaceServiceNuxeoImpl;\r
 import org.collectionspace.services.nuxeo.RelationUtilsNuxeoImpl;\r
@@ -44,12 +45,13 @@ import org.collectionspace.services.relation.Relation;
 //import org.collectionspace.services.relation.RelationshipType;\r
 //import org.collectionspace.services.RelationJAXBSchema;\r
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
+import org.collectionspace.services.common.RelationsManager;\r
 //import org.collectionspace.services.common.ServiceMain;\r
 \r
 import org.dom4j.Document;\r
 //import org.dom4j.Element;\r
 //import org.dom4j.dom.DOMDocument;\r
-import org.dom4j.DocumentException;\r
+//import org.dom4j.DocumentException;\r
 //import org.dom4j.io.SAXReader;\r
 //import org.restlet.resource.Representation;\r
 \r
@@ -84,21 +86,23 @@ public class RelationServiceNuxeoImpl extends
         * @throws DocumentException the document exception\r
         * @throws IOException Signals that an I/O exception has occurred.\r
         */\r
-       public Document deleteRelation(String csid)\r
-                       throws DocumentException, IOException {\r
+       public Document deleteRelation(String csid) throws DocumentException {\r
                Document result = null;\r
-               \r
-        RepositoryInstance repoSession = null;\r
-        try {\r
-            repoSession = getRepositorySession();\r
-            result = deleteDocument(repoSession, csid);\r
-        } catch (Exception e) {\r
-               e.printStackTrace();\r
-        } finally {\r
-            if(repoSession != null) {\r
-                releaseRepositorySession(repoSession);\r
-            }\r
-        }\r
+\r
+               RepositoryInstance repoSession = null;\r
+               try {\r
+                       repoSession = getRepositorySession();\r
+                       result = deleteDocument(repoSession, csid);\r
+               } catch (Exception e) {\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
+               } finally {\r
+                       if (repoSession != null) {\r
+                               releaseRepositorySession(repoSession);\r
+                       }\r
+               }\r
 \r
                return result;\r
        }\r
@@ -122,7 +126,10 @@ public class RelationServiceNuxeoImpl extends
                        repoSession = getRepositorySession();\r
                        result = NuxeoUtils.getDocument(repoSession, csid);\r
                } catch (Exception e) {\r
-                       e.printStackTrace();\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
                } finally {\r
                        if (repoSession != null) {\r
                                releaseRepositorySession(repoSession);\r
@@ -145,17 +152,21 @@ public class RelationServiceNuxeoImpl extends
         * @throws DocumentException the document exception\r
         * @throws IOException Signals that an I/O exception has occurred.\r
         */\r
-       public Document getRelationList() throws DocumentException, IOException {\r
+       public Document getRelationList()\r
+                       throws DocumentException, IOException {\r
                Document result = null;\r
                RepositoryInstance repoSession = null;\r
 \r
                try {\r
                        repoSession = getRepositorySession();\r
-                       RelationUtilsNuxeoImpl.getRelationships(repoSession);\r
+                       List<Relation> relationList = RelationsManager.getRelationships(repoSession);\r
                        \r
-                       result = this.browseWorkspace(repoSession, RelationUtilsNuxeoImpl.CS_RELATION_SERVICE_NAME);\r
+                       result = RelationUtilsNuxeoImpl.getDocument(relationList);\r
                } catch (Exception e) {\r
-                       e.printStackTrace();\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
                } finally {\r
                        if (repoSession != null) {\r
                                releaseRepositorySession(repoSession);\r
@@ -168,6 +179,39 @@ public class RelationServiceNuxeoImpl extends
 \r
                return result;\r
        }\r
+               \r
+       /* (non-Javadoc)\r
+        * @see org.collectionspace.services.RelationService#getRelationList(java.lang.String, java.lang.String, java.lang.String)\r
+        */\r
+       public Document getRelationList(String subjectCsid,\r
+                       String predicate, String objectCsid)\r
+                               throws DocumentException, IOException {\r
+               Document result = null;\r
+               RepositoryInstance repoSession = null;\r
+\r
+               try {\r
+                       repoSession = getRepositorySession();\r
+                       List<Relation> relationList = RelationsManager.getRelationships(repoSession,\r
+                                       subjectCsid, predicate, objectCsid);\r
+                       \r
+                       result = RelationUtilsNuxeoImpl.getDocument(relationList);\r
+               } catch (Exception e) {\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
+               } finally {\r
+                       if (repoSession != null) {\r
+                               releaseRepositorySession(repoSession);\r
+                       }\r
+               }\r
+               \r
+               if (logger.isDebugEnabled() == true) {\r
+                       System.out.println(result.asXML());\r
+               }\r
+\r
+               return result;\r
+               }\r
 \r
        // Create a new relation document\r
        /**\r
@@ -187,32 +231,14 @@ public class RelationServiceNuxeoImpl extends
                \r
                try {\r
                        repoSession = getRepositorySession();\r
-                       \r
-                       // get the Nuxeo 'Relations' workspace\r
-                       DocumentModel workspaceModel = NuxeoUtils.getWorkspaceModel(repoSession,\r
-                                       RelationUtilsNuxeoImpl.CS_RELATION_SERVICE_NAME);\r
-                       \r
-            String docType = RelationUtilsNuxeoImpl.REL_NUXEO_DOCTYPE;\r
-            String id = IdUtils.generateId("New " + docType);\r
-            \r
-            //create document model\r
-            String workspacePath = workspaceModel.getPathAsString();            \r
-            DocumentModel newRelDocModel = repoSession.createDocumentModel(workspacePath, id, docType);\r
-\r
-            //debug\r
-            if (logger.isDebugEnabled() == true) {\r
-               RelationUtilsNuxeoImpl.printDocumentModel(newRelDocModel);\r
-            }\r
-            RelationUtilsNuxeoImpl.fillDocModelFromRelation(co, newRelDocModel);\r
-            \r
-            //create document with the new DocumentModel\r
-            DocumentModel resultDocModel = repoSession.createDocument(newRelDocModel);\r
+            DocumentModel resultDocModel = RelationUtilsNuxeoImpl.createRelationship(repoSession, co);\r
             repoSession.save();\r
-\r
             result = NuxeoUtils.getDocument(repoSession, resultDocModel);\r
-\r
                } catch (Exception e) {\r
-                       e.printStackTrace();\r
+                       if (logger.isDebugEnabled() == true) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
                } finally {\r
                        if (repoSession != null) {\r
                                releaseRepositorySession(repoSession);\r
@@ -252,7 +278,10 @@ public class RelationServiceNuxeoImpl extends
             repoSession.save();\r
             result = NuxeoUtils.getDocument(repoSession, documentModel);\r
         } catch(Exception e){\r
-               e.printStackTrace();\r
+                       if (logger.isDebugEnabled()) {\r
+                               logger.debug("Caught exception ", e);\r
+                       }\r
+                       throw new DocumentException(e);\r
         } finally{\r
             if(repoSession != null){\r
                 releaseRepositorySession(repoSession);\r