View Javadoc

1   /**
2    * Copyright 2004-2012 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.hr.time.clocklog.service;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.time.assignment.Assignment;
20  import org.kuali.hr.time.calendar.CalendarEntries;
21  import org.kuali.hr.time.clocklog.ClockLog;
22  import org.kuali.hr.time.clocklog.dao.ClockLogDao;
23  import org.kuali.hr.time.service.base.TkServiceLocator;
24  import org.kuali.hr.time.task.Task;
25  import org.kuali.hr.time.timeblock.TimeBlock;
26  import org.kuali.hr.time.timesheet.TimesheetDocument;
27  import org.kuali.hr.time.util.TKContext;
28  import org.kuali.hr.time.util.TkConstants;
29  
30  import java.math.BigDecimal;
31  import java.sql.Timestamp;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  public class ClockLogServiceImpl implements ClockLogService {
36  
37      private ClockLogDao clockLogDao;
38  
39      public ClockLogServiceImpl() {
40      }
41  
42      public void saveClockLog(ClockLog clockLog) {
43          clockLogDao.saveOrUpdate(clockLog);
44      }
45  
46      @Override
47      public ClockLog processClockLog(Timestamp clockTimeStamp, Assignment assignment,CalendarEntries pe, String ip, java.sql.Date asOfDate, TimesheetDocument td, String clockAction, String principalId) {
48          return processClockLog(clockTimeStamp, assignment, pe, ip, asOfDate, td, clockAction, principalId, TKContext.getPrincipalId());
49      }
50  
51      @Override
52      public ClockLog processClockLog(Timestamp clockTimeStamp, Assignment assignment,CalendarEntries pe, String ip, java.sql.Date asOfDate, TimesheetDocument td, String clockAction, String principalId, String userPrincipalId) {
53          // process rules
54          Timestamp roundedClockTimestamp = TkServiceLocator.getGracePeriodService().processGracePeriodRule(clockTimeStamp, new java.sql.Date(pe.getBeginPeriodDateTime().getTime()));
55  
56          ClockLog clockLog = TkServiceLocator.getClockLogService().buildClockLog(roundedClockTimestamp, new Timestamp(System.currentTimeMillis()), assignment, td, clockAction, ip, userPrincipalId);
57          TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, asOfDate);
58  
59          // If the clock action is clock out or lunch out, create a time block besides the clock log
60          if (StringUtils.equals(clockAction, TkConstants.CLOCK_OUT) || StringUtils.equals(clockAction, TkConstants.LUNCH_OUT)) {
61              processTimeBlock(clockLog, assignment, pe, td, clockAction, principalId);
62          } else {
63              //Save current clock log to get id for timeblock building
64              TkServiceLocator.getClockLogService().saveClockLog(clockLog);
65          }
66  
67          return clockLog;
68      }
69  
70      private void processTimeBlock(ClockLog clockLog, Assignment assignment, CalendarEntries pe, TimesheetDocument td, String clockAction, String principalId) {
71          ClockLog lastLog = null;
72          Timestamp lastClockTimestamp = null;
73          String beginClockLogId = null;
74          String endClockLogId = null;
75  
76          if (StringUtils.equals(clockAction, TkConstants.LUNCH_OUT)) {
77              lastLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId, TkConstants.CLOCK_IN);
78          } else if (StringUtils.equals(clockAction, TkConstants.CLOCK_OUT)) {
79              lastLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
80          }
81          if (lastLog != null) {
82              lastClockTimestamp = lastLog.getClockTimestamp();
83              beginClockLogId = lastLog.getTkClockLogId();
84          }
85          //Save current clock log to get id for timeblock building
86          TkServiceLocator.getClockLogService().saveClockLog(clockLog);
87          endClockLogId = clockLog.getTkClockLogId();
88  
89          long beginTime = lastClockTimestamp.getTime();
90          Timestamp beginTimestamp = new Timestamp(beginTime);
91          Timestamp endTimestamp = clockLog.getClockTimestamp();
92  
93          // New Time Blocks, pointer reference
94          List<TimeBlock> newTimeBlocks = td.getTimeBlocks();
95          List<TimeBlock> referenceTimeBlocks = new ArrayList<TimeBlock>(td.getTimeBlocks().size());
96          for (TimeBlock tb : td.getTimeBlocks()) {
97              referenceTimeBlocks.add(tb.copy());
98          }
99  
100         // Add TimeBlocks after we store our reference object!
101         List<TimeBlock> aList = TkServiceLocator.getTimeBlockService().buildTimeBlocks(assignment, assignment.getJob().getPayTypeObj().getRegEarnCode(), td, beginTimestamp, endTimestamp, BigDecimal.ZERO, BigDecimal.ZERO, true, false);
102         for (TimeBlock tb : aList) {
103             tb.setClockLogBeginId(beginClockLogId);
104             tb.setClockLogEndId(endClockLogId);
105         }
106         newTimeBlocks.addAll(aList);
107 
108         //reset time block
109         TkServiceLocator.getTimesheetService().resetTimeBlock(newTimeBlocks);
110 
111         //apply any rules for this action
112         TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.CLOCK_OUT, newTimeBlocks, pe, td, principalId);
113 
114         //call persist method that only saves added/deleted/changed timeblocks
115         TkServiceLocator.getTimeBlockService().saveTimeBlocks(referenceTimeBlocks, newTimeBlocks);
116     }
117 
118     @Override
119     public ClockLog buildClockLog(Timestamp clockTimestamp, Timestamp originalTimestamp, Assignment assignment, TimesheetDocument timesheetDocument, String clockAction, String ip) {
120         return buildClockLog(clockTimestamp, originalTimestamp, assignment, timesheetDocument, clockAction, ip, TKContext.getPrincipalId());
121     }
122 
123     @Override
124     public ClockLog buildClockLog(Timestamp clockTimestamp, Timestamp originalTimestamp, Assignment assignment, TimesheetDocument timesheetDocument, String clockAction, String ip, String userPrincipalId) {
125         String principalId = timesheetDocument.getPrincipalId();
126 
127         ClockLog clockLog = new ClockLog();
128         clockLog.setPrincipalId(principalId);
129         //        AssignmentDescriptionKey assignmentDesc = TkServiceLocator.getAssignmentService().getAssignmentDescriptionKey(selectedAssign);
130         //        Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(timesheetDocument, selectedAssign);
131         clockLog.setJob(timesheetDocument.getJob(assignment.getJobNumber()));
132         clockLog.setJobNumber(assignment.getJobNumber());
133         clockLog.setWorkArea(assignment.getWorkArea());
134         clockLog.setTkWorkAreaId(assignment.getWorkAreaObj().getTkWorkAreaId());
135 
136         String tkTaskId = null;
137         for (Task task : assignment.getWorkAreaObj().getTasks()) {
138             if (task.getTask().compareTo(assignment.getTask()) == 0) {
139                 tkTaskId = task.getTkTaskId();
140                 break;
141             }
142         }
143         clockLog.setTask(assignment.getTask());
144         clockLog.setTkTaskId(tkTaskId);
145         clockLog.setClockTimestampTimezone(TkServiceLocator.getTimezoneService().getUserTimezone());
146         clockLog.setClockTimestamp(clockTimestamp);
147         clockLog.setClockAction(clockAction);
148         clockLog.setIpAddress(ip);
149         clockLog.setHrJobId(assignment.getJob().getHrJobId());
150         clockLog.setUserPrincipalId(userPrincipalId);
151         // timestamp should be the original time without Grace Period rule applied
152         clockLog.setTimestamp(originalTimestamp);
153 
154         return clockLog;
155     }
156 
157     public void setClockLogDao(ClockLogDao clockLogDao) {
158         this.clockLogDao = clockLogDao;
159     }
160 
161     public ClockLog getLastClockLog(String principalId) {
162         return clockLogDao.getLastClockLog(principalId);
163     }
164 
165     public ClockLog getLastClockLog(String principalId, String clockAction) {
166         return clockLogDao.getLastClockLog(principalId, clockAction);
167     }
168 
169     public List<ClockLog> getOpenClockLogs(  CalendarEntries payCalendarEntry) {
170         return clockLogDao.getOpenClockLogs(payCalendarEntry);
171     }
172 
173     @Override
174     public ClockLog getClockLog(String tkClockLogId) {
175         return clockLogDao.getClockLog(tkClockLogId);
176     }
177 
178 }