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