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));
28 setCompletionStatus(STATUS_MIN_PROGRESS);
32 * For now, treat any mode as if it were no context.
35 setResults(clearLabelRequests());
36 setCompletionStatus(STATUS_COMPLETE);
39 setCompletionStatus(STATUS_ERROR);
40 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
44 public InvocationResults clearLabelRequests() throws URISyntaxException {
45 List<String> potTagCsids = findLabelRequests();
46 InvocationResults results = null;
48 if (potTagCsids.size() > 0) {
49 results = clearLabelRequests(potTagCsids);
52 results = new InvocationResults();
53 results.setUserNote("No label requests found");
59 public InvocationResults clearLabelRequests(String potTagCsid) throws URISyntaxException {
60 return clearLabelRequests(Arrays.asList(potTagCsid));
63 public InvocationResults clearLabelRequests(List<String> potTagCsids) throws URISyntaxException {
64 InvocationResults results = new InvocationResults();
67 for (String potTagCsid : potTagCsids) {
68 clearLabelRequest(potTagCsid);
69 numAffected = numAffected + 1;
72 results.setNumAffected(numAffected);
73 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
78 private void clearLabelRequest(String potTagCsid) throws URISyntaxException {
79 logger.debug("clear label request: potTagCsid=" + potTagCsid);
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>" +
89 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(PottagClient.SERVICE_NAME);
90 resource.update(getResourceMap(), createUriInfo(), potTagCsid, updatePayload);
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());
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());
110 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
111 return createKeywordSearchUriInfo(PottagConstants.LABEL_REQUESTED_SCHEMA_NAME, PottagConstants.LABEL_REQUESTED_FIELD_NAME, PottagConstants.LABEL_REQUESTED_YES_VALUE);