From 281b90c53be15bf0cd7c109e24c0e3d9d946fc82 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 4 Jan 2013 17:34:42 -0800 Subject: [PATCH] CSPACE-5728: Batch job to update computed current location now uses abstract class; invokes several of Ray's convenience methods. --- services/JaxRsServiceProvider/build.xml | 2 +- services/batch/service/pom.xml | 5 ++ .../nuxeo/UpdateObjectLocationBatchJob.java | 79 +++++++++++++++++-- 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/services/JaxRsServiceProvider/build.xml b/services/JaxRsServiceProvider/build.xml index 6c712920d..4dfd46c9a 100644 --- a/services/JaxRsServiceProvider/build.xml +++ b/services/JaxRsServiceProvider/build.xml @@ -57,7 +57,7 @@ - + diff --git a/services/batch/service/pom.xml b/services/batch/service/pom.xml index f7a6db936..0942dac4b 100644 --- a/services/batch/service/pom.xml +++ b/services/batch/service/pom.xml @@ -29,6 +29,11 @@ org.collectionspace.services.batch.client ${project.version} + + org.collectionspace.services + org.collectionspace.services.collectionobject.client + ${project.version} + org.collectionspace.services org.collectionspace.services.loanout.client diff --git a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateObjectLocationBatchJob.java b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateObjectLocationBatchJob.java index 64db074a9..49a2d1665 100644 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateObjectLocationBatchJob.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/UpdateObjectLocationBatchJob.java @@ -1,8 +1,20 @@ package org.collectionspace.services.batch.nuxeo; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriInfo; import org.collectionspace.services.batch.AbstractBatchInvocable; +import org.collectionspace.services.client.CollectionObjectClient; +import org.collectionspace.services.client.PoxPayloadOut; +import org.collectionspace.services.common.ResourceBase; import org.collectionspace.services.common.api.Tools; +import org.dom4j.DocumentException; +import org.jboss.resteasy.specimpl.UriInfoImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,7 +25,7 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable { // Initialization tasks public UpdateObjectLocationBatchJob() { - setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE)); + setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST)); } /** @@ -23,28 +35,81 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable { public void run() { setCompletionStatus(STATUS_MIN_PROGRESS); + try { // FIXME: Placeholder during early development if (logger.isInfoEnabled()) { logger.info("Invoking " + CLASSNAME + " ..."); logger.info("Invocation context is: " + getInvocationContext().getMode()); } + if (!requestedInvocationModeIsSupported()) { setInvocationModeNotSupportedResult(); } - String csid; + + List csids = new ArrayList(); if (requestIsForInvocationModeSingle()) { - csid = getInvocationContext().getSingleCSID(); - if (Tools.isBlank(csid)) { - throw new Exception("Could not find required CSID value in the context for this batch job."); + String singleCsid = getInvocationContext().getSingleCSID(); + if (Tools.notBlank(singleCsid)) { + csids.add(singleCsid); + } + } else if (requestIsForInvocationModeList()) { + List listCsids = (getInvocationContext().getListCSIDs().getCsid()); + if (listCsids == null || listCsids.isEmpty()) { + throw new Exception(CSID_VALUES_NOT_PROVIDED_IN_INVOCATION_CONTEXT_MESSAGE); + } + csids.addAll(listCsids); + } else if (requestIsForInvocationModeGroup()) { + String groupCsid = getInvocationContext().getGroupCSID(); + // FIXME: Get individual CSIDs from the group + // and add them to the list + } + + if (csids.isEmpty()) { + throw new Exception(CSID_VALUES_NOT_PROVIDED_IN_INVOCATION_CONTEXT_MESSAGE); + } + + ResourceBase collectionObjectResource = getResourceMap().get(CollectionObjectClient.SERVICE_NAME); + PoxPayloadOut collectionObjectPayload; + // For each CollectionObject record: + for (String csid : csids) { + // Get the movement records related to this record + // Get the latest movement record from among those + // Extract its current location value + // Update the computed current location value in the CollectionObject record + collectionObjectPayload = findByCsid(collectionObjectResource, csid); + if (logger.isInfoEnabled()) { + logger.info("Payload: " + "\n" + collectionObjectPayload); } - logger.info("CSID value is: " + csid); } - String docType = getInvocationContext().getDocType(); } catch (Exception e) { String errMsg = "Error encountered in " + CLASSNAME + ": " + e.getLocalizedMessage(); setErrorResult(errMsg); } + setCompletionStatus(STATUS_COMPLETE); } + + // Ray's convenience methods from his AbstractBatchJob class for the UC Berkeley Botanical Garden v2.4 implementation. + + protected PoxPayloadOut findByCsid(String serviceName, String csid) throws URISyntaxException, DocumentException { + ResourceBase resource = getResourceMap().get(serviceName); + return findByCsid(resource, csid); + } + + protected PoxPayloadOut findByCsid(ResourceBase resource, String csid) throws URISyntaxException, DocumentException { + byte[] response = resource.get(null, createUriInfo(), csid); + PoxPayloadOut payload = new PoxPayloadOut(response); + return payload; + } + + protected UriInfo createUriInfo() throws URISyntaxException { + return createUriInfo(""); + } + + protected UriInfo createUriInfo(String queryString) throws URISyntaxException { + URI absolutePath = new URI(""); + URI baseUri = new URI(""); + return new UriInfoImpl(absolutePath, baseUri, "", queryString, Collections.emptyList()); + } } -- 2.47.3