]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4b64312f9c2c7cdbcd18cfd9557e295183b44cd1
[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 javax.ws.rs.core.UriInfo;
9
10 import org.collectionspace.services.client.MovementClient;
11 import org.collectionspace.services.client.PoxPayloadOut;
12 import org.collectionspace.services.client.VocabularyClient;
13 import org.collectionspace.services.common.NuxeoBasedResource;
14 import org.collectionspace.services.common.invocable.InvocationResults;
15 import org.collectionspace.services.jaxb.AbstractCommonList;
16 import org.collectionspace.services.movement.MovementResource;
17 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
18 import org.collectionspace.services.vocabulary.nuxeo.VocabularyConstants;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ClearLocationLabelRequestBatchJob extends AbstractBatchJob {
24         final Logger logger = LoggerFactory.getLogger(ClearLocationLabelRequestBatchJob.class);
25
26         public ClearLocationLabelRequestBatchJob() {
27                 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
28         }
29
30         @Override
31         public void run() {
32                 setCompletionStatus(STATUS_MIN_PROGRESS);
33
34                 try {
35                         /*
36                          * For now, treat any mode as if it were no context.
37                          */
38
39                         setResults(clearLabelRequests());
40                         setCompletionStatus(STATUS_COMPLETE);
41                 }
42                 catch(Exception e) {
43                         setCompletionStatus(STATUS_ERROR);
44                         setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
45                 }
46         }
47
48         public InvocationResults clearLabelRequests() throws Exception  {
49                 List<String> movementCsids = findLabelRequests();
50                 InvocationResults results = null;
51
52                 if (movementCsids.size() > 0) {
53                         results = clearLabelRequests(movementCsids);
54                 }
55                 else {
56                         results = new InvocationResults();
57                         results.setUserNote("No label requests found");
58                 }
59
60                 return results;
61         }
62
63         public InvocationResults clearLabelRequests(String movementCsid) throws Exception  {
64                 return clearLabelRequests(Arrays.asList(movementCsid));
65         }
66
67         public InvocationResults clearLabelRequests(List<String> movementCsids) throws Exception  {
68                 InvocationResults results = new InvocationResults();
69                 long numAffected = 0;
70
71                 for (String movementCsid : movementCsids) {
72                         clearLabelRequest(movementCsid);
73                         numAffected = numAffected + 1;
74                 }
75
76                 results.setNumAffected(numAffected);
77                 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
78
79                 return results;
80         }
81
82         private void clearLabelRequest(String movementCsid) throws Exception {
83                 logger.debug("clear label request: movementCsid=" + movementCsid);
84                 
85                 PoxPayloadOut payloadout = findAuthorityItemByRefName(VocabularyClient.SERVICE_NAME, MovementBotGardenConstants.OTHER_ACTION_CODE);
86                 String termDisplayName = getFieldValue(payloadout, VocabularyConstants.DISPLAY_NAME_SCHEMA_NAME, VocabularyConstants.DISPLAY_NAME_FIELD_NAME);          
87
88                 final String updatePayload =
89                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
90                         "<document name=\"movements\">" +
91                                 "<ns2:movements_common xmlns:ns2=\"http://collectionspace.org/services/movement\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
92                                         getFieldXml("reasonForMove", MovementBotGardenConstants.OTHER_ACTION_CODE + String.format("'%s'", termDisplayName)) +
93                                 "</ns2:movements_common>" +
94                                 "<ns2:movements_botgarden xmlns:ns2=\"http://collectionspace.org/services/movement/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
95                                         getFieldXml("labelRequested", MovementBotGardenConstants.LABEL_REQUESTED_NO_VALUE) +
96                                         getFieldXml("labelSize", "") +
97                                         getFieldXml("labelStandType", "") +
98                                         getFieldXml("labelCount", "") +
99                                 "</ns2:movements_botgarden>" +
100                         "</document>";
101
102                 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(MovementClient.SERVICE_NAME);
103                 resource.update(getServiceContext(), getResourceMap(), createUriInfo(), movementCsid, updatePayload);
104         }
105
106         private List<String> findLabelRequests() throws URISyntaxException {
107                 List<String> csids = new ArrayList<String>();
108                 MovementResource movementResource = (MovementResource) getResourceMap().get(MovementClient.SERVICE_NAME);
109                 AbstractCommonList movementList = movementResource.getList(getServiceContext(), createLabelRequestSearchUriInfo());
110
111                 for (AbstractCommonList.ListItem item : movementList.getListItem()) {
112                         for (org.w3c.dom.Element element : item.getAny()) {
113                                 if (element.getTagName().equals("csid")) {
114                                         csids.add(element.getTextContent());
115                                         break;
116                                 }
117                         }
118                 }
119
120                 return csids;
121         }
122
123         private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
124                 return createKeywordSearchUriInfo(MovementBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, MovementBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
125                                 MovementBotGardenConstants.LABEL_REQUESTED_YES_VALUE);
126         }
127 }