View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }