]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
98b8a4040883f77120ca1866a6661a91ad2c3239
[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.LoanoutClient;
11 import org.collectionspace.services.common.NuxeoBasedResource;
12 import org.collectionspace.services.common.invocable.InvocationResults;
13 import org.collectionspace.services.jaxb.AbstractCommonList;
14 import org.collectionspace.services.loanout.LoanoutResource;
15 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class ClearVoucherLabelRequestBatchJob extends AbstractBatchJob {
20         final Logger logger = LoggerFactory.getLogger(ClearVoucherLabelRequestBatchJob.class);
21
22         public ClearVoucherLabelRequestBatchJob() {
23                 this.setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
24         }
25         
26         public void run() {
27                 setCompletionStatus(STATUS_MIN_PROGRESS);
28                 
29                 try {
30                         /*
31                          * For now, treat any mode as if it were no context.
32                          */
33                         
34                         setResults(clearLabelRequests());
35                         setCompletionStatus(STATUS_COMPLETE);
36                 }
37                 catch(Exception e) {
38                         setCompletionStatus(STATUS_ERROR);
39                         setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
40                 }
41         }
42
43         public InvocationResults clearLabelRequests() throws URISyntaxException  {
44                 List<String> loanoutCsids = findLabelRequests();
45                 InvocationResults results = null;
46                 
47                 if (loanoutCsids.size() > 0) {
48                         results = clearLabelRequests(loanoutCsids);
49                 }
50                 else {
51                         results = new InvocationResults();
52                         results.setUserNote("No label requests found");
53                 }
54                 
55                 return results;
56         }
57         
58         public InvocationResults clearLabelRequests(String loanoutCsid) throws URISyntaxException  {
59                 return clearLabelRequests(Arrays.asList(loanoutCsid));
60         }
61         
62         public InvocationResults clearLabelRequests(List<String> loanoutCsids) throws URISyntaxException  {
63                 InvocationResults results = new InvocationResults();
64                 long numAffected = 0;
65                                 
66                 for (String loanoutCsid : loanoutCsids) {
67                         clearLabelRequest(loanoutCsid);
68                         numAffected = numAffected + 1;
69                 }
70                 
71                 results.setNumAffected(numAffected);
72                 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
73
74                 return results;
75         }
76         
77         private void clearLabelRequest(String loanoutCsid) throws URISyntaxException {
78                 logger.debug("clear label request: loanoutCsid=" + loanoutCsid);
79
80                 final String updatePayload = 
81                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
82                         "<document name=\"loansout\">" +
83                                 "<ns2:loansout_botgarden xmlns:ns2=\"http://collectionspace.org/services/loanout/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
84                                         getFieldXml("labelRequested", LoanoutBotGardenConstants.LABEL_REQUESTED_NO_VALUE) +
85                                 "</ns2:loansout_botgarden>" +
86                         "</document>";
87                                 
88                 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
89                 resource.update(getResourceMap(), createUriInfo(), loanoutCsid, updatePayload);
90         }
91         
92         private List<String> findLabelRequests() throws URISyntaxException {
93                 List<String> csids = new ArrayList<String>();
94                 LoanoutResource loanoutResource = (LoanoutResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
95                 AbstractCommonList loanoutList = loanoutResource.getList(createLabelRequestSearchUriInfo());
96
97                 for (AbstractCommonList.ListItem item : loanoutList.getListItem()) {
98                         for (org.w3c.dom.Element element : item.getAny()) {
99                                 if (element.getTagName().equals("csid")) {
100                                         csids.add(element.getTextContent());
101                                         break;
102                                 }
103                         }
104                 }
105
106                 return csids;
107         }
108
109         private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
110                 return createKeywordSearchUriInfo(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
111                                 LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE);           
112         }
113 }