]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-6937-A: Added support for soft-deleting authority terms that are referenced...
authorRichard Millet <remillet@yahoo.com>
Fri, 22 Apr 2016 07:18:46 +0000 (00:18 -0700)
committerRichard Millet <remillet@yahoo.com>
Fri, 22 Apr 2016 07:18:46 +0000 (00:18 -0700)
services/account/service/src/main/java/org/collectionspace/services/account/storage/AccountRoleDocumentHandler.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java
services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/storage/PermissionRoleDocumentHandler.java
services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java
services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java
services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryClientImpl.java
services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java

index a2dbcbb52511a134282224996ef2fe42396d55b3..d6ee4c05ebec4c8e86a83b7d789c5c053585e8ad 100644 (file)
@@ -123,8 +123,9 @@ public class AccountRoleDocumentHandler
      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
      */
     @Override
-    public void handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
+    public boolean handleDelete(DocumentWrapper<List<AccountRoleRel>> wrapDoc) throws Exception {
         fillCommonPart(getCommonPart(), wrapDoc, true);
+        return true;
     }
 
     /* (non-Javadoc)
index 96a5759b0bbaee219b5a4d9b4433eff10b53fa38..51a12e20abb9b797b06ee2b002164337e38a9e5d 100644 (file)
@@ -696,13 +696,15 @@ public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
     @PUT
     @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH + "/{transition}")
     public byte[] updateItemWorkflowWithTransition(
+            @Context UriInfo uriInfo,
             @PathParam("csid") String parentIdentifier,
             @PathParam("itemcsid") String itemIdentifier,
             @PathParam("transition") String transition) {
         PoxPayloadOut result = null;
         
-        try {            
-            result = updateItemWorkflowWithTransition(NULL_CONTEXT, 
+        try {
+            ServiceContext ctx = createServiceContext(getItemServiceName(), uriInfo);
+            result = updateItemWorkflowWithTransition(ctx, 
                        parentIdentifier, itemIdentifier, transition, AuthorityServiceUtils.UPDATE_REV);
         } catch (Exception e) {
             throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, parentIdentifier);
@@ -1262,6 +1264,7 @@ public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
     @DELETE
     @Path("{csid}/items/{itemcsid}")
     public Response deleteAuthorityItem(
+            @Context UriInfo uriInfo,
             @PathParam("csid") String parentIdentifier,
             @PathParam("itemcsid") String itemIdentifier) {
        Response result = null;
@@ -1273,7 +1276,8 @@ public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
         }
         
         try {
-            deleteAuthorityItem(null, parentIdentifier, itemIdentifier);
+            ServiceContext ctx = createServiceContext(getItemServiceName(), uriInfo);
+            deleteAuthorityItem(ctx, parentIdentifier, itemIdentifier);
             result = Response.status(HttpResponseCodes.SC_OK).build();
         } catch (Exception e) {
             throw bigReThrow(e, ServiceMessages.DELETE_FAILED + "  itemcsid: " + itemIdentifier + " parentcsid:" + parentIdentifier);
@@ -1294,7 +1298,7 @@ public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
             String itemIdentifier) throws Exception {
        Response result = null;
        
-        ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName());
+        ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), existingCtx.getUriInfo());
         String parentcsid = lookupParentCSID(ctx, parentIdentifier, "deleteAuthorityItem(parent)", "DELETE_ITEM", null);
         String itemCsid = lookupItemCSID(ctx, itemIdentifier, parentcsid, "deleteAuthorityItem(item)", "DELETE_ITEM"); //use itemServiceCtx if it is not null
 
index 9d55b196a690b0b787aefb00cff7ebf8e24d16b1..c2a89e7402f02b3463ae251871bb97ac431b2e97 100644 (file)
@@ -234,6 +234,8 @@ public abstract class AuthorityDocumentModelHandler<AuthCommon>
        long result = 0;
        
        ArrayList<String> failureList = new ArrayList<String>();
+        ctx.setProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY, false);
+
        for (String refName:refNameList) {
                AuthorityTermInfo itemInfo = RefNameUtils.parseAuthorityTermInfo(refName);
                AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
index a8879546e64587f78de2ad6f9d3860a6dfa09a15..bef1d45171a3426bc2e52800ada2f7983ffddeb1 100644 (file)
@@ -384,10 +384,18 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
        //
        DocumentModel docModel = wrapDoc.getWrappedObject();
        if (transitionDef.getName().equalsIgnoreCase(WorkflowClient.WORKFLOWTRANSITION_DELETE)) {
-                       if (hasReferencingObjects(this.getServiceContext(), docModel, false) == true) {
-                       throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it.  See the service layer log file for details.",
-                                       docModel.getName()));
-                       }
+               long refsToAllObjects = hasReferencingObjects(ctx, docModel, false);
+               long refsToSoftDeletedObjects = hasReferencingObjects(ctx, docModel, true);
+               if (refsToAllObjects > 0) {
+               if (refsToAllObjects > refsToSoftDeletedObjects) {
+                       //
+                       // If the number of refs to active objects is greater than the number of refs to
+                       // soft deleted objects then we can't delete the item.
+                       //
+                       throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it.  See the service layer log file for details.",
+                                       docModel.getName()));
+               }
+               }
        }
     }
         
@@ -561,17 +569,37 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
      * (non-Javadoc)
      */
     @Override
