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.DocumentUniqueIDPrefix;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
8 import org.kuali.ole.docstore.common.document.BibTree;
9 import org.kuali.ole.docstore.common.document.HoldingsTree;
10 import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
11 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
12 import org.kuali.ole.docstore.common.search.SearchParams;
13 import org.kuali.ole.docstore.common.search.SearchResponse;
14 import org.kuali.ole.docstore.common.search.SearchResult;
15 import org.kuali.ole.docstore.common.search.SearchResultField;
16 import org.kuali.ole.module.purap.PurapConstants;
17 import org.kuali.ole.select.bo.OLEPurchaseOrderSearch;
18 import org.kuali.ole.select.bo.OLESerialReceivingDocument;
19 import org.kuali.ole.select.businessobject.OleCopy;
20 import org.kuali.ole.sys.context.SpringContext;
21 import org.kuali.ole.vnd.businessobject.VendorAlias;
22 import org.kuali.ole.vnd.businessobject.VendorDetail;
23 import org.kuali.rice.kew.api.document.DocumentStatus;
24 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
25 import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
26 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
27 import org.kuali.rice.kew.api.document.search.DocumentSearchResults;
28 import org.kuali.rice.kew.service.KEWServiceLocator;
29 import org.kuali.rice.krad.lookup.LookupableImpl;
30 import org.kuali.rice.krad.service.BusinessObjectService;
31 import org.kuali.rice.krad.util.GlobalVariables;
32 import org.kuali.rice.krad.util.KRADConstants;
33 import org.kuali.rice.krad.web.form.LookupForm;
34
35 import java.math.BigInteger;
36 import java.util.*;
37
38
39
40
41
42
43
44
45 public class OLEPurchaseOrderSearchLookupableImpl extends LookupableImpl {
46 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEPurchaseOrderSearchLookupableImpl.class);
47
48 private BusinessObjectService businessObjectService;
49 private DocstoreClientLocator docstoreClientLocator;
50
51 public DocstoreClientLocator getDocstoreClientLocator() {
52
53 if (docstoreClientLocator == null) {
54 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
55 }
56 return docstoreClientLocator;
57 }
58
59 public BusinessObjectService getBusinessObjectService() {
60 if (businessObjectService == null) {
61 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
62 }
63 return businessObjectService;
64 }
65 @Override
66 public Collection<?> performSearch(LookupForm form, Map<String, String> searchCriteria, boolean bounded) {
67 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
68 List<String> bibDetails = new ArrayList<>();
69 List <OLEPurchaseOrderSearch> finalOlePurchaseOrderSearch=new ArrayList<>();
70
71 String poId;
72 String title;
73 String author;
74 String vendorName;
75 String issn;
76 String instanceId;
77
78
79 poId = searchCriteria.get("poId")!=null? searchCriteria.get("poId") :"";
80 title = searchCriteria.get("title")!=null?searchCriteria.get("title") :"";
81 author = searchCriteria.get("author")!=null?searchCriteria.get("author"):"";
82 issn = searchCriteria.get("issn")!=null?searchCriteria.get("issn"):"";
83 vendorName= searchCriteria.get("vendorName")!=null?searchCriteria.get("vendorName"):"";
84 instanceId=searchCriteria.get("instanceId")!=null?searchCriteria.get("instanceId"):"";
85
86
87
88
89 if((!poId.contains("*"))&&(!instanceId.contains("*"))){
90
91 boolean docsearch=true;
92 DocumentSearchCriteria.Builder docSearchCriteria = DocumentSearchCriteria.Builder.create();
93 docSearchCriteria.setDocumentTypeName(PurapConstants.PurapDocTypeCodes.PO_DOCUMENT);
94 Map<String, List<String>> fixedParameters=new HashMap<>();
95
96 if(!poId.isEmpty())
97 fixedParameters.put("purapDocumentIdentifier",Arrays.asList(poId));
98 if(!vendorName.isEmpty())
99 fixedParameters.put("vendorName", Arrays.asList(vendorName));
100 Map<String, List<String>> attributes = new HashMap<String, List<String>>();
101 if (docSearchCriteria != null) {
102 if (!fixedParameters.isEmpty()) {
103 for (String propertyField : fixedParameters.keySet()) {
104 if (fixedParameters.get(propertyField) != null) {
105 attributes.put(propertyField, fixedParameters.get(propertyField));
106 }
107 }
108 }
109 }
110 List<DocumentStatus> documentStatuses=new ArrayList<>();
111 documentStatuses.add(DocumentStatus.FINAL);
112 docSearchCriteria.setDocumentAttributeValues(attributes);
113 Date currentDate = new Date();
114 docSearchCriteria.setDateCreatedTo(new DateTime(currentDate));
115 docSearchCriteria.setApplicationDocumentStatus(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN);
116
117 DocumentSearchCriteria docSearchCriteriaDTO = docSearchCriteria.build();
118 DocumentSearchResults components = null;
119 components = KEWServiceLocator.getDocumentSearchService().lookupDocuments(GlobalVariables.getUserSession().getPrincipalId(), docSearchCriteriaDTO);
120 List<DocumentSearchResult> docSearchResults = components.getSearchResults();
121
122 if (!docSearchResults.isEmpty()) {
123 if(!title.isEmpty()||!author.isEmpty()||!issn.isEmpty()) {
124 try {
125 bibDetails=getBibDetails(searchCriteria);
126 } catch (Exception e) {
127 e.printStackTrace();
128 }
129 if(bibDetails!=null&&bibDetails.size()>0)
130 docsearch=true;
131 else
132 docsearch=false;
133
134 }
135 HashMap<String,String> instanceMap = new HashMap<>();
136 for (DocumentSearchResult searchResult : docSearchResults) {
137 List<DocumentAttribute> documentAttributeList= searchResult.getDocumentAttributeByName("itemTitleId");
138 for(DocumentAttribute itemTitleId:documentAttributeList){
139 String bibId=((String)itemTitleId.getValue());
140 Map parentCriterial = new HashMap();
141 parentCriterial.put(OLEConstants.BIB_ID,bibId);
142 List<OleCopy> copyList = (List<OleCopy>) businessObjectService.findMatching(OleCopy.class,parentCriterial);
143 if(docsearch && copyList!=null && copyList.size()>0){
144 if(bibDetails!=null&&bibDetails.size()>0){
145 if(bibDetails.contains(bibId)){
146 getOlePurchaseOrderSearchList(finalOlePurchaseOrderSearch,searchResult,bibId,instanceId,instanceMap);
147 }
148
149 }else{
150 getOlePurchaseOrderSearchList(finalOlePurchaseOrderSearch,searchResult,bibId,instanceId,instanceMap);
151 }
152 }
153 }
154 }
155 }
156 }
157 if(GlobalVariables.getMessageMap().getWarningCount()>0){
158 GlobalVariables.getMessageMap().getWarningMessages().clear();
159 }
160 if (finalOlePurchaseOrderSearch.size() == 0) {
161 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, org.kuali.ole.OLEConstants.NO_RECORD_FOUND);
162 }
163
164
165 return finalOlePurchaseOrderSearch;
166
167 }
168 private List<String> getBibDetails( Map<String, String> searchCriteria) throws Exception {
169 SearchParams searchParams=new SearchParams();
170 List<String> bibIdList=new ArrayList<>();
171 String title = ((String) searchCriteria.get("title"))!=null?searchCriteria.get("title") :"";
172 String author = ((String) searchCriteria.get("author"))!=null?searchCriteria.get("author") :"";
173 String issn = ((String) searchCriteria.get("issn"))!=null?searchCriteria.get("issn") :"";
174 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("",searchParams.buildSearchField("bibliographic","TITLE",title),"AND"));
175 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("",searchParams.buildSearchField("bibliographic","AUTHOR",author),"AND"));
176 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField("bibliographic", "ISSN", issn), "AND"));
177 searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("bibliographic","id"));
178 SearchResponse searchResponses=getDocstoreClientLocator().getDocstoreClient().search(searchParams);
179 for(SearchResult searchResults:searchResponses.getSearchResults()){
180 for(SearchResultField searchResultField:searchResults.getSearchResultFields()){
181 if(searchResultField.getDocType().equalsIgnoreCase("bibliographic") && searchResultField.getFieldName().equalsIgnoreCase("id")){
182 bibIdList.add(searchResultField.getFieldValue());
183 }
184 }
185 }
186 return bibIdList;
187
188 }
189 private List<OLEPurchaseOrderSearch> getOlePurchaseOrderSearchList(List<OLEPurchaseOrderSearch> finalOlePurchaseOrderSearch, DocumentSearchResult searchResult, String bibId, String instance_Id, HashMap<String, String> instanceMap) {
190
191 try {
192
193 BibTree bibTree = new BibTree();
194 if (bibId != null && bibId != "") {
195 bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibId);
196 }
197 String instance = instance_Id != null ? instance_Id : "";
198 OLEPurchaseOrderSearch olePurchaseOrderSearch = new OLEPurchaseOrderSearch();
199 olePurchaseOrderSearch.setBibId(bibId);
200 olePurchaseOrderSearch.setTitle(bibTree.getBib().getTitle());
201
202 olePurchaseOrderSearch.setAuthor(bibTree.getBib().getAuthor());
203
204 olePurchaseOrderSearch.setIssn(bibTree.getBib().getIssn());
205
206 olePurchaseOrderSearch.setPublisher(bibTree.getBib().getPublisher());
207
208 olePurchaseOrderSearch.setLocalId(DocumentUniqueIDPrefix.getDocumentId(bibId));
209
210
211 for (HoldingsTree holdingsTree : bibTree.getHoldingsTrees()) {
212 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor=new HoldingOlemlRecordProcessor();
213 OleHoldings oleHoldings=holdingOlemlRecordProcessor.fromXML(holdingsTree.getHoldings().getContent());
214 Map parentCriterial = new HashMap();
215 parentCriterial.put(OLEConstants.BIB_ID, bibId);
216 parentCriterial.put(OLEConstants.INSTANCE_ID, holdingsTree.getHoldings().getId());
217 List<OleCopy> copyList = (List<OleCopy>) getBusinessObjectService().findMatching(OleCopy.class, parentCriterial);
218 boolean existingInstance = false;
219 for (OleCopy copy : copyList) {
220 if((copy.getSerialReceivingIdentifier()!=null && !copy.getSerialReceivingIdentifier().isEmpty())
221 || !copy.getPoDocNum().equalsIgnoreCase(searchResult.getDocument().getDocumentId()) ){
222 existingInstance = true;
223 break;
224 }
225 }
226 parentCriterial = new HashMap();
227 parentCriterial.put("poId", ((BigInteger) searchResult.getDocumentAttributeByName("purapDocumentIdentifier").get(0).getValue()).intValue());
228 parentCriterial.put(OLEConstants.INSTANCE_ID, holdingsTree.getHoldings().getId());
229 List<OLESerialReceivingDocument> oleSerialReceivingDocuments = (List<OLESerialReceivingDocument>) getBusinessObjectService().findMatching(OLESerialReceivingDocument.class, parentCriterial);
230 if (oleSerialReceivingDocuments != null && oleSerialReceivingDocuments.size() > 0) {
231 existingInstance = true;
232 }
233 if (instanceMap.containsKey(holdingsTree.getHoldings().getId())) {
234 existingInstance = true;
235 }
236 if ((instance.equalsIgnoreCase((String) holdingsTree.getHoldings().getId()) || instance.isEmpty()) && copyList != null && copyList.size() > 0 && !existingInstance) {
237
238
239 olePurchaseOrderSearch.setCopyNumber(oleHoldings.getCopyNumber());
240 olePurchaseOrderSearch.setInstanceId((String) holdingsTree.getHoldings().getId());
241 olePurchaseOrderSearch.setCallNumber(oleHoldings.getCallNumber()!=null ? oleHoldings.getCallNumber().getNumber() : null);
242
243 Map vendorNameMap = new HashMap();
244
245 String vendorNumber = (String) searchResult.getDocumentAttributeByName("vendorNumber").get(0).getValue();
246 String vendorHeaderGeneratedIdentifier = "";
247 String vendorDetailAssignedIdentifier = "";
248 String [] vendorNumberArray=vendorNumber.split("-");
249 if(vendorNumberArray.length==1){
250 vendorHeaderGeneratedIdentifier=vendorNumberArray[0];
251 }
252 if(vendorNumberArray.length==2){
253 vendorHeaderGeneratedIdentifier=vendorNumberArray[0];
254 vendorDetailAssignedIdentifier=vendorNumberArray[1];
255 }
256 if(StringUtils.isNotBlank(vendorDetailAssignedIdentifier)){
257 vendorNameMap.put("vendorDetailAssignedIdentifier", vendorDetailAssignedIdentifier);
258 }
259 if(StringUtils.isNotBlank(vendorHeaderGeneratedIdentifier)){
260 vendorNameMap.put("vendorHeaderGeneratedIdentifier", vendorHeaderGeneratedIdentifier);
261 }
262
263
264 List<VendorDetail> vendorDetailList = (List) getBusinessObjectService().findMatching(VendorDetail.class, vendorNameMap);
265 if (vendorDetailList != null && vendorDetailList.size() > 0) {
266
267 olePurchaseOrderSearch.setVendorId(vendorDetailList.get(0).getVendorHeaderGeneratedIdentifier().toString()
268 + "-" + vendorDetailList.get(0).getVendorDetailAssignedIdentifier().toString());
269
270 Map vendorAliasMap = new HashMap();
271 vendorAliasMap.put(org.kuali.ole.sys.OLEConstants.VENDOR_HEADER_IDENTIFIER, vendorDetailList.get(0).getVendorHeaderGeneratedIdentifier());
272 vendorAliasMap.put(org.kuali.ole.sys.OLEConstants.VENDOR_DETAIL_IDENTIFIER, vendorDetailList.get(0).getVendorDetailAssignedIdentifier());
273 List<VendorAlias> vendorAliasList = (List) getBusinessObjectService().findMatching(VendorAlias.class, vendorAliasMap);
274
275 if (vendorAliasList != null && vendorAliasList.size() > 0) {
276 olePurchaseOrderSearch.setVendorAliasName(vendorAliasList.get(0).getVendorAliasName());
277 }
278 }
279
280 olePurchaseOrderSearch.setPoId(((BigInteger) searchResult.getDocumentAttributeByName("purapDocumentIdentifier").get(0).getValue()).intValue());
281 olePurchaseOrderSearch.setPoIdLink(searchResult.getDocument().getDocumentId());
282 olePurchaseOrderSearch.setVendorName((String) searchResult.getDocumentAttributeByName("vendorName").get(0).getValue());
283 instanceMap.put((String) holdingsTree.getHoldings().getId(), (String) holdingsTree.getHoldings().getId());
284 finalOlePurchaseOrderSearch.add(olePurchaseOrderSearch);
285 }
286
287 }
288
289
290 } catch (Exception e) {
291
292 LOG.error("Exception while getting PurchaseOrderSearchList------> " + e);
293 e.printStackTrace();
294
295 }
296
297 return finalOlePurchaseOrderSearch;
298 }
299 }