001package org.kuali.ole.deliver.calendar.controller;
002
003import org.apache.commons.lang.StringUtils;
004import org.apache.commons.lang.time.DateUtils;
005import org.apache.log4j.Logger;
006import org.kuali.ole.OLEConstants;
007import org.kuali.ole.deliver.calendar.bo.*;
008import org.kuali.ole.deliver.calendar.service.OleCalendarService;
009import org.kuali.ole.deliver.calendar.service.impl.OleCalendarDocumentServiceImpl;
010import org.kuali.ole.deliver.calendar.service.impl.OleCalendarServiceImpl;
011import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
012import org.kuali.rice.krad.maintenance.MaintenanceDocument;
013import org.kuali.rice.krad.maintenance.MaintenanceUtils;
014import org.kuali.rice.krad.service.KRADServiceLocator;
015import org.kuali.rice.krad.service.MaintenanceDocumentService;
016import org.kuali.rice.krad.uif.UifParameters;
017import org.kuali.rice.krad.uif.container.CollectionGroup;
018import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
019import org.kuali.rice.krad.uif.view.View;
020import org.kuali.rice.krad.util.GlobalVariables;
021import org.kuali.rice.krad.util.KRADConstants;
022import org.kuali.rice.krad.web.controller.MaintenanceDocumentController;
023import org.kuali.rice.krad.web.form.DocumentFormBase;
024import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
025import org.springframework.stereotype.Controller;
026import org.springframework.validation.BindingResult;
027import org.springframework.web.bind.annotation.ModelAttribute;
028import org.springframework.web.bind.annotation.RequestMapping;
029import org.springframework.web.servlet.ModelAndView;
030
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033import java.sql.Timestamp;
034import java.util.*;
035
036/**
037 * Created with IntelliJ IDEA.
038 * User: arjuns
039 * Date: 7/21/13
040 * Time: 1:32 PM
041 * To change this template use File | Settings | File Templates.
042 */
043@Controller
044@RequestMapping(value = "/OleCalendarController")
045public class OleCalendarController extends MaintenanceDocumentController {
046
047    private static final Logger LOG = Logger.getLogger(OleCalendarController.class);
048    private OleCalendarService oleCalendarService;
049
050
051    /**
052     * Setups a new <code>MaintenanceDocumentView</code> with the edit maintenance
053     * action
054     */
055    @RequestMapping(params = "methodToCall=" + KRADConstants.Maintenance.METHOD_TO_CALL_EDIT)
056    public ModelAndView maintenanceEdit(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
057                                        HttpServletRequest request, HttpServletResponse response) throws Exception {
058        return super.maintenanceEdit(form, result, request, response);
059    }
060
061
062
063    @RequestMapping(params = "methodToCall=" + "maintenanceDelete")
064    public ModelAndView maintenanceDelete(@ModelAttribute("KualiForm") MaintenanceDocumentForm form, BindingResult result,
065                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
066        LOG.debug(" Inside maintenanceDelete ");
067        setupMaintenanceForDelete(form, request, OLEConstants.OlePatron.OLE_PATRON_DELETE);
068        return getUIFModelAndView(form);
069    }
070
071    /**
072     * This method returns the instance of olePatronMaintenanceDocumentService
073     *
074     * @return olePatronMaintenanceDocumentService(MaintenanceDocumentService)
075     */
076    @Override
077    protected MaintenanceDocumentService getMaintenanceDocumentService() {
078        return GlobalResourceLoader.getService(OLEConstants.OLE_CALENDAR_DOC_SERVICE);
079    }
080
081    /**
082     * This method populates confirmation to delete the document.
083     *
084     * @param form
085     * @param request
086     * @param maintenanceAction
087     */
088    protected void setupMaintenanceForDelete(MaintenanceDocumentForm form, HttpServletRequest request, String maintenanceAction) {
089        LOG.debug(" Inside setupMaintenanceForDelete ");
090        MaintenanceDocument document = form.getDocument();
091        if (document == null) {
092            document = getMaintenanceDocumentService()
093                    .setupNewMaintenanceDocument(form.getDataObjectClassName(), form.getDocTypeName(),
094                            maintenanceAction);
095
096            form.setDocument(document);
097            form.setDocTypeName(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
098        }
099        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}