1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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
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
109 TkServiceLocator.getTimesheetService().resetTimeBlock(newTimeBlocks);
110
111
112 TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.CLOCK_OUT, newTimeBlocks, pe, td, principalId);
113
114
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
130
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
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 }