View Javadoc
1   package org.kuali.ole.select.controller;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.select.bo.OLESearchCondition;
8   import org.kuali.ole.select.bo.OLESearchParams;
9   import org.kuali.ole.select.document.OLEEResourceRecordDocument;
10  import org.kuali.ole.select.form.OLEEResourceSearchForm;
11  import org.kuali.ole.service.impl.OLEEResourceSearchServiceImpl;
12  import org.kuali.ole.service.impl.OleLicenseRequestServiceImpl;
13  import org.kuali.ole.vnd.businessobject.VendorDetail;
14  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
15  import org.kuali.rice.krad.service.KRADServiceLocator;
16  import org.kuali.rice.krad.util.GlobalVariables;
17  import org.kuali.rice.krad.util.KRADConstants;
18  import org.kuali.rice.krad.web.controller.UifControllerBase;
19  import org.kuali.rice.krad.web.form.UifFormBase;
20  import org.springframework.stereotype.Controller;
21  import org.springframework.validation.BindingResult;
22  import org.springframework.web.bind.annotation.ModelAttribute;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import java.text.SimpleDateFormat;
29  import java.util.*;
30  
31  /**
32   * Created with IntelliJ IDEA.
33   * User: chenchulakshmig
34   * Date: 6/26/13
35   * Time: 1:18 PM
36   * To change this template use File | Settings | File Templates.
37   */
38  @Controller
39  @RequestMapping(value = "/searchEResourceController")
40  public class OLEEResourceSearchController extends UifControllerBase {
41  
42      private static final Logger LOG = Logger.getLogger(OLEEResourceSearchController.class);
43      private static final SimpleDateFormat dateFormat = new SimpleDateFormat(OLEConstants.OLEEResourceRecord.CREATED_DATE_FORMAT);
44      private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT);
45  
46      @Override
47      protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
48          return new OLEEResourceSearchForm();
49      }
50  
51      @Override
52      @RequestMapping(params = "methodToCall=start")
53      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
54                                HttpServletRequest request, HttpServletResponse response) {
55          OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form;
56          List<OLESearchCondition> oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList();
57          for (OLESearchCondition oleSearchCondition : oleSearchConditions) {
58              oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND);
59          }
60          return super.navigate(oleSearchForm, result, request, response);
61      }
62  
63      @RequestMapping(params = "methodToCall=addSearchCriteria")
64      public ModelAndView addSearchCriteria(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
65                                            HttpServletRequest request, HttpServletResponse response) {
66          OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form;
67          List<OLESearchCondition> oleSearchConditions = new ArrayList<OLESearchCondition>();
68          oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList();
69          oleSearchConditions.add(new OLESearchCondition());
70          for (OLESearchCondition oleSearchCondition : oleSearchConditions) {
71              if (oleSearchCondition.getOperator() == null) {
72                  oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND);
73              }
74          }
75          return super.navigate(oleSearchForm, result, request, response);
76      }
77  
78      @RequestMapping(params = "methodToCall=search")
79      public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
80                                 HttpServletRequest request, HttpServletResponse response) {
81          OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form;
82          OLEEResourceSearchServiceImpl oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE);
83          List<OLESearchCondition> oleSearchConditionsList = oleSearchForm.getOleSearchParams().getSearchFieldsList();
84          List<OLEEResourceRecordDocument> eresourceDocumentList = new ArrayList<OLEEResourceRecordDocument>();
85          List<OLEEResourceRecordDocument> eresourceList = new ArrayList<OLEEResourceRecordDocument>();
86          List<OLEEResourceRecordDocument> eresourceRecordDocumentList = new ArrayList<OLEEResourceRecordDocument>();
87          try {
88              eresourceList = oleEResourceSearchService.performSearch(oleSearchConditionsList);
89          } catch (Exception e) {
90              LOG.error("Exception while hitting the docstore time" + e.getMessage());
91          }
92  
93          Set<String> eResourceIds = new HashSet<>();
94          for (OLEEResourceRecordDocument oleEResourceRecordDocument: eresourceList){
95              if (StringUtils.isNotBlank(oleEResourceRecordDocument.getOleERSIdentifier())){
96                  eResourceIds.add(oleEResourceRecordDocument.getOleERSIdentifier());
97              }
98          }
99          if (CollectionUtils.isNotEmpty(eResourceIds)){
100             Map eResourceMap = new HashMap<>();
101             eResourceMap.put("oleERSIdentifier", eResourceIds);
102             eresourceRecordDocumentList = (List<OLEEResourceRecordDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEEResourceRecordDocument.class, eResourceMap);
103             for (OLEEResourceRecordDocument oleeResourceRecordDocument : eresourceRecordDocumentList){
104                 if (StringUtils.isNotBlank(oleeResourceRecordDocument.getPublisherId())){
105                     String[] publisherDetails = oleeResourceRecordDocument.getPublisherId().split("-");
106                     Map vendorMap = new HashMap<>();
107                     int vendorHeaderGeneratedIdentifier = publisherDetails.length > 0 ? Integer.parseInt(publisherDetails[0]) : 0;
108                     int vendorDetailAssignedIdentifier = publisherDetails.length > 1 ? Integer.parseInt(publisherDetails[1]) : 0;
109                     vendorMap.put(OLEConstants.OLEEResourceRecord.VENDOR_HEADER_GEN_ID, vendorHeaderGeneratedIdentifier);
110                     vendorMap.put(OLEConstants.OLEEResourceRecord.VENDOR_DETAILED_ASSIGNED_ID, vendorDetailAssignedIdentifier);
111                     VendorDetail vendorDetailDoc = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(VendorDetail.class, vendorMap);
112                     if (vendorDetailDoc != null) {
113                         oleeResourceRecordDocument.setActivePublisher(vendorDetailDoc.isActiveIndicator());
114                         oleeResourceRecordDocument.setPublisher(vendorDetailDoc.getVendorName());
115                     }
116                 }
117             }
118         }
119         eresourceList = eresourceRecordDocumentList;
120 
121         if (oleSearchForm.getStatus() != null) {
122             eresourceList = oleEResourceSearchService.statusNotNull(eresourceList, oleSearchForm.getStatus());
123         }
124         if (oleSearchForm.iseResStatusDate()) {
125             try {
126                 Date beginDate = oleSearchForm.getBeginDate();
127                 String begin = null;
128                 if (beginDate != null) {
129                     begin = dateFormat.format(beginDate);
130                 }
131                 Date endDate = oleSearchForm.getEndDate();
132                 String end = null;
133                 if (endDate != null) {
134                     end = dateFormat.format(endDate);
135                 }
136                 boolean isValid = false;
137                 eresourceDocumentList.clear();
138                 for (OLEEResourceRecordDocument oleEResourceRecordDocumentList : eresourceList) {
139                     String status = oleEResourceRecordDocumentList.getStatusDate();
140                     Date statusDate = simpleDateFormat.parse(status);
141                     OleLicenseRequestServiceImpl oleLicenseRequestService = GlobalResourceLoader.getService(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_SERVICE);
142                     isValid = oleLicenseRequestService.validateDate(statusDate, begin, end);
143                     if (isValid) {
144                         eresourceDocumentList.add(oleEResourceRecordDocumentList);
145                     }
146                 }
147             } catch (Exception e) {
148                 LOG.error("Exception while calling the licenseRequest service" + e.getMessage());
149                 throw new RuntimeException(e);
150             }
151             oleSearchForm.setEresourceDocumentList(eresourceDocumentList);
152         } else {
153             oleSearchForm.setEresourceDocumentList(eresourceList);
154         }
155         List<OLEEResourceRecordDocument> eresDocumentList = oleSearchForm.getEresourceDocumentList();
156         removeDuplicateEresDocumentsFromList(eresDocumentList);
157         if (!GlobalVariables.getMessageMap().hasMessages()){
158             if (oleSearchForm.getEresourceDocumentList().size()==0)
159                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND);
160         }
161         else {
162             oleSearchForm.setEresourceDocumentList(null);
163         }
164         return getUIFModelAndView(oleSearchForm);
165     }
166 
167     @RequestMapping(params = "methodToCall=clearSearch")
168     public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
169                                     HttpServletRequest request, HttpServletResponse response) {
170         OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form;
171         List<OLESearchCondition> oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList();
172         int searchConditionSize = oleSearchConditions.size();
173         oleSearchForm.setOleSearchParams(new OLESearchParams());
174         oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList();
175         for (int ersCount = 0; ersCount < searchConditionSize; ersCount++) {
176             oleSearchConditions.add(new OLESearchCondition());
177         }
178         for (OLESearchCondition oleSearchCondition : oleSearchConditions) {
179             oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND);
180         }
181         oleSearchForm.setEresourceDocumentList(null);
182         oleSearchForm.seteResStatusDate(false);
183         oleSearchForm.setBeginDate(null);
184         oleSearchForm.setEndDate(null);
185         oleSearchForm.setStatus(null);
186         return getUIFModelAndView(oleSearchForm);
187     }
188 
189     @RequestMapping(params = "methodToCall=cancel")
190     public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
191                                HttpServletRequest request, HttpServletResponse response) {
192         OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form;
193         return super.cancel(oleSearchForm, result, request, response);
194     }
195 
196     private void removeDuplicateEresDocumentsFromList(List<OLEEResourceRecordDocument> eresourceDocumentList) {
197         Map eresourceMap = new HashMap();
198         List eResourceList = new ArrayList();
199         for (OLEEResourceRecordDocument oleEResourceRecordDocument : eresourceDocumentList) {
200             eresourceMap.put(oleEResourceRecordDocument.getDocumentNumber(), oleEResourceRecordDocument);
201         }
202         eResourceList.addAll((Set) eresourceMap.keySet());
203         eresourceDocumentList.clear();
204         for (int eResourceCount = 0; eResourceCount < eResourceList.size(); eResourceCount++) {
205             eresourceDocumentList.add((OLEEResourceRecordDocument) eresourceMap.get(eResourceList.get(eResourceCount)));
206         }
207     }
208 
209 }
210