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.graceperiod.service;
17  
18  import org.kuali.hr.time.graceperiod.dao.GracePeriodDao;
19  import org.kuali.hr.time.graceperiod.rule.GracePeriodRule;
20  
21  import java.math.BigDecimal;
22  import java.sql.Date;
23  import java.sql.Timestamp;
24  
25  public class GracePeriodServiceImpl implements GracePeriodService {
26  	private GracePeriodDao gracePeriodDao;
27  
28  	public GracePeriodDao getGracePeriodDao() {
29  		return gracePeriodDao;
30  	}
31  
32  	public void setGracePeriodDao(GracePeriodDao gracePeriodDao) {
33  		this.gracePeriodDao = gracePeriodDao;
34  	}
35  
36      @Override
37  	public GracePeriodRule getGracePeriodRule(Date asOfDate){
38  		return gracePeriodDao.getGracePeriodRule(asOfDate);
39  	}
40  
41  	
42  	@SuppressWarnings("deprecation")
43  	public Timestamp processGracePeriodRule(Timestamp actualTime, Date asOfDate){
44  		GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
45  		if(gracePeriodRule!=null){
46  			//go ahead and round off seconds
47  			actualTime.setSeconds(0);
48  			
49  			BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
50  			BigDecimal minutes = new BigDecimal(actualTime.getMinutes());
51  			int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
52  			BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
53  	        BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
54  	        if(bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs().intValue()){
55  	        	actualTime.setMinutes(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
56  	        } else {
57  	        	if(topInterval.equals(new BigDecimal(60))){
58  	        		actualTime.setHours(actualTime.getHours()+1);
59  	        		actualTime.setMinutes(0);
60  	        	} else {
61  	        		actualTime.setMinutes(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
62  	        	}
63  	        }
64  		}
65  		return actualTime;
66  	}
67  
68  	@Override
69  	public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
70  		return gracePeriodDao.getGracePeriodRule(tkGracePeriodId);
71  	}
72  }