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 documentId = (String) request.getParameter("docId"); 092 } 093 094 LOG.debug("DOCID: " + documentId); 095 096 // Here - viewPrincipal will be the principal of the user we intend to 097 // view, be it target user, backdoor or otherwise. 098 String viewPrincipal = TKUser.getCurrentTargetPersonId(); 099 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 }