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