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.approval.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.Comparator;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.lang.ObjectUtils;
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.math.NumberUtils;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.displaytag.tags.TableTagParameters;
37  import org.displaytag.util.ParamEncoder;
38  import org.kuali.hr.time.assignment.Assignment;
39  import org.kuali.hr.time.base.web.ApprovalAction;
40  import org.kuali.hr.time.base.web.ApprovalForm;
41  import org.kuali.hr.time.calendar.Calendar;
42  import org.kuali.hr.time.calendar.CalendarEntries;
43  import org.kuali.hr.time.detail.web.ActionFormUtils;
44  import org.kuali.hr.time.person.TKPerson;
45  import org.kuali.hr.time.service.base.TkServiceLocator;
46  import org.kuali.hr.time.timesheet.TimesheetDocument;
47  import org.kuali.hr.time.util.TKContext;
48  import org.kuali.hr.time.util.TKUser;
49  import org.kuali.hr.time.util.TKUtils;
50  import org.kuali.hr.time.util.TkConstants;
51  import org.kuali.hr.time.workarea.WorkArea;
52  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
53  
54  public class TimeApprovalAction extends ApprovalAction{
55  	
56  	public ActionForward searchResult(ActionMapping mapping, ActionForm form,
57  			HttpServletRequest request, HttpServletResponse response)
58  			throws Exception {
59  		TimeApprovalActionForm taaf = (TimeApprovalActionForm)form;
60  		String searchField = taaf.getSearchField();
61  		String searchTerm = taaf.getSearchTerm();
62  		String principalId;
63  		
64          if(StringUtils.equals("documentId", searchField)){
65          	TimesheetDocumentHeader tdh = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeader(searchTerm);
66          	principalId = tdh.getPrincipalId();
67          } else {
68          	principalId = searchTerm;
69          }
70      	taaf.setSearchField("principalId");
71      	taaf.setSearchTerm(principalId);
72         
73          List<String> principalIds = new ArrayList<String>();
74          principalIds.add(principalId);
75          List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
76          if (persons.isEmpty()) {
77          	taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
78          	taaf.setResultSize(0);
79          } else {
80          	taaf.setResultSize(persons.size());	
81  	        taaf.setApprovalRows(getApprovalRows(taaf, persons));
82  	        
83          	CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(taaf.getHrPyCalendarEntriesId());
84     	        taaf.setPayCalendarEntries(payCalendarEntries);
85     	        taaf.setPayCalendarLabels(TkServiceLocator.getTimeSummaryService().getHeaderForSummary(payCalendarEntries, new ArrayList<Boolean>()));
86          	
87  	        List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(principalId, payCalendarEntries.getEndPeriodDate());
88  	        if(!assignments.isEmpty()){
89  	        	 for(Long wa : taaf.getWorkAreaDescr().keySet()){
90  	        		for (Assignment assign : assignments) {
91  		             	if (assign.getWorkArea().toString().equals(wa.toString())) {
92  		             		taaf.setSelectedWorkArea(wa.toString());
93  		             		break;
94  		             	}
95  	        		}
96  	             }
97  	        }
98          }
99   
100 		return mapping.findForward("basic");
101 	}
102 	
103     public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
104         TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
105         List<ApprovalTimeSummaryRow> lstApprovalRows = taaf.getApprovalRows();
106         for (ApprovalTimeSummaryRow ar : lstApprovalRows) {
107             if (ar.isApprovable() && StringUtils.equals(ar.getSelected(), "on")) {
108                 String documentNumber = ar.getDocumentId();
109                 TimesheetDocument tDoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentNumber);
110                 TkServiceLocator.getTimesheetService().approveTimesheet(TKContext.getPrincipalId(), tDoc);
111             }
112         }
113         return mapping.findForward("basic");
114     }
115     
116 	public ActionForward selectNewDept(ActionMapping mapping, ActionForm form,
117 			HttpServletRequest request, HttpServletResponse response)
118 			throws Exception {
119 		TimeApprovalActionForm taaf = (TimeApprovalActionForm)form;
120 		taaf.setSearchField(null);
121 		taaf.setSearchTerm(null);
122 
123         CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(taaf.getHrPyCalendarEntriesId());
124         taaf.setPayCalendarEntries(payCalendarEntries);
125         taaf.setPayCalendarLabels(TkServiceLocator.getTimeSummaryService().getHeaderForSummary(payCalendarEntries, new ArrayList<Boolean>()));
126 
127 		taaf.getWorkAreaDescr().clear();
128     	List<WorkArea> workAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(taaf.getSelectedDept(), new java.sql.Date(taaf.getPayBeginDate().getTime()));
129         for(WorkArea wa : workAreas){
130         	if (TKContext.getUser().getApproverWorkAreas().contains(wa.getWorkArea())
131         			|| TKContext.getUser().getReviewerWorkAreas().contains(wa.getWorkArea())) {
132         		taaf.getWorkAreaDescr().put(wa.getWorkArea(),wa.getDescription()+"("+wa.getWorkArea()+")");
133         	}
134         }
135 	
136     	List<String> principalIds = TkServiceLocator.getTimeApproveService().getPrincipalIdsByDeptWorkAreaRolename(taaf.getRoleName(), taaf.getSelectedDept(), taaf.getSelectedWorkArea(), new java.sql.Date(taaf.getPayBeginDate().getTime()), new java.sql.Date(taaf.getPayEndDate().getTime()), taaf.getSelectedPayCalendarGroup());
137     	if (principalIds.isEmpty()) {
138     		taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
139     		taaf.setResultSize(0);
140     	}
141     	else {
142 	        List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
143 	        Collections.sort(persons);
144 	        taaf.setApprovalRows(getApprovalRows(taaf, getSubListPrincipalIds(request, persons)));
145 	        taaf.setResultSize(persons.size());
146     	}
147     	
148     	this.populateCalendarAndPayPeriodLists(request, taaf);
149 		return mapping.findForward("basic");
150 	}
151 	
152 	public ActionForward selectNewWorkArea(ActionMapping mapping, ActionForm form,
153 			HttpServletRequest request, HttpServletResponse response)
154 			throws Exception {
155 		TimeApprovalActionForm taaf = (TimeApprovalActionForm)form;
156 		taaf.setSearchField(null);
157 		taaf.setSearchTerm(null);
158 
159 	    CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(taaf.getHrPyCalendarEntriesId());
160         taaf.setPayCalendarLabels(TkServiceLocator.getTimeSummaryService().getHeaderForSummary(payCalendarEntries, new ArrayList<Boolean>()));
161         
162         List<String> principalIds = TkServiceLocator.getTimeApproveService().getPrincipalIdsByDeptWorkAreaRolename(taaf.getRoleName(), taaf.getSelectedDept(), taaf.getSelectedWorkArea(), new java.sql.Date(taaf.getPayBeginDate().getTime()), new java.sql.Date(taaf.getPayEndDate().getTime()), taaf.getSelectedPayCalendarGroup());
163 		if (principalIds.isEmpty()) {
164 			taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
165 			taaf.setResultSize(0);
166 		}
167 		else {
168 	        List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
169 	        Collections.sort(persons);
170 	        taaf.setApprovalRows(getApprovalRows(taaf, getSubListPrincipalIds(request, persons)));
171 	        taaf.setResultSize(persons.size());
172 		}
173 		return mapping.findForward("basic");
174 	}
175 	
176 	public ActionForward loadApprovalTab(ActionMapping mapping, ActionForm form,
177 	HttpServletRequest request, HttpServletResponse response)
178 				throws Exception {
179 		ActionForward fwd = mapping.findForward("basic");
180 		TKUser user = TKContext.getUser();
181         TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
182         Date currentDate = null;
183         CalendarEntries payCalendarEntries = null;
184         Calendar currentPayCalendar = null;
185         String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
186         
187         //reset state
188         if(StringUtils.isBlank(taaf.getSelectedDept())){
189         	resetState(form, request);
190         }
191         // Set calendar groups
192         List<String> calGroups = TkServiceLocator.getTimeApproveService().getUniquePayGroups();
193         taaf.setPayCalendarGroups(calGroups);
194 
195         if (StringUtils.isBlank(taaf.getSelectedPayCalendarGroup())) {
196             taaf.setSelectedPayCalendarGroup(calGroups.get(0));
197         }
198         
199         // Set current pay calendar entries if present. Decide if the current date should be today or the end period date
200         if (taaf.getHrPyCalendarEntriesId() != null) {
201         	payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(taaf.getHrPyCalendarEntriesId());
202             currentDate = payCalendarEntries.getEndPeriodDate();
203         } else {
204             currentDate = TKUtils.getTimelessDate(null);
205             currentPayCalendar = TkServiceLocator.getCalendarService().getCalendarByGroup(taaf.getSelectedPayCalendarGroup());
206             payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(currentPayCalendar.getHrCalendarId(), currentDate);
207         }
208         taaf.setPayCalendarEntries(payCalendarEntries);
209         
210         
211         if(taaf.getPayCalendarEntries() != null) {
212 	        populateCalendarAndPayPeriodLists(request, taaf);
213         }
214         setupDocumentOnFormContext(request,taaf,payCalendarEntries, page);
215         return fwd;
216 	}
217 
218 	@Override
219 	protected void setupDocumentOnFormContext(HttpServletRequest request,ApprovalForm form, CalendarEntries payCalendarEntries, String page) {
220 		super.setupDocumentOnFormContext(request, form, payCalendarEntries, page);
221 		TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
222 		taaf.setPayCalendarLabels(TkServiceLocator.getTimeSummaryService().getHeaderForSummary(payCalendarEntries, new ArrayList<Boolean>()));
223 		
224 		List<String> principalIds = TkServiceLocator.getTimeApproveService().getPrincipalIdsByDeptWorkAreaRolename(taaf.getRoleName(), taaf.getSelectedDept(), taaf.getSelectedWorkArea(), new java.sql.Date(taaf.getPayBeginDate().getTime()), new java.sql.Date(taaf.getPayEndDate().getTime()), taaf.getSelectedPayCalendarGroup());
225 		if (principalIds.isEmpty()) {
226 			taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
227 			taaf.setResultSize(0);
228 		} else {
229 		    List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
230 		    List<ApprovalTimeSummaryRow> approvalRows = getApprovalRows(taaf, getSubListPrincipalIds(request, persons));
231 		    
232 		    final String sortField = request.getParameter("sortField");
233 		    if (StringUtils.equals(sortField, "Name")) {
234 			    final boolean sortNameAscending = Boolean.parseBoolean(request.getParameter("sortNameAscending"));
235 		    	Collections.sort(approvalRows, new Comparator<ApprovalTimeSummaryRow>() {
236 					@Override
237 					public int compare(ApprovalTimeSummaryRow row1, ApprovalTimeSummaryRow row2) {
238 						if (sortNameAscending) {
239 							return ObjectUtils.compare(StringUtils.lowerCase(row1.getName()), StringUtils.lowerCase(row2.getName()));
240 						} else {
241 							return ObjectUtils.compare(StringUtils.lowerCase(row2.getName()), StringUtils.lowerCase(row1.getName()));
242 						}
243 					}
244 		    	});
245 		    } else if (StringUtils.equals(sortField, "DocumentID")) {
246 			    final boolean sortDocumentIdAscending = Boolean.parseBoolean(request.getParameter("sortDocumentIDAscending"));
247 		    	Collections.sort(approvalRows, new Comparator<ApprovalTimeSummaryRow>() {
248 					@Override
249 					public int compare(ApprovalTimeSummaryRow row1, ApprovalTimeSummaryRow row2) {
250 						if (sortDocumentIdAscending) {
251 							return ObjectUtils.compare(NumberUtils.toInt(row1.getDocumentId()), NumberUtils.toInt(row2.getDocumentId()));
252 						} else {
253 							return ObjectUtils.compare(NumberUtils.toInt(row2.getDocumentId()), NumberUtils.toInt(row1.getDocumentId()));
254 						}
255 					}
256 		    	});
257 		    }
258 		    
259 		    taaf.setApprovalRows(approvalRows);
260 		    taaf.setResultSize(persons.size());
261 		}
262 		
263 		taaf.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taaf.getPayCalendarEntries()));
264 	}
265 	
266 	public ActionForward selectNewPayCalendar(ActionMapping mapping, ActionForm form,
267 			HttpServletRequest request, HttpServletResponse response)
268 			throws Exception {
269 		// resets the common fields for approval pages
270 		super.resetMainFields(form);
271 		TimeApprovalActionForm taaf = (TimeApprovalActionForm)form;
272 		// KPME-909
273         taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
274 		return loadApprovalTab(mapping, form, request, response);
275 	}
276 	
277     /**
278      * Helper method to modify / manage the list of records needed to display approval data to the user.
279      *
280      * @param taaf
281      * @return
282      */
283     protected List<ApprovalTimeSummaryRow> getApprovalRows(TimeApprovalActionForm taaf, List<TKPerson> assignmentPrincipalIds) {
284         return TkServiceLocator.getTimeApproveService().getApprovalSummaryRows(taaf.getPayBeginDate(), taaf.getPayEndDate(), taaf.getSelectedPayCalendarGroup(), assignmentPrincipalIds, taaf.getPayCalendarLabels(), taaf.getPayCalendarEntries());
285     }
286 	
287     public void resetState(ActionForm form, HttpServletRequest request) {
288     	  TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
289  	      String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
290  	      
291  	      if (StringUtils.isBlank(page)) {
292  			  taaf.getDepartments().clear();
293  			  taaf.getWorkAreaDescr().clear();
294  			  taaf.setApprovalRows(new ArrayList<ApprovalTimeSummaryRow>());
295  			  taaf.setSelectedDept(null);
296  			  taaf.setSearchField(null);
297  			  taaf.setSearchTerm(null);
298  	      }
299 	}
300 	
301     @Override
302     protected void populateCalendarAndPayPeriodLists(HttpServletRequest request, ApprovalForm taf) {
303     	TimeApprovalActionForm taaf = (TimeApprovalActionForm)taf;
304 		// set calendar year list
305 		Set<String> yearSet = new HashSet<String>();
306 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
307 		// if selected calendar year is passed in
308 		if(!StringUtils.isEmpty(request.getParameter("selectedCY"))) {
309 			taaf.setSelectedCalendarYear(request.getParameter("selectedCY").toString());
310 		} else {
311 			taaf.setSelectedCalendarYear(sdf.format(taaf.getPayCalendarEntries().getBeginPeriodDate()));
312 		}
313 		
314 		List<CalendarEntries> pcListForYear = new ArrayList<CalendarEntries>();
315 		List<CalendarEntries> pceList = TkServiceLocator.getTimeApproveService()
316 			.getAllPayCalendarEntriesForApprover(TKContext.getPrincipalId(), TKUtils.getTimelessDate(null));
317 	    for(CalendarEntries pce : pceList) {
318 	    	yearSet.add(sdf.format(pce.getBeginPeriodDate()));
319 	    	if(sdf.format(pce.getBeginPeriodDate()).equals(taaf.getSelectedCalendarYear())) {
320 	    		pcListForYear.add(pce);
321 	    	}
322 	    }
323 	    List<String> yearList = new ArrayList<String>(yearSet);
324 	    Collections.sort(yearList);
325 	    Collections.reverse(yearList);	// newest on top
326 	    taaf.setCalendarYears(yearList);
327 		
328 		// set pay period list contents
329 		if(!StringUtils.isEmpty(request.getParameter("selectedPP"))) {
330 			taaf.setSelectedPayPeriod(request.getParameter("selectedPP").toString());
331 		} else {
332 			taaf.setSelectedPayPeriod(taaf.getPayCalendarEntries().getHrCalendarEntriesId());
333 			taaf.setPayPeriodsMap(ActionFormUtils.getPayPeriodsMap(pcListForYear));
334 		}
335 		if(taaf.getPayPeriodsMap().isEmpty()) {
336 		    taaf.setPayPeriodsMap(ActionFormUtils.getPayPeriodsMap(pcListForYear));
337 		}
338 	}
339 }