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();
40 setCompletionStatus(STATUS_MIN_PROGRESS);
43 String mode = getInvocationContext().getMode();
45 if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
46 String csid = getInvocationContext().getSingleCSID();
48 if (StringUtils.isEmpty(csid)) {
49 throw new Exception("Missing context csid");
52 setResults(formatVoucherName(csid));
54 else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
55 setResults(formatVoucherNames(getInvocationContext().getListCSIDs().getCsid()));
57 else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
58 setResults(formatQueuedVoucherNames());
61 throw new Exception("Unsupported invocation mode: " + mode);
64 setCompletionStatus(STATUS_COMPLETE);
67 setCompletionStatus(STATUS_ERROR);
68 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
72 public InvocationResults formatQueuedVoucherNames() throws URISyntaxException, DocumentException {
73 return formatVoucherNames(findLabelRequests());
76 public InvocationResults formatVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
77 return formatVoucherNames(Arrays.asList(voucherCsid));
80 public InvocationResults formatVoucherNames(List<String> voucherCsids) throws URISyntaxException, DocumentException {
81 InvocationResults results = new InvocationResults();
83 List<String> formattedNames = new ArrayList<String>();
85 for (String voucherCsid : voucherCsids) {
86 VoucherName name = getVoucherName(voucherCsid);
87 String formattedName = formatVoucherName(name);
89 logger.debug("formattedName=" + formattedName);
91 setStyledName(voucherCsid, formattedName);
93 formattedNames.add(formattedName);
94 numAffected = numAffected + 1;
97 results.setNumAffected(numAffected);
98 results.setUserNote("Updated " + numAffected + " " + (numAffected == 1 ? "voucher" : "vouchers") + (numAffected == 1 ? ": " + formattedNames.get(0) : ""));
103 private List<String> findLabelRequests() throws URISyntaxException {
104 List<String> csids = new ArrayList<String>();
105 LoanoutResource loanoutResource = (LoanoutResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
106 AbstractCommonList loanoutList = loanoutResource.getList(createLabelRequestSearchUriInfo());
108 for (AbstractCommonList.ListItem item : loanoutList.getListItem()) {
109 for (org.w3c.dom.Element element : item.getAny()) {
110 if (element.getTagName().equals("csid")) {
111 csids.add(element.getTextContent());
120 public VoucherName getVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
121 VoucherName name = null;
122 List<String> collectionObjectCsids = findRelatedCollectionObjects(voucherCsid);
123 PoxPayloadOut collectionObjectPayload = null;
125 for (String candidateCsid : collectionObjectCsids) {
126 PoxPayloadOut candidatePayload = findCollectionObjectByCsid(candidateCsid);
127 String workflowState = getFieldValue(candidatePayload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
129 if (!workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
130 collectionObjectPayload = candidatePayload;
134 if (collectionObjectPayload != null) {
135 name = new VoucherName();
137 name.setName(getDisplayNameFromRefName(getFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME,
138 CollectionObjectBotGardenConstants.TAXON_FIELD_NAME)));
139 name.setHybrid(getBooleanFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_FLAG_SCHEMA_NAME,
140 CollectionObjectBotGardenConstants.HYBRID_FLAG_FIELD_NAME));
142 if (name.isHybrid()) {
143 List<String> hybridParents = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_PARENT_SCHEMA_NAME,
144 CollectionObjectBotGardenConstants.HYBRID_PARENT_FIELD_NAME);
145 List<String> hybridQualifiers = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_SCHEMA_NAME,
146 CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FIELD_NAME);
148 int femaleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FEMALE_VALUE);
149 int maleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_MALE_VALUE);
151 if (femaleIndex >= 0) {
152 name.setFemaleParentName(getDisplayNameFromRefName(hybridParents.get(femaleIndex)));
155 if (maleIndex >= 0) {
156 name.setMaleParentName(getDisplayNameFromRefName(hybridParents.get(maleIndex)));
164 public String formatVoucherName(VoucherName name) {
165 String formattedName = "";
168 if (name.isHybrid()) {
169 if (name.getFemaleParentName() != null) {
170 formattedName += taxonFormatter.format(name.getFemaleParentName());
173 formattedName += HYBRID_SEPARATOR;
175 if (name.getMaleParentName() != null) {
176 formattedName += taxonFormatter.format(name.getMaleParentName());
180 if (name.getName() != null) {
181 formattedName = taxonFormatter.format(name.getName());
186 return formattedName;
189 private void setStyledName(String loanoutCsid, String styledName) throws URISyntaxException {
190 final String updatePayload =
191 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
192 "<document name=\"loansout\">" +
193 "<ns2:loansout_botgarden xmlns:ns2=\"http://collectionspace.org/services/loanout/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
194 getFieldXml("styledName", styledName) +
195 "</ns2:loansout_botgarden>" +
198 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
199 resource.update(getResourceMap(), createUriInfo(), loanoutCsid, updatePayload);
202 private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
203 return createKeywordSearchUriInfo(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME,
204 LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE);
207 public class VoucherName {
208 private boolean isHybrid = false;
210 private String femaleParentName;
211 private String maleParentName;
213 public boolean isHybrid() {
217 public void setHybrid(boolean isHybrid) {
218 this.isHybrid = isHybrid;
221 public String getName() {
225 public void setName(String name) {
229 public String getFemaleParentName() {
230 return femaleParentName;
233 public void setFemaleParentName(String femaleParentName) {
234 this.femaleParentName = femaleParentName;
237 public String getMaleParentName() {
238 return maleParentName;
241 public void setMaleParentName(String maleParentName) {
242 this.maleParentName = maleParentName;