1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.assignment.service;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28 import org.joda.time.DateTime;
29 import org.joda.time.LocalDate;
30 import org.kuali.kpme.core.KPMENamespace;
31 import org.kuali.kpme.core.assignment.Assignment;
32 import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
33 import org.kuali.kpme.core.assignment.dao.AssignmentDao;
34 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
35 import org.kuali.kpme.core.department.Department;
36 import org.kuali.kpme.core.permission.KPMEPermissionTemplate;
37 import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
38 import org.kuali.kpme.core.service.HrServiceLocator;
39 import org.kuali.kpme.core.util.HrConstants;
40 import org.kuali.kpme.core.util.TKUtils;
41 import org.kuali.rice.kim.api.KimConstants;
42 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
43
44 public class AssignmentServiceImpl implements AssignmentService {
45
46 private static final Logger LOG = Logger.getLogger(AssignmentServiceImpl.class);
47 private AssignmentDao assignmentDao;
48
49 public AssignmentDao getAssignmentDao() {
50 return assignmentDao;
51 }
52
53 public void setAssignmentDao(AssignmentDao assignmentDao) {
54 this.assignmentDao = assignmentDao;
55 }
56
57
58 @Override
59 public List<Assignment> getAssignments(String principalId, LocalDate asOfDate) {
60 List<Assignment> assignments;
61
62 if (asOfDate == null) {
63 asOfDate = LocalDate.now();
64 }
65
66 assignments = assignmentDao.findAssignments(principalId, asOfDate);
67
68 for (Assignment assignment : assignments) {
69 populateAssignment(assignment, asOfDate);
70 }
71
72 return assignments;
73 }
74
75 public List<Assignment> getAssignments(String principalId, LocalDate beginDate, LocalDate endDate) {
76 List<Assignment> assignments;
77
78 assignments = assignmentDao.findAssignmentsWithinPeriod(principalId, beginDate, endDate);
79
80 for (Assignment assignment : assignments) {
81 populateAssignment(assignment, assignment.getEffectiveLocalDate());
82 }
83
84 return assignments;
85 }
86
87
88 @Override
89 public List<Assignment> searchAssignments(String userPrincipalId, LocalDate fromEffdt, LocalDate toEffdt, String principalId, String jobNumber,
90 String dept, String workArea, String active, String showHistory) {
91 List<Assignment> results = new ArrayList<Assignment>();
92
93 List<Assignment> assignmentObjs = assignmentDao.searchAssignments(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
94
95 for (Assignment assignmentObj : assignmentObjs) {
96 String department = assignmentObj.getDept();
97 Department departmentObj = HrServiceLocator.getDepartmentService().getDepartment(department, assignmentObj.getEffectiveLocalDate());
98 String location = departmentObj != null ? departmentObj.getLocation() : null;
99
100 Map<String, String> roleQualification = new HashMap<String, String>();
101 roleQualification.put(KimConstants.AttributeConstants.PRINCIPAL_ID, userPrincipalId);
102 roleQualification.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department);
103 roleQualification.put(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), location);
104
105 if (!KimApiServiceLocator.getPermissionService().isPermissionDefinedByTemplate(KPMENamespace.KPME_WKFLW.getNamespaceCode(),
106 KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>())
107 || KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(userPrincipalId, KPMENamespace.KPME_WKFLW.getNamespaceCode(),
108 KPMEPermissionTemplate.VIEW_KPME_RECORD.getPermissionTemplateName(), new HashMap<String, String>(), roleQualification)) {
109 results.add(assignmentObj);
110 }
111 }
112
113 return results;
114 }
115
116 public List<Assignment> getAssignmentsByPayEntry(String principalId, CalendarEntry payCalendarEntry) {
117 DateTime entryEndDate = payCalendarEntry.getEndPeriodLocalDateTime().toDateTime();
118 if (entryEndDate.getHourOfDay() == 0) {
119 entryEndDate = entryEndDate.minusDays(1);
120 }
121 List<Assignment> beginPeriodAssign = getAssignments(principalId, payCalendarEntry.getBeginPeriodFullDateTime().toLocalDate());
122 List<Assignment> endPeriodAssign = getAssignments(principalId, entryEndDate.toLocalDate());
123 List<Assignment> assignsWithPeriod = getAssignments(principalId, payCalendarEntry.getBeginPeriodFullDateTime().toLocalDate(), entryEndDate.toLocalDate());
124
125 List<Assignment> finalAssignments = new ArrayList<Assignment>();
126 Map<String, Assignment> assignKeyToAssignmentMap = new HashMap<String, Assignment>();
127 for (Assignment assign : endPeriodAssign) {
128 assignKeyToAssignmentMap.put(TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask()), assign);
129 finalAssignments.add(assign);
130 }
131
132
133 for (Assignment assign : beginPeriodAssign) {
134 String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
135 if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
136 finalAssignments.add(assign);
137 }
138 }
139
140
141 for (Assignment assign : assignsWithPeriod) {
142 String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
143 if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
144 finalAssignments.add(assign);
145 }
146 }
147
148 return finalAssignments;
149
150 }
151
152 public List<Assignment> getAssignmentsByCalEntryForTimeCalendar(String principalId, CalendarEntry payCalendarEntry){
153 if (StringUtils.isEmpty(principalId)
154 || payCalendarEntry == null) {
155 return Collections.emptyList();
156 }
157 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignmentsByPayEntry(principalId, payCalendarEntry);
158 List<Assignment> results = HrServiceLocator.getAssignmentService().filterAssignments(assignments, HrConstants.FLSA_STATUS_NON_EXEMPT, false);
159 return results;
160 }
161
162 public List<Assignment> getAssignmentsByCalEntryForLeaveCalendar(String principalId, CalendarEntry payCalendarEntry){
163 if (StringUtils.isEmpty(principalId)
164 || payCalendarEntry == null) {
165 return Collections.emptyList();
166 }
167 List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignmentsByPayEntry(principalId, payCalendarEntry);
168 List<Assignment> results = HrServiceLocator.getAssignmentService().filterAssignments(assignments, null, true);
169 return results;
170 }
171
172 public List<Assignment> filterAssignments(List<Assignment> assignments, String flsaStatus, boolean chkForLeaveEligible) {
173 List<Assignment> results = new ArrayList<Assignment>();
174 for(Assignment assignment : assignments) {
175 boolean flag = false;
176 if(StringUtils.isNotEmpty(flsaStatus)) {
177 if(assignment != null
178 && assignment.getJob() != null
179 && assignment.getJob().getFlsaStatus() != null
180 && assignment.getJob().getFlsaStatus().equalsIgnoreCase(flsaStatus)) {
181 if(chkForLeaveEligible) {
182 if(assignment.getJob().isEligibleForLeave()) {
183 flag = true;
184 }
185 }else {
186 flag = true;
187 }
188 }
189 }else {
190 if(chkForLeaveEligible) {
191 if(assignment != null && assignment.getJob() != null && assignment.getJob().isEligibleForLeave()) {
192 flag = true;
193 }
194 } else {
195 flag = true;
196 }
197 }
198
199 if(flag) {
200 results.add(assignment);
201 }
202 }
203
204 return results;
205
206 }
207
208 @Override
209 public AssignmentDescriptionKey getAssignmentDescriptionKey(String assignmentKey) {
210 return AssignmentDescriptionKey.get(assignmentKey);
211 }
212
213 @Override
214 public Map<String, String> getAssignmentDescriptions(Assignment assignment) {
215 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
216 if (assignment == null) {
217 LOG.warn("Assignment is null");
218
219 } else {
220 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
221 }
222 return assignmentDescriptions;
223
224 }
225
226 @Override
227 public Assignment getAssignment(String tkAssignmentId) {
228 return getAssignmentDao().getAssignment(tkAssignmentId);
229 }
230
231
232 @Override
233 public List<Assignment> getActiveAssignmentsForWorkArea(Long workArea, LocalDate asOfDate) {
234 List<Assignment> assignments = assignmentDao.getActiveAssignmentsInWorkArea(workArea, asOfDate);
235 for (Assignment assignment : assignments) {
236 populateAssignment(assignment, asOfDate);
237 }
238 return assignments;
239 }
240
241 @Override
242 public List<Assignment> getActiveAssignments(LocalDate asOfDate) {
243 return assignmentDao.getActiveAssignments(asOfDate);
244 }
245
246 private void populateAssignment(Assignment assignment, LocalDate asOfDate) {
247 assignment.setJob(HrServiceLocator.getJobService().getJob(assignment.getPrincipalId(), assignment.getJobNumber(), asOfDate));
248 assignment.setWorkAreaObj(HrServiceLocator.getWorkAreaService().getWorkArea(assignment.getWorkArea(), asOfDate));
249 }
250
251 public Assignment getAssignment(String principalId, AssignmentDescriptionKey key, LocalDate asOfDate) {
252 Assignment a = null;
253
254 if (key != null) {
255 a = assignmentDao.getAssignment(principalId, key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
256 }
257
258 return a;
259 }
260
261 @Override
262 public Assignment getAssignmentForTargetPrincipal(AssignmentDescriptionKey key, LocalDate asOfDate) {
263 Assignment a = null;
264
265 if (key != null) {
266 a = assignmentDao.getAssignmentForTargetPrincipal(key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
267 }
268
269 return a;
270 }
271
272
273
274
275
276 @Override
277 public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, LocalDate asOfDate) {
278 List<Assignment> assignments = assignmentDao.getActiveAssignmentsForJob(principalId, jobNumber, asOfDate);
279
280 return assignments;
281 }
282
283 @Override
284 public Map<String, String> getAssignmentDescriptionsForAssignments(List<Assignment> assignments) {
285 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
286 for (Assignment assignment : assignments) {
287 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
288 }
289 return assignmentDescriptions;
290 }
291
292 public Assignment getAssignment(List<Assignment> assignments, String assignmentKey, LocalDate beginDate) {
293 AssignmentDescriptionKey desc = getAssignmentDescriptionKey(assignmentKey);
294 if (CollectionUtils.isNotEmpty(assignments)) {
295 for (Assignment assignment : assignments) {
296 if (assignment.getJobNumber().compareTo(desc.getJobNumber()) == 0 &&
297 assignment.getWorkArea().compareTo(desc.getWorkArea()) == 0 &&
298 assignment.getTask().compareTo(desc.getTask()) == 0) {
299 return assignment;
300 }
301 }
302 }
303
304
305 Assignment assign = getAssignmentForTargetPrincipal(desc, beginDate);
306 if (assign != null) {
307 return assign;
308 }
309
310 LOG.warn("no matched assignment found");
311 return new Assignment();
312 }
313
314 @Override
315 public Assignment getMaxTimestampAssignment(String principalId) {
316 return assignmentDao.getMaxTimestampAssignment(principalId);
317 }
318
319 public List<String> getPrincipalIds(List<String> workAreaList, LocalDate effdt, LocalDate startDate, LocalDate endDate) {
320 if (CollectionUtils.isEmpty(workAreaList)) {
321 return new ArrayList<String>();
322 }
323 return assignmentDao.getPrincipalIds(workAreaList, effdt, startDate, endDate);
324 }
325
326 public List<Assignment> getAssignments(List<String> workAreaList, LocalDate effdt, LocalDate startDate, LocalDate endDate) {
327 if (CollectionUtils.isEmpty(workAreaList)) {
328 return new ArrayList<Assignment>();
329 }
330 return assignmentDao.getAssignments(workAreaList, effdt, startDate, endDate);
331 }
332
333 }