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.missedpunch;
17  
18  import java.sql.Timestamp;
19  import java.text.ParseException;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.joda.time.DateTime;
27  import org.joda.time.DateTimeZone;
28  import org.joda.time.LocalTime;
29  import org.kuali.hr.time.clocklog.ClockLog;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.timesheet.TimesheetDocument;
32  import org.kuali.hr.time.util.TKUtils;
33  import org.kuali.hr.time.util.TkConstants;
34  import org.kuali.rice.krad.document.Document;
35  import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  
38  public class MissedPunchValidation extends TransactionalDocumentRuleBase {
39  
40      /**
41       * Checks the provided MissedPunch for a valid ClockAction.
42       *
43       * @param mp The MissedPunch we are validating.
44       * @param lastClock The ClockLog entry that happened before the one we want to create.
45       *
46       * @return true if valid, false otherwise.
47       */
48      boolean validateClockAction(MissedPunchDocument mp, ClockLog lastClock) {
49          boolean valid = true;
50          Set<String> validActions = (lastClock != null) ? TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(lastClock.getClockAction()) : new HashSet<String>();
51  
52          // if a clockIn/lunchIn has been put in by missed punch, do not allow missed punch for clockOut/LunchOut
53          // missed punch can only be used on a tiemblock once.
54          if(lastClock != null 
55          		&& (lastClock.getClockAction().equals(TkConstants.CLOCK_IN) || lastClock.getClockAction().equals(TkConstants.LUNCH_IN))) {
56          	MissedPunchDocument mpd = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
57          	if(mpd != null) {
58  	       	 	GlobalVariables.getMessageMap().putError("document.clockAction", "clock.mp.onlyOne.action");
59  	            return false;
60          	}
61          }
62          if (!StringUtils.equals("A", mp.getDocumentStatus()) && !validActions.contains(mp.getClockAction())) {
63              GlobalVariables.getMessageMap().putError("document.clockAction", "clock.mp.invalid.action");
64              valid = false;
65          }
66          return valid;
67      }
68  
69      /**
70       * Checks whether the provided MissedPunch has a valid time based on the
71       * previous ClockLog.
72       *
73       * @param mp The MissedPunch we are validating.
74       * @param lastClock The ClockLog entry that happened before the one we want to create.
75       *
76       * @return true if valid, false otherwise.
77       * @throws ParseException 
78       */
79      boolean validateClockTime(MissedPunchDocument mp, ClockLog lastClock) throws ParseException {
80          boolean valid = true;
81  
82          if (lastClock == null)
83              return valid;
84  
85          DateTime clockLogDateTime = new DateTime(lastClock.getClockTimestamp().getTime());
86          DateTime boundaryMax = clockLogDateTime.plusDays(1);
87          DateTime nowTime = new DateTime(TKUtils.getCurrentDate());
88          
89          SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
90          String s = formatter.format(mp.getActionDate());
91  		Date tempDate = formatter.parse(s);
92  		Timestamp dateLocal = new Timestamp(tempDate.getTime());
93          LocalTime timeLocal = new LocalTime(mp.getActionTime().getTime());
94          DateTime actionDateTime = new DateTime(dateLocal.getTime());
95          actionDateTime = actionDateTime.plus(timeLocal.getMillisOfDay());
96  
97          // convert the action time to the system zone 
98          Timestamp ts = new Timestamp(actionDateTime.getMillis());
99          ClockLog lastLog = TkServiceLocator.getClockLogService().getLastClockLog(mp.getPrincipalId());
100         Long zoneOffset = TkServiceLocator.getTimezoneService().getTimezoneOffsetFromServerTime(DateTimeZone.forID(lastLog.getClockTimestampTimezone()));
101         Timestamp actionTime = new Timestamp(ts.getTime()-zoneOffset);
102         DateTime newDateTime = new DateTime(actionTime.getTime());
103 
104         // if date is a future date
105         if(actionDateTime.getYear()> nowTime.getYear()
106         		|| (actionDateTime.getYear()==nowTime.getYear() && actionDateTime.getDayOfYear() > nowTime.getDayOfYear())) {
107         	GlobalVariables.getMessageMap().putError("document.actionDate", "clock.mp.future.date");
108         	return false;
109         }
110 
111         // if time is a future time
112         if(actionDateTime.getMillis() > nowTime.getMillis()) {
113         	GlobalVariables.getMessageMap().putError("document.actionTime", "clock.mp.future.time");
114         	return false;
115         }
116         
117         
118         if ( ((!StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT) && actionDateTime.isAfter(boundaryMax)) 
119         		|| newDateTime.isBefore(clockLogDateTime)) && StringUtils.equals(mp.getDocumentStatus(),"R")) {
120         	GlobalVariables.getMessageMap().putError("document.actionTime", "clock.mp.invalid.datetime");
121             valid = false;
122         }
123 
124         return valid;
125     }
126  
127     // do not allow a missed punch is the time sheet document is enroute or final
128     boolean validateTimeSheet(MissedPunchDocument mp) {
129     	boolean valid = true;
130     	TimesheetDocument tsd = TkServiceLocator.getTimesheetService().getTimesheetDocument(mp.getTimesheetDocumentId());
131     	if(tsd != null 
132     			&& (tsd.getDocumentHeader().getDocumentStatus().equals(TkConstants.ROUTE_STATUS.ENROUTE) 
133     					|| tsd.getDocumentHeader().getDocumentStatus().equals(TkConstants.ROUTE_STATUS.FINAL))) {
134     		GlobalVariables.getMessageMap().putError("document.timesheetDocumentId", "clock.mp.invalid.timesheet");
135     		valid = false;
136     	}
137     	return valid;
138     }
139 	@Override
140 	public boolean processRouteDocument(Document document) {
141         boolean ret = super.processRouteDocument(document);
142         MissedPunchDocument mpDoc = (MissedPunchDocument)document;
143         if(!validateTimeSheet(mpDoc)) {
144         	return false;
145         }
146         ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(mpDoc.getPrincipalId());
147         ret &= validateClockAction(mpDoc, lastClock);
148         try {
149 			ret &= validateClockTime(mpDoc, lastClock);
150 		} catch (ParseException e) {
151 			// TODO Auto-generated catch block
152 			e.printStackTrace();
153 		}
154         
155         return ret;
156 	}
157 }