1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.assignment.service;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.joda.time.DateTime;
22 import org.kuali.hr.job.Job;
23 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
24 import org.kuali.hr.time.assignment.Assignment;
25 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
26 import org.kuali.hr.time.assignment.dao.AssignmentDao;
27 import org.kuali.hr.time.calendar.CalendarEntries;
28 import org.kuali.hr.time.service.base.TkServiceLocator;
29 import org.kuali.hr.time.timesheet.TimesheetDocument;
30 import org.kuali.hr.time.util.TKContext;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.hr.time.util.TkConstants;
33
34 import java.sql.Date;
35 import java.util.*;
36
37 public class AssignmentServiceImpl implements AssignmentService {
38
39 private static final Logger LOG = Logger.getLogger(AssignmentServiceImpl.class);
40 private AssignmentDao assignmentDao;
41
42 public AssignmentDao getAssignmentDao() {
43 return assignmentDao;
44 }
45
46 public void setAssignmentDao(AssignmentDao assignmentDao) {
47 this.assignmentDao = assignmentDao;
48 }
49
50
51 @Override
52 public List<Assignment> getAssignments(String principalId, Date asOfDate) {
53 List<Assignment> assignments;
54
55 if (asOfDate == null) {
56 asOfDate = TKUtils.getCurrentDate();
57 }
58
59 assignments = assignmentDao.findAssignments(principalId, asOfDate);
60
61 for (Assignment assignment : assignments) {
62 populateAssignment(assignment, asOfDate);
63 }
64
65 return assignments;
66 }
67
68 public List<Assignment> getAssignments(String principalId, Date beginDate, Date endDate) {
69 List<Assignment> assignments;
70
71 assignments = assignmentDao.findAssignmentsWithinPeriod(principalId, beginDate, endDate);
72
73 for (Assignment assignment : assignments) {
74 populateAssignment(assignment, assignment.getEffectiveDate());
75 }
76
77 return assignments;
78 }
79
80
81 @Override
82 public List<Assignment> searchAssignments(Date fromEffdt, Date toEffdt, String principalId, String jobNumber,
83 String dept, String workArea, String active, String showHistory) {
84 return assignmentDao.searchAssignments(fromEffdt, toEffdt, principalId, jobNumber, dept, workArea, active, showHistory);
85 }
86
87 public List<Assignment> getAssignmentsByPayEntry(String principalId, CalendarEntries payCalendarEntry) {
88 DateTime entryEndDate = payCalendarEntry.getEndLocalDateTime().toDateTime();
89 if (entryEndDate.getHourOfDay() == 0) {
90 entryEndDate = entryEndDate.minusDays(1);
91 }
92 Date endDate = new java.sql.Date(entryEndDate.getMillis());
93 List<Assignment> beginPeriodAssign = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate());
94 List<Assignment> endPeriodAssign = getAssignments(principalId, endDate);
95 List<Assignment> assignsWithPeriod = getAssignments(principalId, payCalendarEntry.getBeginPeriodDate(), endDate);
96
97 List<Assignment> finalAssignments = new ArrayList<Assignment>();
98 Map<String, Assignment> assignKeyToAssignmentMap = new HashMap<String, Assignment>();
99 for (Assignment assign : endPeriodAssign) {
100 assignKeyToAssignmentMap.put(TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask()), assign);
101 finalAssignments.add(assign);
102 }
103
104
105 for (Assignment assign : beginPeriodAssign) {
106 String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
107 if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
108 finalAssignments.add(assign);
109 }
110 }
111
112
113 for (Assignment assign : assignsWithPeriod) {
114 String assignKey = TKUtils.formatAssignmentKey(assign.getJobNumber(), assign.getWorkArea(), assign.getTask());
115 if (!assignKeyToAssignmentMap.containsKey(assignKey)) {
116 finalAssignments.add(assign);
117 }
118 }
119
120 return finalAssignments;
121
122 }
123
124 public List<Assignment> getAssignmentsByCalEntryForTimeCalendar(String principalId, CalendarEntries payCalendarEntry){
125 if (StringUtils.isEmpty(principalId)
126 || payCalendarEntry == null) {
127 return Collections.emptyList();
128 }
129 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignmentsByPayEntry(principalId, payCalendarEntry);
130 List<Assignment> results = TkServiceLocator.getAssignmentService().filterAssignments(assignments, TkConstants.FLSA_STATUS_NON_EXEMPT, false);
131 return results;
132 }
133
134 public List<Assignment> getAssignmentsByCalEntryForLeaveCalendar(String principalId, CalendarEntries payCalendarEntry){
135 if (StringUtils.isEmpty(principalId)
136 || payCalendarEntry == null) {
137 return Collections.emptyList();
138 }
139 List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignmentsByPayEntry(principalId, payCalendarEntry);
140 List<Assignment> results = TkServiceLocator.getAssignmentService().filterAssignments(assignments, null, true);
141 return results;
142 }
143
144 public List<Assignment> filterAssignments(List<Assignment> assignments, String flsaStatus, boolean chkForLeaveEligible) {
145 List<Assignment> results = new ArrayList<Assignment>();
146 for(Assignment assignment : assignments) {
147 boolean flag = false;
148 if(StringUtils.isNotEmpty(flsaStatus)) {
149 if(assignment != null
150 && assignment.getJob() != null
151 && assignment.getJob().getFlsaStatus() != null
152 && assignment.getJob().getFlsaStatus().equalsIgnoreCase(flsaStatus)) {
153 if(chkForLeaveEligible) {
154 if(assignment.getJob().isEligibleForLeave()) {
155 flag = true;
156 }
157 }else {
158 flag = true;
159 }
160 }
161 }else {
162 if(chkForLeaveEligible) {
163 if(assignment != null && assignment.getJob() != null && assignment.getJob().isEligibleForLeave()) {
164 flag = true;
165 }
166 } else {
167 flag = true;
168 }
169 }
170
171 if(flag) {
172 results.add(assignment);
173 }
174 }
175
176 return results;
177
178 }
179
180 @Override
181 public AssignmentDescriptionKey getAssignmentDescriptionKey(String assignmentKey) {
182 return new AssignmentDescriptionKey(assignmentKey);
183 }
184
185 @Override
186 public Map<String, String> getAssignmentDescriptions(TimesheetDocument td, boolean clockOnlyAssignments) {
187 if (td == null) {
188
189 return Collections.emptyMap();
190 }
191 List<Assignment> assignments = td.getAssignments();
192
193
194
195
196 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
197 for (Assignment assignment : assignments) {
198
199
200 if (!StringUtils.equals(TKContext.getTargetPrincipalId(), TKContext.getPrincipalId())) {
201 if (!TKContext.getUser().isSystemAdmin() && !TKContext.getUser().getReportingWorkAreas().contains(assignment.getWorkArea())) {
202 continue;
203 }
204 }
205
206
207
208 if (!clockOnlyAssignments || assignment.isSynchronous()) {
209 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
210 }
211 }
212
213 return assignmentDescriptions;
214 }
215
216 @Override
217 public Map<String, String> getAssignmentDescriptions(Assignment assignment) {
218 if (assignment == null) {
219 throw new RuntimeException("Assignment is null");
220 }
221
222 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
223 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
224
225 return assignmentDescriptions;
226
227 }
228
229 @Override
230 public Assignment getAssignment(TimesheetDocument timesheetDocument, String assignmentKey) {
231 List<Assignment> assignments = timesheetDocument.getAssignments();
232 AssignmentDescriptionKey desc = getAssignmentDescriptionKey(assignmentKey);
233
234 for (Assignment assignment : assignments) {
235 if (assignment.getJobNumber().compareTo(desc.getJobNumber()) == 0 &&
236 assignment.getWorkArea().compareTo(desc.getWorkArea()) == 0 &&
237 assignment.getTask().compareTo(desc.getTask()) == 0) {
238 return assignment;
239 }
240 }
241
242
243 Assignment assign = TkServiceLocator.getAssignmentService().getAssignment(desc, timesheetDocument.getCalendarEntry().getBeginPeriodDate());
244 if (assign != null) {
245 return assign;
246 }
247
248
249 LOG.warn("no matched assignment found");
250 return new Assignment();
251 }
252
253 @Override
254 public Assignment getAssignment(String tkAssignmentId) {
255 return getAssignmentDao().getAssignment(tkAssignmentId);
256 }
257
258
259 @Override
260 public List<Assignment> getActiveAssignmentsForWorkArea(Long workArea, Date asOfDate) {
261 List<Assignment> assignments = assignmentDao.getActiveAssignmentsInWorkArea(workArea, asOfDate);
262 for (Assignment assignment : assignments) {
263 populateAssignment(assignment, asOfDate);
264 }
265 return assignments;
266 }
267
268 @Override
269 public List<Assignment> getActiveAssignments(Date asOfDate) {
270 return assignmentDao.getActiveAssignments(asOfDate);
271 }
272
273 private void populateAssignment(Assignment assignment, Date asOfDate) {
274 assignment.setJob(TkServiceLocator.getJobService().getJob(assignment.getPrincipalId(), assignment.getJobNumber(), asOfDate));
275 assignment.setTimeCollectionRule(TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(assignment.getJob().getDept(), assignment.getWorkArea(), assignment.getJob().getHrPayType(),asOfDate));
276 assignment.setWorkAreaObj(TkServiceLocator.getWorkAreaService().getWorkArea(assignment.getWorkArea(), asOfDate));
277 assignment.setDeptLunchRule(TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule(assignment.getJob().getDept(),
278 assignment.getWorkArea(), assignment.getPrincipalId(), assignment.getJobNumber(), asOfDate));
279 }
280
281 public Assignment getAssignment(String principalId, AssignmentDescriptionKey key, Date asOfDate) {
282 Assignment a = null;
283
284 if (key != null) {
285 a = assignmentDao.getAssignment(principalId, key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
286 }
287
288 return a;
289 }
290
291 @Override
292 public Assignment getAssignment(AssignmentDescriptionKey key, Date asOfDate) {
293 Assignment a = null;
294
295 if (key != null) {
296 a = assignmentDao.getAssignment(key.getJobNumber(), key.getWorkArea(), key.getTask(), asOfDate);
297 }
298
299 return a;
300 }
301
302
303
304
305
306 @Override
307 public List<Assignment> getActiveAssignmentsForJob(String principalId, Long jobNumber, Date asOfDate) {
308 List<Assignment> assignments = assignmentDao.getActiveAssignmentsForJob(principalId, jobNumber, asOfDate);
309
310 return assignments;
311 }
312
313 @Override
314 public Map<String, String> getAssignmentDescriptions(LeaveCalendarDocument lcd) {
315 if (lcd == null) {
316 throw new RuntimeException("leave document is null.");
317 }
318 List<Assignment> assignments = lcd.getAssignments();
319 return TkServiceLocator.getAssignmentService().getAssignmentDescriptionsForAssignments(assignments);
320 }
321
322 public Map<String, String> getAssignmentDescriptionsForAssignments(List<Assignment> assignments) {
323 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
324 for (Assignment assignment : assignments) {
325 assignmentDescriptions.putAll(TKUtils.formatAssignmentDescription(assignment));
326 }
327 return assignmentDescriptions;
328 }
329
330 @Override
331 public Assignment getAssignment(LeaveCalendarDocument leaveCalendarDocument, String assignmentKey) {
332 List<Assignment> assignments = leaveCalendarDocument.getAssignments();
333 return TkServiceLocator.getAssignmentService().getAssignment(assignments, assignmentKey, leaveCalendarDocument.getCalendarEntry().getBeginPeriodDate());
334 }
335
336 public Assignment getAssignment(List<Assignment> assignments, String assignmentKey, Date beginDate) {
337 AssignmentDescriptionKey desc = getAssignmentDescriptionKey(assignmentKey);
338 if (CollectionUtils.isNotEmpty(assignments)) {
339 for (Assignment assignment : assignments) {
340 if (assignment.getJobNumber().compareTo(desc.getJobNumber()) == 0 &&
341 assignment.getWorkArea().compareTo(desc.getWorkArea()) == 0 &&
342 assignment.getTask().compareTo(desc.getTask()) == 0) {
343 return assignment;
344 }
345 }
346 }
347
348
349 Assignment assign = TkServiceLocator.getAssignmentService().getAssignment(desc, beginDate);
350 if (assign != null) {
351 return assign;
352 }
353
354 LOG.warn("no matched assignment found");
355 return new Assignment();
356 }
357
358 @Override
359 public Assignment getMaxTimestampAssignment(String principalId) {
360 return assignmentDao.getMaxTimestampAssignment(principalId);
361 }
362
363 public Assignment getAssignmentToApplyScheduledTimeOff(TimesheetDocument timesheetDocument, java.sql.Date payEndDate) {
364 Job primaryJob = TkServiceLocator.getJobService().getPrimaryJob(timesheetDocument.getPrincipalId(), payEndDate);
365 for(Assignment assign : timesheetDocument.getAssignments()){
366 if(assign.getJobNumber().equals(primaryJob.getJobNumber())){
367 return assign;
368 }
369 }
370 return null;
371 }
372
373 public List<String> getPrincipalIds(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
374 if (CollectionUtils.isEmpty(workAreaList)) {
375 return new ArrayList<String>();
376 }
377 return assignmentDao.getPrincipalIds(workAreaList, effdt, startDate, endDate);
378 }
379
380 public List<Assignment> getAssignments(List<String> workAreaList, Date effdt, Date startDate, Date endDate) {
381 if (CollectionUtils.isEmpty(workAreaList)) {
382 return new ArrayList<Assignment>();
383 }
384 return assignmentDao.getAssignments(workAreaList, effdt, startDate, endDate);
385 }
386 }