001package org.kuali.ole.select.lookup; 002 003import org.apache.commons.lang3.StringUtils; 004import org.joda.time.DateTime; 005import org.kuali.ole.docstore.common.client.DocstoreClientLocator; 006import org.kuali.ole.docstore.common.document.Bib; 007import org.kuali.ole.docstore.common.document.BibTree; 008import org.kuali.ole.docstore.common.document.content.enums.DocType; 009import org.kuali.ole.docstore.common.search.SearchParams; 010import org.kuali.ole.docstore.common.search.SearchResponse; 011import org.kuali.ole.docstore.common.search.SearchResult; 012import org.kuali.ole.docstore.common.search.SearchResultField; 013import org.kuali.ole.module.purap.PurapConstants; 014import org.kuali.ole.select.OleSelectConstant; 015import org.kuali.ole.select.document.OLEInvoicePurchaseOrderSearch; 016import org.kuali.ole.sys.context.SpringContext; 017import org.kuali.rice.core.api.util.type.KualiDecimal; 018import org.kuali.rice.kew.api.document.attribute.DocumentAttribute; 019import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria; 020import org.kuali.rice.kew.api.document.search.DocumentSearchResult; 021import org.kuali.rice.kew.api.document.search.DocumentSearchResults; 022import org.kuali.rice.kew.service.KEWServiceLocator; 023import org.kuali.rice.krad.lookup.LookupableImpl; 024import org.kuali.rice.krad.service.BusinessObjectService; 025import org.kuali.rice.krad.util.GlobalVariables; 026import org.kuali.rice.krad.util.KRADConstants; 027import org.kuali.rice.krad.web.form.LookupForm; 028 029import java.util.*; 030 031/** 032 * This is the Lookupable Impl class for the PurchaseOrder Documents 033 */ 034public class OLEInvoicePurchaseOrderSearchLookupableImpl extends LookupableImpl { 035 036 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEInvoicePurchaseOrderSearchLookupableImpl.class); 037 038 private String poId; 039 private String title; 040 private String author; 041 private String vendorName; 042 private String isbn; 043 private String documentNumber; 044 private String searchResultPoId; 045 private String searchResultPostingYear; 046 private String searchResultTotalAmount; 047 private static transient BusinessObjectService businessObjectService; 048 private DocstoreClientLocator docstoreClientLocator; 049 050 public DocstoreClientLocator getDocstoreClientLocator() { 051 052 if (docstoreClientLocator == null) { 053 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class); 054 } 055 return docstoreClientLocator; 056 } 057 058 public BusinessObjectService getBusinessObjectService() { 059 if (businessObjectService == null) { 060 businessObjectService = SpringContext.getBean(BusinessObjectService.class); 061 } 062 return businessObjectService; 063 } 064 065 public boolean isNumber(String poNum) { 066 boolean flag = true; 067 if (poNum != null && !(poNum.isEmpty())) { 068 if (StringUtils.isNumeric(poNum)) { 069 } else { 070 flag = false; 071 } 072 } 073 return flag; 074 } 075 076 @Override 077 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) { 078 079 LOG.debug("Inside performSearch()"); 080 List<String> bibDetails = new ArrayList<>(); 081 List<OLEInvoicePurchaseOrderSearch> finalInvoicePurchaseOrderList = new ArrayList<>(); 082 poId = searchCriteria.get(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER) : ""; 083 if (isNumber(poId.toString()) && !poId.contains("*")) { 084 title = searchCriteria.get(org.kuali.ole.OLEConstants.TITLE) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.TITLE) : ""; 085 author = searchCriteria.get(org.kuali.ole.OLEConstants.AUTHOR) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.AUTHOR) : ""; 086 isbn = searchCriteria.get(org.kuali.ole.OLEConstants.ISBN) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.ISBN) : ""; 087 vendorName = searchCriteria.get(org.kuali.ole.OLEConstants.VENDOR_NAME) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.VENDOR_NAME) : ""; 088 documentNumber = searchCriteria.get(org.kuali.ole.OLEConstants.DOC_NUM) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.DOC_NUM) : ""; 089 searchUsingSearchCriteria(finalInvoicePurchaseOrderList, vendorName, documentNumber, poId, title, author, isbn); 090 if (GlobalVariables.getMessageMap().getWarningCount() > 0) { 091 GlobalVariables.getMessageMap().getWarningMessages().clear(); 092 } 093 if (finalInvoicePurchaseOrderList.size() == 0) { 094 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, org.kuali.ole.OLEConstants.NO_RECORD_FOUND); 095 } 096 } else { 097 GlobalVariables.getMessageMap().putWarning(KRADConstants.GLOBAL_ERRORS, 098 OleSelectConstant.ERROR_NO_PO_WILD_CARD_SEARCH, 099 new String[]{org.kuali.ole.OLEConstants.PURCHASE_ORDER_NUM}); 100 } 101 return finalInvoicePurchaseOrderList; 102 } 103 104 105 public void searchUsingSearchCriteria(List<OLEInvoicePurchaseOrderSearch> finalInvoicePurchaseOrderList, String vendorName, String documentNumber, String poId, String title, String author, String isbn) { 106 DocumentSearchCriteria.Builder docSearchCriteria = DocumentSearchCriteria.Builder.create(); 107 docSearchCriteria.setDocumentTypeName(PurapConstants.PurapDocTypeCodes.PO_DOCUMENT); 108 Map<String, List<String>> fixedParameters = new HashMap<>(); 109 if (!poId.isEmpty()) 110 fixedParameters.put(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER, Arrays.asList(poId)); 111 if (!vendorName.isEmpty()) 112 fixedParameters.put(org.kuali.ole.OLEConstants.VENDOR_NAME, Arrays.asList(vendorName)); 113 Map<String, List<String>> attributes = new HashMap<String, List<String>>(); 114 if (docSearchCriteria != null) { 115 if (!fixedParameters.isEmpty()) { 116 for (String propertyField : fixedParameters.keySet()) { 117 if (fixedParameters.get(propertyField) != null) { 118 attributes.put(propertyField, fixedParameters.get(propertyField)); 119 } 120 } 121 } 122 } 123 docSearchCriteria.setDocumentAttributeValues(attributes); 124 docSearchCriteria.setDocumentId(documentNumber); 125 Date currentDate = new Date(); 126 docSearchCriteria.setDateCreatedTo(new DateTime(currentDate)); 127 docSearchCriteria.setApplicationDocumentStatus(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN); 128 //docSearchCriteria.setDocumentStatuses(documentStatuses); 129 DocumentSearchCriteria docSearchCriteriaDTO = docSearchCriteria.build(); 130 DocumentSearchResults components = null; 131 components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(), docSearchCriteriaDTO); 132 List<DocumentSearchResult> docSearchResults = components.getSearchResults(); 133 if (docSearchResults!=null && !docSearchResults.isEmpty()) { 134 for (DocumentSearchResult searchResult : docSearchResults) { 135 List<DocumentAttribute> documentAttributeLists = searchResult.getDocumentAttributeByName(org.kuali.ole.OLEConstants.ITEM_TITLE_ID); 136 Set distinctDocsearchResult = new HashSet(); 137 for (DocumentAttribute distinctDocsearchResultList : documentAttributeLists) { 138 distinctDocsearchResult.add(distinctDocsearchResultList); 139 } 140 List<DocumentAttribute> documentAttributeList = new ArrayList(); 141 documentAttributeList.addAll(distinctDocsearchResult); 142 for (DocumentAttribute itemTitleId : documentAttributeList) { 143 String bibId = ((String) itemTitleId.getValue()); 144 if (!title.isEmpty() || !author.isEmpty() || !isbn.isEmpty()) { 145 searchFromDocstore(finalInvoicePurchaseOrderList, searchResult, bibId, title, author, isbn); 146 } else { 147 getOlePurchaseOrderSearchList(finalInvoicePurchaseOrderList, searchResult, bibId, "", "", ""); 148 } 149 } 150 } 151 } 152 } 153 154 155 public void searchFromDocstore(List<OLEInvoicePurchaseOrderSearch> finalInvoicePurchaseOrderList, DocumentSearchResult search_Result, String bibId, String title, String author, String isbn) { 156 String title_display = ""; 157 String author_display = ""; 158 String isbn_display = ""; 159 SearchResponse search_Response = new SearchResponse(); 160 SearchParams search_Params = new SearchParams(); 161 if (!title.isEmpty()) { 162 search_Params.getSearchConditions().add(search_Params.buildSearchCondition(org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE, search_Params.buildSearchField(DocType.ITEM.getCode(), Bib.TITLE, title), org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE)); 163 } 164 if (!author.isEmpty()) { 165 search_Params.getSearchConditions().add(search_Params.buildSearchCondition(org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE, search_Params.buildSearchField(DocType.ITEM.getCode(), Bib.AUTHOR, author), org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE)); 166 } 167 if (!isbn.isEmpty()) { 168 search_Params.getSearchConditions().add(search_Params.buildSearchCondition(org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE, search_Params.buildSearchField(DocType.ITEM.getCode(), Bib.ISBN, isbn), org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE)); 169 } 170 search_Params.getSearchConditions().add(search_Params.buildSearchCondition(org.kuali.ole.OLEConstants.PO_NONE, search_Params.buildSearchField(DocType.ITEM.getCode(), org.kuali.ole.OLEConstants.PURCHASE_ORDER_LINE_ID_SEARCH, org.kuali.ole.OLEConstants.RANGE_QUERY), org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE)); 171 search_Params.getSearchConditions().add(search_Params.buildSearchCondition(org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE, search_Params.buildSearchField(DocType.ITEM.getCode(), org.kuali.ole.OLEConstants.BIB_SEARCH, bibId), org.kuali.ole.OLEConstants.AND_SEARCH_SCOPE)); 172 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), Bib.TITLE)); 173 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), Bib.AUTHOR)); 174 search_Params.getSearchResultFields().add(search_Params.buildSearchResultField(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode(), Bib.ISBN)); 175 try { 176 search_Response = getDocstoreClientLocator().getDocstoreClient().search(search_Params); 177 if (search_Response != null) { 178 if (search_Response.getSearchResults() != null && search_Response.getSearchResults().size() > 0) { 179 for (SearchResult searchResult : search_Response.getSearchResults()) { 180 if (searchResult.getSearchResultFields() != null && searchResult.getSearchResultFields().size() > 0) { 181 for (SearchResultField searchResultField : searchResult.getSearchResultFields()) { 182 if (searchResultField.getDocType().equalsIgnoreCase(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode()) && searchResultField.getFieldName().equalsIgnoreCase(Bib.TITLE) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) { 183 title_display = searchResultField.getFieldValue(); 184 } 185 if (searchResultField.getDocType().equalsIgnoreCase(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode()) && searchResultField.getFieldName().equalsIgnoreCase(Bib.AUTHOR) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) { 186 author_display = searchResultField.getFieldValue(); 187 } 188 if (searchResultField.getDocType().equalsIgnoreCase(org.kuali.ole.docstore.common.document.content.enums.DocType.BIB.getCode()) && searchResultField.getFieldName().equalsIgnoreCase(Bib.ISBN) && searchResultField.getFieldValue() != null && !searchResultField.getFieldValue().isEmpty()) { 189 isbn_display = searchResultField.getFieldValue(); 190 } 191 } 192 } 193 } 194 getOlePurchaseOrderSearchList(finalInvoicePurchaseOrderList, search_Result, bibId, title_display, author_display, isbn_display); 195 } 196 } 197 } catch (Exception e) { 198 e.printStackTrace(); 199 } 200 } 201 202 private List<OLEInvoicePurchaseOrderSearch> getOlePurchaseOrderSearchList(List<OLEInvoicePurchaseOrderSearch> finalInvoicePurchaseOrderList, DocumentSearchResult searchResult, String bibId, String title, String author, String isbn) { 203 204 try { 205 OLEInvoicePurchaseOrderSearch oleInvoicePurchaseOrderSearch = new OLEInvoicePurchaseOrderSearch(); 206 BibTree bibTree = new BibTree(); 207 if ((bibId != null && bibId != "") && title.isEmpty() && author.isEmpty() && isbn.isEmpty()) { 208 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId); 209 oleInvoicePurchaseOrderSearch.setTitle(bibTree.getBib().getTitle()); 210 211 oleInvoicePurchaseOrderSearch.setAuthor(bibTree.getBib().getAuthor()); 212 oleInvoicePurchaseOrderSearch.setIsbn(bibTree.getBib().getIsbn()); 213 } else { 214 if (!title.isEmpty() && title != null) { 215 oleInvoicePurchaseOrderSearch.setTitle(title); 216 } 217 if (!author.isEmpty() && author != null) { 218 oleInvoicePurchaseOrderSearch.setAuthor(author); 219 } 220 if (!isbn.isEmpty() && isbn != null) { 221 oleInvoicePurchaseOrderSearch.setIsbn(isbn); 222 } 223 } 224 Map<String, String> searchParam = new HashMap<>(); 225 searchParam.put(org.kuali.ole.OLEConstants.DOC_NUM, searchResult.getDocument().getDocumentId()); 226 searchResultPoId = searchResult.getDocumentAttributeByName(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER).get(0).getValue().toString(); 227 if (searchResultPoId != null && (!searchResultPoId.isEmpty())) { 228 oleInvoicePurchaseOrderSearch.setPurapDocumentIdentifier(searchResultPoId); 229 } 230 oleInvoicePurchaseOrderSearch.setDocumentNumber(searchResult.getDocument().getDocumentId()); 231 searchResultPostingYear = searchResult.getDocumentAttributeByName(org.kuali.ole.OLEConstants.POSTING_YEAR).get(0).getValue().toString(); 232 if (searchResultPostingYear != null && (!searchResultPostingYear.isEmpty())) { 233 oleInvoicePurchaseOrderSearch.setPostingYear(new Integer(searchResultPostingYear)); 234 } 235 searchResultTotalAmount = searchResult.getDocumentAttributeByName(org.kuali.ole.OLEConstants.TOTAL_DOLLAR_AMOUNT).get(0).getValue().toString(); 236 if (searchResultTotalAmount != null && (!searchResultTotalAmount.isEmpty())) { 237 oleInvoicePurchaseOrderSearch.setAmount(new KualiDecimal(searchResultTotalAmount)); 238 } 239 finalInvoicePurchaseOrderList.add(oleInvoicePurchaseOrderSearch); 240 } catch (Exception e) { 241 LOG.error("Exception while getting PurchaseOrderSearchList ------> " + e); 242 e.printStackTrace(); 243 } 244 return finalInvoicePurchaseOrderList; 245 } 246 247 248 @Override 249 public Map<String, String> performClear(LookupForm form, Map<String, String> searchCriteria) { 250 super.performClear(form, searchCriteria); 251 Map<String, String> clearedSearchCriteria = new HashMap<String, String>(); 252 for (Map.Entry<String, String> searchKeyValue : searchCriteria.entrySet()) { 253 String searchPropertyName = searchKeyValue.getKey(); 254 if (searchPropertyName.equals(org.kuali.ole.OLEConstants.VENDOR_NAME)) { 255 clearedSearchCriteria.put(searchPropertyName, searchKeyValue.getValue()); 256 } 257 } 258 return clearedSearchCriteria; 259 } 260 261 262}