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.client.PoxPayloadOut;
12 import org.collectionspace.services.client.VocabularyClient;
13 import org.collectionspace.services.common.NuxeoBasedResource;
14 import org.collectionspace.services.common.invocable.InvocationResults;
15 import org.collectionspace.services.jaxb.AbstractCommonList;
16 import org.collectionspace.services.movement.MovementResource;
17 import org.collectionspace.services.movement.nuxeo.MovementBotGardenConstants;
18 import org.collectionspace.services.vocabulary.nuxeo.VocabularyConstants;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class ClearLocationLabelRequestBatchJob extends AbstractBatchJob {
24 final Logger logger = LoggerFactory.getLogger(ClearLocationLabelRequestBatchJob.class);
26 public ClearLocationLabelRequestBatchJob() {
27 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
32 setCompletionStatus(STATUS_MIN_PROGRESS);
36 * For now, treat any mode as if it were no context.
39 setResults(clearLabelRequests());
40 setCompletionStatus(STATUS_COMPLETE);
43 setCompletionStatus(STATUS_ERROR);
44 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
48 public InvocationResults clearLabelRequests() throws Exception {
49 List<String> movementCsids = findLabelRequests();
50 InvocationResults results = null;
52 if (movementCsids.size() > 0) {
53 results = clearLabelRequests(movementCsids);
56 results = new InvocationResults();
57 results.setUserNote("No label requests found");
63 public InvocationResults clearLabelRequests(String movementCsid) throws Exception {
64 return clearLabelRequests(Arrays.asList(movementCsid));
67 public InvocationResults clearLabelRequests(List<String> movementCsids) throws Exception {
68 InvocationResults results = new InvocationResults();
71 for (String movementCsid : movementCsids) {
72 clearLabelRequest(movementCsid);
73 numAffected = numAffected + 1;
76 results.setNumAffected(numAffected);
77 results.setUserNote("Removed " + numAffected + " label " + (numAffected == 1 ? "request" : "requests"));
82 private void clearLabelRequest(String movementCsid) throws Exception {
83 logger.debug("clear label request: movementCsid=" + movementCsid);
85 PoxPayloadOut payloadout = findAuthorityItemByRefName(VocabularyClient.SERVICE_NAME, MovementBotGardenConstants.OTHER_ACTION_CODE);
86 String termDisplayName = getFieldValue(payloadout, VocabularyConstants.DISPLAY_NAME_SCHEMA_NAME, VocabularyConstants.DISPLAY_NAME_FIELD_NAME);
88 final String updatePayload =
89 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
90 "<document name=\"movements\">" +
91 "<ns2:movements_common xmlns:ns2=\"http://collectionspace.org/services/movement\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
92 getFieldXml("reasonForMove", MovementBotGardenConstants.OTHER_ACTION_CODE + String.format("'%s'", termDisplayName)) +
93 "</ns2:movements_common>" +
94 "<ns2:movements_botgarden xmlns:ns2=\"http://collectionspace.org/services/movement/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
95 getFieldXml("labelRequested", MovementBotGardenConstants.LABEL_REQUESTED_NO_VALUE) +
96 getFieldXml("labelSize", "") +
97 getFieldXml("labelStandType", "") +
98 getFieldXml("labelCount", "") +
99 "</ns2:movements_botgarden>" +
102 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(MovementClient.SERVICE_NAME);
103 resource.update(getServiceContext(), getResourceMap(), createUriInfo(), movementCsid, updatePayload);
106 private List<String> findLabelRequests() throws URISyntaxException {
107 List<String> csids = new ArrayList<String>();
108 MovementResource movementResource = (MovementResource) getResourceMap().get(MovementClient.SERVICE_NAME);
109 AbstractCommonList movementList = movementResource.getList(getServiceContext(), createLabelRequestSearchUriInfo());
111 for (AbstractCommonList.ListItem item : movementList.getListItem()) {
112 for (org.w3c.dom.Element element : item.getAny()) {
113 if (element.getTagName().equals("csid")) {
114 csids.add(element.getTextContent());
123 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
124 return createKeywordSearchUriInfo(MovementBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, MovementBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
125 MovementBotGardenConstants.LABEL_REQUESTED_YES_VALUE);