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