1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.roles.UserRoles;
38 import org.kuali.hr.time.service.base.TkServiceLocator;
39 import org.kuali.hr.time.util.TKContext;
40 import org.kuali.hr.time.util.TKUser;
41 import org.kuali.hr.time.util.TKUtils;
42 import org.kuali.hr.time.util.TkConstants;
43 import org.kuali.hr.time.workarea.WorkArea;
44 import org.kuali.rice.krad.exception.AuthorizationException;
45 import org.kuali.rice.krad.util.GlobalVariables;
46
47 public class ApprovalAction extends TkAction{
48
49 @Override
50 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
51 if (form instanceof TkForm) {
52 String methodToCall = ((TkForm)form).getMethodToCall();
53 if(StringUtils.isEmpty(methodToCall)) {
54 super.execute(mapping, form, request, response);
55 return loadApprovalTab(mapping, form, request, response);
56 }
57 }
58 return super.execute(mapping, form, request, response);
59 }
60
61 public ActionForward loadApprovalTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
62 return mapping.findForward("basic");
63 }
64
65 protected List<TKPerson> getSubListPrincipalIds(HttpServletRequest request, List<TKPerson> assignmentPrincipalIds) {
66 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
67
68
69 Integer beginIndex = StringUtils.isBlank(page) || StringUtils.equals(page, "1") ? 0 : (Integer.parseInt(page) - 1)*TkConstants.PAGE_SIZE;
70 Integer endIndex = beginIndex + TkConstants.PAGE_SIZE > assignmentPrincipalIds.size() ? assignmentPrincipalIds.size() : beginIndex + TkConstants.PAGE_SIZE;
71
72 return assignmentPrincipalIds.subList(beginIndex, endIndex);
73 }
74
75 protected Boolean isAscending(HttpServletRequest request) {
76
77 String ascending = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_ORDER)));
78 return StringUtils.equals(ascending, "1") ? true : false;
79 }
80
81 protected String getSortField(HttpServletRequest request) {
82 return request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_SORT)));
83 }
84
85 protected void checkTKAuthorization(ActionForm form, String methodToCall)
86 throws AuthorizationException {
87 if (!TKUser.isTimesheetReviewer() && !TKUser.isAnyApproverActive() && !TKUser.isSystemAdmin()
88 && !TKUser.isLocationAdmin() && !TKUser.isGlobalViewOnly() && !TKUser.isDeptViewOnly()
89 && !TKUser.isDepartmentAdmin()) {
90 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "ApprovalAction", "");
91 }
92 }
93
94 protected void resetMainFields(ActionForm form) {
95 ApprovalForm taf = (ApprovalForm) form;
96 taf.setSearchField(null);
97 taf.setSearchTerm(null);
98 taf.setSelectedWorkArea(null);
99 taf.setSelectedDept(null);
100 taf.setPayBeginDate(null);
101 taf.setPayEndDate(null);
102 taf.setHrPyCalendarEntriesId(null);
103 }
104
105 protected void setupDocumentOnFormContext(HttpServletRequest request,
106 ApprovalForm taf, CalendarEntries payCalendarEntries, String page) {
107 if(payCalendarEntries == null) {
108 return;
109 }
110 taf.setHrPyCalendarId(payCalendarEntries.getHrCalendarId());
111 taf.setHrPyCalendarEntriesId(payCalendarEntries.getHrCalendarEntriesId());
112 taf.setPayBeginDate(payCalendarEntries.getBeginPeriodDateTime());
113 taf.setPayEndDate(DateUtils.addMilliseconds(payCalendarEntries.getEndPeriodDateTime(),-1));
114
115 CalendarEntries prevPayCalendarEntries = TkServiceLocator.getCalendarEntriesService().getPreviousCalendarEntriesByCalendarId(taf.getHrPyCalendarId(), payCalendarEntries);
116 if (prevPayCalendarEntries != null) {
117 taf.setPrevPayCalendarId(prevPayCalendarEntries.getHrCalendarEntriesId());
118 } else {
119 taf.setPrevPayCalendarId(null);
120 }
121
122 CalendarEntries nextPayCalendarEntries = TkServiceLocator.getCalendarEntriesService().getNextCalendarEntriesByCalendarId(taf.getHrPyCalendarId(), payCalendarEntries);
123 if (nextPayCalendarEntries != null) {
124 taf.setNextPayCalendarId(nextPayCalendarEntries.getHrCalendarEntriesId());
125 } else {
126 taf.setNextPayCalendarId(null);
127 }
128 if (StringUtils.isBlank(page)) {
129 List<String> depts = new ArrayList<String>(TKUser.getReportingApprovalDepartments().keySet());
130 if ( depts.isEmpty() ) {
131 return;
132 }
133 Collections.sort(depts);
134 taf.setDepartments(depts);
135
136 if (taf.getDepartments().size() == 1 || taf.getSelectedDept() != null) {
137 if (StringUtils.isEmpty(taf.getSelectedDept()))
138 taf.setSelectedDept(taf.getDepartments().get(0));
139
140 List<WorkArea> workAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(taf.getSelectedDept(), new java.sql.Date(taf.getPayBeginDate().getTime()));
141 UserRoles ur = TKUser.getCurrentTargetRoles();
142 for(WorkArea wa : workAreas){
143 if (ur.getApproverWorkAreas().contains(wa.getWorkArea())
144 || ur.getReviewerWorkAreas().contains(wa.getWorkArea())) {
145 taf.getWorkAreaDescr().put(wa.getWorkArea(),wa.getDescription()+"("+wa.getWorkArea()+")");
146 }
147 }
148 }
149 }
150
151 }
152
153 public ActionForward gotoCurrentPayPeriod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
154
155 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
156 ApprovalForm taf = (ApprovalForm) form;
157 Date currentDate = TKUtils.getTimelessDate(null);
158 Calendar currentPayCalendar = TkServiceLocator.getCalendarService().getCalendarByGroup(taf.getSelectedPayCalendarGroup());
159 CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntriesByCalendarId(currentPayCalendar.getHrCalendarId(), currentDate);
160 taf.setPayCalendarEntries(payCalendarEntries);
161 taf.setSelectedCalendarYear(new SimpleDateFormat("yyyy").format(payCalendarEntries.getBeginPeriodDate()));
162 taf.setSelectedPayPeriod(payCalendarEntries.getHrCalendarEntriesId());
163 populateCalendarAndPayPeriodLists(request, taf);
164 setupDocumentOnFormContext(request, taf, payCalendarEntries, page);
165 return mapping.findForward("basic");
166 }
167
168
169
170
171 public ActionForward changeCalendarYear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
172 ApprovalForm taf = (ApprovalForm) form;
173 if(!StringUtils.isEmpty(request.getParameter("selectedCY"))) {
174 taf.setSelectedCalendarYear(request.getParameter("selectedCY").toString());
175 populateCalendarAndPayPeriodLists(request, taf);
176 }
177 return mapping.findForward("basic");
178 }
179
180
181 public ActionForward changePayPeriod(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
182 String page = request.getParameter((new ParamEncoder(TkConstants.APPROVAL_TABLE_ID).encodeParameterName(TableTagParameters.PARAMETER_PAGE)));
183 ApprovalForm taf = (ApprovalForm) form;
184 if(!StringUtils.isEmpty(request.getParameter("selectedPP"))) {
185 taf.setSelectedPayPeriod(request.getParameter("selectedPP").toString());
186 CalendarEntries pce = TkServiceLocator.getCalendarEntriesService()
187 .getCalendarEntries(request.getParameter("selectedPP").toString());
188 if(pce != null) {
189 taf.setPayCalendarEntries(pce);
190 setupDocumentOnFormContext(request, taf, pce, page);
191 }
192 }
193 return mapping.findForward("basic");
194 }
195
196 protected void populateCalendarAndPayPeriodLists(HttpServletRequest request, ApprovalForm taf) {
197
198 }
199
200
201 }