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 
182         getOleCalendarService().deleteCalendarCollections(oldCalendar);//added for OLE-5386
183 
184         return modelAndView1;
185     }
186 
187     @RequestMapping(params = "methodToCall=exceptionDateMethod")
188     public ModelAndView exceptionDateMethod(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
189                                             HttpServletRequest request, HttpServletResponse response) {
190 
191         MaintenanceDocumentForm mainForm = (MaintenanceDocumentForm) form;
192         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
193         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
194 
195         if (oleCalendar.getExceptionDayChecking().equals("Holiday")) {
196             oleCalendar.setHideTime(false);
197         } else {
198             oleCalendar.setHideTime(true);
199         }
200         return getUIFModelAndView(mainForm);
201     }
202 
203 
204     @RequestMapping(params = "methodToCall=" + "deleteDocument")
205     public ModelAndView deleteDocument(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
206                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
207 
208         LOG.debug(" Inside deleteDocument ");
209         MaintenanceDocument document = form.getDocument();
210         OleCalendar oleCalendar = (OleCalendar) document.getDocumentDataObject();
211         getOleCalendarService().deleteCalendarDocument(oleCalendar);
212         if(GlobalVariables.getMessageMap().getErrorCount()>0){
213             return getUIFModelAndView(form);
214         }
215         return back(form, result, request, response);
216     }
217 
218 
219     public OleCalendarService getOleCalendarService() {
220         if(oleCalendarService==null){
221             oleCalendarService = new OleCalendarServiceImpl();
222         }
223         return oleCalendarService;
224     }
225 
226     public void setOleCalendarService(OleCalendarService oleCalendarService) {
227         this.oleCalendarService = oleCalendarService;
228     }
229     @RequestMapping(params = "methodToCall=" + "calendarWeekDelete")
230     public ModelAndView calendarWeekDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
231                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
232         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
233         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
234         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
235         oleCalendar.getOleCalendarWeekDeleteList().add(oleCalendar.getOleCalendarWeekList().get(Integer.parseInt(selectedLineIndex)));
236         return deleteLine(form, result, request, response);
237     }
238     @RequestMapping(params = "methodToCall=" + "exceptionDayDelete")
239     public ModelAndView exceptionDayDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
240                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
241         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
242         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
243         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
244         oleCalendar.getOleCalendarExceptionDateDeleteList().add(oleCalendar.getOleCalendarExceptionDateList().get(Integer.parseInt(selectedLineIndex)));
245         return deleteLine(form, result, request, response);
246     }
247     @RequestMapping(params = "methodToCall=" + "exceptionPeriodListDelete")
248     public ModelAndView exceptionPeriodListDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
249                                            HttpServletRequest request, HttpServletResponse response) throws Exception {
250         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
251         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
252         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
253         oleCalendar.getOleCalendarExceptionPeriodDeleteList().add(oleCalendar.getOleCalendarExceptionPeriodList().get(Integer.parseInt(selectedLineIndex)));
254         if(!oleCalendar.getOleCalendarExceptionPeriodList().get(Integer.parseInt(selectedLineIndex)).getExceptionPeriodType().equals("Holiday")){
255             oleCalendar.getOleCalendarExceptionPeriodDeleteWeekList().addAll(oleCalendar.getOleCalendarExceptionPeriodList().get(Integer.parseInt(selectedLineIndex)).getOleCalendarExceptionPeriodWeekList());
256         }
257         return deleteLine(form, result, request, response);
258     }
259     @RequestMapping(params = "methodToCall=" + "exceptionPeriodWeekListDelete")
260     public ModelAndView exceptionPeriodWeekListDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
261                                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
262         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
263         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
264         Map<String,String> actionParameters = form.getActionParameters();
265         String subCollectionIndex = actionParameters.get(UifParameters.SELECTED_LINE_INDEX);
266         String mainCollectionIndex = StringUtils.substringBefore(StringUtils.substringAfter(actionParameters.get(UifParameters.SELLECTED_COLLECTION_PATH), "["), "]");
267         OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek = oleCalendar.getOleCalendarExceptionPeriodList().get(Integer.parseInt(mainCollectionIndex)).getOleCalendarExceptionPeriodWeekList().get(Integer.parseInt(subCollectionIndex));
268         if(oleCalendar.getCalendarId()!=null){
269             oleCalendar.getOleCalendarExceptionPeriodDeleteWeekList().add(oleCalendarExceptionPeriodWeek);    //inr list delete
270             oleCalendar.getOleCalendarExceptionPeriodList().get(Integer.parseInt(mainCollectionIndex)).getOleCalendarExceptionPeriodWeekList().remove(Integer.parseInt(subCollectionIndex));
271             return getUIFModelAndView(form);
272         }else{
273             oleCalendar.getOleCalendarExceptionPeriodDeleteWeekList().add(oleCalendarExceptionPeriodWeek);
274             return deleteLine(form, result, request, response);
275         }
276     }
277     @RequestMapping(params = "methodToCall=" + "exceptionPeriodListAdd")
278     public ModelAndView exceptionPeriodListAdd(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
279                                                   HttpServletRequest request, HttpServletResponse response) throws Exception {
280         String selectedLineIndex = form.getActionParamaterValue(UifParameters.SELECTED_LINE_INDEX);
281         MaintenanceDocument document = (MaintenanceDocument) form.getDocument();
282         OleCalendar oleCalendar = (OleCalendar) document.getNewMaintainableObject().getDataObject();
283         List<OleCalendarExceptionPeriod>  oleCalendarExceptionPeriods=oleCalendar.getOleCalendarExceptionPeriodList();
284         String selectedCollectionPath = form.getActionParamaterValue(UifParameters.SELLECTED_COLLECTION_PATH);
285         CollectionGroup collectionGroup = form.getPostedView().getViewIndex().getCollectionGroupByPath(
286                 selectedCollectionPath);
287         String addLinePath = collectionGroup.getAddLineBindingInfo().getBindingPath();
288         Object eventObject = ObjectPropertyUtils.getPropertyValue(form, addLinePath);
289         OleCalendarExceptionPeriod oleCalendarExceptionPeriod=(OleCalendarExceptionPeriod)eventObject;
290         boolean isValid=true;
291         if(oleCalendarExceptionPeriod!=null){
292 
293                 if(oleCalendarExceptionPeriod.getBeginDate()==null ||
294                         oleCalendarExceptionPeriod.getBeginDate()!=null && oleCalendarExceptionPeriod.getBeginDate().toString().equalsIgnoreCase("")){
295                     GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_PERIOD_BEGIN_DATE_EMPTY);
296                     isValid=false;
297                 }
298                 if(isValid && oleCalendarExceptionPeriod.getEndDate()==null ||
299                         oleCalendarExceptionPeriod.getEndDate()!=null && oleCalendarExceptionPeriod.getEndDate().toString().equalsIgnoreCase("")){
300                     GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod",OLEConstants.CALENDAR_PERIOD_END_DATE_EMPTY);
301                     isValid=false;
302                 }
303 
304                 if (isValid && oleCalendarExceptionPeriod.getBeginDate() != null && !oleCalendarExceptionPeriod.getBeginDate().toString().equalsIgnoreCase("")
305                         && !oleCalendarExceptionPeriod.getEndDate().toString().equalsIgnoreCase("") && oleCalendarExceptionPeriod.getEndDate() != null) {
306 
307                     if(isValid && oleCalendarExceptionPeriod.getBeginDate().after(oleCalendarExceptionPeriod.getEndDate())){
308                         GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_BEGIN_END_DATE);
309                         isValid=false;
310                     }
311                     if(oleCalendarExceptionPeriods!=null){
312                         for (OleCalendarExceptionPeriod calendarExceptionPeriod : oleCalendarExceptionPeriods) {
313                             if (validateDateRange(calendarExceptionPeriod.getBeginDate(), calendarExceptionPeriod.getEndDate(), oleCalendarExceptionPeriod.getBeginDate())) {
314                                 GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod",OLEConstants.CALENDAR_PERIOD_RANGE_OVERLAP_ERROR);
315                                 isValid=false;
316                                 break;
317                             }
318                             if (validateDateRange(calendarExceptionPeriod.getBeginDate(), calendarExceptionPeriod.getEndDate(), oleCalendarExceptionPeriod.getEndDate())) {
319                                 GlobalVariables.getMessageMap().putErrorForSectionId("ExceptionPeriod", OLEConstants.CALENDAR_PERIOD_RANGE_OVERLAP_ERROR);
320                                 isValid=false;
321                                 break;
322                             }
323                         }
324                     }
325 
326                 }
327             if (isValid) {
328                 View view = form.getPostedView();
329                 view.getViewHelperService().processCollectionAddLine(view, form, selectedCollectionPath);
330             } else {
331                 return getUIFModelAndView(form);
332             }
333 
334         }
335         return getUIFModelAndView(form);
336     }
337 
338     private boolean validateDateRange(Date fromDate ,Date toDate ,Date date){
339         if(date.after(fromDate) && date.before(toDate) || DateUtils.isSameDay(fromDate,date) || DateUtils.isSameDay(toDate,date)){
340             return true;
341         } else {
342             return false;
343         }
344     }
345 
346 }