]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
eb7be0a42d14eff033ac9c06b9ce142861d232de
[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.List;
7
8 import javax.ws.rs.core.UriInfo;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.collectionspace.services.client.LoanoutClient;
12 import org.collectionspace.services.client.PoxPayloadOut;
13 import org.collectionspace.services.client.workflow.WorkflowClient;
14 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectBotGardenConstants;
15 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
16 import org.collectionspace.services.common.NuxeoBasedResource;
17 import org.collectionspace.services.common.api.TaxonFormatter;
18 import org.collectionspace.services.common.invocable.InvocationResults;
19 import org.collectionspace.services.jaxb.AbstractCommonList;
20 import org.collectionspace.services.loanout.LoanoutResource;
21 import org.collectionspace.services.loanout.nuxeo.LoanoutBotGardenConstants;
22
23 import org.dom4j.DocumentException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class FormatVoucherNameBatchJob extends AbstractBatchJob {
28         public static final String HYBRID_SEPARATOR = " x ";
29
30         final Logger logger = LoggerFactory.getLogger(FormatVoucherNameBatchJob.class);
31
32         private TaxonFormatter taxonFormatter;
33         
34         public FormatVoucherNameBatchJob() {
35                 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST, INVOCATION_MODE_NO_CONTEXT));
36                 this.taxonFormatter = new TaxonFormatter();
37         }
38
39         @Override
40         public void run() {
41                 setCompletionStatus(STATUS_MIN_PROGRESS);
42
43                 try {
44                         String mode = getInvocationContext().getMode();
45
46                         if (mode.equalsIgnoreCase(INVOCATION_MODE_SINGLE)) {
47                                 String csid = getInvocationContext().getSingleCSID();
48
49                                 if (StringUtils.isEmpty(csid)) {
50                                         throw new Exception("Missing context csid");
51                                 }
52
53                                 setResults(formatVoucherName(csid));
54                         }
55                         else if (mode.equalsIgnoreCase(INVOCATION_MODE_LIST)) {
56                                 setResults(formatVoucherNames(getInvocationContext().getListCSIDs().getCsid()));
57                         }
58                         else if (mode.equalsIgnoreCase(INVOCATION_MODE_NO_CONTEXT)) {
59                                 setResults(formatQueuedVoucherNames());
60                         }
61                         else {
62                                 throw new Exception("Unsupported invocation mode: " + mode);
63                         }
64
65                         setCompletionStatus(STATUS_COMPLETE);
66                 }
67                 catch(Exception e) {
68                         setCompletionStatus(STATUS_ERROR);
69                         setErrorInfo(new InvocationError(INT_ERROR_STATUS, e.getMessage()));
70                 }
71         }
72         
73         public InvocationResults formatQueuedVoucherNames() throws URISyntaxException, DocumentException {
74                 return formatVoucherNames(findLabelRequests());
75         }       
76         
77         public InvocationResults formatVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
78                 return formatVoucherNames(Arrays.asList(voucherCsid));
79         }
80         
81         public InvocationResults formatVoucherNames(List<String> voucherCsids) throws URISyntaxException, DocumentException {
82                 InvocationResults results = new InvocationResults();
83                 int numAffected = 0;
84                 List<String> formattedNames = new ArrayList<String>();
85                 
86                 for (String voucherCsid : voucherCsids) {
87                         VoucherName name = getVoucherName(voucherCsid);
88                         String formattedName = formatVoucherName(name);
89                         
90                         logger.debug("formattedName=" + formattedName);
91                         
92                         setStyledName(voucherCsid, formattedName);
93                         
94                         formattedNames.add(formattedName);
95                         numAffected = numAffected + 1;
96                 }
97                 
98                 results.setNumAffected(numAffected);
99                 results.setUserNote("Updated " + numAffected + " " + (numAffected == 1 ? "voucher" : "vouchers") + (numAffected == 1 ? ": " + formattedNames.get(0) : ""));
100                 
101                 return results;
102         }
103         
104         private List<String> findLabelRequests() throws URISyntaxException {
105                 List<String> csids = new ArrayList<String>();
106                 LoanoutResource loanoutResource = (LoanoutResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
107                 AbstractCommonList loanoutList = loanoutResource.getList(createLabelRequestSearchUriInfo());
108
109                 for (AbstractCommonList.ListItem item : loanoutList.getListItem()) {
110                         for (org.w3c.dom.Element element : item.getAny()) {
111                                 if (element.getTagName().equals("csid")) {
112                                         csids.add(element.getTextContent());
113                                         break;
114                                 }
115                         }
116                 }
117
118                 return csids;
119         }
120         
121         public VoucherName getVoucherName(String voucherCsid) throws URISyntaxException, DocumentException {
122                 VoucherName name = null;
123                 List<String> collectionObjectCsids = findRelatedCollectionObjects(voucherCsid);
124                 PoxPayloadOut collectionObjectPayload = null;
125                 
126                 for (String candidateCsid : collectionObjectCsids) {
127                         PoxPayloadOut candidatePayload = findCollectionObjectByCsid(candidateCsid);
128                         String workflowState = getFieldValue(candidatePayload, CollectionObjectConstants.WORKFLOW_STATE_SCHEMA_NAME, CollectionObjectConstants.WORKFLOW_STATE_FIELD_NAME);
129                         
130                         if (!workflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
131                                 collectionObjectPayload = candidatePayload;
132                         }
133                 }
134                 
135                 if (collectionObjectPayload != null) {
136                         name = new VoucherName();
137                         
138                         name.setName(getDisplayNameFromRefName(getFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME,
139                                         CollectionObjectBotGardenConstants.TAXON_FIELD_NAME)));
140                         name.setHybrid(getBooleanFieldValue(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_FLAG_SCHEMA_NAME, 
141                                         CollectionObjectBotGardenConstants.HYBRID_FLAG_FIELD_NAME));
142
143                         if (name.isHybrid()) {
144                                 List<String> hybridParents = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_PARENT_SCHEMA_NAME, 
145                                                 CollectionObjectBotGardenConstants.HYBRID_PARENT_FIELD_NAME);
146                                 List<String> hybridQualifiers = this.getFieldValues(collectionObjectPayload, CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_SCHEMA_NAME, 
147                                                 CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FIELD_NAME);
148
149                                 int femaleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_FEMALE_VALUE);
150                                 int maleIndex = hybridQualifiers.indexOf(CollectionObjectBotGardenConstants.HYBRID_QUALIFIER_MALE_VALUE);
151                                 
152                                 if (femaleIndex >= 0) {
153                                         name.setFemaleParentName(getDisplayNameFromRefName(hybridParents.get(femaleIndex)));
154                                 }
155                                 
156                                 if (maleIndex >= 0) {
157                                         name.setMaleParentName(getDisplayNameFromRefName(hybridParents.get(maleIndex)));
158                                 }
159                         }
160                 }
161                 
162                 return name;
163         }
164                 
165         public String formatVoucherName(VoucherName name) {             
166                 String formattedName = "";
167                 
168                 if (name != null) {
169                         if (name.isHybrid()) {
170                                 if (name.getFemaleParentName() != null) {
171                                         formattedName += taxonFormatter.format(name.getFemaleParentName());
172                                 }
173                                 
174                                 formattedName += HYBRID_SEPARATOR;
175                                 
176                                 if (name.getMaleParentName() != null) {
177                                         formattedName += taxonFormatter.format(name.getMaleParentName());
178                                 }
179                         }
180                         else {
181                                 if (name.getName() != null) {
182                                         formattedName = taxonFormatter.format(name.getName());
183                                 }
184                         }
185                 }
186                 
187                 return formattedName;
188         }
189
190         private void setStyledName(String loanoutCsid, String styledName) throws URISyntaxException {
191                 final String updatePayload = 
192                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
193                         "<document name=\"loansout\">" +
194                                 "<ns2:loansout_botgarden xmlns:ns2=\"http://collectionspace.org/services/loanout/local/botgarden\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
195                                         getFieldXml("styledName", styledName) +
196                                 "</ns2:loansout_botgarden>" +
197                         "</document>";
198                         
199                 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(LoanoutClient.SERVICE_NAME);
200                 resource.update(getResourceMap(), createUriInfo(), loanoutCsid, updatePayload);         
201         }
202         
203         private UriInfo createLabelRequestSearchUriInfo() throws URISyntaxException {
204                 return createKeywordSearchUriInfo(LoanoutBotGardenConstants.LABEL_REQUESTED_SCHEMA_NAME, LoanoutBotGardenConstants.LABEL_REQUESTED_FIELD_NAME, 
205                                 LoanoutBotGardenConstants.LABEL_REQUESTED_YES_VALUE);           
206         }
207         
208         public class VoucherName {              
209                 private boolean isHybrid = false;
210                 private String name;
211                 private String femaleParentName;
212                 private String maleParentName;
213                 
214                 public boolean isHybrid() {
215                         return isHybrid;
216                 }
217                 
218                 public void setHybrid(boolean isHybrid) {
219                         this.isHybrid = isHybrid;
220                 }
221                 
222                 public String getName() {
223                         return name;
224                 }
225                 
226                 public void setName(String name) {
227                         this.name = name;
228                 }
229
230                 public String getFemaleParentName() {
231                         return femaleParentName;
232                 }
233                 
234                 public void setFemaleParentName(String femaleParentName) {
235                         this.femaleParentName = femaleParentName;
236                 }
237                                 
238                 public String getMaleParentName() {
239                         return maleParentName;
240                 }
241                 
242                 public void setMaleParentName(String maleParentName) {
243                         this.maleParentName = maleParentName;
244                 }
245         }
246 }