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