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