]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b745018f128b27a0c241718b8636f842b4cce3d7
[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<Element> termGroupElements = termGroupListNode.selectNodes(termGroupFieldName);
111                         
112                         for (Element termGroupElement : termGroupElements) {
113                                 Node displayNameNode = termGroupElement.selectSingleNode(displayNameFieldName);
114                                 String displayName = (displayNameNode == null) ? "" : displayNameNode.getText();
115                                 String formattedDisplayName = taxonFormatter.format(displayName);
116
117                                 Element formattedDisplayNameElement = (Element) termGroupElement.selectSingleNode(formattedDisplayNameFieldName);
118                                 
119                                 if (formattedDisplayNameElement == null) {
120                                         formattedDisplayNameElement = termGroupElement.addElement(formattedDisplayNameFieldName);
121                                 }
122                                 
123                                 formattedDisplayNameElement.setText(formattedDisplayName);
124                                 formattedDisplayNames.add(formattedDisplayName);
125                         }
126                         
127                         String updatePayload = 
128                                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
129                                 "<document name=\"taxon\">" +
130                                         "<ns2:taxon_common xmlns:ns2=\"http://collectionspace.org/services/taxonomy\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
131                                                 termGroupListNode.asXML() +
132                                         "</ns2:taxon_common>" +
133                                 "</document>";
134
135                         AuthorityResource<?, ?> resource = (AuthorityResource<?, ?>) getResourceMap().get(TaxonomyAuthorityClient.SERVICE_NAME);
136                         resource.updateAuthorityItem(getResourceMap(), createUriInfo(), inAuthority, taxonCsid, updatePayload);
137                 }               
138                 
139                 return formattedDisplayNames;
140         }
141         
142         private List<String> findAllTaxonRecords() {
143                 // TODO
144                 return Collections.emptyList();
145         }
146 }