1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.missedpunch.service;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.joda.time.DateTime;
21 import org.joda.time.DateTimeZone;
22 import org.joda.time.LocalTime;
23 import org.kuali.hr.time.assignment.Assignment;
24 import org.kuali.hr.time.clocklog.ClockLog;
25 import org.kuali.hr.time.missedpunch.MissedPunchDocument;
26 import org.kuali.hr.time.missedpunch.dao.MissedPunchDao;
27 import org.kuali.hr.time.service.base.TkServiceLocator;
28 import org.kuali.hr.time.timeblock.TimeBlock;
29 import org.kuali.hr.time.timesheet.TimesheetDocument;
30 import org.kuali.hr.time.util.TKContext;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.hr.time.util.TkConstants;
33 import org.kuali.rice.core.api.config.property.ConfigContext;
34 import org.kuali.rice.kew.api.WorkflowDocument;
35 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
36 import org.kuali.rice.kim.api.identity.principal.Principal;
37 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38 import org.kuali.rice.krad.service.KRADServiceLocator;
39 import org.kuali.rice.krad.util.GlobalVariables;
40
41 import java.math.BigDecimal;
42 import java.sql.Timestamp;
43 import java.util.ArrayList;
44 import java.util.List;
45
46 public class MissedPunchServiceImpl implements MissedPunchService {
47
48 private static final Logger LOG = Logger.getLogger(MissedPunchServiceImpl.class);
49
50 MissedPunchDao missedPunchDao;
51
52 @Override
53 public MissedPunchDocument getMissedPunchByRouteHeader(String headerId) {
54 return missedPunchDao.getMissedPunchByRouteHeader(headerId);
55 }
56
57 public void setMissedPunchDao(MissedPunchDao missedPunchDao) {
58 this.missedPunchDao = missedPunchDao;
59 }
60
61 @Override
62 public void updateClockLogAndTimeBlockIfNecessary(MissedPunchDocument missedPunch) {
63 java.util.Date actionDate = missedPunch.getActionDate();
64 java.sql.Time actionTime = missedPunch.getActionTime();
65
66 LocalTime actionTimeLocal = new LocalTime(actionTime.getTime());
67 DateTime actionDateTime = new DateTime(actionDate.getTime());
68
69 actionDateTime = actionDateTime.plus(actionTimeLocal.getMillisOfDay());
70
71 ClockLog cl = TkServiceLocator.getClockLogService().getClockLog(missedPunch.getTkClockLogId());
72
73
74 if(cl == null) {
75 MissedPunchDocument mpd = TkServiceLocator.getMissedPunchService().getMissedPunchByRouteHeader(missedPunch.getDocumentNumber());
76 if(mpd != null) {
77 missedPunch.setTkClockLogId(mpd.getTkClockLogId());
78 cl = TkServiceLocator.getClockLogService().getClockLog(missedPunch.getTkClockLogId());
79 }
80 }
81
82 if(cl != null && cl.getClockTimestamp().compareTo(new Timestamp(actionDateTime.getMillis())) != 0){
83
84
85 String logEndId = null;
86 String logBeginId = null;
87 List<TimeBlock> timeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocksForClockLogEndId(cl.getTkClockLogId());
88 if(timeBlocks.isEmpty()) {
89
90 timeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocksForClockLogBeginId(cl.getTkClockLogId());
91 if(!timeBlocks.isEmpty()) {
92 logEndId = timeBlocks.get(0).getClockLogEndId();
93 }
94 } else {
95 logBeginId = timeBlocks.get(0).getClockLogBeginId();
96 }
97
98
99 for(TimeBlock tb : timeBlocks){
100 TkServiceLocator.getTimeBlockService().deleteTimeBlock(tb);
101 }
102 KRADServiceLocator.getBusinessObjectService().delete(cl);
103
104 addClockLogForMissedPunch(missedPunch, logEndId, logBeginId);
105 }
106 }
107
108 @Override
109 public void addClockLogForMissedPunch(MissedPunchDocument missedPunch) {
110 java.util.Date actionDate = missedPunch.getActionDate();
111 java.sql.Time actionTime = missedPunch.getActionTime();
112
113 LocalTime actionTimeLocal = new LocalTime(actionTime.getTime());
114 DateTime actionDateTime = new DateTime(actionDate.getTime());
115
116 actionDateTime = actionDateTime.plus(actionTimeLocal.getMillisOfDay());
117 missedPunch.setActionDate(new java.util.Date(actionDateTime.getMillis()));
118 TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(missedPunch.getTimesheetDocumentId());
119 Assignment assign = TkServiceLocator.getAssignmentService().getAssignment(tdoc, missedPunch.getAssignment());
120
121
122 Timestamp ts = new Timestamp(missedPunch.getActionDate().getTime());
123 ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(missedPunch.getPrincipalId());
124 Long zoneOffset = TkServiceLocator.getTimezoneService().getTimezoneOffsetFromServerTime(DateTimeZone.forID(lastClockLog.getClockTimestampTimezone()));
125 Timestamp clockLogTime = new Timestamp(ts.getTime() - zoneOffset);
126
127 ClockLog clockLog = TkServiceLocator.getClockLogService().buildClockLog(clockLogTime, clockLogTime,
128 assign,
129 tdoc,
130 missedPunch.getClockAction(),
131 TKUtils.getIPAddressFromRequest(TKContext.getHttpServletRequest()));
132
133 TkServiceLocator.getClockLogService().saveClockLog(clockLog);
134 missedPunch.setTkClockLogId(clockLog.getTkClockLogId());
135
136 if (StringUtils.equals(clockLog.getClockAction(), TkConstants.CLOCK_OUT) ||
137 StringUtils.equals(clockLog.getClockAction(), TkConstants.LUNCH_OUT)) {
138 String earnCode = assign.getJob().getPayTypeObj().getRegEarnCode();
139 this.buildTimeBlockRunRules(lastClockLog, clockLog, tdoc, assign, earnCode, lastClockLog.getClockTimestamp(), clockLog.getClockTimestamp());
140 }
141 }
142
143 @Override
144
145
146 public void addClockLogForMissedPunch(MissedPunchDocument missedPunch, String logEndId, String logBeginId) {
147 java.util.Date actionDate = missedPunch.getActionDate();
148 java.sql.Time actionTime = missedPunch.getActionTime();
149
150 LocalTime actionTimeLocal = new LocalTime(actionTime.getTime());
151 DateTime actionDateTime = new DateTime(actionDate.getTime());
152
153 actionDateTime = actionDateTime.plus(actionTimeLocal.getMillisOfDay());
154 missedPunch.setActionDate(new java.util.Date(actionDateTime.getMillis()));
155 TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().getTimesheetDocument(missedPunch.getTimesheetDocumentId());
156 Assignment assign = TkServiceLocator.getAssignmentService().getAssignment(tdoc, missedPunch.getAssignment());
157
158 Timestamp ts = new Timestamp(missedPunch.getActionDate().getTime());
159 ClockLog lastLog = TkServiceLocator.getClockLogService().getLastClockLog(missedPunch.getPrincipalId());
160 Long zoneOffset = TkServiceLocator.getTimezoneService().getTimezoneOffsetFromServerTime(DateTimeZone.forID(lastLog.getClockTimestampTimezone()));
161 Timestamp clockLogTime = new Timestamp(ts.getTime() - zoneOffset);
162
163 ClockLog clockLog = TkServiceLocator.getClockLogService().buildClockLog(clockLogTime, clockLogTime,
164 assign,
165 tdoc,
166 missedPunch.getClockAction(),
167 TKUtils.getIPAddressFromRequest(TKContext.getHttpServletRequest()));
168 TkServiceLocator.getClockLogService().saveClockLog(clockLog);
169 missedPunch.setTkClockLogId(clockLog.getTkClockLogId());
170
171
172
173
174
175 if(!(logEndId == null && logBeginId == null)) {
176 ClockLog endLog = null;
177 ClockLog beginLog = null;
178 if(logEndId != null) {
179 endLog = TkServiceLocator.getClockLogService().getClockLog(logEndId);
180 } else {
181 endLog = clockLog;
182 }
183 if (logBeginId != null) {
184 beginLog = TkServiceLocator.getClockLogService().getClockLog(logBeginId);
185 } else {
186 beginLog = clockLog;
187 }
188
189 if (beginLog != null && endLog != null && beginLog.getClockTimestamp().before(endLog.getClockTimestamp())) {
190 String earnCode = assign.getJob().getPayTypeObj().getRegEarnCode();
191 this.buildTimeBlockRunRules(beginLog, endLog, tdoc, assign, earnCode, beginLog.getClockTimestamp(), endLog.getClockTimestamp());
192 } else {
193
194 GlobalVariables.getMessageMap().putError("document.actionTime", "clock.mp.invalid.datetime");
195 }
196 }
197 }
198
199
200
201
202
203 private void buildTimeBlockRunRules(ClockLog beginClockLog, ClockLog endClockLog, TimesheetDocument tdoc, Assignment assignment, String earnCode, Timestamp beginTimestamp, Timestamp endTimestamp) {
204
205 List<TimeBlock> newTimeBlocks = tdoc.getTimeBlocks();
206 List<TimeBlock> referenceTimeBlocks = new ArrayList<TimeBlock>(newTimeBlocks);
207 for (TimeBlock tb : newTimeBlocks) {
208 referenceTimeBlocks.add(tb.copy());
209 }
210
211
212 List<TimeBlock> blocks = TkServiceLocator.getTimeBlockService().buildTimeBlocks(
213 assignment, earnCode, tdoc, beginTimestamp,
214 endTimestamp, BigDecimal.ZERO, BigDecimal.ZERO, true, false, TKContext.getPrincipalId());
215
216
217
218 for (TimeBlock block : blocks) {
219 block.setClockLogBeginId(beginClockLog.getTkClockLogId());
220 block.setClockLogEndId(endClockLog.getTkClockLogId());
221 }
222
223 newTimeBlocks.addAll(blocks);
224
225
226 TkServiceLocator.getTimesheetService().resetTimeBlock(newTimeBlocks);
227
228 TkServiceLocator.getTkRuleControllerService().applyRules(
229 TkConstants.ACTIONS.CLOCK_OUT, newTimeBlocks,
230 tdoc.getCalendarEntry(),
231 tdoc, tdoc.getPrincipalId()
232 );
233
234 TkServiceLocator.getTimeBlockService().saveTimeBlocks(referenceTimeBlocks, newTimeBlocks, TKContext.getPrincipalId());
235 }
236 public MissedPunchDocument getMissedPunchByClockLogId(String clockLogId){
237 return missedPunchDao.getMissedPunchByClockLogId(clockLogId);
238
239 }
240
241 @Override
242 public void approveMissedPunch(MissedPunchDocument document) {
243 String batchUserPrincipalId = getBatchUserPrincipalId();
244
245 if (batchUserPrincipalId != null) {
246 String rhid = document.getDocumentNumber();
247 WorkflowDocument wd = WorkflowDocumentFactory.loadDocument(batchUserPrincipalId, rhid);
248 wd.superUserBlanketApprove("Batch job superuser approving missed punch document.");
249 } else {
250 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
251 LOG.error("Could not approve missed punch document due to missing batch user " + principalName);
252 }
253 }
254
255 private String getBatchUserPrincipalId() {
256 String principalName = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.BATCH_USER_PRINCIPAL_NAME);
257 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(principalName);
258 return principal == null ? null : principal.getPrincipalId();
259 }
260
261 @Override
262 public List<MissedPunchDocument> getMissedPunchDocsByTimesheetDocumentId(String timesheetDocumentId) {
263 return missedPunchDao.getMissedPunchDocsByTimesheetDocumentId(timesheetDocumentId);
264 }
265
266 }