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.base.web; 017 018 import java.sql.Date; 019 import java.text.SimpleDateFormat; 020 import java.util.ArrayList; 021 import java.util.Collections; 022 import java.util.List; 023 024 import javax.servlet.http.HttpServletRequest; 025 import javax.servlet.http.HttpServletResponse; 026 027 import org.apache.commons.lang.StringUtils; 028 import org.apache.commons.lang.time.DateUtils; 029 import org.apache.struts.action.ActionForm; 030 import org.apache.struts.action.ActionForward; 031 import org.apache.struts.action.ActionMapping; 032 import org.displaytag.tags.TableTagParameters; 033 import org.displaytag.util.ParamEncoder; 034 import org.kuali.hr.time.calendar.Calendar; 035 import org.kuali.hr.time.calendar.CalendarEntries; 036 import org.kuali.hr.time.person.TKPerson; 037 import org.kuali.hr.time.service.base.TkServiceLocator; 038 import org.kuali.hr.time.util.TKContext; 039 import org.kuali.hr.time.util.TKUtils; 040 import org.kuali.hr.time.util.TkConstants; 041 import org.kuali.hr.time.workarea.WorkArea; 042 import org.kuali.rice.krad.exception.AuthorizationException; 043 import org.kuali.rice.krad.util.GlobalVariables; 044 045 public class ApprovalAction extends TkAction{ 046 047 @Override 048 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 049 if (form instanceof TkForm) { 050 String methodToCall = ((TkForm)form).getMethodToCall(); 051 if(StringUtils.isEmpty(methodToCall)) { 052 super.execute(mapping, form, request, response); 053 return loadApprovalTab(mapping, form, request, response); 054 } 055 } 056 return super.execute(mapping, form, request, response); 057 } 058 059 public ActionForward loadApprovalTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 060 return mapping.findForward("basic"); 061 } 062 063 protected List<TKPerson> getSubListPrincipalIds(HttpServletRequest request, List<TKPerson> assignmentPrincipalIds) { 064 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE))); 065 // The paging index begins from 1, but the sublist index begins from 0. 066 // So the logic below sets the sublist begin index to 0 if the page number is null or equals 1 067 Integer beginIndex = StringUtils.isBlank(page) || StringUtils.equals(page, "1") ? 0 : (Integer.parseInt(page) - 1)*TkConstants.PAGE_SIZE; 068 Integer endIndex = beginIndex + TkConstants.PAGE_SIZE > assignmentPrincipalIds.size() ? assignmentPrincipalIds.size() : beginIndex + TkConstants.PAGE_SIZE; 069 070 return assignmentPrincipalIds.subList(beginIndex, endIndex); 071 } 072 073 protected Boolean isAscending(HttpServletRequest request) { 074 // returned value 1 = ascending; 2 = descending 075 String ascending = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_ORDER))); 076 return StringUtils.equals(ascending, "1") ? true : false; 077 } 078 079 protected String getSortField(HttpServletRequest request) { 080 return request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_SORT))); 081 } 082 083 protected void checkTKAuthorization(ActionForm form, String methodToCall) 084 throws AuthorizationException { 085 if (!TKContext.getUser().isTimesheetReviewer() && !TKContext.getUser().isAnyApproverActive() && !TKContext.getUser().isSystemAdmin() 086 && !TKContext.getUser().isLocationAdmin() && !TKContext.getUser().isGlobalViewOnly() && !TKContext.getUser().isDeptViewOnly() 087 && !TKContext.getUser().isDepartmentAdmin()) { 088 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "ApprovalAction", ""); 089 } 090 } 091 092 protected void resetMainFields(ActionForm form) { 093 ApprovalForm taf = (ApprovalForm) form; 094 taf.setSearchField(null); 095 taf.setSearchTerm(null); 096 taf.setSelectedWorkArea(null); 097 taf.setSelectedDept(null); 098 taf.setPayBeginDate(null); 099 taf.setPayEndDate(null); 100 taf.setHrPyCalendarEntriesId(null); 101 } 102 103 protected void setupDocumentOnFormContext(HttpServletRequest request, 104 ApprovalForm taf, CalendarEntries payCalendarEntries, String page) { 105 if(payCalendarEntries == null) { 106 return; 107 } 108 taf.setHrPyCalendarId(payCalendarEntries.getHrCalendarId()); 109 taf.setHrPyCalendarEntriesId(payCalendarEntries.getHrCalendarEntriesId()); 110 taf.setPayBeginDate(payCalendarEntries.getBeginPeriodDateTime()); 111 taf.setPayEndDate(DateUtils.addMilliseconds(payCalendarEntries.getEndPeriodDateTime(),-1)); 112 113 CalendarEntries prevPayCalendarEntries = TkServiceLocator.getCalendarEntriesService().getPreviousCalendarEntriesByCalendarId(taf.getHrPyCalendarId(), payCalendarEntries); 114 if (prevPayCalendarEntries != null) { 115 taf.setPrevPayCalendarId(prevPayCalendarEntries.getHrCalendarEntriesId()); 116 } else { 117 taf.setPrevPayCalendarId(null); 118 } 119 120 CalendarEntries nextPayCalendarEntries = TkServiceLocator.getCalendarEntriesService().getNextCalendarEntriesByCalendarId(taf.getHrPyCalendarId(), payCalendarEntries); 121 if (nextPayCalendarEntries != null) { 122 taf.setNextPayCalendarId(nextPayCalendarEntries.getHrCalendarEntriesId()); 123 } else { 124 taf.setNextPayCalendarId(null); 125 } 126 if (StringUtils.isBlank(page)) { 127 List<String> depts = new ArrayList<String>(TKContext.getUser().getReportingApprovalDepartments().keySet()); 128 if ( depts.isEmpty() ) { 129 return; 130 } 131 Collections.sort(depts); 132 taf.setDepartments(depts); 133 134 if (taf.getDepartments().size() == 1 || taf.getSelectedDept() != null) { 135 if (StringUtils.isEmpty(taf.getSelectedDept())) 136 taf.setSelectedDept(taf.getDepartments().get(0)); 137 138 List<WorkArea> workAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(taf.getSelectedDept(), new java.sql.Date(taf.getPayBeginDate().getTime())); 139 for(WorkArea wa : workAreas){ 140 if (TKContext.getUser().getApproverWorkAreas().contains(wa.getWorkArea()) 141 || TKContext.getUser().getReviewerWorkAreas().contains(wa.getWorkArea())) { 142 taf.getWorkAreaDescr().put(wa.getWorkArea(),wa.getDescription()+"("+wa.getWorkArea()+")"); 143 } 144 } 145 } 146 } 147 148 } 149 150 public ActionForward gotoCurrentPayPeriod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 151 152 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE))); 153 ApprovalForm taf = (ApprovalForm) form; 154 Date currentDate = TKUtils.getTimelessDate(null); 155 Calendar currentPayCalendar = TkServiceLocator.getCalendarService().getCalendarByGroup(taf.getSelectedPayCalendarGroup()); 156 CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(currentPayCalendar.getHrCalendarId(), currentDate); 157 taf.setPayCalendarEntries(payCalendarEntries); 158 taf.setSelectedCalendarYear(new SimpleDateFormat("yyyy").format(payCalendarEntries.getBeginPeriodDate())); 159 taf.setSelectedPayPeriod(payCalendarEntries.getHrCalendarEntriesId()); 160 populateCalendarAndPayPeriodLists(request, taf); 161 setupDocumentOnFormContext(request, taf, payCalendarEntries, page); 162 return mapping.findForward("basic"); 163 } 164 165 166 167 // Triggered by changes of calendar year drop down list, reloads the pay period list 168 public ActionForward changeCalendarYear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 169 ApprovalForm taf = (ApprovalForm) form; 170 if(!StringUtils.isEmpty(request.getParameter("selectedCY"))) { 171 taf.setSelectedCalendarYear(request.getParameter("selectedCY").toString()); 172 populateCalendarAndPayPeriodLists(request, taf); 173 } 174 return mapping.findForward("basic"); 175 } 176 177 // Triggered by changes of pay period drop down list, reloads the whole page based on the selected pay period 178 public ActionForward changePayPeriod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 179 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE))); 180 ApprovalForm taf = (ApprovalForm) form; 181 if(!StringUtils.isEmpty(request.getParameter("selectedPP"))) { 182 taf.setSelectedPayPeriod(request.getParameter("selectedPP").toString()); 183 CalendarEntries pce = TkServiceLocator.getCalendarEntriesService() 184 .getCalendarEntries(request.getParameter("selectedPP").toString()); 185 if(pce != null) { 186 taf.setPayCalendarEntries(pce); 187 setupDocumentOnFormContext(request, taf, pce, page); 188 } 189 } 190 return mapping.findForward("basic"); 191 } 192 // sets the CalendarYear and Pay Period lists. Should be overridden by subclasses 193 protected void populateCalendarAndPayPeriodLists(HttpServletRequest request, ApprovalForm taf) { 194 195 } 196 197 198 }