001 /** 002 * Copyright 2004-2013 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 */ 016 package org.kuali.hr.time.timesheet.web; 017 018 import java.math.BigDecimal; 019 import java.sql.Date; 020 import java.util.ArrayList; 021 import java.util.HashMap; 022 import java.util.HashSet; 023 import java.util.List; 024 import java.util.Map; 025 import java.util.Map.Entry; 026 import java.util.Set; 027 028 import javax.servlet.http.HttpServletRequest; 029 import javax.servlet.http.HttpServletResponse; 030 031 import org.apache.commons.lang.StringUtils; 032 import org.apache.commons.lang.time.DateUtils; 033 import org.apache.log4j.Logger; 034 import org.apache.struts.action.ActionForm; 035 import org.apache.struts.action.ActionForward; 036 import org.apache.struts.action.ActionMapping; 037 import org.apache.struts.action.ActionRedirect; 038 import org.kuali.hr.lm.LMConstants; 039 import org.kuali.hr.lm.accrual.AccrualCategory; 040 import org.kuali.hr.lm.accrual.AccrualCategoryRule; 041 import org.kuali.hr.lm.balancetransfer.BalanceTransfer; 042 import org.kuali.hr.lm.leaveSummary.LeaveSummary; 043 import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow; 044 import org.kuali.hr.lm.leaveblock.LeaveBlock; 045 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil; 046 import org.kuali.hr.lm.leavepayout.LeavePayout; 047 import org.kuali.hr.time.base.web.TkAction; 048 import org.kuali.hr.time.calendar.Calendar; 049 import org.kuali.hr.time.calendar.CalendarEntries; 050 import org.kuali.hr.time.detail.web.ActionFormUtils; 051 import org.kuali.hr.time.principal.PrincipalHRAttributes; 052 import org.kuali.hr.time.roles.TkUserRoles; 053 import org.kuali.hr.time.roles.UserRoles; 054 import org.kuali.hr.time.service.base.TkServiceLocator; 055 import org.kuali.hr.time.timesheet.TimesheetDocument; 056 import org.kuali.hr.time.timesummary.EarnCodeSection; 057 import org.kuali.hr.time.timesummary.EarnGroupSection; 058 import org.kuali.hr.time.util.TKContext; 059 import org.kuali.hr.time.util.TKUser; 060 import org.kuali.hr.time.util.TKUtils; 061 import org.kuali.hr.time.util.TkConstants; 062 import org.kuali.hr.time.workflow.TimesheetDocumentHeader; 063 import org.kuali.rice.kim.api.identity.principal.Principal; 064 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 065 import org.kuali.rice.krad.exception.AuthorizationException; 066 import org.kuali.rice.krad.util.GlobalVariables; 067 import org.kuali.rice.krad.util.KRADConstants; 068 import org.kuali.rice.krad.util.ObjectUtils; 069 070 public class TimesheetAction extends TkAction { 071 072 private static final Logger LOG = Logger.getLogger(TimesheetAction.class); 073 074 @Override 075 protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException { 076 UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()); 077 TimesheetDocument doc = TKContext.getCurrentTimesheetDocument(); 078 079 if (!roles.isDocumentReadable(doc)) { 080 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + (doc == null ? "" : doc.getDocumentId()), ""); 081 } 082 } 083 084 @Override 085 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 086 TimesheetActionForm taForm = (TimesheetActionForm) form; 087 String documentId = taForm.getDocumentId(); 088 089 if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView") 090 || StringUtils.equals(request.getParameter("command"), "displayActionListView") 091 || StringUtils.equals(request.getParameter("command"), "displaySuperUserView")) { 092 documentId = request.getParameter("docId"); 093 } 094 095 LOG.debug("DOCID: " + documentId); 096 097 // Here - viewPrincipal will be the principal of the user we intend to 098 // view, be it target user, backdoor or otherwise. 099 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 }