1 package org.kuali.ole.select.lookup;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.joda.time.DateTime;
5 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
6 import org.kuali.ole.docstore.common.document.Bib;
7 import org.kuali.ole.docstore.common.document.BibTree;
8 import org.kuali.ole.docstore.common.document.content.enums.DocType;
9 import org.kuali.ole.docstore.common.search.SearchParams;
10 import org.kuali.ole.docstore.common.search.SearchResponse;
11 import org.kuali.ole.docstore.common.search.SearchResult;
12 import org.kuali.ole.docstore.common.search.SearchResultField;
13 import org.kuali.ole.module.purap.PurapConstants;
14 import org.kuali.ole.select.OleSelectConstant;
15 import org.kuali.ole.select.document.OLEInvoicePurchaseOrderSearch;
16 import org.kuali.ole.sys.context.SpringContext;
17 import org.kuali.rice.core.api.util.type.KualiDecimal;
18 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
19 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
20 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
21 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
22 import org.kuali.rice.kew.service.KEWServiceLocator;
23 import org.kuali.rice.krad.lookup.LookupableImpl;
24 import org.kuali.rice.krad.service.BusinessObjectService;
25 import org.kuali.rice.krad.util.GlobalVariables;
26 import org.kuali.rice.krad.util.KRADConstants;
27 import org.kuali.rice.krad.web.form.LookupForm;
28
29 import java.util.*;
30
31
32
33
34 public class OLEInvoicePurchaseOrderSearchLookupableImpl extends LookupableImpl {
35
36 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEInvoicePurchaseOrderSearchLookupableImpl.class);
37
38 private String poId;
39 private String title;
40 private String author;
41 private String vendorName;
42 private String isbn;
43 private String documentNumber;
44 private String searchResultPoId;
45 private String searchResultPostingYear;
46 private String searchResultTotalAmount;
47 private static transient BusinessObjectService businessObjectService;
48 private DocstoreClientLocator docstoreClientLocator;
49
50 public DocstoreClientLocator getDocstoreClientLocator() {
51
52 if (docstoreClientLocator == null) {
53 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
54 }
55 return docstoreClientLocator;
56 }
57
58 public BusinessObjectService getBusinessObjectService() {
59 if (businessObjectService == null) {
60 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
61 }
62 return businessObjectService;
63 }
64
65 public boolean isNumber(String poNum) {
66 boolean flag = true;
67 if (poNum != null && !(poNum.isEmpty())) {
68 if (StringUtils.isNumeric(poNum)) {
69 } else {
70 flag = false;
71 }
72 }
73 return flag;
74 }
75
76 @Override
77 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
78
79 LOG.debug("Inside performSearch()");
80 List<String> bibDetails = new ArrayList<>();
81 List<OLEInvoicePurchaseOrderSearch> finalInvoicePurchaseOrderList = new ArrayList<>();
82 poId = searchCriteria.get(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.PURAP_DOC_IDENTIFIER) : "";
83 if (isNumber(poId.toString()) && !poId.contains("*")) {
84 title = searchCriteria.get(org.kuali.ole.OLEConstants.TITLE) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.TITLE) : "";
85 author = searchCriteria.get(org.kuali.ole.OLEConstants.AUTHOR) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.AUTHOR) : "";
86 isbn = searchCriteria.get(org.kuali.ole.OLEConstants.ISBN) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.ISBN) : "";
87 vendorName = searchCriteria.get(org.kuali.ole.OLEConstants.VENDOR_NAME) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.VENDOR_NAME) : "";
88 documentNumber = searchCriteria.get(org.kuali.ole.OLEConstants.DOC_NUM) != null ? searchCriteria.get(org.kuali.ole.OLEConstants.DOC_NUM) : "";
89 searchUsingSearchCriteria(finalInvoicePurchaseOrderList, vendorName, documentNumber, poId, title, author, isbn);
90 if (GlobalVariables.getMessageMap().getWarningCount() > 0) {
91 GlobalVariables.getMessageMap().getWarningMessages().clear();
92 }
93 if (finalInvoicePurchaseOrderList.size() == 0) {
94 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, org.kuali.ole.OLEConstants.NO_RECORD_FOUND);
95 }
96 } else {
97 GlobalVariables.getMessageMap().putWarning(KRADConstants.GLOBAL_ERRORS,
98 OleSelectConstant.ERROR_NO_PO_WILD_CARD_SEARCH,
99 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
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 }