View Javadoc

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