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.timesheet.web;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.Set;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.time.DateUtils;
33  import org.apache.log4j.Logger;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.apache.struts.action.ActionRedirect;
38  import org.kuali.hr.lm.LMConstants;
39  import org.kuali.hr.lm.accrual.AccrualCategory;
40  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
41  import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
42  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
43  import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
44  import org.kuali.hr.lm.leaveblock.LeaveBlock;
45  import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
46  import org.kuali.hr.lm.leavepayout.LeavePayout;
47  import org.kuali.hr.time.base.web.TkAction;
48  import org.kuali.hr.time.calendar.Calendar;
49  import org.kuali.hr.time.calendar.CalendarEntries;
50  import org.kuali.hr.time.detail.web.ActionFormUtils;
51  import org.kuali.hr.time.principal.PrincipalHRAttributes;
52  import org.kuali.hr.time.roles.TkUserRoles;
53  import org.kuali.hr.time.roles.UserRoles;
54  import org.kuali.hr.time.service.base.TkServiceLocator;
55  import org.kuali.hr.time.timesheet.TimesheetDocument;
56  import org.kuali.hr.time.timesummary.EarnCodeSection;
57  import org.kuali.hr.time.timesummary.EarnGroupSection;
58  import org.kuali.hr.time.util.TKContext;
59  import org.kuali.hr.time.util.TKUser;
60  import org.kuali.hr.time.util.TKUtils;
61  import org.kuali.hr.time.util.TkConstants;
62  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
63  import org.kuali.rice.kim.api.identity.principal.Principal;
64  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
65  import org.kuali.rice.krad.exception.AuthorizationException;
66  import org.kuali.rice.krad.util.GlobalVariables;
67  import org.kuali.rice.krad.util.KRADConstants;
68  import org.kuali.rice.krad.util.ObjectUtils;
69  
70  public class TimesheetAction extends TkAction {
71  
72  	private static final Logger LOG = Logger.getLogger(TimesheetAction.class);
73  
74      @Override
75      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
76          UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
77          TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
78  
79          if (!roles.isDocumentReadable(doc)) {
80              throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + (doc == null ? "" : doc.getDocumentId()), "");
81          }
82      }
83  
84      @Override
85  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
86  		TimesheetActionForm taForm = (TimesheetActionForm) form;
87  		String documentId = taForm.getDocumentId();
88  
89          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
90          		|| StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
91          	documentId = (String) request.getParameter("docId");
92          }
93  
94          LOG.debug("DOCID: " + documentId);
95  
96          // Here - viewPrincipal will be the principal of the user we intend to
97          // view, be it target user, backdoor or otherwise.
98          String viewPrincipal = TKUser.getCurrentTargetPersonId();
99          Date currentDate = TKUtils.getTimelessDate(null);
100 		CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
101 
102         // By handling the prev/next in the execute method, we are saving one
103         // fetch/construction of a TimesheetDocument. If it were broken out into
104         // methods, we would first fetch the current document, and then fetch
105         // the next one instead of doing it in the single action.
106 		TimesheetDocument td;
107         if (StringUtils.isNotBlank(documentId)) {
108             td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
109         } else {
110             // Default to whatever is active for "today".
111             if (payCalendarEntry == null) {
112                 Principal prin = KimApiServiceLocator.getIdentityService().getPrincipal(viewPrincipal);
113                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "clock.error.missing.payCalendar", prin.getPrincipalName());
114                 return super.execute(mapping, form, request, response);
115                 //throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
116             }
117             td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntry);
118         }
119 
120         // Set the TKContext for the current timesheet document id.
121         if (td != null) {
122            setupDocumentOnFormContext(taForm, td);
123         } else {
124             LOG.error("Null timesheet document in TimesheetAction.");
125         }
126         
127 
128 
129         // Do this at the end, so we load the document first,
130         // then check security permissions via the superclass execution chain.
131 		return super.execute(mapping, form, request, response);
132 	}
133 
134     public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
135         ActionForward forward = mapping.findForward("basic");
136     	String command = request.getParameter("command");
137     	
138     	if (StringUtils.equals(command, "displayDocSearchView") || StringUtils.equals(command, "displayActionListView")) {
139         	String docId = (String) request.getParameter("docId");
140         	TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
141         	String timesheetPrincipalName = KimApiServiceLocator.getPersonService().getPerson(timesheetDocument.getPrincipalId()).getPrincipalName();
142         	
143         	String principalId = TKUser.getCurrentTargetPersonId();
144         	String principalName = KimApiServiceLocator.getPersonService().getPerson(principalId).getPrincipalName();
145         	
146         	StringBuilder builder = new StringBuilder();
147         	if (!StringUtils.equals(principalName, timesheetPrincipalName)) {
148             	if (StringUtils.equals(command, "displayDocSearchView")) {
149             		builder.append("changeTargetPerson.do?methodToCall=changeTargetPerson");
150             		builder.append("&documentId=");
151             		builder.append(docId);
152             		builder.append("&principalName=");
153             		builder.append(timesheetPrincipalName);
154             		builder.append("&targetUrl=TimeDetail.do");
155             		builder.append("?docmentId=" + docId);
156             		builder.append("&returnUrl=TimeApproval.do");
157             	} else {
158             		builder.append("TimeApproval.do");
159             	}
160         	} else {
161         		builder.append("TimeDetail.do");
162         		builder.append("?docmentId=" + docId);
163         	}
164 
165         	forward = new ActionRedirect(builder.toString());
166         }
167     	
168     	return forward;
169     }
170 
171     protected void setupDocumentOnFormContext(TimesheetActionForm taForm, TimesheetDocument td) throws Exception{
172     	String viewPrincipal = TKUser.getCurrentTargetPersonId();
173     	TKContext.setCurrentTimesheetDocumentId(td.getDocumentId());
174         TKContext.setCurrentTimesheetDocument(td);
175 	    taForm.setTimesheetDocument(td);
176 	    taForm.setDocumentId(td.getDocumentId());
177         TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.PREV_TIMESHEET, viewPrincipal);
178         TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.NEXT_TIMESHEET, viewPrincipal);
179 
180         taForm.setPrevDocumentId(prevTdh != null ? prevTdh.getDocumentId() : null);
181         taForm.setNextDocumentId(nextTdh != null ? nextTdh.getDocumentId() : null);
182       
183         taForm.setPayCalendarDates(td.getCalendarEntry());
184         taForm.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taForm.getPayCalendarDates()));
185         
186     }
187 
188 }