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 java.math.BigDecimal;
19 import java.sql.Timestamp;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.hr.time.assignment.Assignment;
28 import org.kuali.hr.time.calendar.CalendarEntries;
29 import org.kuali.hr.time.clocklog.ClockLog;
30 import org.kuali.hr.time.clocklog.dao.ClockLogDao;
31 import org.kuali.hr.time.service.base.TkServiceLocator;
32 import org.kuali.hr.time.timeblock.TimeBlock;
33 import org.kuali.hr.time.timesheet.TimesheetDocument;
34 import org.kuali.hr.time.util.TKContext;
35 import org.kuali.hr.time.util.TkConstants;
36 import org.kuali.rice.krad.service.KRADServiceLocator;
37 import org.kuali.rice.krad.util.KRADConstants;
38
39 public class ClockLogServiceImpl implements ClockLogService {
40
41 private ClockLogDao clockLogDao;
42
43 public ClockLogServiceImpl() {
44 }
45
46 public void saveClockLog(ClockLog clockLog) {
47 clockLogDao.saveOrUpdate(clockLog);
48 }
49
50 @Override
51 public ClockLog processClockLog(Timestamp clockTimeStamp, Assignment assignment,CalendarEntries pe, String ip, java.sql.Date asOfDate, TimesheetDocument td, String clockAction, String principalId) {
52 return processClockLog(clockTimeStamp, assignment, pe, ip, asOfDate, td, clockAction, principalId, TKContext.getPrincipalId());
53 }
54
55 @Override
56 public ClockLog processClockLog(Timestamp clockTimeStamp, Assignment assignment,CalendarEntries pe, String ip, java.sql.Date asOfDate, TimesheetDocument td, String clockAction, String principalId, String userPrincipalId) {
57
58 Timestamp roundedClockTimestamp = TkServiceLocator.getGracePeriodService().processGracePeriodRule(clockTimeStamp, new java.sql.Date(pe.getBeginPeriodDateTime().getTime()));
59
60 ClockLog clockLog = buildClockLog(roundedClockTimestamp, new Timestamp(System.currentTimeMillis()), assignment, td, clockAction, ip, userPrincipalId);
61 TkServiceLocator.getClockLocationRuleService().processClockLocationRule(clockLog, asOfDate);
62
63
64 if (StringUtils.equals(clockAction, TkConstants.CLOCK_OUT) || StringUtils.equals(clockAction, TkConstants.LUNCH_OUT)) {
65 processTimeBlock(clockLog, assignment, pe, td, clockAction, principalId, userPrincipalId);
66 } else {
67
68 KRADServiceLocator.getBusinessObjectService().save(clockLog);
69 }
70
71 return clockLog;
72 }
73
74 private void processTimeBlock(ClockLog clockLog, Assignment assignment, CalendarEntries pe, TimesheetDocument td, String clockAction, String principalId, String userPrincipalId) {
75 ClockLog lastLog = null;
76 Timestamp lastClockTimestamp = null;
77 String beginClockLogId = null;
78 String endClockLogId = null;
79
80 if (StringUtils.equals(clockAction, TkConstants.LUNCH_OUT)) {
81 lastLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId, TkConstants.CLOCK_IN);
82 } else if (StringUtils.equals(clockAction, TkConstants.CLOCK_OUT)) {
83 lastLog = TkServiceLocator.getClockLogService().getLastClockLog(principalId);
84 }
85 if (lastLog != null) {
86 lastClockTimestamp = lastLog.getClockTimestamp();
87 beginClockLogId = lastLog.getTkClockLogId();
88 }
89
90 KRADServiceLocator.getBusinessObjectService().save(clockLog);
91 endClockLogId = clockLog.getTkClockLogId();
92
93 long beginTime = lastClockTimestamp.getTime();
94 Timestamp beginTimestamp = new Timestamp(beginTime);
95 Timestamp endTimestamp = clockLog.getClockTimestamp();
96
97
98 List<TimeBlock> newTimeBlocks = td.getTimeBlocks();
99 List<TimeBlock> referenceTimeBlocks = new ArrayList<TimeBlock>(td.getTimeBlocks().size());
100 for (TimeBlock tb : td.getTimeBlocks()) {
101 referenceTimeBlocks.add(tb.copy());
102 }
103
104
105 List<TimeBlock> aList = TkServiceLocator.getTimeBlockService().buildTimeBlocks(assignment, assignment.getJob().getPayTypeObj().getRegEarnCode(), td, beginTimestamp, endTimestamp, BigDecimal.ZERO, BigDecimal.ZERO, true, false, userPrincipalId);
106 for (TimeBlock tb : aList) {
107 tb.setClockLogBeginId(beginClockLogId);
108 tb.setClockLogEndId(endClockLogId);
109 }
110 newTimeBlocks.addAll(aList);
111
112
113 TkServiceLocator.getTimesheetService().resetTimeBlock(newTimeBlocks);
114
115
116 TkServiceLocator.getTkRuleControllerService().applyRules(TkConstants.ACTIONS.CLOCK_OUT, newTimeBlocks, pe, td, principalId);
117
118
119 TkServiceLocator.getTimeBlockService().saveTimeBlocks(referenceTimeBlocks, newTimeBlocks, userPrincipalId);
120 }
121
122 @Override
123 public ClockLog buildClockLog(Timestamp clockTimestamp, Timestamp originalTimestamp, Assignment assignment, TimesheetDocument timesheetDocument, String clockAction, String ip) {
124 return buildClockLog(clockTimestamp, originalTimestamp, assignment, timesheetDocument, clockAction, ip, TKContext.getPrincipalId());
125 }
126
127 @Override
128 public ClockLog buildClockLog(Timestamp clockTimestamp, Timestamp originalTimestamp, Assignment assignment, TimesheetDocument timesheetDocument, String clockAction, String ip, String userPrincipalId) {
129 String principalId = timesheetDocument.getPrincipalId();
130
131 ClockLog clockLog = new ClockLog();
132 clockLog.setPrincipalId(principalId);
133
134
135 clockLog.setJob(timesheetDocument.getJob(assignment.getJobNumber()));
136 clockLog.setJobNumber(assignment.getJobNumber());
137 clockLog.setWorkArea(assignment.getWorkArea());
138 clockLog.setTask(assignment.getTask());
139 clockLog.setClockTimestampTimezone(TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback().getID());
140 clockLog.setClockTimestamp(clockTimestamp);
141 clockLog.setClockAction(clockAction);
142 clockLog.setIpAddress(ip);
143 clockLog.setUserPrincipalId(userPrincipalId);
144
145 clockLog.setTimestamp(originalTimestamp);
146
147 return clockLog;
148 }
149
150 public void setClockLogDao(ClockLogDao clockLogDao) {
151 this.clockLogDao = clockLogDao;
152 }
153
154 public ClockLog getLastClockLog(String principalId) {
155 return clockLogDao.getLastClockLog(principalId);
156 }
157
158 public ClockLog getLastClockLog(String principalId, String clockAction) {
159 return clockLogDao.getLastClockLog(principalId, clockAction);
160 }
161
162 public ClockLog getLastClockLog(String principalId, String jobNumber, String workArea, String task, CalendarEntries calendarEntry) {
163 return clockLogDao.getLastClockLog(principalId, jobNumber, workArea, task, calendarEntry);
164 }
165
166 @Override
167 public ClockLog getClockLog(String tkClockLogId) {
168 return clockLogDao.getClockLog(tkClockLogId);
169 }
170
171 public List<String> getUnapprovedIPWarning(List<TimeBlock> timeBlocks) {
172 List<String> warningMessages = new ArrayList<String>();
173 if (CollectionUtils.isNotEmpty(timeBlocks)) {
174 Set<String> aSet = new HashSet<String>();
175 for(TimeBlock tb : timeBlocks) {
176 if(tb.getClockLogCreated()) {
177 if(StringUtils.isNotEmpty(tb.getClockLogBeginId())){
178 ClockLog cl = TkServiceLocator.getClockLogService().getClockLog(tb.getClockLogBeginId());
179 if(cl.getUnapprovedIP()) {
180 aSet.add(buildUnapprovedIPWarning(cl));
181 }
182 }
183 if(StringUtils.isNotEmpty(tb.getClockLogEndId())){
184 ClockLog cl = TkServiceLocator.getClockLogService().getClockLog(tb.getClockLogEndId());
185 if(cl.getUnapprovedIP()) {
186 aSet.add(buildUnapprovedIPWarning(cl));
187 }
188 }
189 }
190 }
191 warningMessages.addAll(aSet);
192 }
193
194 return warningMessages;
195 }
196
197 public String buildUnapprovedIPWarning(ClockLog cl) {
198 String warning = "Warning: Action '" + TkConstants.CLOCK_ACTION_STRINGS.get(cl.getClockAction()) + "' taken at "
199 + cl.getClockTimestamp() + " was from an unapproved IP address - " + cl.getIpAddress();
200 return warning;
201 }
202
203 }