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