From b101593811e9f70030d91dfb21bb34eee75d8207 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Mon, 7 Jan 2013 16:51:59 -0800 Subject: [PATCH] CSPACE-5728: Sample updating of CollectionSpace payload. Several XML helper functions are not working as expected, per comments. --- .../nuxeo/UpdateObjectLocationBatchJob.java | 172 +++++++++++++++--- 1 file changed, 148 insertions(+), 24 deletions(-) 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 49a2d1665..66d94d929 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 @@ -5,21 +5,36 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.collectionspace.services.batch.AbstractBatchInvocable; import org.collectionspace.services.client.CollectionObjectClient; +import org.collectionspace.services.client.CollectionSpaceClientUtils; +import org.collectionspace.services.client.PayloadOutputPart; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.ResourceBase; +import org.collectionspace.services.common.ResourceMap; import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.invocable.InvocationResults; import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.Node; +import org.dom4j.XPath; import org.jboss.resteasy.specimpl.UriInfoImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable { + // FIXME; Get from existing constants and replace these local declarations + final static String COLLECTIONOBJECTS_COMMON_SCHEMA_NAME = "collectionobjects_common"; + final static String OBJECT_NUMBER_FIELD_NAME = "objectNumber"; + private InvocationResults results = new InvocationResults(); final String CLASSNAME = this.getClass().getSimpleName(); final Logger logger = LoggerFactory.getLogger(UpdateObjectLocationBatchJob.class); @@ -33,20 +48,20 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable { */ @Override 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(); } - + List csids = new ArrayList(); if (requestIsForInvocationModeSingle()) { String singleCsid = getInvocationContext().getSingleCSID(); @@ -64,52 +79,161 @@ public class UpdateObjectLocationBatchJob extends AbstractBatchInvocable { // 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); - } - } + // Update the current computed location field for each CollectionObject + setResults(updateComputedCurrentLocations(csids)); + setCompletionStatus(STATUS_COMPLETE); + } 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()); } + + /** + * Get a field value from a PoxPayloadOut, given a part name and namespace-qualified xpath + * expression. + */ + protected String getFieldValue(PoxPayloadOut payload, String partLabel, String namespacePrefix, String namespace, String fieldPath) { + String value = null; + PayloadOutputPart part = payload.getPart(partLabel); + + if (part != null) { + Element element = part.asElement(); + logger.info(partLabel + " part element =" + element.asXML()); + + Map namespaceUris = new HashMap(); + namespaceUris.put(namespacePrefix, namespace); + + XPath xPath = DocumentHelper.createXPath(fieldPath); + xPath.setNamespaceURIs(namespaceUris); + + Node node = xPath.selectSingleNode(element); + // Node node = element.selectSingleNode(fieldPath); + + if (node != null) { + value = node.getText(); + } + } + + return value; + } + + protected List getFieldValues(PoxPayloadOut payload, String partLabel, String fieldPath) { + List values = new ArrayList(); + PayloadOutputPart part = payload.getPart(partLabel); + + if (part != null) { + Element element = part.asElement(); + List nodes = element.selectNodes(fieldPath); + + if (nodes != null) { + for (Node node : nodes) { + values.add(node.getText()); + } + } + } + + return values; + } + + private InvocationResults updateComputedCurrentLocations(List csids) { + + ResourceMap resourcemap = getResourceMap(); + ResourceBase collectionObjectResource = resourcemap.get(CollectionObjectClient.SERVICE_NAME); + PoxPayloadOut collectionObjectPayload; + String objectNumber; + String computedCurrentLocation; + int numAffected = 0; + // FIXME: Temporary during testing/development + final String COMPUTED_CURRENT_LOCATION = "FOO_COMPUTED_CURRENT_LOCATION"; + + try { + + // 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 + // FIXME: Temporary during testing/development + computedCurrentLocation = COMPUTED_CURRENT_LOCATION; + // Update the computed current location value in the CollectionObject record + collectionObjectPayload = findByCsid(collectionObjectResource, csid); + if (Tools.notBlank(collectionObjectPayload.toXML())) { + if (logger.isInfoEnabled()) { + logger.info("Payload: " + "\n" + collectionObjectPayload); + } + // Silenly fails at various places in dom4j calls (selectSingleNode, selectNode, + // createXpath) in any of the methods tried above, without throwing an Exception + /* + objectNumber = getFieldValue(collectionObjectPayload, + COLLECTIONOBJECTS_COMMON_SCHEMA_NAME, + "ns2", "http://collectionspace.org/services/collectionobject", + OBJECT_NUMBER_FIELD_NAME); + if (logger.isInfoEnabled()) { + logger.info("Object number: " + objectNumber); + } + */ + objectNumber = "BAR"; // FIXME + if (Tools.notBlank(objectNumber)) { + String collectionObjectUpdatePayload = + "" + + "" + + "" + + "" + objectNumber + "" + + "" + computedCurrentLocation + "" + + ""; + if (logger.isInfoEnabled()) { + logger.info("Update payload: " + "\n" + collectionObjectUpdatePayload); + } + byte[] response = collectionObjectResource.update(resourcemap, null, csid, collectionObjectUpdatePayload); + /* + * if (response.getStatus() != OK_STATUS) { + String errMsg = "Error encountered in " + CLASSNAME + ": " + "Updating CollectionObject failed."; + setErrorResult(errMsg); + } else { + results.setUserNote("Computed current location value for CollectionObject " + csid + " set to " + computedCurrentLocation); + numAffected++; + } + */ + } + + } + } + } catch (Exception e) { + String errMsg = "Error encountered in " + CLASSNAME + ": " + e.getLocalizedMessage(); + setErrorResult(errMsg); + getResults().setNumAffected(numAffected); + return getResults(); + } + getResults().setNumAffected(numAffected); + return getResults(); + } } -- 2.47.3