From d97ebc776f4254758341f2774855f9f748c54661 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Fri, 16 Nov 2018 21:36:42 -0800 Subject: [PATCH] DRYD-473: Added constraint on relations_common table. Now need to update the tests to deal with this. --- .../7.10-HF17/config/vcsconfig.sql.txt | 4 +-- .../common/relation/RelationJAXBSchema.java | 33 +++++++++---------- .../nuxeo/RelationDocumentModelHandler.java | 23 ++++++++++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/3rdparty/nuxeo/nuxeo-server/7.10-HF17/config/vcsconfig.sql.txt b/3rdparty/nuxeo/nuxeo-server/7.10-HF17/config/vcsconfig.sql.txt index e9bb4bd72..ba2415640 100644 --- a/3rdparty/nuxeo/nuxeo-server/7.10-HF17/config/vcsconfig.sql.txt +++ b/3rdparty/nuxeo/nuxeo-server/7.10-HF17/config/vcsconfig.sql.txt @@ -34,7 +34,7 @@ ALTER TABLE batch_common add CONSTRAINT batchname_unique UNIQUE (name); #LOG.INFO Adding constraint to the relations table to prevent duplicate relationships #TEST: -#SELECT constraint_name FROM information_schema.constraint_column_usage WHERE table_name = 'relations_common' AND constraint_name = 'relations_unique'; +SELECT constraint_name FROM information_schema.constraint_column_usage WHERE table_name = 'relations_common' AND constraint_name = 'relations_unique'; #IF: emptyResult -#ALTER TABLE relations_common add CONSTRAINT relations_unique UNIQUE (subjectcsid, subjectrefname, relationshiptype, objectcsid, objectrefname); +ALTER TABLE relations_common add CONSTRAINT relations_unique UNIQUE (subjectcsid, subjectrefname, relationshiptype, objectcsid, objectrefname, active); diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java index b125cc069..4a8ceda3d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationJAXBSchema.java @@ -32,33 +32,32 @@ import org.collectionspace.services.client.IRelationsManager; * The Interface RelationJAXBSchema. */ 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 - + // Need to fix conflict between the Nuxeo XSD and the JAX-B XSD for the + // "relation" entity + /** The Constant CSID. */ final static String CSID = "csid"; - + /** The Constant RELATIONSHIP_TYPE. */ final static String RELATIONSHIP_TYPE = "relationshipType"; - + /** The Constant RELATIONSHIP_TYPE_DISPLAYNAME. */ final static String RELATIONSHIP_TYPE_DISPLAYNAME = "predicateDisplayName"; - - final static String RELATIONSHIP_META_TYPE = "relationshipMetaType"; + final static String RELATIONSHIP_ACTIVE = "active"; + final static String RELATIONSHIP_META_TYPE = "relationshipMetaType"; - final static String SUBJECT_URI = "subjectUri"; - final static String SUBJECT_CSID = IRelationsManager.SUBJECT; - final static String SUBJECT_REFNAME = IRelationsManager.SUBJECT_REFNAME; - final static String SUBJECT_DOCTYPE = "subjectDocumentType"; + final static String SUBJECT_URI = "subjectUri"; + final static String SUBJECT_CSID = IRelationsManager.SUBJECT; + final static String SUBJECT_REFNAME = IRelationsManager.SUBJECT_REFNAME; + final static String SUBJECT_DOCTYPE = "subjectDocumentType"; - final static String OBJECT_URI = "objectUri"; - final static String OBJECT_CSID = IRelationsManager.OBJECT; - final static String OBJECT_REFNAME = IRelationsManager.OBJECT_REFNAME; - final static String OBJECT_DOCTYPE = "objectDocumentType"; + final static String OBJECT_URI = "objectUri"; + final static String OBJECT_CSID = IRelationsManager.OBJECT; + final static String OBJECT_REFNAME = IRelationsManager.OBJECT_REFNAME; + final static String OBJECT_DOCTYPE = "objectDocumentType"; } - - diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java index 6cd9d0120..d5b757470 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java @@ -29,6 +29,7 @@ import java.net.HttpURLConnection; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.client.RelationClient; import org.collectionspace.services.common.NuxeoBasedResource; import org.collectionspace.services.common.ServiceException; import org.collectionspace.services.common.ServiceMain; @@ -122,12 +123,26 @@ public class RelationDocumentModelHandler * * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#handleWorkflowTransition(org.collectionspace.services.common.document.DocumentWrapper, org.collectionspace.services.lifecycle.TransitionDef) */ - public void handleWorkflowTransition(ServiceContext ctx, DocumentWrapper wrapDoc, TransitionDef transitionDef) - throws Exception { + public void handleWorkflowTransition(ServiceContext ctx, DocumentWrapper wrapDoc, + TransitionDef transitionDef) throws Exception { if (subjectOrObjectInWorkflowState(wrapDoc, WorkflowClient.WORKFLOWSTATE_LOCKED) == true) { - throw new ServiceException(HttpURLConnection.HTTP_FORBIDDEN, - "Cannot change a relationship if either end of it is in the workflow state: " + WorkflowClient.WORKFLOWSTATE_LOCKED); + throw new ServiceException(HttpURLConnection.HTTP_FORBIDDEN, + "Cannot change a relationship if either end of it is in the workflow state: " + + WorkflowClient.WORKFLOWSTATE_LOCKED); + } else { + // + // Toggle the 'active' flag of the relationship record -needed to correctly apply a uniqueness constrain on rows in the relations_common table + // + String transitionName = transitionDef.getName(); + if (transitionName.equalsIgnoreCase(WorkflowClient.WORKFLOWTRANSITION_UNDELETE)) { + DocumentModel doc = wrapDoc.getWrappedObject(); + doc.setProperty(RelationClient.SERVICE_COMMONPART_NAME, RelationJAXBSchema.RELATIONSHIP_ACTIVE, Boolean.TRUE); + } else if (transitionName.equalsIgnoreCase(WorkflowClient.WORKFLOWTRANSITION_DELETE)) { + DocumentModel doc = wrapDoc.getWrappedObject(); + doc.setProperty(RelationClient.SERVICE_COMMONPART_NAME, RelationJAXBSchema.RELATIONSHIP_ACTIVE, Boolean.FALSE); + } } + } @Override -- 2.47.3