View Javadoc

1   /**
2    * Copyright 2004-2013 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  import java.util.List;
25  
26  public class GracePeriodServiceImpl implements GracePeriodService {
27  	private GracePeriodDao gracePeriodDao;
28  
29  	public GracePeriodDao getGracePeriodDao() {
30  		return gracePeriodDao;
31  	}
32  
33  	public void setGracePeriodDao(GracePeriodDao gracePeriodDao) {
34  		this.gracePeriodDao = gracePeriodDao;
35  	}
36  
37      @Override
38  	public GracePeriodRule getGracePeriodRule(Date asOfDate){
39  		return gracePeriodDao.getGracePeriodRule(asOfDate);
40  	}
41  
42  	
43  	@SuppressWarnings("deprecation")
44  	public Timestamp processGracePeriodRule(Timestamp actualTime, Date asOfDate){
45  		GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
46  		if(gracePeriodRule!=null){
47  			//go ahead and round off seconds
48  			actualTime.setSeconds(0);
49  			
50  			BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
51  			BigDecimal minutes = new BigDecimal(actualTime.getMinutes());
52  			int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
53  			BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
54  	        BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
55  	        if(bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs().intValue()){
56  	        	actualTime.setMinutes(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
57  	        } else {
58  	        	if(topInterval.equals(new BigDecimal(60))){
59  	        		actualTime.setHours(actualTime.getHours()+1);
60  	        		actualTime.setMinutes(0);
61  	        	} else {
62  	        		actualTime.setMinutes(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
63  	        	}
64  	        }
65  		}
66  		return actualTime;
67  	}
68  
69  	@Override
70  	public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
71  		return gracePeriodDao.getGracePeriodRule(tkGracePeriodId);
72  	}
73  
74      @Override
75      public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active, String showHistory) {
76          return gracePeriodDao.getGracePeriodRules(hourFactor, active, showHistory);
77      }
78  }