View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.common;
17  
18  import java.util.*;
19  
20  import javax.servlet.http.HttpServletRequest;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.struts.action.ActionForm;
24  import org.displaytag.tags.TableTagParameters;
25  import org.displaytag.util.ParamEncoder;
26  import org.kuali.kpme.core.assignment.Assignment;
27  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.HrConstants;
30  import org.kuali.kpme.core.util.HrContext;
31  import org.kuali.kpme.core.workarea.WorkArea;
32  import org.kuali.kpme.tklm.time.detail.web.ActionFormUtils;
33  import org.kuali.kpme.tklm.time.util.TkContext;
34  import org.kuali.rice.krad.exception.AuthorizationException;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  public abstract class CalendarApprovalFormAction extends ApprovalFormAction {
38  	
39  	@Override
40  	protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
41  		if (!HrContext.isReviewer() && !HrContext.isAnyApprover() && !HrContext.isSystemAdmin() && !TkContext.isLocationAdmin() && !HrContext.isGlobalViewOnly() 
42  				&& !TkContext.isDepartmentViewOnly() && !TkContext.isDepartmentAdmin() && !HrContext.isAnyPayrollProcessor()) {
43  			throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "CalendarApprovalFormAction", "");
44  		}
45  	}
46  	
47  	protected void setSearchFields(CalendarApprovalForm calendarApprovalForm) {
48  		super.setSearchFields(calendarApprovalForm);
49  
50  		if (calendarApprovalForm.getCalendarDocument() != null) {
51  			calendarApprovalForm.setSelectedPayCalendarGroup(calendarApprovalForm.getCalendarDocument().getCalendarEntry().getCalendarName());
52  			for (Assignment assignment : calendarApprovalForm.getCalendarDocument().getAssignments()) {
53  				WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(assignment.getWorkArea(), assignment.getEffectiveLocalDate());
54  				if (calendarApprovalForm.getDepartments().contains(workArea.getDept())) {
55  					calendarApprovalForm.setSelectedDept(workArea.getDept());
56  					break;
57  				}
58  			}
59  		}
60  	}
61  	
62      protected void setCalendarFields(CalendarApprovalForm calendarApprovalForm) {
63  		Set<String> calendarYears = new TreeSet<String>(Collections.reverseOrder());
64  		List<CalendarEntry> calendarEntries = getCalendarEntries(calendarApprovalForm.getCalendarEntry());
65  		
66  	    for (CalendarEntry calendarEntry : calendarEntries) {
67  	    	String calendarEntryYear = calendarEntry.getBeginPeriodFullDateTime().toString("yyyy");
68  	    	calendarYears.add(calendarEntryYear);
69  	    }
70  	    
71          String currentCalendarYear = calendarApprovalForm.getCalendarEntry().getBeginPeriodFullDateTime().toString("yyyy");
72          String selectedCalendarYear = StringUtils.isNotBlank(calendarApprovalForm.getSelectedCalendarYear()) ? calendarApprovalForm.getSelectedCalendarYear() : currentCalendarYear;
73  	    
74          calendarApprovalForm.setCalendarYears(new ArrayList<String>(calendarYears));
75          calendarApprovalForm.setPayPeriodsMap(ActionFormUtils.getPayPeriodsMap(ActionFormUtils.getAllCalendarEntriesForYear(calendarEntries, selectedCalendarYear), null));
76  	    
77          calendarApprovalForm.setSelectedCalendarYear(selectedCalendarYear);
78          calendarApprovalForm.setSelectedPayPeriod(calendarApprovalForm.getCalendarEntry().getHrCalendarEntryId());
79  	}
80  
81      protected List<CalendarEntry> getCalendarEntries(CalendarEntry currentCalendarEntry) {
82  		return HrServiceLocator.getCalendarEntryService().getAllCalendarEntriesForCalendarId(currentCalendarEntry.getHrCalendarId());
83  	}
84  	
85  //	protected List<String> getSubListPrincipalIds(HttpServletRequest request, List<String> assignmentPrincipalIds) {
86  //	    String page = request.getParameter((new ParamEncoder(HrConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
87  //	    // The paging index begins from 1, but the sublist index begins from 0.
88  //	    // So the logic below sets the sublist begin index to 0 if the page number is null or equals 1
89  //	    Integer beginIndex = StringUtils.isBlank(page) || StringUtils.equals(page, "1") ? 0 : (Integer.parseInt(page) - 1) * HrConstants.PAGE_SIZE;
90  //	    Integer endIndex = beginIndex + HrConstants.PAGE_SIZE > assignmentPrincipalIds.size() ? assignmentPrincipalIds.size() : beginIndex + HrConstants.PAGE_SIZE;
91  //	
92  //	    return assignmentPrincipalIds.subList(beginIndex, endIndex);
93  //	}
94  
95  	protected Boolean getAscending(HttpServletRequest request) {
96  	    // returned value 1 = ascending; 2 = descending
97  	    String ascending = request.getParameter((new ParamEncoder(HrConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
98  	    return StringUtils.isEmpty(ascending) || StringUtils.equals(ascending, "1");
99  	}
100 
101 	protected String getSortField(HttpServletRequest request) {
102 	    return request.getParameter((new ParamEncoder(HrConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_SORT)));
103 	}
104 
105 }