]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
948a29e1d7ab6e8b64f419baaf798d8e7d3b9294
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.batch.nuxeo;
2
3 import java.net.URISyntaxException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Collections;
7 import java.util.List;
8
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;
22
23 public class FormatTaxonBatchJob extends AbstractBatchJob {
24         final Logger logger = LoggerFactory.getLogger(FormatTaxonBatchJob.class);
25
26         private TaxonFormatter taxonFormatter;
27
28         public FormatTaxonBatchJob() {
29                 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
30                 this.taxonFormatter = new TaxonFormatter();
31         }
32
33         @Override
34         public void run() {
35                 setCompletionStatus(STATUS_MIN_PROGRESS);
36
37                 try {
38                         String mode = getInvocationContext().getMode();
39
40                         if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
41                                 String csid = getInvocationContext().getSingleCSID();
42
43                                 if (StringUtils.isEmpty(csid)) {
44                                         throw new Exception("Missing context csid");
45                                 }
46
47                                 setResults(formatTaxon(csid));
48                         }
49                         else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
50                                 setResults(formatTaxons(getInvocationContext().getListCSIDs().getCsid()));
51                         }
52                         else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
53                                 setResults(formatAllTaxons());
54                         }
55                         else {
56                                 throw new Exception("Unsupported invocation mode: " + mode);
57                         }
58
59                         setCompletionStatus(STATUS_COMPLETE);
60                 }
61                 catch(Exception e) {
62                         setCompletionStatus(STATUS_ERROR);
63                         setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
64                 }
65         }
66
67         public InvocationResults formatAllTaxons() throws URISyntaxException, DocumentException {
68                 return formatTaxons(findAllTaxonRecords());
69         }
70
71         public InvocationResults formatTaxon(String taxonCsid) throws URISyntaxException, DocumentException {
72                 return formatTaxons(Arrays.asList(taxonCsid));
73         }
74
75         public InvocationResults formatTaxons(List<String> taxonCsids) throws URISyntaxException, DocumentException {
76                 InvocationResults results = new InvocationResults();
77                 int numAffected = 0;
78
79                 for (String taxonCsid : taxonCsids) {
80                         formatDisplayNames(taxonCsid);
81
82                         numAffected = numAffected + 1;
83                 }
84
85                 results.setNumAffected(numAffected);
86                 results.setUserNote("Updated " + numAffected + " taxonomy " + (numAffected == 1 ? "record" : "records"));
87
88                 return results;
89         }
90
91         private List<String> formatDisplayNames(String taxonCsid) throws URISyntaxException, DocumentException {
92                 List<String> formattedDisplayNames = new ArrayList<String>();
93
94                 PoxPayloadOut taxonPayload = findTaxonByCsid(taxonCsid);
95                 String inAuthority = getFieldValue(taxonPayload, TaxonConstants.IN_AUTHORITY_SCHEMA_NAME, TaxonConstants.IN_AUTHORITY_FIELD_NAME);
96
97                 String[] displayNamePathElements = TaxonConstants.DISPLAY_NAME_FIELD_NAME.split("/");
98                 String termGroupListFieldName = displayNamePathElements[0];
99                 String termGroupFieldName = displayNamePathElements[1];
100                 String displayNameFieldName = displayNamePathElements[2];
101
102                 String[] formattedDisplayNamePathElements = TaxonConstants.FORMATTED_DISPLAY_NAME_FIELD_NAME.split("/");
103                 String formattedDisplayNameFieldName = formattedDisplayNamePathElements[2];
104
105                 PayloadOutputPart part = taxonPayload.getPart(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME);
106
107                 if (part != null) {
108                         Element element = part.asElement();
109                         Node termGroupListNode = element.selectSingleNode(termGroupListFieldName);
110                         List<Node> termGroupNodes = termGroupListNode.selectNodes(termGroupFieldName);
111
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);
117
118                                 Element formattedDisplayNameElement = (Element) termGroupElement.selectSingleNode(formattedDisplayNameFieldName);
119
120                                 if (formattedDisplayNameElement == null) {
121                                         formattedDisplayNameElement = termGroupElement.addElement(formattedDisplayNameFieldName);
122                                 }
123
124                                 formattedDisplayNameElement.setText(formattedDisplayName);
125                                 formattedDisplayNames.add(formattedDisplayName);
126                         }
127
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>" +
134                                 "</document>";
135
136                         AuthorityResource<?, ?> resource = (AuthorityResource<?, ?>) getResourceMap().get(TaxonomyAuthorityClient.SERVICE_NAME);
137                         resource.updateAuthorityItem(getResourceMap(), createUriInfo(), inAuthority, taxonCsid, updatePayload);
138                 }
139
140                 return formattedDisplayNames;
141         }
142
143         private List<String> findAllTaxonRecords() {
144                 // TODO
145                 return Collections.emptyList();
146         }
147 }