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