]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-857: Error when attempting to merge an auth item into an item from a different...
authorRay Lee <ray.lee@lyrasis.org>
Wed, 3 Jun 2020 03:35:32 +0000 (23:35 -0400)
committerRay Lee <ray.lee@lyrasis.org>
Wed, 3 Jun 2020 03:35:32 +0000 (23:35 -0400)
services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/AbstractBatchJob.java
services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/MergeAuthorityItemsBatchJob.java
services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateInventoryStatusBatchJob.java
services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateObjectLocationBatchJob.java

index 31b381f6432f628fee480f537a66fdccecbd647a..ef24d0e6f56f135ecfd5b97ec963e44a8c3a382f 100644 (file)
@@ -63,12 +63,12 @@ public abstract class AbstractBatchJob extends AbstractBatchInvocable {
        private Map<String, String> authorityServiceNamesByDocType;
 
        @SuppressWarnings("unchecked")
-       protected static <T> Set<T> convertListToSet(List<T> list) 
-    { 
-        // create a set from the List 
-        return (Set<T>) list.stream().collect(Collectors.toSet()); 
+       protected static <T> Set<T> convertListToSet(List<T> list)
+    {
+        // create a set from the List
+        return (Set<T>) list.stream().collect(Collectors.toSet());
     }
-       
+
        @Override
        public void run(BatchCommon batchCommon) {
                String errMsg = String.format("%s class does not support run(BatchCommon batchCommon) method.", getClass().getName());
@@ -395,10 +395,10 @@ public abstract class AbstractBatchJob extends AbstractBatchInvocable {
        protected PoxPayloadOut findTaxonByCsid(String csid) throws URISyntaxException, DocumentException {
                return findAuthorityItemByCsid(TaxonomyAuthorityClient.SERVICE_NAME, csid);
        }
-       
+
        protected PoxPayloadOut findAuthorityItemByShortId(String serviceName, String vocabularyShortId, String itemShortId) throws URISyntaxException, DocumentException, Exception {
                AuthorityResource<?, ?> resource = (AuthorityResource<?, ?>) getResourceMap().get(serviceName);
-               PoxPayloadOut payload = resource.getAuthorityItemWithExistingContext(getServiceContext(), createDeleteFilterUriInfo(), getResourceMap(), 
+               PoxPayloadOut payload = resource.getAuthorityItemWithExistingContext(getServiceContext(), createDeleteFilterUriInfo(), getResourceMap(),
                                "urn:cspace:name(" + vocabularyShortId + ")", "urn:cspace:name(" + itemShortId + ")");
 
                return payload;
@@ -453,7 +453,7 @@ public abstract class AbstractBatchJob extends AbstractBatchInvocable {
         *                    Only records that reference the given item in the specified field are returned.
         *                    If null, returns records that reference the item in any field.
         * @return            A List containing the csids of referencing records.
-        * @throws URISyntaxException 
+        * @throws URISyntaxException
         */
        protected List<String> findReferencingObjects(String serviceName, String parentCsid, String csid, String type, String sourceField) throws URISyntaxException, Exception {
                logger.debug("findReferencingObjects serviceName=" + serviceName + " parentCsid=" + parentCsid + " csid=" + csid + " type=" + type + " sourceField=" + sourceField);
index 80b9b304081a8d3dcc6ab56fa58fd18091dc5307..b62751e4e3a4c5ebabfb3c606fb8b224cf40bf03 100644 (file)
@@ -15,12 +15,15 @@ import java.util.Set;
 
 import org.apache.commons.lang.StringUtils;
 
+import org.collectionspace.services.batch.BatchCommon;
 import org.collectionspace.services.client.PayloadOutputPart;
 import org.collectionspace.services.client.PoxPayloadOut;
 import org.collectionspace.services.client.RelationClient;
 import org.collectionspace.services.client.workflow.WorkflowClient;
 import org.collectionspace.services.common.NuxeoBasedResource;
+import org.collectionspace.services.common.api.RefName;
 import org.collectionspace.services.common.api.RefNameUtils;
+import org.collectionspace.services.common.api.RefNameUtils.AuthorityInfo;
 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
 import org.collectionspace.services.common.invocable.InvocationContext.Params.Param;
@@ -71,6 +74,11 @@ public class MergeAuthorityItemsBatchJob extends AbstractBatchJob {
 
        @Override
        public void run() {
+               run(null);
+       }
+
+       @Override
+       public void run(BatchCommon batchCommon) {
                setCompletionStatus(STATUS_MIN_PROGRESS);
 
                try {
@@ -137,10 +145,21 @@ public class MergeAuthorityItemsBatchJob extends AbstractBatchJob {
                logger.debug("Merging docType=" + docType + " target=" + target + " sourceCsids=" + StringUtils.join(sourceCsids, ","));
 
                String serviceName = getAuthorityServiceNameForDocType(docType);
+               PoxPayloadOut targetItemPayload;
+
+               if (RefNameUtils.isTermRefname(target)) {
+                       AuthorityTermInfo termInfo = RefNameUtils.parseAuthorityTermInfo(target);
+                       AuthorityInfo authorityInfo = termInfo.inAuthority;
+                       String targetServiceName = authorityInfo.resource;
+
+                       if (!targetServiceName.equals(serviceName)) {
+                               throw new DocumentException("Source item and target item must be the same record type.");
+                       }
 
-               PoxPayloadOut targetItemPayload = RefNameUtils.isTermRefname(target)
-                       ? findAuthorityItemByRefName(serviceName, target)
-                       : findAuthorityItemByCsid(serviceName, target);
+                       targetItemPayload       = findAuthorityItemByRefName(serviceName, target);
+               } else {
+                       targetItemPayload = findAuthorityItemByCsid(serviceName, target);
+               }
 
                String targetItemCsid = getCsid(targetItemPayload);
 
@@ -150,10 +169,19 @@ public class MergeAuthorityItemsBatchJob extends AbstractBatchJob {
                        }
                }
 
+               String targetDocName = getFieldValue(targetItemPayload, "/document/@name");
+
                List<PoxPayloadOut> sourceItemPayloads = new ArrayList<PoxPayloadOut>();
 
                for (String sourceCsid : sourceCsids) {
-                       sourceItemPayloads.add(findAuthorityItemByCsid(serviceName, sourceCsid));
+                       PoxPayloadOut sourceItemPayload = findAuthorityItemByCsid(serviceName, sourceCsid);
+                       String sourceDocName = getFieldValue(sourceItemPayload, "/document/@name");
+
+                       if (!sourceDocName.equals(targetDocName)) {
+                               throw new DocumentException("Source item and target item must be the same record type.");
+                       }
+
+                       sourceItemPayloads.add(sourceItemPayload);
                }
 
                return merge(docType, targetItemPayload, sourceItemPayloads);
index bbb0325a369c580add200eafb5c9841d594c9ff9..2f25bb05fde3d0aed44e2a59c0c23976e670732c 100644 (file)
@@ -5,6 +5,8 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
+
+import org.collectionspace.services.batch.BatchCommon;
 import org.collectionspace.services.client.CollectionObjectClient;
 import org.collectionspace.services.client.CollectionObjectFactory;
 import org.collectionspace.services.client.PoxPayloadOut;
@@ -27,6 +29,11 @@ public class UpdateInventoryStatusBatchJob extends AbstractBatchJob {
 
        @Override
        public void run() {
+               run(null);
+       }
+
+       @Override
+       public void run(BatchCommon batchCommon) {
                setCompletionStatus(STATUS_MIN_PROGRESS);
 
                try {
index e5b97d128be6559606f11e6fd57613879d56e840..78215df40634f25d14226a01e4718bad988cc1f0 100644 (file)
@@ -75,17 +75,16 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
                 INVOCATION_MODE_GROUP, INVOCATION_MODE_NO_CONTEXT));
     }
 
-       @Override
-       public void run(BatchCommon batchCommon) {
-               String errMsg = String.format("%s class does not support run(BatchCommon batchCommon) method.", getClass().getName());
-               throw new java.lang.UnsupportedOperationException(errMsg);
-       }
+    @Override
+    public void run() {
+        run(null);
+    }
 
     /**
      * The main work logic of the batch job. Will be called after setContext.
      */
     @Override
-    public void run() {
+    public void run(BatchCommon batchCommon) {
 
         setCompletionStatus(STATUS_MIN_PROGRESS);
 
@@ -153,7 +152,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
 
             // For each CollectionObject record
             for (String collectionObjectCsid : csids) {
-               
+
                // Log progress at INFO level
                if (processed % logInterval == 0) {
                        logger.info(String.format("Recalculated computed location for %d of %d cataloging records.",
@@ -213,20 +212,20 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         getResults().setNumAffected(numUpdated);
         return getResults();
     }
-    
+
     //
     // Returns the number of distinct/unique CSID values in the list
     //
     private int getNumberOfDistinceRecords(AbstractCommonList abstractCommonList) {
        Set<String> resultSet = new HashSet<String>();
-       
+
         for (AbstractCommonList.ListItem listItem : abstractCommonList.getListItem()) {
                String csid = AbstractCommonListUtils.ListItemGetElementValue(listItem, CSID_ELEMENT_NAME);
                if (!Tools.isBlank(csid)) {
                    resultSet.add(csid);
                }
         }
-       
+
         return resultSet.size();
     }
 
@@ -239,7 +238,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         String updateDate;
         String mostRecentLocationDate = "";
         String comparisonUpdateDate = "";
-        
+
         //
         // If there is only one related movement record, then return it as the most recent
         // movement record -if it's current location element is not empty.
@@ -252,7 +251,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
             }
             return mostRecentMovement;
         }
-        
+
         for (AbstractCommonList.ListItem movementListItem : relatedMovements.getListItem()) {
             movementCsid = AbstractCommonListUtils.ListItemGetElementValue(movementListItem, CSID_ELEMENT_NAME);
             if (Tools.isBlank(movementCsid)) {
@@ -289,13 +288,13 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
                     currentLocation));
                  continue;
             }
-           
+
             if (logger.isTraceEnabled()) {
                 logger.trace("Location date value = " + locationDate);
                 logger.trace("Update date value = " + updateDate);
                 logger.trace("Current location value = " + currentLocation);
             }
-            
+
             // If this record's location date value is more recent than that of other
             // Movement records processed so far, set the current Movement record
             // as the most recent Movement.
@@ -321,7 +320,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
             }
 
         }
-        
+
         return mostRecentMovement;
     }
 
@@ -359,7 +358,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         if (!shouldUpdateLocation(previousComputedCurrentLocation, computedCurrentLocation)) {
             return numUpdated;
         }
-    
+
         // Perform the update only if there is a non-blank object number available.
         //
         // In the default CollectionObject validation handler, the object number
@@ -401,7 +400,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         if (logger.isTraceEnabled()) {
             logger.trace("Update payload: " + "\n" + collectionObjectUpdatePayload);
         }
-        
+
         //
         // Update the record and save the response for debugging message
         //
@@ -413,7 +412,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         if (logger.isDebugEnabled()) {
                logger.debug(String.format("Batch resource: Resonse from collectionobject (cataloging record) update: %s", new String(responseBytes)));
         }
-        
+
         if (logger.isTraceEnabled()) {
             logger.trace("Computed current location value for CollectionObject " + collectionObjectCsid
                     + " was set to " + computedCurrentLocation);
@@ -421,7 +420,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
 
         return numUpdated;
     }
-    
+
     protected boolean shouldUpdateLocation(String previousLocation, String currentLocation) {
         boolean shouldUpdate = true;
         if (Tools.isBlank(previousLocation) && Tools.isBlank(currentLocation)) {
@@ -443,7 +442,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
 
     protected PoxPayloadOut findByCsid(NuxeoBasedResource resource, String csid) throws URISyntaxException, DocumentException {
        PoxPayloadOut result = null;
-       
+
        try {
                        result = resource.getWithParentCtx(getServiceContext(), csid);
                } catch (Exception e) {
@@ -455,7 +454,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
                                logger.error(msg);
                        }
                }
-       
+
        return result;
     }
 
@@ -476,10 +475,10 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         URI uri = new URI(null, null, null, queryString, null);
         return createUriInfo(uri.getRawQuery());
     }
-    
+
     protected UriInfo setupQueryParamForUpdateRecords() throws URISyntaxException {
        UriInfo result = null;
-       
+
        //
        // Check first to see if we've got a query param.  It will override any invocation context value
        //
@@ -488,16 +487,16 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
                //
                // Since there is no query param, let's check the invocation context
                //
-               updateCoreValues = getInvocationContext().getUpdateCoreValues();                
+               updateCoreValues = getInvocationContext().getUpdateCoreValues();
        }
-       
+
        //
        // If we found a value, then use it to create a query parameter
        //
        if (Tools.notBlank(updateCoreValues)) {
                result = createUriInfo(IClientQueryParams.UPDATE_CORE_VALUES + "=" + updateCoreValues);
        }
-       
+
        return result;
     }
 
@@ -531,7 +530,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
     private boolean isRecordDeleted(NuxeoBasedResource resource, String collectionObjectCsid)
             throws URISyntaxException, DocumentException {
         boolean isDeleted = false;
-        
+
         byte[] workflowResponse = resource.getWorkflowWithExistingContext(getServiceContext(), createUriInfo(), collectionObjectCsid);
         if (workflowResponse != null) {
             PoxPayloadOut payloadOut = new PoxPayloadOut(workflowResponse);
@@ -542,7 +541,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
                 isDeleted = true;
             }
         }
-        
+
         return isDeleted;
     }
 
@@ -553,7 +552,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         uriInfo.getQueryParameters().add(WorkflowClient.WORKFLOW_QUERY_DELETED_QP, Boolean.FALSE.toString());
         return uriInfo;
     }
-    
+
     private UriInfo addFilterForPageSize(UriInfo uriInfo, long startPage, long pageSize) throws URISyntaxException {
        if (uriInfo == null) {
             uriInfo = createUriInfo();
@@ -641,13 +640,13 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         }
         return csids;
     }
-    
+
     private void appendItemsToCsidsList(List<String> existingList, AbstractCommonList abstractCommonList) {
         for (AbstractCommonList.ListItem listitem : abstractCommonList.getListItem()) {
                existingList.add(AbstractCommonListUtils.ListItemGetCSID(listitem));
         }
     }
-    
+
     private List<String> getMemberCsidsFromGroup(String serviceName, String groupCsid) throws URISyntaxException, DocumentException {
         ResourceMap resourcemap = getResourceMap();
         NuxeoBasedResource resource = (NuxeoBasedResource) resourcemap.get(serviceName);
@@ -673,19 +672,19 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable {
         long currentPage = 0;
         long pageSize = DEFAULT_PAGE_SIZE;
         List<String> noContextCsids = new ArrayList<String>();
-        
+
         while (morePages == true) {
                uriInfo = addFilterForPageSize(uriInfo, currentPage, pageSize);
                AbstractCommonList collectionObjects = collectionObjectResource.getList(getServiceContext(), uriInfo);
                appendItemsToCsidsList(noContextCsids, collectionObjects);
-               
+
                if (collectionObjects.getItemsInPage() == pageSize) { // We know we're at the last page when the number of items returned in the last request is less than the page size.
                        currentPage++;
                } else {
-                       morePages = false;                      
+                       morePages = false;
                }
         }
-        
+
         return noContextCsids;
     }
 }