1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.common;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.struts.action.ActionForm;
26 import org.hsqldb.lib.StringUtil;
27 import org.joda.time.DateTime;
28 import org.joda.time.LocalDate;
29 import org.kuali.kpme.core.KPMENamespace;
30 import org.kuali.kpme.core.role.KPMERole;
31 import org.kuali.kpme.core.service.HrServiceLocator;
32 import org.kuali.kpme.core.util.HrContext;
33 import org.kuali.kpme.core.web.KPMEAction;
34 import org.kuali.kpme.core.workarea.WorkArea;
35 import org.kuali.rice.kim.api.role.RoleService;
36 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37
38 public abstract class ApprovalFormAction extends KPMEAction {
39
40 protected void setSearchFields(ApprovalForm approvalForm) {
41 String principalId = HrContext.getTargetPrincipalId();
42
43 LocalDate currentDate = LocalDate.now();
44 DateTime currentDateTime = currentDate.toDateTimeAtStartOfDay();
45
46 List<String> roleIds = new ArrayList<String>();
47 RoleService roleService = KimApiServiceLocator.getRoleService();
48 roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName()));
49 roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName()));
50 roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName()));
51 roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName()));
52 roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName()));
53
54 Set<Long> workAreas = new TreeSet<Long>();
55
56 workAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRoles(principalId, roleIds, currentDateTime, true));
57 if (CollectionUtils.isEmpty(approvalForm.getPayCalendarGroups())) {
58 List<String> principalIds = HrServiceLocator.getAssignmentService().getPrincipalIdsInActiveAssignmentsForWorkAreas(new ArrayList<Long>(workAreas), currentDate);
59
60 List<String> calendarGroups = new ArrayList<String>();
61 if (CollectionUtils.isNotEmpty(principalIds)) {
62 calendarGroups = getCalendars(principalIds);
63 }
64 if (StringUtils.isEmpty(approvalForm.getSelectedPayCalendarGroup())) {
65 approvalForm.setSelectedPayCalendarGroup(CollectionUtils.isNotEmpty(calendarGroups) ? calendarGroups.get(0) : null);
66 }
67 approvalForm.setPayCalendarGroups(calendarGroups);
68 }
69
70 List<WorkArea> workAreasWithoutRoles = HrServiceLocator.getWorkAreaService().getWorkAreasWithoutRoles(new ArrayList<Long>(workAreas), currentDate);
71 if (CollectionUtils.isEmpty(approvalForm.getDepartments())) {
72 Set<String> departments = new TreeSet<String>();
73
74
75 for (WorkArea workArea : workAreasWithoutRoles) {
76 departments.add(workArea.getDept());
77 }
78 approvalForm.setDepartments(new ArrayList<String>(departments));
79 if (StringUtils.isEmpty(approvalForm.getSelectedDept())) {
80 approvalForm.setSelectedDept(CollectionUtils.isNotEmpty(approvalForm.getDepartments()) ? approvalForm.getDepartments().get(0) : null);
81 }
82 }
83
84
85
86
87
88 approvalForm.getWorkAreaDescr().clear();
89
90
91 for (WorkArea workAreaObj : workAreasWithoutRoles) {
92
93 if (StringUtils.equals(workAreaObj.getDept(), approvalForm.getSelectedDept())) {
94 Long workArea = workAreaObj.getWorkArea();
95
96 String description = workAreaObj.getDescription();
97
98
99
100
101
102
103
104 approvalForm.getWorkAreaDescr().put(workArea, description + "(" + workArea + ")");
105
106 }
107 }
108 }
109
110 protected abstract List<String> getCalendars(List<String> principalIds);
111
112 protected List<String> getWorkAreas(ActionForm form) {
113 ApprovalForm approvalForm = (ApprovalForm) form;
114
115 List<String> workAreas = new ArrayList<String>();
116
117 if (StringUtil.isEmpty(approvalForm.getSelectedWorkArea())) {
118 for (Long workAreaKey : approvalForm.getWorkAreaDescr().keySet()) {
119 workAreas.add(workAreaKey.toString());
120 }
121 } else {
122 workAreas.add(approvalForm.getSelectedWorkArea());
123 }
124
125 return workAreas;
126 }
127
128 }