View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.calendar.web;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.struts.action.ActionForm;
22  import org.apache.struts.action.ActionForward;
23  import org.apache.struts.action.ActionMapping;
24  import org.kuali.hr.time.base.web.TkAction;
25  import org.kuali.hr.time.calendar.CalendarEntries;
26  import org.kuali.hr.time.calendar.CalendarEntryPeriodType;
27  import org.kuali.hr.time.calendar.service.CalendarEntriesService;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  
30  import org.kuali.rice.krad.util.GlobalVariables;
31  
32  public class CalendarEntryAction extends TkAction {
33  
34  	public ActionForward createCalendarEntry(ActionMapping mapping,
35  			ActionForm form, HttpServletRequest request,
36  			HttpServletResponse response) throws Exception {
37  		CalendarEntryActionForm ceaf = (CalendarEntryActionForm) form;
38  		if (ceaf.getNoOfPeriods() == null || ceaf.getNoOfPeriods() < 1) {
39  			GlobalVariables.getMessageMap().putError("document.noOfPeriods",
40  					"periods.greater.than.one");
41  			return mapping.findForward("basic");
42  		} else if (ceaf.getHrPyCalendarEntryId() == null) {
43  			GlobalVariables.getMessageMap().putError(
44  					"document.hrPyCalendarEntryId",
45  					"error.calendar.not.available");
46  			return mapping.findForward("basic");
47  		}
48          CalendarEntryPeriodType periodType = ceaf.getCalendarEntryPeriodType() == null ?
49                                                  CalendarEntryPeriodType.BI_WEEKLY :
50                                                  CalendarEntryPeriodType.fromCode(ceaf.getCalendarEntryPeriodType());
51          CalendarEntriesService calendarEntriesService = TkServiceLocator.getCalendarEntriesService();
52  		CalendarEntries calendarEntries = calendarEntriesService.getCalendarEntries(
53  						ceaf.getHrPyCalendarEntryId().toString());
54  		if (calendarEntries == null) {
55  			GlobalVariables.getMessageMap().putError(
56  					"document.hrPyCalendarEntryId",
57  					"error.calendar.not.available");
58  		} else {
59  				for (int i = 0; i < ceaf.getNoOfPeriods(); i++) {
60  					CalendarEntries nextCalendarEntries = calendarEntriesService.getNextCalendarEntriesByCalendarId(
61  									calendarEntries.getHrCalendarId(), calendarEntries);
62  					if (nextCalendarEntries == null) {
63                          calendarEntries = calendarEntriesService.createNextCalendarEntry(calendarEntries, periodType);
64  					}
65  				}
66  				ceaf.setMessage("Calendar entry sucessfully created.");
67  		}
68  		return mapping.findForward("basic");
69  	}
70  	
71  }