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.apache.commons.lang.StringUtils;
11 import org.collectionspace.services.client.LoanoutClient;
12 import org.collectionspace.services.client.PoxPayloadOut;
13 import org.collectionspace.services.client.workflow.WorkflowClient;
14 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectBotGardenConstants;
15 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
16 import org.collectionspace.services.common.NuxeoBasedResource;
17 import org.collectionspace.services.common.api.TaxonFormatter;
18 import org.collectionspace.services.common.invocable.InvocationResults;
19 import org.collectionspace.services.jaxb.AbstractCommonList;
20 import org.collectionspace.services.loanout.LoanoutResource;
21 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
23 import org.dom4j.DocumentException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class FormatVoucherNameBatchJob extends AbstractBatchJob {
28 public static final String HYBRID_SEPARATOR = " x ";
30 final Logger logger = LoggerFactory.getLogger(FormatVoucherNameBatchJob.class);
32 private TaxonFormatter taxonFormatter;
34 public FormatVoucherNameBatchJob() {
35 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
36 this.taxonFormatter = new TaxonFormatter();
41 setCompletionStatus(STATUS_MIN_PROGRESS);
44 String mode = getInvocationContext().getMode();
46 if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
47 String csid = getInvocationContext().getSingleCSID();
49 if (StringUtils.isEmpty(csid)) {
50 throw new Exception("Missing context csid");
53 setResults(formatVoucherName(csid));
55 else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
56 setResults(formatVoucherNames(getInvocationContext().getListCSIDs().getCsid()));
58 else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
59 setResults(formatQueuedVoucherNames());
62 throw new Exception("Unsupported invocation mode: " + mode);
65 setCompletionStatus(STATUS_COMPLETE);
68 setCompletionStatus(STATUS_ERROR);
69 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
73 public InvocationResults formatQueuedVoucherNames() throws URISyntaxException, DocumentException {
74 return formatVoucherNames(findLabelRequests());
77 public InvocationResults formatVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
78 return formatVoucherNames(Arrays.asList(voucherCsid));
81 public InvocationResults formatVoucherNames(List<String> voucherCsids) throws URISyntaxException, DocumentException {
82 InvocationResults results = new InvocationResults();
84 List<String> formattedNames = new ArrayList<String>();
86 for (String voucherCsid : voucherCsids) {
87 VoucherName name = getVoucherName(voucherCsid);
88 String formattedName = formatVoucherName(name);
90 logger.debug("formattedName=" + formattedName);
92 setStyledName(voucherCsid, formattedName);
94 formattedNames.add(formattedName);
95 numAffected = numAffected + 1;
98 results.setNumAffected(numAffected);
99 results.setUserNote("Updated " + numAffected + " " + (numAffected == 1 ? "voucher" : "vouchers") + (numAffected == 1 ? ": " + formattedNames.get(0) : ""));
104 private List<String> findLabelRequests() throws URISyntaxException {
105 List<String> csids = new ArrayList<String>();
106 LoanoutResource loanoutResource = (LoanoutResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
107 AbstractCommonList loanoutList = loanoutResource.getList(createLabelRequestSearchUriInfo());
109 for (AbstractCommonList.ListItem item : loanoutList.getListItem()) {
110 for (org.w3c.dom.Element element : item.getAny()) {
111 if (element.getTagName().equals("csid")) {
112 csids.add(element.getTextContent());
121 public VoucherName getVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
122 VoucherName name = null;
123 List<String> collectionObjectCsids = findRelatedCollectionObjects(voucherCsid);
124 PoxPayloadOut collectionObjectPayload = null;
126 for (String candidateCsid : collectionObjectCsids) {
127 PoxPayloadOut candidatePayload = findCollectionObjectByCsid(candidateCsid);
128 String workflowState = getFieldValue(candidatePayload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
130 if (!workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
131 collectionObjectPayload = candidatePayload;
135 if (collectionObjectPayload != null) {
136 name = new VoucherName();
138 name.setName(getDisplayNameFromRefName(getFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME,
139 CollectionObjectBotGardenConstants.TAXON_FIELD_NAME)));
140 name.setHybrid(getBooleanFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_FLAG_SCHEMA_NAME,
141 CollectionObjectBotGardenConstants.HYBRID_FLAG_FIELD_NAME));
143 if (name.isHybrid()) {
144 List<String> hybridParents = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_PARENT_SCHEMA_NAME,
145 CollectionObjectBotGardenConstants.HYBRID_PARENT_FIELD_NAME);
146 List<String> hybridQualifiers = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_SCHEMA_NAME,
147 CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FIELD_NAME);
149 int femaleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FEMALE_VALUE);
150 int maleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_MALE_VALUE);
152 if (femaleIndex >= 0) {
153 name.setFemaleParentName(getDisplayNameFromRefName(hybridParents.get(femaleIndex)));
156 if (maleIndex >= 0) {
157 name.setMaleParentName(getDisplayNameFromRefName(hybridParents.get(maleIndex)));
165 public String formatVoucherName(VoucherName name) {
166 String formattedName = "";
169 if (name.isHybrid()) {
170 if (name.getFemaleParentName() != null) {
171 formattedName += taxonFormatter.format(name.getFemaleParentName());
174 formattedName += HYBRID_SEPARATOR;
176 if (name.getMaleParentName() != null) {
177 formattedName += taxonFormatter.format(name.getMaleParentName());
181 if (name.getName() != null) {
182 formattedName = taxonFormatter.format(name.getName());
187 return formattedName;
190 private void setStyledName(String loanoutCsid, String styledName) throws URISyntaxException {
191 final String updatePayload =
192 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
193 "<document name=\"loansout\">" +
194 "<ns2:loansout_botgarden xmlns:ns2=\"http://collectionspace.org/services/loanout/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
195 getFieldXml("styledName", styledName) +
196 "</ns2:loansout_botgarden>" +
199 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
200 resource.update(getResourceMap(), createUriInfo(), loanoutCsid, updatePayload);
203 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
204 return createKeywordSearchUriInfo(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
205 LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE);
208 public class VoucherName {
209 private boolean isHybrid = false;
211 private String femaleParentName;
212 private String maleParentName;
214 public boolean isHybrid() {
218 public void setHybrid(boolean isHybrid) {
219 this.isHybrid = isHybrid;
222 public String getName() {
226 public void setName(String name) {
230 public String getFemaleParentName() {
231 return femaleParentName;
234 public void setFemaleParentName(String femaleParentName) {
235 this.femaleParentName = femaleParentName;
238 public String getMaleParentName() {
239 return maleParentName;
242 public void setMaleParentName(String maleParentName) {
243 this.maleParentName = maleParentName;