View Javadoc

1   /**
2    * Copyright 2004-2012 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.base.web;
17  
18  import java.sql.Date;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.struts.action.ActionForm;
29  import org.apache.struts.action.ActionForward;
30  import org.apache.struts.action.ActionMapping;
31  import org.displaytag.tags.TableTagParameters;
32  import org.displaytag.util.ParamEncoder;
33  import org.kuali.hr.time.calendar.Calendar;
34  import org.kuali.hr.time.calendar.CalendarEntries;
35  import org.kuali.hr.time.person.TKPerson;
36  import org.kuali.hr.time.service.base.TkServiceLocator;
37  import org.kuali.hr.time.util.TKContext;
38  import org.kuali.hr.time.util.TKUtils;
39  import org.kuali.hr.time.util.TkConstants;
40  import org.kuali.hr.time.workarea.WorkArea;
41  import org.kuali.rice.krad.exception.AuthorizationException;
42  import org.kuali.rice.krad.util.GlobalVariables;
43  
44  public class ApprovalAction extends TkAction{
45  
46  	protected List<TKPerson> getSubListPrincipalIds(HttpServletRequest request, List<TKPerson> assignmentPrincipalIds) {
47  	    String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
48  	    // The paging index begins from 1, but the sublist index begins from 0.
49  	    // So the logic below sets the sublist begin index to 0 if the page number is null or equals 1
50  	    Integer beginIndex = StringUtils.isBlank(page) || StringUtils.equals(page, "1") ? 0 : (Integer.parseInt(page) - 1)*TkConstants.PAGE_SIZE;
51  	    Integer endIndex = beginIndex + TkConstants.PAGE_SIZE > assignmentPrincipalIds.size() ? assignmentPrincipalIds.size() : beginIndex + TkConstants.PAGE_SIZE;
52  	
53  	    return assignmentPrincipalIds.subList(beginIndex, endIndex);
54  	}
55  
56  	protected Boolean isAscending(HttpServletRequest request) {
57  	    // returned value 1 = ascending; 2 = descending
58  	    String ascending = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
59  	    return StringUtils.equals(ascending, "1") ? true : false;
60  	}
61  
62  	protected String getSortField(HttpServletRequest request) {
63  	    return request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_SORT)));
64  	}
65  	
66  	protected void checkTKAuthorization(ActionForm form, String methodToCall)
67  			throws AuthorizationException {
68  			    if (!TKContext.getUser().isTimesheetReviewer() && !TKContext.getUser().isAnyApproverActive() && !TKContext.getUser().isSystemAdmin() 
69  			    		&& !TKContext.getUser().isLocationAdmin() && !TKContext.getUser().isGlobalViewOnly() && !TKContext.getUser().isDeptViewOnly() 
70  			    		&& !TKContext.getUser().isDepartmentAdmin()) {
71  			        throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "ApprovalAction", "");
72  			    }
73  			}
74  	
75  	protected void resetMainFields(ActionForm form) {
76  		ApprovalForm taf = (ApprovalForm) form;
77  		taf.setSearchField(null);
78  		taf.setSearchTerm(null);
79  		taf.setSelectedWorkArea(null);
80  		taf.setSelectedDept(null);
81  		taf.setPayBeginDate(null);
82  		taf.setPayEndDate(null);
83  		taf.setHrPyCalendarEntriesId(null);
84  	}
85  	
86  	protected void setupDocumentOnFormContext(HttpServletRequest request,
87  			ApprovalForm taf, CalendarEntries payCalendarEntries, String page) {
88  		if(payCalendarEntries == null) {
89  			return;
90  		}
91  		taf.setHrPyCalendarId(payCalendarEntries.getHrCalendarId());
92  		taf.setHrPyCalendarEntriesId(payCalendarEntries.getHrCalendarEntriesId());
93  		taf.setPayBeginDate(payCalendarEntries.getBeginPeriodDateTime());
94  		taf.setPayEndDate(payCalendarEntries.getEndPeriodDateTime());
95  		
96  		CalendarEntries prevPayCalendarEntries = TkServiceLocator.getCalendarEntriesService().getPreviousCalendarEntriesByCalendarId(taf.getHrPyCalendarId(), payCalendarEntries);
97  		if (prevPayCalendarEntries != null) {
98  		    taf.setPrevPayCalendarId(prevPayCalendarEntries.getHrCalendarEntriesId());
99  		} 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 }