-    public void handleDelete(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+    public boolean handleDelete(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+       boolean result = true;
+       
        ServiceContext ctx = getServiceContext();
        DocumentModel docModel = wrapDoc.getWrappedObject();
        
-       long refsToObjects = hasReferencingObjects(ctx, docModel, false);
+       long refsToAllObjects = hasReferencingObjects(ctx, docModel, false);
        long refsToSoftDeletedObjects = hasReferencingObjects(ctx, docModel, true);
-       
-       if (refsToObjects > refsToSoftDeletedObjects) {
-               throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it.  See the service layer log file for details.",
-                               docModel.getName()));
+       if (refsToAllObjects > 0) {
+               if (refsToAllObjects > refsToSoftDeletedObjects) {
+                       //
+                       // If the number of refs to active objects is greater than the number of refs to
+                       // soft deleted objects then we can't delete the item.
+                       //
+                       throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it.  See the service layer log file for details.",
+                                       docModel.getName()));
+               } else {
+                       //
+                       // If all the refs are to soft-deleted objects, we should soft-delete this authority item instead of hard-deleting it and instead of failing.
+                       //
+                       Boolean shouldUpdateRev = (Boolean) ctx.getProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY);
+                       String parentCsid = (String) NuxeoUtils.getProperyValue(docModel, AuthorityItemJAXBSchema.IN_AUTHORITY);
+                       String itemCsid = docModel.getName();
+                       AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
+                       authorityResource.updateItemWorkflowWithTransition(ctx, parentCsid, itemCsid, WorkflowClient.WORKFLOWTRANSITION_DELETE, 
+                                       shouldUpdateRev != null ? shouldUpdateRev : true);
+                       result = false; // Don't delete since we just soft-deleted it.
+               }
        }
+       
+       return result;
     }
     
     /**
@@ -589,7 +617,13 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
        AuthorityResource authorityResource = (AuthorityResource)ctx.getResource(getAuthorityServicePath());
        String itemCsid = docModel.getName();
         UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry();
-        ctx.getUriInfo().getQueryParameters().add(WorkflowClient.WORKFLOW_QUERY_ONLY_DELETED, Boolean.toString(onlyRefsToDeletedObjects));  // Add the wf_deleted query param to the resource call
+        if (ctx.getUriInfo() == null) {
+               //
+               // We need a UriInfo object so we can pass "query" params to the AuthorityResource's getReferencingObjects() method
+               //
+               ctx.setUriInfo(this.getServiceContext().getUriInfo()); // try to get a UriInfo instance from the handler's context
+        }
+        ctx.getUriInfo().getQueryParameters().addFirst(WorkflowClient.WORKFLOW_QUERY_ONLY_DELETED, Boolean.toString(onlyRefsToDeletedObjects));  // Add the wf_deleted query param to the resource call
        AuthorityRefDocList refObjs = authorityResource.getReferencingObjects(ctx, inAuthorityCsid, itemCsid, 
                        uriTemplateRegistry, ctx.getUriInfo());
        
index 40ebde150afebe11b1ac953d5dfbe16a87da1fe7..5ed610a13befc064d327e82800a14a7b732e5e9e 100644 (file)
@@ -126,8 +126,9 @@ public class PermissionRoleDocumentHandler
      * @see org.collectionspace.services.common.document.AbstractDocumentHandlerImpl#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
      */
     @Override
