View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.enrollment.class2.acal.controller;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.core.api.util.RiceKeyConstants;
21  import org.kuali.rice.krad.uif.UifParameters;
22  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  import org.kuali.rice.krad.util.KRADConstants;
25  import org.kuali.rice.krad.web.controller.UifControllerBase;
26  import org.kuali.rice.krad.web.form.UifFormBase;
27  import org.kuali.student.enrollment.uif.util.KSControllerHelper;
28  import org.kuali.student.r2.common.util.constants.AcademicCalendarServiceConstants;
29  import org.kuali.student.enrollment.acal.dto.AcademicCalendarInfo;
30  import org.kuali.student.enrollment.acal.dto.HolidayCalendarInfo;
31  import org.kuali.student.enrollment.acal.dto.TermInfo;
32  import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
33  import org.kuali.student.enrollment.class2.acal.form.CalendarSearchForm;
34  import org.kuali.student.enrollment.class2.acal.service.CalendarSearchViewHelperService;
35  import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
36  import org.kuali.student.r2.common.dto.ContextInfo;
37  import org.kuali.student.r2.common.dto.StatusInfo;
38  import org.kuali.student.mock.utilities.TestHelper;
39  import org.springframework.stereotype.Controller;
40  import org.springframework.validation.BindingResult;
41  import org.springframework.web.bind.annotation.ModelAttribute;
42  import org.springframework.web.bind.annotation.RequestMapping;
43  import org.springframework.web.bind.annotation.RequestMethod;
44  import org.springframework.web.servlet.ModelAndView;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  import javax.xml.namespace.QName;
49  import java.util.ArrayList;
50  import java.util.Collection;
51  import java.util.List;
52  import java.util.Properties;
53  
54  /**
55   * This class handles all the request for Searching Holiday calender, Academic Calendars and Academic terms.
56   * This handles requests from CalendarSearchView for different calendars and terms.
57   * This controller is mapped to the view defined in <code>CalendarSearchView.xml</code>
58   *
59   * @author Kuali Student Team
60   *
61   */
62  
63  @Controller
64  @RequestMapping(value = "/calendarSearch")
65  public class CalendarSearchController  extends UifControllerBase {
66  
67      private transient AcademicCalendarService acalService;
68      private ContextInfo contextInfo;
69  
70      @Override
71      protected UifFormBase createInitialForm(HttpServletRequest request) {
72           return new CalendarSearchForm();
73      }
74  
75  
76      @Override
77      @RequestMapping(method = RequestMethod.GET, params = "methodToCall=start")
78      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
79                                HttpServletRequest request, HttpServletResponse response) {
80          CalendarSearchForm calendarSearchForm = (CalendarSearchForm)form;
81  
82          String calendarSearchType = request.getParameter(CalendarConstants.CALENDAR_SEARCH_TYPE);
83          if (null != calendarSearchType) {
84              calendarSearchForm.setCalendarType(calendarSearchType);
85          }
86  
87          return super.start(form, result, request, response);
88      }
89  
90       /**
91       * Method used to search atps
92       */
93      @RequestMapping(params = "methodToCall=search")
94      public ModelAndView search(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result,
95                                                HttpServletRequest request, HttpServletResponse response) throws Exception {
96          String calendarType = searchForm.getCalendarType();
97  
98          resetForm(searchForm);
99          CalendarSearchViewHelperService viewHelperService = (CalendarSearchViewHelperService) KSControllerHelper.getViewHelperService(searchForm);
100 
101         if(calendarType.equals(CalendarConstants.HOLIDAYCALENDER)){
102                List<HolidayCalendarInfo> hCals = viewHelperService.searchForHolidayCalendars(searchForm.getName(), searchForm.getYear(), getContextInfo());
103                searchForm.setHolidayCalendars(hCals);
104         } else if(calendarType.equals(CalendarConstants.ACADEMICCALENDER)) {
105                List<AcademicCalendarInfo> aCals = viewHelperService.searchForAcademicCalendars(searchForm.getName(), searchForm.getYear(), getContextInfo());
106                searchForm.setAcademicCalendars(aCals);
107         } else if(calendarType.equals(CalendarConstants.TERM)){
108                List<TermInfo> terms = viewHelperService.searchForTerms(searchForm.getName(), searchForm.getYear(), getContextInfo());
109                searchForm.setTerms(terms);
110         } else {
111             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: invalid calendar type.");
112         }
113 
114        return getUIFModelAndView(searchForm, null);
115     }
116 
117     /**
118      * This is called when the user clicked on Search button in the Calendar Search page.
119      *
120      * @param searchForm
121      * @param result
122      * @param request
123      * @param response
124      * @return
125      */
126 
127     @RequestMapping(params = "methodToCall=view")
128     public ModelAndView view(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result,
129                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
130 
131         Object atp = getSelectedAtp(searchForm, "view");
132         Properties urlParameters;
133         String controllerPath;
134         CalendarSearchViewHelperService viewHelperService = (CalendarSearchViewHelperService) KSControllerHelper.getViewHelperService(searchForm);
135 
136          if(atp instanceof HolidayCalendarInfo){
137              urlParameters = viewHelperService.buildHCalURLParameters((HolidayCalendarInfo) atp, CalendarConstants.HC_VIEW_METHOD, true, getContextInfo());
138              controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH;
139          } else if(atp instanceof AcademicCalendarInfo) {
140              urlParameters = viewHelperService.buildACalURLParameters((AcademicCalendarInfo) atp, CalendarConstants.AC_VIEW_METHOD, true, getContextInfo());
141              controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH;
142          } else if(atp instanceof TermInfo){
143              urlParameters = viewHelperService.buildTermURLParameters((TermInfo) atp, CalendarConstants.AC_VIEW_METHOD, true, getContextInfo());
144              controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH;
145          } else {
146              throw new RuntimeException("Invalid calendar type. This search supports Acal/HCal/Term only");
147          }
148 
149          return super.performRedirect(searchForm,controllerPath, urlParameters);
150     }
151 
152     /**
153      * This is called when the user clicked on Edit button in the Calendar Search page.
154      *
155      * @param searchForm
156      * @param result
157      * @param request
158      * @param response
159      * @return
160      */
161     @RequestMapping(params = "methodToCall=edit")
162     public ModelAndView edit(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result,
163                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
164 
165         Object atp = getSelectedAtp(searchForm, "edit");
166 
167         Properties urlParameters;
168         String controllerPath;
169         CalendarSearchViewHelperService viewHelperService = (CalendarSearchViewHelperService) KSControllerHelper.getViewHelperService(searchForm);
170 
171          if(atp instanceof HolidayCalendarInfo){
172              urlParameters = viewHelperService.buildHCalURLParameters((HolidayCalendarInfo) atp, CalendarConstants.HC_EDIT_METHOD, false, getContextInfo());
173              controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH;
174          } else if(atp instanceof AcademicCalendarInfo) {
175              urlParameters = viewHelperService.buildACalURLParameters((AcademicCalendarInfo) atp, CalendarConstants.AC_EDIT_METHOD, false, getContextInfo());
176              controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH;
177          } else if(atp instanceof TermInfo){
178              urlParameters = viewHelperService.buildTermURLParameters((TermInfo) atp, CalendarConstants.AC_EDIT_METHOD, false, getContextInfo());
179              controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH;
180          } else {
181              throw new RuntimeException("Invalid calendar type. This search supports Acal/HCal/Term only");
182          }
183 
184         return super.performRedirect(searchForm,controllerPath, urlParameters);
185 
186     }
187 
188     /**
189      * This is called when the user clicked on Copy button in the Calendar Search page with Holiday calendar selected.
190      *
191      * @param searchForm
192      * @param result
193      * @param request
194      * @param response
195      * @return
196      */
197     @RequestMapping(params = "methodToCall=copy")
198     public ModelAndView copy(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result,
199                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
200 
201         Object atp = getSelectedAtp(searchForm, "copy");
202 
203         Properties urlParameters;
204         String controllerPath;
205         CalendarSearchViewHelperService viewHelperService = (CalendarSearchViewHelperService) KSControllerHelper.getViewHelperService(searchForm);
206 
207          if(atp instanceof HolidayCalendarInfo){
208              controllerPath = CalendarConstants.HCAL_CONTROLLER_PATH;
209              urlParameters = viewHelperService.buildHCalURLParameters((HolidayCalendarInfo) atp, CalendarConstants.HC_COPY_METHOD, false, getContextInfo());
210          }else if(atp instanceof AcademicCalendarInfo) {
211              urlParameters = viewHelperService.buildACalURLParameters((AcademicCalendarInfo) atp, CalendarConstants.AC_COPY_METHOD, false, getContextInfo());
212              controllerPath = CalendarConstants.ACAL_CONTROLLER_PATH;
213          } else {
214              throw new RuntimeException("Invalid calendar type. This search supports Acal and HCal only");
215          }
216 
217         return super.performRedirect(searchForm,controllerPath, urlParameters);
218 
219     }
220 
221     /**
222      * This is called when the user clicked on Delete button in the Calendar Search page with Holiday calendar selected.
223      *
224      * @param searchForm
225      * @param result
226      * @param request
227      * @param response
228      * @return
229      */
230     @RequestMapping(params = "methodToCall=delete")
231     public ModelAndView delete(@ModelAttribute("KualiForm") CalendarSearchForm searchForm, BindingResult result,
232                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
233         Object atp = getSelectedAtp(searchForm, "delete");
234 
235          if(atp instanceof HolidayCalendarInfo){
236              StatusInfo status = getAcademicCalendarService().deleteHolidayCalendar(((HolidayCalendarInfo)atp).getId(),getContextInfo());
237              if (status.getIsSuccess()){
238                  GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.INFO_SEARCH_DELETE_SUCCESS,((HolidayCalendarInfo) atp).getName());
239                  searchForm.getHolidayCalendars().remove(atp);
240              } else{
241                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage());
242              }
243          } else if(atp instanceof AcademicCalendarInfo) {
244              StatusInfo status = getAcademicCalendarService().deleteAcademicCalendar(((AcademicCalendarInfo)atp).getId(),getContextInfo());
245              if (status.getIsSuccess()){
246                  GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.INFO_SEARCH_DELETE_SUCCESS,((AcademicCalendarInfo) atp).getName());
247                  searchForm.getAcademicCalendars().remove(atp);
248              } else{
249                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage());
250              }
251          } else if(atp instanceof TermInfo){
252              StatusInfo status = getAcademicCalendarService().deleteTerm(((TermInfo)atp).getId(),getContextInfo());
253              if (status.getIsSuccess()){
254                  GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_MESSAGES, CalendarConstants.MessageKeys.INFO_SEARCH_DELETE_SUCCESS,((TermInfo) atp).getName());
255                  searchForm.getTerms().remove(atp);
256              } else{
257                  GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, status.getMessage());
258              }
259          } else {
260              GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, "ERROR: invalid calendar type.");
261          }
262 
263         return getUIFModelAndView(searchForm);
264 
265     }
266 
267     private void resetForm(CalendarSearchForm searchForm) {
268         searchForm.setHolidayCalendars(new ArrayList<HolidayCalendarInfo>());
269         searchForm.setAcademicCalendars(new ArrayList<AcademicCalendarInfo>());
270         searchForm.setTerms(new ArrayList<TermInfo>());
271     }
272 
273     private Object getSelectedAtp(CalendarSearchForm searchForm, String actionLink){
274         String selectedCollectionPath = searchForm.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
275         if (StringUtils.isBlank(selectedCollectionPath)) {
276             throw new RuntimeException("Selected collection was not set for " + actionLink);
277         }
278 
279         int selectedLineIndex = KSControllerHelper.getSelectedCollectionLineIndex(searchForm);
280 
281         Collection<Object> collection = ObjectPropertyUtils.getPropertyValue(searchForm, selectedCollectionPath);
282         Object atp = ((List<Object>) collection).get(selectedLineIndex);
283 
284         return atp;
285     }
286 
287     private ContextInfo getContextInfo() {
288         if (null == contextInfo) {
289             //TODO - get real ContextInfo
290             contextInfo = TestHelper.getContext1();
291         }
292         return contextInfo;
293     }
294 
295     protected AcademicCalendarService getAcademicCalendarService(){
296         if(acalService == null) {
297             acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
298         }
299         return acalService;
300     }
301 }