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                  || StringUtils.equals(request.getParameter("command"), "displaySuperUserView")) {
92          	documentId = request.getParameter("docId");
93          }
94  
95          LOG.debug("DOCID: " + documentId);
96  
97          // Here - viewPrincipal will be the principal of the user we intend to
98          // view, be it target user, backdoor or otherwise.
99          String viewPrincipal = TKUser.getCurrentTargetPersonId();
100         Date currentDate = TKUtils.getTimelessDate(null);
101 		CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
102 
103         // By handling the prev/next in the execute method, we are saving one
104         // fetch/construction of a TimesheetDocument. If it were broken out into
105         // methods, we would first fetch the current document, and then fetch
106         // the next one instead of doing it in the single action.
107 		TimesheetDocument td;
108         if (StringUtils.isNotBlank(documentId)) {
109             td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
110         } else {
111             // Default to whatever is active for "today".
112             if (payCalendarEntry == null) {
113                 Principal prin = KimApiServiceLocator.getIdentityService().getPrincipal(viewPrincipal);
114                 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "clock.error.missing.payCalendar", prin.getPrincipalName());
115                 return super.execute(mapping, form, request, response);
116                 //throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
117             }
118             td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntry);
119         }
120 
121         // Set the TKContext for the current timesheet document id.
122         if (td != null) {
123            setupDocumentOnFormContext(taForm, td);
124         } else {
125             LOG.error("Null timesheet document in TimesheetAction.");
126         }
127         
128 
129 
130         // Do this at the end, so we load the document first,
131         // then check security permissions via the superclass execution chain.
132 		return super.execute(mapping, form, request, response);
133 	}
134 
135     public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
136         ActionForward forward = mapping.findForward("basic");
137     	String command = request.getParameter("command");
138     	
139     	if (StringUtils.equals(command, "displayDocSearchView")
140                 || StringUtils.equals(command, "displayActionListView")
141                 || StringUtils.equals(command, "displaySuperUserView")) {
142         	String docId = (String) request.getParameter("docId");
143         	TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
144         	String timesheetPrincipalName = KimApiServiceLocator.getPersonService().getPerson(timesheetDocument.getPrincipalId()).getPrincipalName();
145         	
146         	String principalId = TKUser.getCurrentTargetPersonId();
147         	String principalName = KimApiServiceLocator.getPersonService().getPerson(principalId).getPrincipalName();
148         	
149         	StringBuilder builder = new StringBuilder();
150         	if (!StringUtils.equals(principalName, timesheetPrincipalName)) {
151             	if (StringUtils.equals(command, "displayDocSearchView")
152                         || StringUtils.equals(command, "displaySuperUserView")) {
153             		builder.append("changeTargetPerson.do?methodToCall=changeTargetPerson");
154             		builder.append("&documentId=");
155             		builder.append(docId);
156             		builder.append("&principalName=");
157             		builder.append(timesheetPrincipalName);
158             		builder.append("&targetUrl=TimeDetail.do");
159             		builder.append("?documentId=" + docId);
160             		builder.append("&returnUrl=TimeApproval.do");
161             	} else {
162             		builder.append("TimeApproval.do");
163                     builder.append("?documentId=").append(docId);
164             	}
165         	} else {
166         		builder.append("TimeDetail.do");
167         		builder.append("?documentId=" + docId);
168         	}
169 
170         	forward = new ActionRedirect(builder.toString());
171         }
172     	
173     	return forward;
174     }
175 
176     protected void setupDocumentOnFormContext(TimesheetActionForm taForm, TimesheetDocument td) throws Exception{
177     	String viewPrincipal = TKUser.getCurrentTargetPersonId();
178     	TKContext.setCurrentTimesheetDocumentId(td.getDocumentId());
179         TKContext.setCurrentTimesheetDocument(td);
180 	    taForm.setTimesheetDocument(td);
181 	    taForm.setDocumentId(td.getDocumentId());
182         TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.PREV_TIMESHEET, viewPrincipal);
183         TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.NEXT_TIMESHEET, viewPrincipal);
184 
185         taForm.setPrevDocumentId(prevTdh != null ? prevTdh.getDocumentId() : null);
186         taForm.setNextDocumentId(nextTdh != null ? nextTdh.getDocumentId() : null);
187       
188         taForm.setPayCalendarDates(td.getCalendarEntry());
189         taForm.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taForm.getPayCalendarDates()));
190         
191     }
192 
193 }