View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.common.util.PropertyUtils;
21  import org.kuali.ole.module.purap.PurapConstants;
22  import org.kuali.ole.select.OleSelectConstant;
23  import org.kuali.ole.select.bo.OLEInvoiceSearchDocument;
24  import org.kuali.ole.select.businessobject.DocInfoBean;
25  import org.kuali.ole.select.document.OleInvoiceDocument;
26  import org.kuali.ole.select.lookup.DocData;
27  import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.rice.core.api.util.RiceKeyConstants;
30  import org.kuali.rice.kew.api.document.Document;
31  import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
32  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
33  import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
34  import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
35  import org.kuali.rice.kew.api.exception.WorkflowException;
36  import org.kuali.rice.kew.exception.WorkflowServiceError;
37  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
38  import org.kuali.rice.kew.service.KEWServiceLocator;
39  import org.kuali.rice.kim.api.identity.Person;
40  import org.kuali.rice.kim.api.identity.PersonService;
41  import org.kuali.rice.krad.bo.DocumentHeader;
42  import org.kuali.rice.krad.datadictionary.DocumentEntry;
43  import org.kuali.rice.krad.service.DataDictionaryService;
44  import org.kuali.rice.krad.service.DocumentHeaderService;
45  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
46  import org.kuali.rice.krad.util.GlobalVariables;
47  
48  import java.lang.reflect.Method;
49  import java.text.DateFormat;
50  import java.text.SimpleDateFormat;
51  import java.util.*;
52  
53  public class OLEInvoiceSearchService {
54  
55      public static final Map<String, String> INVOICE_FIELDS = getInvoiceNames();
56      public static final List<String> INVOICE_FIELD_RESULT =getResultNames();
57      public static List<String> INVOICE_SEARCH_CRITERIA_LIST=new ArrayList<String>();
58      public List<OLEInvoiceSearchDocument> searchResults(Map<String, String> searchCriteria) throws Exception {
59          List<OLEInvoiceSearchDocument> resultRows = new ArrayList<OLEInvoiceSearchDocument>();
60          Map<String, List<String>> searchCriterias = new HashMap<String, List<String>>();
61          INVOICE_SEARCH_CRITERIA_LIST=buildSearchCriteriaList(searchCriteria);
62          try {
63              searchCriterias.putAll(getSearchCriteria(searchCriteria));
64              resultRows.addAll(populateCriteriaAndSearch(PurapConstants.PurapDocTypeCodes.INVOICE_DOCUMENT.toUpperCase(), searchCriterias));
65          } catch (Exception ex) {
66              throw new RuntimeException(ex);
67          }
68          return resultRows;
69      }
70  
71      public Map<String, List<String>> getSearchCriteria(Map<String, String> itemFields) throws Exception {
72          Map<String, List<String>> searchCriteriaMap = new HashMap<String, List<String>>();
73          for (Map.Entry<String, String> entry : itemFields.entrySet()) {
74              List<String> list = new ArrayList<String>();
75              if (entry.getKey().equalsIgnoreCase(OleSelectConstant.InvoiceSearch.PURAP_ID)) {
76                  StringTokenizer stringTokenizer = new StringTokenizer(entry.getValue(), ",");
77                  while (stringTokenizer.hasMoreElements()) {
78                      list.add(stringTokenizer.nextElement().toString());
79                  }
80                  searchCriteriaMap.put(entry.getKey().toString(), list);
81              } else {
82                  searchCriteriaMap.put(entry.getKey().toString(), new ArrayList<String>(Arrays.asList(entry.getValue().toString())));
83              }
84  
85  
86          }
87          return searchCriteriaMap;
88      }
89  
90      public List<OLEInvoiceSearchDocument> populateCriteriaAndSearch(String docTypeFullName, Map<String, List<String>> fixedParameters)
91              throws WorkflowException, Exception {
92          DocumentSearchCriteria.Builder docSearchCriteria = DocumentSearchCriteria.Builder.create();
93          DateTime dateTime = new DateTime(new Date());
94          docSearchCriteria.setDateCreatedTo((dateTime));
95  
96          docSearchCriteria.setDocumentTypeName(docTypeFullName);
97  
98          List<OLEInvoiceSearchDocument> resultRows = new ArrayList<OLEInvoiceSearchDocument>();
99          resultRows = performDocumentSearch(docSearchCriteria.build(), fixedParameters);
100 
101         return resultRows;
102     }
103 
104     public List<OLEInvoiceSearchDocument> performDocumentSearch(DocumentSearchCriteria docSearchCriteria, Map<String, List<String>> fixedParameters) {
105        /* List<String> strings=new ArrayList<String>();
106         strings.add("1");
107         fixedParameters.put(OleSelectConstant.InvoiceSearch.INV_NUMBER,strings);*/
108         DocumentSearchCriteria docSearchCriteriaDTO = addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder.create(docSearchCriteria), fixedParameters);
109         List result = new ArrayList();
110         List<OLEInvoiceSearchDocument> finalResult = new ArrayList<OLEInvoiceSearchDocument>();
111         DocumentSearchResults components = null;
112         try {
113             components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(),
114                     docSearchCriteriaDTO);
115 
116             List<DocumentSearchResult> docSearchResults = components.getSearchResults();
117             if (!fixedParameters.containsKey("displayType")) {
118                 finalResult = getFinalDocumentTypeResult(docSearchResults);
119             }
120         } catch (WorkflowServiceErrorException wsee) {
121             for (WorkflowServiceError workflowServiceError : (List<WorkflowServiceError>) wsee.getServiceErrors()) {
122                 if (workflowServiceError.getMessageMap() != null && workflowServiceError.getMessageMap().hasErrors()) {
123                     GlobalVariables.getMessageMap().merge(workflowServiceError.getMessageMap());
124                 } else {
125                     GlobalVariables.getMessageMap().putError(workflowServiceError.getMessage(), RiceKeyConstants.ERROR_CUSTOM,
126                             workflowServiceError.getMessage());
127                 }
128             }
129             ;
130         }
131         return finalResult;
132     }
133 
134     private DocumentSearchCriteria addDocumentAttributesToCriteria(DocumentSearchCriteria.Builder criteria, Map<String, List<String>> propertyFields) {
135         Map<String, List<String>> attributes = new HashMap<String, List<String>>();
136         if (criteria != null) {
137             if (!propertyFields.isEmpty()) {
138                 for (String propertyField : propertyFields.keySet()) {
139                     if (propertyFields.get(propertyField) != null) {
140                         attributes.put(propertyField, propertyFields.get(propertyField));
141                     }
142                 }
143             }
144         }
145         criteria.setDocumentAttributeValues(attributes);
146         return criteria.build();
147     }
148 
149     private List<OLEInvoiceSearchDocument> getFinalDocumentTypeResult(List<DocumentSearchResult> componentResults) {
150         List<OLEInvoiceSearchDocument> docResult = new ArrayList<OLEInvoiceSearchDocument>();
151         OLEInvoiceSearchDocument oleInvoiceDocument;
152         if (!componentResults.isEmpty()) {
153             for (DocumentSearchResult searchResult : componentResults) {
154                 if (!searchResult.getDocument().getApplicationDocumentStatus().equalsIgnoreCase("exception")) {
155                     oleInvoiceDocument = convertToOleInvoiceDocument(searchResult);
156                     oleInvoiceDocument.setDocumentNumber(searchResult.getDocument().getDocumentId());
157                     oleInvoiceDocument.setDocumentStatus(searchResult.getDocument().getApplicationDocumentStatus());
158                     Person principalPerson = SpringContext.getBean(PersonService.class).getPerson(GlobalVariables.getUserSession().getPerson().getPrincipalId());
159                     DocumentHeader documentHeader = SpringContext.getBean(DocumentHeaderService.class).getDocumentHeaderById(searchResult.getDocument().getDocumentId());
160                     try {
161                         if (documentHeader != null) {
162                             documentHeader.setWorkflowDocument(KRADServiceLocatorWeb.getWorkflowDocumentService().loadWorkflowDocument(searchResult.getDocument().getDocumentId(),
163                                     principalPerson));
164                             if (documentHeader.getWorkflowDocument() != null) {
165                                 oleInvoiceDocument.setWorkFlowDocumentStatus(documentHeader.getWorkflowDocument().getDocument().getStatus().getLabel());
166                             }
167                         }
168 
169                     }
170                     catch (WorkflowException e) {
171                        throw new RuntimeException(e);
172                     }
173 
174 
175                     if (oleInvoiceDocument != null) {
176                         docResult.add(oleInvoiceDocument);
177                     }
178                 }
179             }
180         }
181         return docResult;
182 
183     }
184     public List<String> buildSearchCriteriaList(Map<String, String> searchCriteria){
185         List<String> buildSearchCriteriaList=new ArrayList<String>();
186         for(Map.Entry<String,String> entry:searchCriteria.entrySet()){
187             if(StringUtils.isNotEmpty(entry.getValue())){
188                  buildSearchCriteriaList.add(entry.getKey());
189             }
190         }
191         return buildSearchCriteriaList;
192     }
193 
194     public OLEInvoiceSearchDocument convertToOleInvoiceDocument(DocumentSearchResult documentSearchResult) {
195         OLEInvoiceSearchDocument invoiceDocument = new OLEInvoiceSearchDocument();
196         Document document = documentSearchResult.getDocument();
197         List<DocumentAttribute> documentAttributes = documentSearchResult.getDocumentAttributes();
198         for (DocumentAttribute docAttribute : documentAttributes) {
199             String name = docAttribute.getName();
200 
201             if (name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_DATE)) {
202                 if (docAttribute.getValue() != null) {
203                     DateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
204                     String stringDateObj = (String) docAttribute.getValue().toString();
205                     Method getMethod;
206                     try {
207                         java.util.Date date = sourceFormat.parse(stringDateObj);
208                         invoiceDocument.setInvoiceDate(date);
209                     } catch (Exception ex) {
210                         ex.printStackTrace();
211                     }
212                 }
213             }
214             if (name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_PAY_DATE)) {
215                 if (docAttribute.getValue() != null) {
216                     DateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy");
217                     String stringDateObj = (String) docAttribute.getValue().toString();
218                     Method getMethod;
219                     try {
220                         java.util.Date date = sourceFormat.parse(stringDateObj);
221                         invoiceDocument.setInvoicePayDate(date);
222                     } catch (Exception ex) {
223                         ex.printStackTrace();
224                     }
225                 }
226 
227             }
228             if (name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_SUB_TYP_ID)) {
229                 if (docAttribute.getValue() != null) {
230                     String stringDateObj = (String) docAttribute.getValue().toString();
231                     invoiceDocument.setInvoiceSubTypeId(Integer.parseInt(stringDateObj));
232                 }
233             }
234             if (name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.PURAP_ID)) {
235                 if (invoiceDocument.getPurapDocumentIdentifier() != null && (!invoiceDocument.getPurapDocumentIdentifier().equalsIgnoreCase(""))) {
236                     Set<String> hashSet = new HashSet<String>();
237                     String purarIdList = "";
238                     StringTokenizer stringTokenizer = new StringTokenizer(invoiceDocument.getPurapDocumentIdentifier(), ",");
239                     while (stringTokenizer.hasMoreElements()) {
240                         hashSet.add(stringTokenizer.nextElement().toString());
241                     }
242                     hashSet.add(((String) docAttribute.getValue().toString()).toString());
243                     for (String s : hashSet) {
244                         if (purarIdList.equalsIgnoreCase("")) {
245                             purarIdList = s;
246                         } else {
247                             purarIdList = s + "," + purarIdList;
248                         }
249                     }
250                     invoiceDocument.setPurapDocumentIdentifier(purarIdList);
251                 } else {
252                     invoiceDocument.setPurapDocumentIdentifier(((String) docAttribute.getValue().toString()).toString());
253                 }
254 
255             }
256             if (name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_TYP_ID)) {
257                 invoiceDocument.setInvoiceTypeId(((String) docAttribute.getValue().toString()));
258             }
259             if(name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_VND_NM)){
260                 invoiceDocument.setVendorName((String)docAttribute.getValue().toString());
261             }
262             if(name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_VND_NUM)){
263                 invoiceDocument.setVendorNumber((String)docAttribute.getValue().toString());
264             }
265             if(name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_NUMBER)){
266                 invoiceDocument.setInvoiceNumber((String)docAttribute.getValue().toString());
267                 invoiceDocument.setInvoiceNbr((String)docAttribute.getValue().toString());
268             }
269 
270             if( name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_DOC_NUM)
271                     || name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_TYP)
272                     || name.equalsIgnoreCase(OleSelectConstant.InvoiceSearch.INV_SUB_TYP)) {
273                 Method getMethod;
274                 try {
275                     getMethod = getSetMethod(OLEInvoiceSearchDocument.class, name, new Class[]{String.class});
276                     getMethod.invoke(invoiceDocument, docAttribute.getValue().toString());
277                 } catch (Exception ex) {
278                     ex.printStackTrace();
279                 }
280             }
281 
282         }
283         return invoiceDocument;
284     }
285     private Method getSetMethod(Class targetClass, String attr, Class[] objectAttributes) throws Exception {
286         Method method = targetClass.getMethod("set" + StringUtils.capitalize(attr), objectAttributes);
287         return method;
288     }
289 
290     public static final List<String> getResultNames() {
291         List<String> resultFields = new ArrayList<String>();
292         resultFields.add(OleSelectConstant.InvoiceSearch.PURAP_ID);
293         resultFields.add(OleSelectConstant.InvoiceSearch.INV_DATE);
294         resultFields.add(OleSelectConstant.InvoiceSearch.INV_PAY_DATE);
295         resultFields.add(OleSelectConstant.InvoiceSearch.INV_NUMBER);
296         resultFields.add(OleSelectConstant.InvoiceSearch.INV_DOC_NUM);
297         /*resultFields.add(OleSelectConstant.InvoiceSearch.INV_TYP);*/
298         resultFields.add(OleSelectConstant.InvoiceSearch.INV_TYP_ID);
299         resultFields.add(OleSelectConstant.InvoiceSearch.INV_SUB_TYP_ID);
300         resultFields.add(OleSelectConstant.InvoiceSearch.INV_VND_NM);
301         resultFields.add(OleSelectConstant.InvoiceSearch.INV_VND_NUM);
302 
303         return Collections.unmodifiableList(resultFields);
304     }
305 
306     public static final Map<String, String> getInvoiceNames() {
307         Map<String, String> invoiceFields = new HashMap<String, String>();
308         invoiceFields.put(OleSelectConstant.InvoiceSearch.PURAP_ID,OleSelectConstant.InvoiceSearch.PURAP_ID);
309         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_DATE, OleSelectConstant.InvoiceSearch.INV_DATE);
310         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_PAY_DATE,OleSelectConstant.InvoiceSearch.INV_PAY_DATE);
311         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_SUB_TYP_ID, OleSelectConstant.InvoiceSearch.INV_SUB_TYP_ID);
312         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_NUMBER,OleSelectConstant.InvoiceSearch.INV_NUMBER);
313         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_DOC_NUM,OleSelectConstant.InvoiceSearch.INV_DOC_NUM);
314         /*invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_TYP,OleSelectConstant.InvoiceSearch.INV_TYP);*/
315         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_TYP_ID,OleSelectConstant.InvoiceSearch.INV_TYP_ID);
316         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_VND_NM,OleSelectConstant.InvoiceSearch.INV_VND_NM);
317         invoiceFields.put(OleSelectConstant.InvoiceSearch.INV_VND_NUM,OleSelectConstant.InvoiceSearch.INV_VND_NUM);
318 
319         return Collections.unmodifiableMap(invoiceFields);
320     }
321 
322 
323 }