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));
30 setCompletionStatus(STATUS_MIN_PROGRESS);
33 String mode = getInvocationContext().getMode();
35 if (!mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
36 throw new Exception("Unsupported invocation mode: " + mode);
39 String movementCsid = getInvocationContext().getSingleCSID();
41 if (StringUtils.isEmpty(movementCsid)) {
42 throw new Exception("Missing context csid");
45 setResults(updateRelatedDeadFlags(movementCsid));
46 setCompletionStatus(STATUS_COMPLETE);
49 setCompletionStatus(STATUS_ERROR);
50 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
55 * Update the dead flag for all collectionobjects related to the given movement record,
56 * based on the assumption that the action code of the specified movement record has just changed.
58 * @param movementCsid the csid of the movement that was updated
60 * @throws URISyntaxException
61 * @throws DocumentException
63 public InvocationResults updateRelatedDeadFlags(String movementCsid) throws URISyntaxException, DocumentException {
64 InvocationResults results = new InvocationResults();
66 List<String> userNotes = new ArrayList<String>();
68 PoxPayloadOut payload = findMovementByCsid(movementCsid);
70 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
71 logger.debug("actionCode=" + actionCode);
73 if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
74 String actionDate = getFieldValue(payload, MovementBotGardenConstants.ACTION_DATE_SCHEMA_NAME,
75 MovementBotGardenConstants.ACTION_DATE_FIELD_NAME);
76 logger.debug("actionDate=" + actionDate);
78 List<String> collectionObjectCsids = findRelatedCollectionObjects(movementCsid);
80 for (String collectionObjectCsid : collectionObjectCsids) {
81 logger.debug("found related collectionobject: " + collectionObjectCsid);
83 InvocationResults collectionObjectResults = updateDeadFlag(collectionObjectCsid, movementCsid, actionCode, actionDate);
85 if (collectionObjectResults.getNumAffected() > 0) {
86 numAffected = numAffected + collectionObjectResults.getNumAffected();
87 userNotes.add(collectionObjectResults.getUserNote());
92 if (numAffected > 0) {
93 results.setNumAffected(numAffected);
94 results.setUserNote(StringUtils.join(userNotes, ", "));
101 * Update the dead flag for the given collectionobject, based on the assumption that the action code
102 * of the specified movement record has just changed, and that the movement record is related to
103 * the collectionobject.
105 * @param collectionObjectCsid the csid of the collectionobject to update
106 * @param updatedMovementCsid the csid of the related movement that was updated
108 * @throws URISyntaxException
109 * @throws DocumentException
111 public InvocationResults updateDeadFlag(String collectionObjectCsid, String updatedMovementCsid) throws URISyntaxException, DocumentException {
112 InvocationResults results = new InvocationResults();
113 PoxPayloadOut payload = findMovementByCsid(updatedMovementCsid);
115 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME,
116 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
117 logger.debug("actionCode=" + actionCode);
119 if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE) || actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
120 String actionDate = getFieldValue(payload, MovementBotGardenConstants.ACTION_DATE_SCHEMA_NAME,
121 MovementBotGardenConstants.ACTION_DATE_FIELD_NAME);
122 logger.debug("actionDate=" + actionDate);
124 results = updateDeadFlag(collectionObjectCsid, updatedMovementCsid, actionCode, actionDate);
131 * Update the dead flag for the given collectionobject, based on the assumption that the action code
132 * of the specified movement record has just changed, and that the movement record is related to
133 * the collectionobject.
135 * @param collectionObjectCsid the csid of the collectionobject to update
136 * @param updatedMovementCsid the csid of the related movement that was updated
137 * @param actionCode the action code of the movement
138 * @param actionDate the action date of the movement
140 * @throws URISyntaxException
141 * @throws DocumentException
143 private InvocationResults updateDeadFlag(String collectionObjectCsid, String updatedMovementCsid, String actionCode, String actionDate) throws URISyntaxException, DocumentException {
144 InvocationResults results = new InvocationResults();
145 PoxPayloadOut payload = findCollectionObjectByCsid(collectionObjectCsid);
147 String workflowState = getFieldValue(payload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
149 if (workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
150 logger.debug("skipping deleted collectionobject: " + collectionObjectCsid);
153 String deadFlag = getFieldValue(payload, CollectionObjectBotGardenConstants.DEAD_FLAG_SCHEMA_NAME,
154 CollectionObjectBotGardenConstants.DEAD_FLAG_FIELD_NAME);
155 boolean isDead = (deadFlag != null) && (deadFlag.equalsIgnoreCase("true"));
157 logger.debug("updating dead flag: collectionObjectCsid=" + collectionObjectCsid + " actionCode=" + actionCode + " isDead=" + isDead);
159 if (actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
162 * The object is dead, but a location was revived. Unset the dead flag and date on the object.
164 setDeadFlag(collectionObjectCsid, false, null);
166 results.setNumAffected(1);
167 results.setUserNote(collectionObjectCsid + " set to alive");
170 else if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
173 * The object is not dead, but a location was marked dead. If there are no remaining live locations,
174 * set the dead flag and date on the object. Any movement record that is not deleted represents
175 * a live location, with one exception: the movement record that was just marked dead may not have
176 * been deleted yet, but it should not count as a live location.
178 List<String> movementCsids = findRelatedMovements(collectionObjectCsid);
179 boolean liveLocationExists = false;
181 for (String movementCsid : movementCsids) {
182 logger.debug("found related movement: movementCsid=" + movementCsid);
184 if (!movementCsid.equals(updatedMovementCsid)) {
185 PoxPayloadOut movementPayload = findMovementByCsid(movementCsid);
186 String movementWorkflowState = getFieldValue(movementPayload, MovementConstants.WORKFLOW_STATE_SCHEMA_NAME, MovementConstants.WORKFLOW_STATE_FIELD_NAME);
188 if (!movementWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
189 logger.debug("found live location: movementCsid=" + movementCsid);
191 liveLocationExists = true;
197 if (!liveLocationExists) {
198 setDeadFlag(collectionObjectCsid, true, actionDate);
200 results.setNumAffected(1);
201 results.setUserNote(collectionObjectCsid + " set to dead");
211 * Update the dead flag and dead date of the specified collectionobject.
213 * @param collectionObjectCsid the csid of the collectionobject to update
214 * @param deadFlag the new value of the dead flag field
215 * @param deadDate the new value of the dead date field
216 * @throws URISyntaxException
218 private void setDeadFlag(String collectionObjectCsid, boolean deadFlag, String deadDate) throws URISyntaxException {
219 String updatePayload =
220 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
221 "<document name=\"collectionobjects\">" +
222 "<ns2:collectionobjects_botgarden xmlns:ns2=\"http://collectionspace.org/services/collectionobject/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
223 getFieldXml("deadFlag", (deadFlag ? "true" : "false")) +
224 getFieldXml("deadDate", deadDate) +
225 "</ns2:collectionobjects_botgarden>" +
226 "<ns2:collectionobjects_common xmlns:ns2=\"http://collectionspace.org/services/collectionobject\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
227 "</ns2:collectionobjects_common>" +
230 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(CollectionObjectClient.SERVICE_NAME);
231 resource.update(getResourceMap(), createUriInfo(), collectionObjectCsid, updatePayload);