-    public void handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
+    public boolean handleDelete(DocumentWrapper<List<PermissionRoleRel>> wrapDoc) throws Exception {
         fillCommonPart(getCommonPart(), wrapDoc, true);
+        return true;
     }
 
     /* (non-Javadoc)
index 61ff77bb81d90c57e7ce04578df0652c5793b5d7..028dd4cea81141cce845d0027e4425122cc91535 100644 (file)
@@ -248,7 +248,7 @@ public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
                 break;
 
             case DELETE:
-                handleDelete((DocumentWrapper<WT>) wrapDoc);
+                result = handleDelete((DocumentWrapper<WT>) wrapDoc);
                 break;
                 
             case SYNC:
@@ -297,8 +297,8 @@ public abstract class AbstractDocumentHandlerImpl<T, TL, WT, WTL>
      * @see org.collectionspace.services.common.document.DocumentHandler#handleDelete(org.collectionspace.services.common.document.DocumentWrapper)
      */
     @Override
-    public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
-        // Do nothing. Subclasses can override if they want/need to.
+    public boolean handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception {
+        return true;
     }
     
     /* (non-Javadoc)
index 90ed731e09183e62c8547f90dc5b74a5670dc384..5e1db504603edf10bbef97a9087610a3ffe26fd5 100644 (file)
@@ -160,7 +160,7 @@ public interface DocumentHandler<T, TL, WT, WTL> {
      * @param wrapDoc
      * @throws Exception
      */
-    public void handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception;
+    public boolean handleDelete(DocumentWrapper<WT> wrapDoc) throws Exception;
 
     /**
      * complete is called by the client to provide an opportunity to the handler
index 8e89459dcc90ce9320e636f471a381cb9db5ba5a..931446ec91521571a1e0c6396ad6c839d55f3dd1 100644 (file)
@@ -1618,8 +1618,9 @@ public class RepositoryClientImpl implements RepositoryClient<PoxPayloadIn, PoxP
                 DocumentRef docRef = NuxeoUtils.createPathRef(ctx, id);
                 wrapDoc = new DocumentWrapperImpl<DocumentModel>(repoSession.getDocument(docRef));
                 ((DocumentModelHandler) handler).setRepositorySession(repoSession);
-                handler.handle(Action.DELETE, wrapDoc);
-                repoSession.removeDocument(docRef);
+                if (handler.handle(Action.DELETE, wrapDoc)) {
+                       repoSession.removeDocument(docRef);
+                }
             } catch (ClientException ce) {
                 String msg = logException(ce, "Could not find document to delete with CSID=" + id);
                 throw new DocumentNotFoundException(msg, ce);
index 8d30d6c873c6d81927b4db7077e41756bb8f4c2d..a15c7fb477799b18caf25b812c3ecedfec9aeb34 100644 (file)
@@ -149,7 +149,9 @@ public class RelationDocumentModelHandler
     }
     
     @Override
-    public void handleDelete(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+    public boolean handleDelete(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
+       boolean result = true;
+       
        String workflowState = WorkflowClient.WORKFLOWSTATE_LOCKED;
        // Neither the subject nor the object can be locked
        if (subjectOrObjectInWorkflowState(wrapDoc, workflowState) == false) {
@@ -158,6 +160,8 @@ public class RelationDocumentModelHandler
                throw new ServiceException(HttpURLConnection.HTTP_FORBIDDEN,
                     "Cannot delete a relationship if either end is in the workflow state: " + workflowState);
        }
+       
+       return result;
     }
     
     private void populateSubjectAndObjectValues(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {