]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0e5397da921b5e4fa63fe30a1fc5c518372dac9d
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.batch.nuxeo;
2
3 import java.net.URISyntaxException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7
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;
21
22 public class UpdateDeadFlagBatchJob extends AbstractBatchJob {
23         final Logger logger = LoggerFactory.getLogger(UpdateDeadFlagBatchJob.class);
24
25         public UpdateDeadFlagBatchJob() {
26                 this.setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE));
27         }
28
29         public void run() {
30                 setCompletionStatus(STATUS_MIN_PROGRESS);
31                 
32                 try {
33                         String mode = getInvocationContext().getMode();
34                         
35                         if (!mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
36                                 throw new Exception("Unsupported invocation mode: " + mode);
37                         }
38                         
39                         String movementCsid = getInvocationContext().getSingleCSID();
40                         
41                         if (StringUtils.isEmpty(movementCsid)) {
42                                 throw new Exception("Missing context csid");
43                         }
44                         
45                         setResults(updateRelatedDeadFlags(movementCsid));
46                         setCompletionStatus(STATUS_COMPLETE);
47                 }
48                 catch(Exception e) {
49                         setCompletionStatus(STATUS_ERROR);
50                         setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
51                 }
52         }
53         
54         /**
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.
57          * 
58          * @param movementCsid  the csid of the movement that was updated
59          * @return
60          * @throws URISyntaxException
61          * @throws DocumentException
62          */
63         public InvocationResults updateRelatedDeadFlags(String movementCsid) throws URISyntaxException, DocumentException {
64                 InvocationResults results = new InvocationResults();
65                 long numAffected = 0;
66                 List<String> userNotes = new ArrayList<String>();
67                 
68                 PoxPayloadOut payload = findMovementByCsid(movementCsid);
69
70                 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
71                 logger.debug("actionCode=" + actionCode);
72                 
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);
77                         
78                         List<String> collectionObjectCsids = findRelatedCollectionObjects(movementCsid);
79                         
80                         for (String collectionObjectCsid : collectionObjectCsids) {
81                                 logger.debug("found related collectionobject: " + collectionObjectCsid);
82
83                                 InvocationResults collectionObjectResults = updateDeadFlag(collectionObjectCsid, movementCsid, actionCode, actionDate);
84
85                                 if (collectionObjectResults.getNumAffected() > 0) {
86                                         numAffected = numAffected + collectionObjectResults.getNumAffected();
87                                         userNotes.add(collectionObjectResults.getUserNote());
88                                 }
89                         }
90                 }
91                 
92                 if (numAffected > 0) {
93                         results.setNumAffected(numAffected);
94                         results.setUserNote(StringUtils.join(userNotes, ", "));
95                 }
96                 
97                 return results;
98         }
99
100         /**
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.
104          * 
105          * @param collectionObjectCsid  the csid of the collectionobject to update
106          * @param updatedMovementCsid   the csid of the related movement that was updated
107          * @return
108          * @throws URISyntaxException
109          * @throws DocumentException
110          */
111         public InvocationResults updateDeadFlag(String collectionObjectCsid, String updatedMovementCsid) throws URISyntaxException, DocumentException {
112                 InvocationResults results = new InvocationResults();
113                 PoxPayloadOut payload = findMovementByCsid(updatedMovementCsid);
114
115                 String actionCode = getFieldValue(payload, MovementBotGardenConstants.ACTION_CODE_SCHEMA_NAME, 
116                                 MovementBotGardenConstants.ACTION_CODE_FIELD_NAME);
117                 logger.debug("actionCode=" + actionCode);
118                 
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);
123
124                         results = updateDeadFlag(collectionObjectCsid, updatedMovementCsid, actionCode, actionDate);
125                 }
126                 
127                 return results;
128         }
129         
130         /**
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.
134          * 
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
139          * @return
140          * @throws URISyntaxException
141          * @throws DocumentException
142          */
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);
146
147                 String workflowState = getFieldValue(payload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
148                 
149                 if (workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
150                         logger.debug("skipping deleted collectionobject: " + collectionObjectCsid);
151                 }
152                 else {                  
153                         String deadFlag = getFieldValue(payload, CollectionObjectBotGardenConstants.DEAD_FLAG_SCHEMA_NAME, 
154                                         CollectionObjectBotGardenConstants.DEAD_FLAG_FIELD_NAME);
155                         boolean isDead = (deadFlag != null) && (deadFlag.equalsIgnoreCase("true"));
156
157                         logger.debug("updating dead flag: collectionObjectCsid=" + collectionObjectCsid + " actionCode=" + actionCode + " isDead=" + isDead);
158
159                         if (actionCode.equals(MovementBotGardenConstants.REVIVED_ACTION_CODE)) {
160                                 if (isDead) {
161                                         /*
162                                          * The object is dead, but a location was revived. Unset the dead flag and date on the object.
163                                          */
164                                         setDeadFlag(collectionObjectCsid, false, null);
165                                         
166                                         results.setNumAffected(1);
167                                         results.setUserNote(collectionObjectCsid + " set to alive");
168                                 }
169                         }
170                         else if (actionCode.equals(MovementBotGardenConstants.DEAD_ACTION_CODE)) {
171                                 if (!isDead) {
172                                         /*
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.
177                                          */
178                                         List<String> movementCsids = findRelatedMovements(collectionObjectCsid);
179                                         boolean liveLocationExists = false;
180                                         
181                                         for (String movementCsid : movementCsids) {
182                                                 logger.debug("found related movement: movementCsid=" + movementCsid);
183                                                 
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);
187                                                 
188                                                         if (!movementWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
189                                                                 logger.debug("found live location: movementCsid=" + movementCsid);
190                                                                 
191                                                                 liveLocationExists = true;
192                                                                 break;
193                                                         }
194                                                 }
195                                         }
196                                         
197                                         if (!liveLocationExists) {
198                                                 setDeadFlag(collectionObjectCsid, true, actionDate);
199
200                                                 results.setNumAffected(1);
201                                                 results.setUserNote(collectionObjectCsid + " set to dead");
202                                         }
203                                 }
204                         }
205                 }
206                 
207                 return results;
208         }
209         
210         /**
211          * Update the dead flag and dead date of the specified collectionobject.
212          * 
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 
217          */
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>" +                                     
228                         "</document>";
229                 
230                 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(CollectionObjectClient.SERVICE_NAME);
231                 resource.update(getResourceMap(), createUriInfo(), collectionObjectCsid, updatePayload);
232         }
233 }