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();
35 setCompletionStatus(STATUS_MIN_PROGRESS);
38 String mode = getInvocationContext().getMode();
40 if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
41 String csid = getInvocationContext().getSingleCSID();
43 if (StringUtils.isEmpty(csid)) {
44 throw new Exception("Missing context csid");
47 setResults(formatTaxon(csid));
49 else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
50 setResults(formatTaxons(getInvocationContext().getListCSIDs().getCsid()));
52 else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
53 setResults(formatAllTaxons());
56 throw new Exception("Unsupported invocation mode: " + mode);
59 setCompletionStatus(STATUS_COMPLETE);
62 setCompletionStatus(STATUS_ERROR);
63 setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
67 public InvocationResults formatAllTaxons() throws URISyntaxException, DocumentException {
68 return formatTaxons(findAllTaxonRecords());
71 public InvocationResults formatTaxon(String taxonCsid) throws URISyntaxException, DocumentException {
72 return formatTaxons(Arrays.asList(taxonCsid));
75 public InvocationResults formatTaxons(List<String> taxonCsids) throws URISyntaxException, DocumentException {
76 InvocationResults results = new InvocationResults();
79 for (String taxonCsid : taxonCsids) {
80 formatDisplayNames(taxonCsid);
82 numAffected = numAffected + 1;
85 results.setNumAffected(numAffected);
86 results.setUserNote("Updated " + numAffected + " taxonomy " + (numAffected == 1 ? "record" : "records"));
91 private List<String> formatDisplayNames(String taxonCsid) throws URISyntaxException, DocumentException {
92 List<String> formattedDisplayNames = new ArrayList<String>();
94 PoxPayloadOut taxonPayload = findTaxonByCsid(taxonCsid);
95 String inAuthority = getFieldValue(taxonPayload, TaxonConstants.IN_AUTHORITY_SCHEMA_NAME, TaxonConstants.IN_AUTHORITY_FIELD_NAME);
97 String[] displayNamePathElements = TaxonConstants.DISPLAY_NAME_FIELD_NAME.split("/");
98 String termGroupListFieldName = displayNamePathElements[0];
99 String termGroupFieldName = displayNamePathElements[1];
100 String displayNameFieldName = displayNamePathElements[2];
102 String[] formattedDisplayNamePathElements = TaxonConstants.FORMATTED_DISPLAY_NAME_FIELD_NAME.split("/");
103 String formattedDisplayNameFieldName = formattedDisplayNamePathElements[2];
105 PayloadOutputPart part = taxonPayload.getPart(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME);
108 Element element = part.asElement();
109 Node termGroupListNode = element.selectSingleNode(termGroupListFieldName);
110 List<Node> termGroupNodes = termGroupListNode.selectNodes(termGroupFieldName);
112 for (Node termGroupNode : termGroupNodes) {
113 Element termGroupElement = (Element) termGroupNode;
114 Node displayNameNode = termGroupElement.selectSingleNode(displayNameFieldName);
115 String displayName = (displayNameNode == null) ? "" : displayNameNode.getText();
116 String formattedDisplayName = taxonFormatter.format(displayName);
118 Element formattedDisplayNameElement = (Element) termGroupElement.selectSingleNode(formattedDisplayNameFieldName);
120 if (formattedDisplayNameElement == null) {
121 formattedDisplayNameElement = termGroupElement.addElement(formattedDisplayNameFieldName);
124 formattedDisplayNameElement.setText(formattedDisplayName);
125 formattedDisplayNames.add(formattedDisplayName);
128 String updatePayload =
129 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
130 "<document name=\"taxon\">" +
131 "<ns2:taxon_common xmlns:ns2=\"http://collectionspace.org/services/taxonomy\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
132 termGroupListNode.asXML() +
133 "</ns2:taxon_common>" +
136 AuthorityResource<?, ?> resource = (AuthorityResource<?, ?>) getResourceMap().get(TaxonomyAuthorityClient.SERVICE_NAME);
137 resource.updateAuthorityItem(getResourceMap(), createUriInfo(), inAuthority, taxonCsid, updatePayload);
140 return formattedDisplayNames;
143 private List<String> findAllTaxonRecords() {
145 return Collections.emptyList();