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