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