1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  }