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.service.KRADServiceLocator; 013import org.kuali.rice.krad.util.GlobalVariables; 014import org.kuali.rice.krad.util.KRADConstants; 015import org.kuali.rice.krad.web.controller.UifControllerBase; 016import org.kuali.rice.krad.web.form.UifFormBase; 017import org.springframework.stereotype.Controller; 018import org.springframework.validation.BindingResult; 019import org.springframework.web.bind.annotation.ModelAttribute; 020import org.springframework.web.bind.annotation.RequestMapping; 021import org.springframework.web.servlet.ModelAndView; 022 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025import java.text.SimpleDateFormat; 026import java.util.*; 027 028/** 029 * Created with IntelliJ IDEA. 030 * User: chenchulakshmig 031 * Date: 6/26/13 032 * Time: 1:18 PM 033 * To change this template use File | Settings | File Templates. 034 */ 035@Controller 036@RequestMapping(value = "/searchEResourceController") 037public class OLEEResourceSearchController extends UifControllerBase { 038 039 private static final Logger LOG = Logger.getLogger(OLEEResourceSearchController.class); 040 private static final SimpleDateFormat dateFormat = new SimpleDateFormat(OLEConstants.OLEEResourceRecord.CREATED_DATE_FORMAT); 041 private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(OLEConstants.CHECK_IN_DATE_TIME_FORMAT); 042 043 @Override 044 protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) { 045 return new OLEEResourceSearchForm(); 046 } 047 048 @Override 049 @RequestMapping(params = "methodToCall=start") 050 public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 051 HttpServletRequest request, HttpServletResponse response) { 052 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 053 List<OLESearchCondition> oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 054 for (OLESearchCondition oleSearchCondition : oleSearchConditions) { 055 oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND); 056 } 057 return super.navigate(oleSearchForm, result, request, response); 058 } 059 060 @RequestMapping(params = "methodToCall=addSearchCriteria") 061 public ModelAndView addSearchCriteria(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 062 HttpServletRequest request, HttpServletResponse response) { 063 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 064 List<OLESearchCondition> oleSearchConditions = new ArrayList<OLESearchCondition>(); 065 oleSearchConditions = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 066 oleSearchConditions.add(new OLESearchCondition()); 067 for (OLESearchCondition oleSearchCondition : oleSearchConditions) { 068 if (oleSearchCondition.getOperator() == null) { 069 oleSearchCondition.setOperator(OLEConstants.OLEEResourceRecord.AND); 070 } 071 } 072 return super.navigate(oleSearchForm, result, request, response); 073 } 074 075 @RequestMapping(params = "methodToCall=search") 076 public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, 077 HttpServletRequest request, HttpServletResponse response) { 078 OLEEResourceSearchForm oleSearchForm = (OLEEResourceSearchForm) form; 079 OLEEResourceSearchServiceImpl oleEResourceSearchService = GlobalResourceLoader.getService(OLEConstants.OLEEResourceRecord.ERESOURSE_SEARCH_SERVICE); 080 List<OLESearchCondition> oleSearchConditionsList = oleSearchForm.getOleSearchParams().getSearchFieldsList(); 081 List<OLEEResourceRecordDocument> eresourceDocumentList = new ArrayList<OLEEResourceRecordDocument>(); 082 List<OLEEResourceRecordDocument> eresourceList = new ArrayList<OLEEResourceRecordDocument>(); 083 List<OLEEResourceRecordDocument> eresourceRecordDocumentList = new ArrayList<OLEEResourceRecordDocument>(); 084 try { 085 eresourceList = oleEResourceSearchService.performSearch(oleSearchConditionsList); 086 } catch (Exception e) { 087 LOG.error("Exception while hitting the docstore time" + e.getMessage()); 088 } 089 090 for (OLEEResourceRecordDocument oleEResourceRecordDocument: eresourceList){ 091 if (oleEResourceRecordDocument.getOleERSIdentifier()!=null){ 092 OLEEResourceRecordDocument document = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, oleEResourceRecordDocument.getOleERSIdentifier()); 093 if (document!=null){ 094 eresourceRecordDocumentList.add(document); 095 } 096 } 097 } 098 eresourceList = eresourceRecordDocumentList; 099 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