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