1 package org.collectionspace.services.batch.nuxeo;
3 import java.net.URISyntaxException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
8 import javax.ws.rs.core.UriInfo;
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;
19 public class ClearPotTagLabelRequestBatchJob extends AbstractBatchJob {
20 final Logger logger = LoggerFactory.getLogger(ClearPotTagLabelRequestBatchJob.class);
22 public ClearPotTagLabelRequestBatchJob() {
23 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
27 setCompletionStatus(STATUS_MIN_PROGRESS);
31 * For now, treat any mode as if it were no context.
34 setResults(clearLabelRequests());
35 setCompletionStatus(STATUS_COMPLETE);
38 setCompletionStatus(STATUS_ERROR);
39 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
43 public InvocationResults clearLabelRequests() throws URISyntaxException {
44 List<String> potTagCsids = findLabelRequests();
45 InvocationResults results = null;
47 if (potTagCsids.size() > 0) {
48 results = clearLabelRequests(potTagCsids);
51 results = new InvocationResults();
52 results.setUserNote("No label requests found");
58 public InvocationResults clearLabelRequests(String potTagCsid) throws URISyntaxException {
59 return clearLabelRequests(Arrays.asList(potTagCsid));
62 public InvocationResults clearLabelRequests(List<String> potTagCsids) throws URISyntaxException {
63 InvocationResults results = new InvocationResults();
66 for (String potTagCsid : potTagCsids) {
67 clearLabelRequest(potTagCsid);
68 numAffected = numAffected + 1;
71 results.setNumAffected(numAffected);
72 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
77 private void clearLabelRequest(String potTagCsid) throws URISyntaxException {
78 logger.debug("clear label request: potTagCsid=" + potTagCsid);
80 final String updatePayload =
81 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
82 "<document name=\"pottags\">" +
83 "<ns2:pottags_common xmlns:ns2=\"http://collectionspace.org/services/pottag\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
84 getFieldXml(PottagConstants.LABEL_REQUESTED_FIELD_NAME, PottagConstants.LABEL_REQUESTED_NO_VALUE) +
85 "</ns2:pottags_common>" +
88 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(PottagClient.SERVICE_NAME);
89 resource.update(getResourceMap(), createUriInfo(), potTagCsid, updatePayload);
92 private List<String> findLabelRequests() throws URISyntaxException {
93 List<String> csids = new ArrayList<String>();
94 PottagResource potTagResource = (PottagResource) getResourceMap().get(PottagClient.SERVICE_NAME);
95 AbstractCommonList potTagList = potTagResource.getList(createLabelRequestSearchUriInfo());
97 for (AbstractCommonList.ListItem item : potTagList.getListItem()) {
98 for (org.w3c.dom.Element element : item.getAny()) {
99 if (element.getTagName().equals("csid")) {
100 csids.add(element.getTextContent());
109 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
110 return createKeywordSearchUriInfo(PottagConstants.LABEL_REQUESTED_SCHEMA_NAME, PottagConstants.LABEL_REQUESTED_FIELD_NAME, PottagConstants.LABEL_REQUESTED_YES_VALUE);