001package org.kuali.ole.select.controller; 002 003import org.apache.log4j.Logger; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.select.bo.OLESearchCondition; 006import org.kuali.ole.select.bo.OLESearchParams; 007import org.kuali.ole.select.document.OLEEResourceRecordDocument; 008import org.kuali.ole.select.form.OLEEResourceSearchForm; 009import org.kuali.ole.service.impl.OLEEResourceSearchServiceImpl; 010import org.kuali.ole.service.impl.OleLicenseRequestServiceImpl; 011import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 012import org.kuali.rice.krad.util.GlobalVariables; 013import org.kuali.rice.krad.util.KRADConstants; 014import org.kuali.rice.krad.web.controller.UifControllerBase; 015import org.kuali.rice.krad.web.form.UifFormBase; 016import org.springframework.stereotype.Controller; 017import org.springframework.validation.BindingResult; 018import org.springframework.web.bind.annotation.ModelAttribute; 019import org.springframework.web.bind.annotation.RequestMapping; 020import org.springframework.web.servlet.ModelAndView; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024import java.text.SimpleDateFormat; 025import java.util.*; 026 027/** 028 * Created with IntelliJ IDEA. 029 * User: chenchulakshmig 030 * Date: 6/26/13 031 * Time: 1:18 PM 032 * To change this template use File | Settings | File Templates. 033 */ 034@Controller 035@RequestMapping(value = "/searchEResourceController") 036public class OLEEResourceSearchController extends UifControllerBase { 037 038 private static final Logger LOG = Logger.getLogger(OLEEResourceSearchController.class); 039 private static final SimpleDateFormat dateFormat = new SimpleDateFormat(OLEConstants.OLEEResourceRecord.CREATED_DATE_FORMAT); 040 private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT); 041 042 @Override 043 protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) { 044 return new OLEEResourceSearchForm(); 045 } 046 047 @Override 048 @RequestMapping(params = "methodToCall=start") 049 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 050 HttpServletRequest request, HttpServletResponse response) { 051 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 052 return super.navigate(oleSearchForm, result, request, response); 053 } 054 055 @RequestMapping(params = "methodToCall=addSearchCriteria") 056 public ModelAndView addSearchCriteria(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 057 HttpServletRequest request, HttpServletResponse response) { 058 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 059 List<OLESearchCondition> oleSearchConditions = new ArrayList<OLESearchCondition>(); 060 oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 061 oleSearchConditions.add(new OLESearchCondition()); 062 for (OLESearchCondition oleSearchCondition : oleSearchConditions) { 063 if (oleSearchCondition.getOperator() == null) { 064 oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND); 065 } 066 } 067 return super.navigate(oleSearchForm, result, request, response); 068 } 069 070 @RequestMapping(params = "methodToCall=search") 071 public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 072 HttpServletRequest request, HttpServletResponse response) { 073 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 074 OLEEResourceSearchServiceImpl oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE); 075 List<OLESearchCondition> oleSearchConditionsList = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 076 List<OLEEResourceRecordDocument> eresourceDocumentList = new ArrayList<OLEEResourceRecordDocument>(); 077 List<OLEEResourceRecordDocument> eresourceList = new ArrayList<OLEEResourceRecordDocument>(); 078 try { 079 eresourceList = oleEResourceSearchService.performSearch(oleSearchConditionsList); 080 } 081 catch (Exception e){ 082 LOG.error("Error while hitting the docstore time" + e.getMessage()); 083 } 084 085 if (oleSearchForm.getStatus() != null) { 086 eresourceList = oleEResourceSearchService.statusNotNull(eresourceList, oleSearchForm.getStatus()); 087 } 088 if (oleSearchForm.isStatusDate()) { 089 try { 090 Date beginDate = oleSearchForm.getBeginDate(); 091 String begin = null; 092 if (beginDate != null) { 093 begin = dateFormat.format(beginDate); 094 } 095 Date enddate = oleSearchForm.getEndDate(); 096 String end = null; 097 if (enddate != null) { 098 end = dateFormat.format(enddate); 099 } 100 boolean isValid = false; 101 eresourceDocumentList.clear(); 102 for (OLEEResourceRecordDocument oleEResourceRecordDocumentList : eresourceList) { 103 String status = oleEResourceRecordDocumentList.getStatusDate(); 104 Date statusDate = simpleDateFormat.parse(status); 105 OleLicenseRequestServiceImpl oleLicenseRequestService = GlobalResourceLoader.getService(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_SERVICE); 106 isValid = oleLicenseRequestService.validateDate(statusDate, begin, end); 107 if (isValid) { 108 eresourceDocumentList.add(oleEResourceRecordDocumentList); 109 } 110 } 111 } catch (Exception e) { 112 LOG.error("Error while calling the licenseRequest service" + e.getMessage()); 113 throw new RuntimeException(e); 114 } 115 oleSearchForm.setEresourceDocumentList(eresourceDocumentList); 116 } else { 117 oleSearchForm.setEresourceDocumentList(eresourceList); 118 } 119 List<OLEEResourceRecordDocument> eresDocumentList = oleSearchForm.getEresourceDocumentList(); 120 removeDuplicateEresDocumentsFromList(eresDocumentList); 121 if (oleSearchForm.getEresourceDocumentList().size() == 0) { 122 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.NO_RECORD_FOUND); 123 } 124 return getUIFModelAndView(oleSearchForm); 125 } 126 127 @RequestMapping(params = "methodToCall=clearSearch") 128 public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 129 HttpServletRequest request, HttpServletResponse response) { 130 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 131 List<OLESearchCondition> oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 132 int searchConditionSize = oleSearchConditions.size(); 133 oleSearchForm.setOleSearchParams(new OLESearchParams()); 134 oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 135 for (int ersCount = 0; ersCount < searchConditionSize; ersCount++) { 136 oleSearchConditions.add(new OLESearchCondition()); 137 } 138 for (OLESearchCondition oleSearchCondition : oleSearchConditions) { 139 oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND); 140 } 141 oleSearchForm.setEresourceDocumentList(null); 142 oleSearchForm.setStatusDate(false); 143 oleSearchForm.setBeginDate(null); 144 oleSearchForm.setEndDate(null); 145 oleSearchForm.setStatus(null); 146 return getUIFModelAndView(oleSearchForm); 147 } 148 149 @RequestMapping(params = "methodToCall=cancel") 150 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 151 HttpServletRequest request, HttpServletResponse response) { 152 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 153 return super.cancel(oleSearchForm, result, request, response); 154 } 155 156 private void removeDuplicateEresDocumentsFromList(List<OLEEResourceRecordDocument> eresourceDocumentList) { 157 Map eresourceMap = new HashMap(); 158 List eResourceList = new ArrayList(); 159 for (OLEEResourceRecordDocument oleEResourceRecordDocument : eresourceDocumentList) { 160 eresourceMap.put(oleEResourceRecordDocument.getDocumentNumber(), oleEResourceRecordDocument); 161 } 162 eResourceList.addAll((Set) eresourceMap.keySet()); 163 eresourceDocumentList.clear(); 164 for (int eResourceCount = 0; eResourceCount < eResourceList.size(); eResourceCount++) { 165 eresourceDocumentList.add((OLEEResourceRecordDocument) eresourceMap.get(eResourceList.get(eResourceCount))); 166 } 167 } 168 169} 170