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.MovementClient;
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.movement.MovementResource;
15 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class ClearLocationLabelRequestBatchJob extends AbstractBatchJob {
21 final Logger logger = LoggerFactory.getLogger(ClearLocationLabelRequestBatchJob.class);
23 public ClearLocationLabelRequestBatchJob() {
24 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
29 setCompletionStatus(STATUS_MIN_PROGRESS);
33 * For now, treat any mode as if it were no context.
36 setResults(clearLabelRequests());
37 setCompletionStatus(STATUS_COMPLETE);
40 setCompletionStatus(STATUS_ERROR);
41 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
45 public InvocationResults clearLabelRequests() throws URISyntaxException {
46 List<String> movementCsids = findLabelRequests();
47 InvocationResults results = null;
49 if (movementCsids.size() > 0) {
50 results = clearLabelRequests(movementCsids);
53 results = new InvocationResults();
54 results.setUserNote("No label requests found");
60 public InvocationResults clearLabelRequests(String movementCsid) throws URISyntaxException {
61 return clearLabelRequests(Arrays.asList(movementCsid));
64 public InvocationResults clearLabelRequests(List<String> movementCsids) throws URISyntaxException {
65 InvocationResults results = new InvocationResults();
68 for (String movementCsid : movementCsids) {
69 clearLabelRequest(movementCsid);
70 numAffected = numAffected + 1;
73 results.setNumAffected(numAffected);
74 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
79 private void clearLabelRequest(String movementCsid) throws URISyntaxException {
80 logger.debug("clear label request: movementCsid=" + movementCsid);
82 final String updatePayload =
83 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
84 "<document name=\"movements\">" +
85 "<ns2:movements_common xmlns:ns2=\"http://collectionspace.org/services/movement\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
86 getFieldXml("reasonForMove", MovementBotGardenConstants.OTHER_ACTION_CODE) +
87 "</ns2:movements_common>" +
88 "<ns2:movements_botgarden xmlns:ns2=\"http://collectionspace.org/services/movement/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
89 getFieldXml("labelRequested", MovementBotGardenConstants.LABEL_REQUESTED_NO_VALUE) +
90 getFieldXml("labelSize", "") +
91 getFieldXml("labelStandType", "") +
92 getFieldXml("labelCount", "") +
93 "</ns2:movements_botgarden>" +
96 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(MovementClient.SERVICE_NAME);
97 resource.update(getResourceMap(), createUriInfo(), movementCsid, updatePayload);
100 private List<String> findLabelRequests() throws URISyntaxException {
101 List<String> csids = new ArrayList<String>();
102 MovementResource movementResource = (MovementResource) getResourceMap().get(MovementClient.SERVICE_NAME);
103 AbstractCommonList movementList = movementResource.getList(createLabelRequestSearchUriInfo());
105 for (AbstractCommonList.ListItem item : movementList.getListItem()) {
106 for (org.w3c.dom.Element element : item.getAny()) {
107 if (element.getTagName().equals("csid")) {
108 csids.add(element.getTextContent());
117 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
118 return createKeywordSearchUriInfo(MovementBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, MovementBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
119 MovementBotGardenConstants.LABEL_REQUESTED_YES_VALUE);