001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.calendar.web;
017    
018    import javax.servlet.http.HttpServletRequest;
019    import javax.servlet.http.HttpServletResponse;
020    
021    import org.apache.struts.action.ActionForm;
022    import org.apache.struts.action.ActionForward;
023    import org.apache.struts.action.ActionMapping;
024    import org.kuali.hr.time.base.web.TkAction;
025    import org.kuali.hr.time.calendar.CalendarEntries;
026    import org.kuali.hr.time.calendar.CalendarEntryPeriodType;
027    import org.kuali.hr.time.calendar.service.CalendarEntriesService;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    
030    import org.kuali.rice.krad.util.GlobalVariables;
031    
032    public class CalendarEntryAction extends TkAction {
033    
034            public ActionForward createCalendarEntry(ActionMapping mapping,
035                            ActionForm form, HttpServletRequest request,
036                            HttpServletResponse response) throws Exception {
037                    CalendarEntryActionForm ceaf = (CalendarEntryActionForm) form;
038                    if (ceaf.getNoOfPeriods() == null || ceaf.getNoOfPeriods() < 1) {
039                            GlobalVariables.getMessageMap().putError("document.noOfPeriods",
040                                            "periods.greater.than.one");
041                            return mapping.findForward("basic");
042                    } else if (ceaf.getHrPyCalendarEntryId() == null) {
043                            GlobalVariables.getMessageMap().putError(
044                                            "document.hrPyCalendarEntryId",
045                                            "error.calendar.not.available");
046                            return mapping.findForward("basic");
047                    }
048            CalendarEntryPeriodType periodType = ceaf.getCalendarEntryPeriodType() == null ?
049                                                    CalendarEntryPeriodType.BI_WEEKLY :
050                                                    CalendarEntryPeriodType.fromCode(ceaf.getCalendarEntryPeriodType());
051            CalendarEntriesService calendarEntriesService = TkServiceLocator.getCalendarEntriesService();
052                    CalendarEntries calendarEntries = calendarEntriesService.getCalendarEntries(
053                                                    ceaf.getHrPyCalendarEntryId().toString());
054                    if (calendarEntries == null) {
055                            GlobalVariables.getMessageMap().putError(
056                                            "document.hrPyCalendarEntryId",
057                                            "error.calendar.not.available");
058                    } else {
059                                    for (int i = 0; i < ceaf.getNoOfPeriods(); i++) {
060                                            CalendarEntries nextCalendarEntries = calendarEntriesService.getNextCalendarEntriesByCalendarId(
061                                                                            calendarEntries.getHrCalendarId(), calendarEntries);
062                                            if (nextCalendarEntries == null) {
063                            calendarEntries = calendarEntriesService.createNextCalendarEntry(calendarEntries, periodType);
064                                            }
065                                    }
066                                    ceaf.setMessage("Calendar entry sucessfully created.");
067                    }
068                    return mapping.findForward("basic");
069            }
070            
071    }