View Javadoc
1   package org.kuali.ole.deliver.calendar.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.commons.lang.time.DateUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.calendar.bo.*;
8   import org.kuali.ole.deliver.calendar.service.OleCalendarService;
9   import org.kuali.ole.deliver.calendar.service.impl.OleCalendarDocumentServiceImpl;
10  import org.kuali.ole.deliver.calendar.service.impl.OleCalendarServiceImpl;
11  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
12  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
13  import org.kuali.rice.krad.maintenance.MaintenanceUtils;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  import org.kuali.rice.krad.service.MaintenanceDocumentService;
16  import org.kuali.rice.krad.uif.UifParameters;
17  import org.kuali.rice.krad.uif.container.CollectionGroup;
18  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
19  import org.kuali.rice.krad.uif.view.View;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  import org.kuali.rice.krad.util.KRADConstants;
22  import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
23  import org.kuali.rice.krad.web.form.DocumentFormBase;
24  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
25  import org.springframework.stereotype.Controller;
26  import org.springframework.validation.BindingResult;
27  import org.springframework.web.bind.annotation.ModelAttribute;
28  import org.springframework.web.bind.annotation.RequestMapping;
29  import org.springframework.web.servlet.ModelAndView;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.sql.Timestamp;
34  import java.util.*;
35  
36  /**
37   * Created with IntelliJ IDEA.
38   * User: arjuns
39   * Date: 7/21/13
40   * Time: 1:32 PM
41   * To change this template use File | Settings | File Templates.
42   */
43  @Controller
44  @RequestMapping(value = "/OleCalendarController")
45  public class OleCalendarController extends MaintenanceDocumentController {
46  
47      private static final Logger LOG = Logger.getLogger(OleCalendarController.class);
48      private OleCalendarService oleCalendarService;
49  
50  
51      /**
52       * Setups a new <code>MaintenanceDocumentView</code> with the edit maintenance
53       * action
54       */
55      @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
56      public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
57                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
58          return super.maintenanceEdit(form, result, request, response);
59      }
60  
61  
62  
63      @RequestMapping(params = "methodToCall=" + "maintenanceDelete")
64      public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
65                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
66          LOG.debug(" Inside maintenanceDelete ");
67          setupMaintenanceForDelete(form, request, OLEConstants.OlePatron.OLE_PATRON_DELETE);
68          return getUIFModelAndView(form);
69      }
70  
71      /**
72       * This method returns the instance of olePatronMaintenanceDocumentService
73       *
74       * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
75       */
76      @Override
77      protected MaintenanceDocumentService getMaintenanceDocumentService() {
78          return GlobalResourceLoader.getService(OLEConstants.OLE_CALENDAR_DOC_SERVICE);
79      }
80  
81      /**
82       * This method populates confirmation to delete the document.
83       *
84       * @param form
85       * @param request
86       * @param maintenanceAction
87       */
88      protected void setupMaintenanceForDelete(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) {
89          LOG.debug(" Inside setupMaintenanceForDelete ");
90          MaintenanceDocument document = form.getDocument();
91          if (document == null) {
92              document = getMaintenanceDocumentService()
93                      .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
94                              maintenanceAction);
95  
96              form.setDocument(document);
97              form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
98          }
99          form.setMaintenanceAction(maintenanceAction);
100         OleCalendarDocumentServiceImpl oleCalendarDocumentService = (OleCalendarDocumentServiceImpl) getMaintenanceDocumentService();
101         oleCalendarDocumentService.setupMaintenanceObjectForDelete(document, maintenanceAction, request.getParameterMap());
102         MaintenanceUtils.checkForLockingDocument(document, false);
103     }
104 
105     /**
106      * Performs the blanket approve workflow action on the form document instance
107      *
108      * @param form - document form base containing the document instance that will be blanket approved
109      * @return ModelAndView
110      */
111     @RequestMapping(params = "methodToCall=blanketApprove")
112     public ModelAndView blanketApprove(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
113                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
114 
115         LOG.debug(" Inside route method of patron maintenance controller ");
116         ModelAndView modelAndView;
117         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
118         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
119         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
120         boolean isNew = document.isNew();
121         getOleCalendarService().generalInfoValidation(oleCalendar, isNew);
122         ModelAndView modelAndView1 = super.blanketApprove(mainForm, result, request, response);//changed
123         getOleCalendarService().assignEndDate(oleCalendar);
124         OleCalendar oldCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
125         getOleCalendarService().convert12HrsFormat(oldCalendar);
126         return modelAndView1;
127     }
128 
129 
130     @RequestMapping(params = "methodToCall=calendarPopUp")
131     public ModelAndView calendarPopUp(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
132                               HttpServletRequest request, HttpServletResponse response) {
133         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
134         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
135         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
136         getOleCalendarService().validateCalendarDocument(oleCalendar);
137         MaintenanceUtils.checkForLockingDocument(document, false);
138         return getUIFModelAndView(mainForm);
139     }
140 
141     @RequestMapping(params = "methodToCall=cancelOperation")
142     public ModelAndView cancelOperation(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
143                                       HttpServletRequest request, HttpServletResponse response) {
144         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
145         MaintenanceDocument maintenanceDocument = (MaintenanceDocument)mainForm.getDocument();
146         OleCalendar oleCalendar = (OleCalendar)maintenanceDocument.getNewMaintainableObject().getDataObject();
147         oleCalendar.setCancelOperationFlag(true);
148         oleCalendar.setCancelOperationEndDateFlag(true);
149         return getUIFModelAndView(mainForm);
150     }
151 
152     /**
153      * This method returns the instance of olePatronMaintenanceDocumentService
154      *
155      * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
156      */
157     @Override
158     @RequestMapping(params = "methodToCall=route")
159     public ModelAndView route(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
160                               HttpServletRequest request, HttpServletResponse response) {
161         LOG.debug(" Inside route method of patron maintenance controller ");
162         ModelAndView modelAndView;
163         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
164         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
165         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
166         oleCalendar.setCancelOperationFlag(true);
167 //      Concat of 23:59:59 to EndDate while setting endDate manually
168         if( oleCalendar.getEndDate()!= null && !oleCalendar.getEndDate().toString().equals("")){
169         String endDateString = oleCalendar.getEndDate().toString();
170         String endDateSplit[] = endDateString.split(" ");
171         oleCalendar.setEndDate(Timestamp.valueOf(endDateSplit[0]+" "+"23:59:59"));
172         }
173 //      Concat of 23:59:59 to EndDate while setting endDate manually
174         oleCalendar.sortCalendarWeek(oleCalendar); //added for OLE-5381
175         boolean isNew = document.isNew();
176         getOleCalendarService().generalInfoValidation(oleCalendar, isNew);
177         ModelAndView modelAndView1 = super.route(mainForm, result, request, response);//changed
178         getOleCalendarService().assignEndDate(oleCalendar);
179         OleCalendar oldCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
180         getOleCalendarService().convert12HrsFormat(oldCalendar);
181         return modelAndView1;
182     }
183 
184     @RequestMapping(params = "methodToCall=exceptionDateMethod")
185     public ModelAndView exceptionDateMethod(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
186                                             HttpServletRequest request, HttpServletResponse response) {
187 
188         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
189         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
190         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
191 
192         if (oleCalendar.getExceptionDayChecking().equals("Holiday")) {
193             oleCalendar.setHideTime(false);
194         } else {
195             oleCalendar.setHideTime(true);
196         }
197         return getUIFModelAndView(mainForm);
198     }
199 
200 
201     @RequestMapping(params = "methodToCall=" + "deleteDocument")
202     public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
203                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
204 
205         LOG.debug(" Inside deleteDocument ");
206         MaintenanceDocument document = form.getDocument();
207         OleCalendar oleCalendar = (OleCalendar) document.getDocumentDataObject();
208         getOleCalendarService().deleteCalendarDocument(oleCalendar);
209         if(GlobalVariables.getMessageMap().getErrorCount()>0){
210             return getUIFModelAndView(form);
211         }
212         return back(form, result, request, response);
213     }
214 
215 
216     public OleCalendarService getOleCalendarService() {
217         if(oleCalendarService==null){
218             oleCalendarService = new OleCalendarServiceImpl();
219         }
220         return oleCalendarService;
221     }
222 
223     public void setOleCalendarService(OleCalendarService oleCalendarService) {
224         this.oleCalendarService = oleCalendarService;
225     }
226 
227     @RequestMapping(params = "methodToCall=" + "exceptionPeriodListAdd")
228     public ModelAndView exceptionPeriodListAdd(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
229                                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
230         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
231         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
232         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
233         List<OleCalendarExceptionPeriod>  oleCalendarExceptionPeriods=oleCalendar.getOleCalendarExceptionPeriodList();
234         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
235         CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
236                 selectedCollectionPath);
237         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
238         Object eventObject = ObjectPropertyUtils.getPropertyValue(form, addLinePath);
239         OleCalendarExceptionPeriod oleCalendarExceptionPeriod=(OleCalendarExceptionPeriod)eventObject;
240         boolean isValid=true;
241         if(oleCalendarExceptionPeriod!=null){
242 
243                 if(oleCalendarExceptionPeriod.getBeginDate()==null ||
244                         oleCalendarExceptionPeriod.getBeginDate()!=null && oleCalendarExceptionPeriod.getBeginDate().toString().equalsIgnoreCase("")){
245                     GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_PERIOD_BEGIN_DATE_EMPTY);
246                     isValid=false;
247                 }
248                 if(isValid && oleCalendarExceptionPeriod.getEndDate()==null ||
249                         oleCalendarExceptionPeriod.getEndDate()!=null && oleCalendarExceptionPeriod.getEndDate().toString().equalsIgnoreCase("")){
250                     GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod",OLEConstants.CALENDAR_PERIOD_END_DATE_EMPTY);
251                     isValid=false;
252                 }
253 
254                 if (isValid && oleCalendarExceptionPeriod.getBeginDate() != null && !oleCalendarExceptionPeriod.getBeginDate().toString().equalsIgnoreCase("")
255                         && !oleCalendarExceptionPeriod.getEndDate().toString().equalsIgnoreCase("") && oleCalendarExceptionPeriod.getEndDate() != null) {
256 
257                     if(isValid && oleCalendarExceptionPeriod.getBeginDate().after(oleCalendarExceptionPeriod.getEndDate())){
258                         GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_BEGIN_END_DATE);
259                         isValid=false;
260                     }
261                     if(oleCalendarExceptionPeriods!=null){
262                         for (OleCalendarExceptionPeriod calendarExceptionPeriod : oleCalendarExceptionPeriods) {
263                             if (validateDateRange(calendarExceptionPeriod.getBeginDate(), calendarExceptionPeriod.getEndDate(), oleCalendarExceptionPeriod.getBeginDate())) {
264                                 GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod",OLEConstants.CALENDAR_PERIOD_RANGE_OVERLAP_ERROR);
265                                 isValid=false;
266                                 break;
267                             }
268                             if (validateDateRange(calendarExceptionPeriod.getBeginDate(), calendarExceptionPeriod.getEndDate(), oleCalendarExceptionPeriod.getEndDate())) {
269                                 GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_PERIOD_RANGE_OVERLAP_ERROR);
270                                 isValid=false;
271                                 break;
272                             }
273                         }
274                     }
275 
276                 }
277             if (isValid) {
278                 View view = form.getPostedView();
279                 view.getViewHelperService().processCollectionAddLine(view, form, selectedCollectionPath);
280             } else {
281                 return getUIFModelAndView(form);
282             }
283 
284         }
285         return getUIFModelAndView(form);
286     }
287 
288     private boolean validateDateRange(Date fromDate ,Date toDate ,Date date){
289         if(date.after(fromDate) && date.before(toDate) || DateUtils.isSameDay(fromDate,date) || DateUtils.isSameDay(toDate,date)){
290             return true;
291         } else {
292             return false;
293         }
294     }
295 
296 }