1 package org.collectionspace.services.batch.nuxeo;
3 import java.net.URISyntaxException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
8 import org.apache.commons.lang.StringUtils;
9 import org.collectionspace.services.client.CollectionObjectClient;
10 import org.collectionspace.services.client.PoxPayloadOut;
11 import org.collectionspace.services.client.workflow.WorkflowClient;
12 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectBotGardenConstants;
13 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
14 import org.collectionspace.services.common.NuxeoBasedResource;
15 import org.collectionspace.services.common.invocable.InvocationResults;
16 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
17 import org.collectionspace.services.movement.nuxeo.MovementConstants;
18 import org.dom4j.DocumentException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class UpdateDeadFlagBatchJob extends AbstractBatchJob {
23 final Logger logger = LoggerFactory.getLogger(UpdateDeadFlagBatchJob.class);
25 public UpdateDeadFlagBatchJob() {
26 this.setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE));
31 setCompletionStatus(STATUS_MIN_PROGRESS);
34 String mode = getInvocationContext().getMode();
36 if (!mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
37 throw new Exception("Unsupported invocation mode: " + mode);
40 String movementCsid = getInvocationContext().getSingleCSID();
42 if (StringUtils.isEmpty(movementCsid)) {
43 throw new Exception("Missing context csid");
46 setResults(updateRelatedDeadFlags(movementCsid));
47 setCompletionStatus(STATUS_COMPLETE);
50 setCompletionStatus(STATUS_ERROR);
51 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
56 * Update the dead flag for all collectionobjects related to the given movement record,
57 * based on the assumption that the action code of the specified movement record has just changed.
59 * @param movementCsid the csid of the movement that was updated
61 * @throws URISyntaxException
62 * @throws DocumentException
64 public InvocationResults updateRelatedDeadFlags(String movementCsid) throws URISyntaxException, DocumentException {
65 InvocationResults results = new InvocationResults();
67 List<String> userNotes = new ArrayList<String>();
69 PoxPayloadOut payload = findMovementByCsid(movementCsid);
71 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
72 logger.debug("actionCode=" + actionCode);
74 if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
75 String actionDate = getFieldValue(payload, MovementBotGardenConstants.ACTION_DATE_SCHEMA_NAME,
76 MovementBotGardenConstants.ACTION_DATE_FIELD_NAME);
77 logger.debug("actionDate=" + actionDate);
79 List<String> collectionObjectCsids = findRelatedCollectionObjects(movementCsid);
81 for (String collectionObjectCsid : collectionObjectCsids) {
82 logger.debug("found related collectionobject: " + collectionObjectCsid);
84 InvocationResults collectionObjectResults = updateDeadFlag(collectionObjectCsid, movementCsid, actionCode, actionDate);
86 if (collectionObjectResults.getNumAffected() > 0) {
87 numAffected = numAffected + collectionObjectResults.getNumAffected();
88 userNotes.add(collectionObjectResults.getUserNote());
93 if (numAffected > 0) {
94 results.setNumAffected(numAffected);
95 results.setUserNote(StringUtils.join(userNotes, ", "));
102 * Update the dead flag for the given collectionobject, based on the assumption that the action code
103 * of the specified movement record has just changed, and that the movement record is related to
104 * the collectionobject.
106 * @param collectionObjectCsid the csid of the collectionobject to update
107 * @param updatedMovementCsid the csid of the related movement that was updated
109 * @throws URISyntaxException
110 * @throws DocumentException
112 public InvocationResults updateDeadFlag(String collectionObjectCsid, String updatedMovementCsid) throws URISyntaxException, DocumentException {
113 InvocationResults results = new InvocationResults();
114 PoxPayloadOut payload = findMovementByCsid(updatedMovementCsid);
116 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
117 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
118 logger.debug("actionCode=" + actionCode);
120 if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
121 String actionDate = getFieldValue(payload, MovementBotGardenConstants.ACTION_DATE_SCHEMA_NAME,
122 MovementBotGardenConstants.ACTION_DATE_FIELD_NAME);
123 logger.debug("actionDate=" + actionDate);
125 results = updateDeadFlag(collectionObjectCsid, updatedMovementCsid, actionCode, actionDate);
132 * Update the dead flag for the given collectionobject, based on the assumption that the action code
133 * of the specified movement record has just changed, and that the movement record is related to
134 * the collectionobject.
136 * @param collectionObjectCsid the csid of the collectionobject to update
137 * @param updatedMovementCsid the csid of the related movement that was updated
138 * @param actionCode the action code of the movement
139 * @param actionDate the action date of the movement
141 * @throws URISyntaxException
142 * @throws DocumentException
144 private InvocationResults updateDeadFlag(String collectionObjectCsid, String updatedMovementCsid, String actionCode, String actionDate) throws URISyntaxException, DocumentException {
145 InvocationResults results = new InvocationResults();
146 PoxPayloadOut payload = findCollectionObjectByCsid(collectionObjectCsid);
148 String workflowState = getFieldValue(payload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
150 if (workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
151 logger.debug("skipping deleted collectionobject: " + collectionObjectCsid);
154 String deadFlag = getFieldValue(payload, CollectionObjectBotGardenConstants.DEAD_FLAG_SCHEMA_NAME,
155 CollectionObjectBotGardenConstants.DEAD_FLAG_FIELD_NAME);
156 boolean isDead = (deadFlag != null) && (deadFlag.equalsIgnoreCase("true"));
158 logger.debug("updating dead flag: collectionObjectCsid=" + collectionObjectCsid + " actionCode=" + actionCode + " isDead=" + isDead);
160 if (actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
163 * The object is dead, but a location was revived. Unset the dead flag and date on the object.
165 setDeadFlag(collectionObjectCsid, false, null);
167 results.setNumAffected(1);
168 results.setUserNote(collectionObjectCsid + " set to alive");
171 else if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
174 * The object is not dead, but a location was marked dead. If there are no remaining live locations,
175 * set the dead flag and date on the object. Any movement record that is not deleted represents
176 * a live location, with one exception: the movement record that was just marked dead may not have
177 * been deleted yet, but it should not count as a live location.
179 List<String> movementCsids = findRelatedMovements(collectionObjectCsid);
180 boolean liveLocationExists = false;
182 for (String movementCsid : movementCsids) {
183 logger.debug("found related movement: movementCsid=" + movementCsid);
185 if (!movementCsid.equals(updatedMovementCsid)) {
186 PoxPayloadOut movementPayload = findMovementByCsid(movementCsid);
187 String movementWorkflowState = getFieldValue(movementPayload, MovementConstants.WORKFLOW_STATE_SCHEMA_NAME, MovementConstants.WORKFLOW_STATE_FIELD_NAME);
189 if (!movementWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
190 logger.debug("found live location: movementCsid=" + movementCsid);
192 liveLocationExists = true;
198 if (!liveLocationExists) {
199 setDeadFlag(collectionObjectCsid, true, actionDate);
201 results.setNumAffected(1);
202 results.setUserNote(collectionObjectCsid + " set to dead");
212 * Update the dead flag and dead date of the specified collectionobject.
214 * @param collectionObjectCsid the csid of the collectionobject to update
215 * @param deadFlag the new value of the dead flag field
216 * @param deadDate the new value of the dead date field
217 * @throws URISyntaxException
219 private void setDeadFlag(String collectionObjectCsid, boolean deadFlag, String deadDate) throws URISyntaxException {
220 String updatePayload =
221 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
222 "<document name=\"collectionobjects\">" +
223 "<ns2:collectionobjects_botgarden xmlns:ns2=\"http://collectionspace.org/services/collectionobject/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
224 getFieldXml("deadFlag", (deadFlag ? "true" : "false")) +
225 getFieldXml("deadDate", deadDate) +
226 "</ns2:collectionobjects_botgarden>" +
227 "<ns2:collectionobjects_common xmlns:ns2=\"http://collectionspace.org/services/collectionobject\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
228 "</ns2:collectionobjects_common>" +
231 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(CollectionObjectClient.SERVICE_NAME);
232 resource.update(getResourceMap(), createUriInfo(), collectionObjectCsid, updatePayload);