View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.time.web;
17  
18  import java.util.List;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.struts.action.ActionForm;
25  import org.apache.struts.action.ActionForward;
26  import org.apache.struts.action.ActionMapping;
27  import org.apache.struts.action.ActionRedirect;
28  import org.joda.time.LocalDate;
29  import org.kuali.kpme.core.assignment.Assignment;
30  import org.kuali.kpme.core.job.Job;
31  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
32  import org.kuali.kpme.core.service.HrServiceLocator;
33  import org.kuali.kpme.core.util.HrConstants;
34  import org.kuali.kpme.core.util.HrContext;
35  import org.kuali.kpme.core.web.KPMEAction;
36  import org.kuali.kpme.core.web.KPMEForm;
37  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
38  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
39  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
40  import org.kuali.kpme.tklm.time.util.TkContext;
41  import org.kuali.rice.krad.exception.AuthorizationException;
42  import org.kuali.rice.krad.util.GlobalVariables;
43  
44  public class TimeAction extends KPMEAction {
45  
46      @Override
47      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
48          KPMEForm kPMEForm = (KPMEForm) form;
49  
50          if (StringUtils.equals(methodToCall, "targetEmployee") || StringUtils.equals(methodToCall, "changeEmployee") || StringUtils.equals(methodToCall, "clearBackdoor") || StringUtils.equals(methodToCall, "clearChangeUser")) {
51              // Handle security validation in targetEmployee action, we may need
52              // to check the document for validity, since the user may not
53              // necessarily be a system administrator.
54          } else {
55          	String principalId = GlobalVariables.getUserSession().getPrincipalId();
56          	TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(kPMEForm.getDocumentId());
57              if (!HrContext.isSystemAdmin()
58          			&& !TkContext.isLocationAdmin()
59          			&& !TkContext.isDepartmentAdmin()
60          			&& !HrContext.isGlobalViewOnly()
61          			&& !TkContext.isDepartmentViewOnly()
62          			&& (kPMEForm.getDocumentId() != null && !HrServiceLocator.getHRPermissionService().canApproveCalendarDocument(principalId, tdoc))
63          			&& (kPMEForm.getDocumentId() != null && !HrServiceLocator.getHRPermissionService().canViewCalendarDocument(principalId, tdoc)))  {
64                  throw new AuthorizationException("", "TimeAction", "");
65              }
66          }
67      }
68  
69  
70      @Override
71      public ActionForward execute(ActionMapping mapping, ActionForm form,
72                                   HttpServletRequest request, HttpServletResponse response)
73              throws Exception {
74          //boolean synch = TKUser.isSynchronous();
75          String principalId = HrContext.getTargetPrincipalId();
76          if (HrContext.isSystemAdmin()) {
77              return new ActionRedirect("/portal.do");
78          }
79          PrincipalHRAttributes phra = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(principalId, LocalDate.now());
80          if (phra == null) {
81              return new ActionRedirect("/PersonInfo.do");
82          }
83          Job job = HrServiceLocator.getJobService().getPrimaryJob(principalId, LocalDate.now());
84  
85          if (job != null) {
86              List<Assignment> assignments = HrServiceLocator.getAssignmentService().getActiveAssignmentsForJob(principalId, job.getJobNumber(), LocalDate.now());
87              for (Assignment asmnt : assignments) {
88                  if (asmnt.isActive()) {
89                      if (job.getFlsaStatus().equals(HrConstants.FLSA_STATUS_NON_EXEMPT)) {
90                          TimeCollectionRule tcr = null;
91                          if(asmnt.getJob() != null)
92                          	tcr = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(asmnt.getJob().getDept(), asmnt.getWorkArea(), asmnt.getJob().getHrPayType(), LocalDate.now());
93                          if (tcr != null && tcr.isClockUserFl()) {
94                              return new ActionRedirect("/Clock.do");
95                          } else {
96                              return new ActionRedirect("/TimeDetail.do");
97                          }
98                      } else {
99                          if (job.isEligibleForLeave()) {
100                             return new ActionRedirect("/LeaveCalendar.do");
101                         }
102                     }
103                 }
104             }
105         }
106 
107         return new ActionRedirect("/PersonInfo.do");
108     }
109 }