1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.timesheet;
17
18 import org.kuali.kpme.core.api.assignment.Assignment;
19 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
20 import org.kuali.kpme.tklm.api.common.TkConstants;
21 import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
22 import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
23 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
24 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 public final class TimesheetUtils {
33 public static void processTimeBlocksWithRuleChange(List<TimeBlock> timeBlocks, List<TimeBlock> referenceTimeBlocks,
34 List<LeaveBlock> leaveBlocks, CalendarEntry calendarEntry,
35 TimesheetDocument td, String userPrincipalId) {
36 timeBlocks = TkServiceLocator.getTimesheetService().resetTimeBlock(timeBlocks, td.getAsOfDate());
37 timeBlocks = TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.ADD_TIME_BLOCK, timeBlocks, leaveBlocks, calendarEntry, td, userPrincipalId);
38 TkServiceLocator.getTimeBlockService().saveOrUpdateTimeBlocks(referenceTimeBlocks, timeBlocks, userPrincipalId);
39 }
40
41 public static List<TimeBlock> getTimesheetTimeblocksForProcessing(TimesheetDocument doc, boolean includeOverlap) {
42 List<TimeBlock> newTimeBlocks = doc.getTimeBlocks();
43
44 if (includeOverlap) {
45 List<TimeBlock> extraBlocks = TkServiceLocator.getShiftDifferentialRuleService().getTimeblocksOverlappingTimesheetShift(doc);
46 for (TimeBlock extraTimeBlock : extraBlocks) {
47 if (!newTimeBlocks.contains(extraTimeBlock)) {
48 newTimeBlocks.add(extraTimeBlock);
49 }
50 }
51 }
52 return newTimeBlocks;
53 }
54
55 public static List<TimeBlock> getReferenceTimeBlocks(List<TimeBlock> existingBlocks) {
56 List<TimeBlock> referenceTimeBlocks = new ArrayList<TimeBlock>(existingBlocks.size());
57 for (TimeBlock tb : existingBlocks) {
58 referenceTimeBlocks.add(TimeBlock.copy(tb));
59 }
60 return Collections.unmodifiableList(referenceTimeBlocks);
61 }
62
63 public static List<LeaveBlock> getLeaveBlocksForTimesheet(TimesheetDocument doc) {
64 CalendarEntry ce = doc.getCalendarEntry();
65 List<Assignment> assignments = doc.getAllAssignments();
66 Set<String> assignmentKeys = new HashSet<String>();
67 for (Assignment assignment : assignments) {
68 assignmentKeys.add(assignment.getAssignmentKey());
69 }
70 List<String> uniqueKeys = new ArrayList<String>(assignmentKeys);
71 return LmServiceLocator.getLeaveBlockService().getLeaveBlocksForTimeCalendar(doc.getPrincipalId(), ce.getBeginPeriodFullDateTime().toLocalDate(), ce.getEndPeriodFullDateTime().toLocalDate(), uniqueKeys);
72 }
73 }