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