001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.common;
017
018import javax.servlet.http.HttpServletRequest;
019import javax.servlet.http.HttpServletResponse;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.struts.action.ActionForm;
023import org.apache.struts.action.ActionForward;
024import org.apache.struts.action.ActionMapping;
025import org.kuali.kpme.core.calendar.Calendar;
026import org.kuali.kpme.core.calendar.entry.CalendarEntry;
027import org.kuali.kpme.core.service.HrServiceLocator;
028import org.kuali.kpme.core.web.KPMEAction;
029import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
030import org.kuali.kpme.tklm.time.service.TkServiceLocator;
031import org.kuali.rice.kim.api.identity.principal.Principal;
032import org.kuali.rice.kim.api.services.KimApiServiceLocator;
033
034public class InitiateDocumentAction extends KPMEAction {
035
036    public ActionForward initiateDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
037        InitiateDocumentForm initiateDocumentForm = (InitiateDocumentForm) form;
038        String principalId = initiateDocumentForm.getPrincipalId();
039        String hrCalendarEntryId = initiateDocumentForm.getHrCalendarEntryId();
040        
041        if (StringUtils.isNotBlank(principalId) && StringUtils.isNotBlank(hrCalendarEntryId)) {
042                Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
043                CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(hrCalendarEntryId);
044                
045                if (principal != null && calendarEntry != null) {
046                        Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
047                        
048                        if (calendar != null) {
049                                if (StringUtils.equals(calendar.getCalendarTypes(), TkConstants.CALENDAR_TYPE_PAY)) {
050                                        TkServiceLocator.getTimesheetService().openTimesheetDocument(principalId, calendarEntry);
051                                } else if (StringUtils.equals(calendar.getCalendarTypes(), TkConstants.CALENDAR_TYPE_LEAVE)) {
052                                        LmServiceLocator.getLeaveCalendarService().openLeaveCalendarDocument(principalId, calendarEntry);
053                                }
054                        }
055                }
056        }
057        
058        return mapping.findForward("basic");
059    }
060    
061}