1 package org.collectionspace.services.batch.nuxeo;
3 import java.net.URISyntaxException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
9 import org.apache.commons.lang.StringUtils;
10 import org.collectionspace.services.client.PayloadOutputPart;
11 import org.collectionspace.services.client.PoxPayloadOut;
12 import org.collectionspace.services.client.TaxonomyAuthorityClient;
13 import org.collectionspace.services.common.api.TaxonFormatter;
14 import org.collectionspace.services.common.invocable.InvocationResults;
15 import org.collectionspace.services.common.vocabulary.AuthorityResource;
16 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
17 import org.dom4j.DocumentException;
18 import org.dom4j.Element;
19 import org.dom4j.Node;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class FormatTaxonBatchJob extends AbstractBatchJob {
24 final Logger logger = LoggerFactory.getLogger(FormatTaxonBatchJob.class);
26 private TaxonFormatter taxonFormatter;
28 public FormatTaxonBatchJob() {
29 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
30 this.taxonFormatter = new TaxonFormatter();
34 setCompletionStatus(STATUS_MIN_PROGRESS);
37 String mode = getInvocationContext().getMode();
39 if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
40 String csid = getInvocationContext().getSingleCSID();
42 if (StringUtils.isEmpty(csid)) {
43 throw new Exception("Missing context csid");
46 setResults(formatTaxon(csid));
48 else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
49 setResults(formatTaxons(getInvocationContext().getListCSIDs().getCsid()));
51 else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
52 setResults(formatAllTaxons());
55 throw new Exception("Unsupported invocation mode: " + mode);
58 setCompletionStatus(STATUS_COMPLETE);
61 setCompletionStatus(STATUS_ERROR);
62 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
66 public InvocationResults formatAllTaxons() throws URISyntaxException, DocumentException {
67 return formatTaxons(findAllTaxonRecords());
70 public InvocationResults formatTaxon(String taxonCsid) throws URISyntaxException, DocumentException {
71 return formatTaxons(Arrays.asList(taxonCsid));
74 public InvocationResults formatTaxons(List<String> taxonCsids) throws URISyntaxException, DocumentException {
75 InvocationResults results = new InvocationResults();
78 for (String taxonCsid : taxonCsids) {
79 formatDisplayNames(taxonCsid);
81 numAffected = numAffected + 1;
84 results.setNumAffected(numAffected);
85 results.setUserNote("Updated " + numAffected + " taxonomy " + (numAffected == 1 ? "record" : "records"));
90 private List<String> formatDisplayNames(String taxonCsid) throws URISyntaxException, DocumentException {
91 List<String> formattedDisplayNames = new ArrayList<String>();
93 PoxPayloadOut taxonPayload = findTaxonByCsid(taxonCsid);
94 String inAuthority = getFieldValue(taxonPayload, TaxonConstants.IN_AUTHORITY_SCHEMA_NAME, TaxonConstants.IN_AUTHORITY_FIELD_NAME);
96 String[] displayNamePathElements = TaxonConstants.DISPLAY_NAME_FIELD_NAME.split("/");
97 String termGroupListFieldName = displayNamePathElements[0];
98 String termGroupFieldName = displayNamePathElements[1];
99 String displayNameFieldName = displayNamePathElements[2];
101 String[] formattedDisplayNamePathElements = TaxonConstants.FORMATTED_DISPLAY_NAME_FIELD_NAME.split("/");
102 String formattedDisplayNameFieldName = formattedDisplayNamePathElements[2];
104 PayloadOutputPart part = taxonPayload.getPart(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME);
107 Element element = part.asElement();
108 Node termGroupListNode = element.selectSingleNode(termGroupListFieldName);
109 List<Element> termGroupElements = termGroupListNode.selectNodes(termGroupFieldName);
111 for (Element termGroupElement : termGroupElements) {
112 Node displayNameNode = termGroupElement.selectSingleNode(displayNameFieldName);
113 String displayName = (displayNameNode == null) ? "" : displayNameNode.getText();
114 String formattedDisplayName = taxonFormatter.format(displayName);
116 Element formattedDisplayNameElement = (Element) termGroupElement.selectSingleNode(formattedDisplayNameFieldName);
118 if (formattedDisplayNameElement == null) {
119 formattedDisplayNameElement = termGroupElement.addElement(formattedDisplayNameFieldName);
122 formattedDisplayNameElement.setText(formattedDisplayName);
123 formattedDisplayNames.add(formattedDisplayName);
126 String updatePayload =
127 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
128 "<document name=\"taxon\">" +
129 "<ns2:taxon_common xmlns:ns2=\"http://collectionspace.org/services/taxonomy\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
130 termGroupListNode.asXML() +
131 "</ns2:taxon_common>" +
134 AuthorityResource<?, ?> resource = (AuthorityResource<?, ?>) getResourceMap().get(TaxonomyAuthorityClient.SERVICE_NAME);
135 resource.updateAuthorityItem(getResourceMap(), createUriInfo(), inAuthority, taxonCsid, updatePayload);
138 return formattedDisplayNames;
141 private List<String> findAllTaxonRecords() {
143 return Collections.emptyList();