001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.graceperiod.service;
017    
018    import org.kuali.hr.time.graceperiod.dao.GracePeriodDao;
019    import org.kuali.hr.time.graceperiod.rule.GracePeriodRule;
020    
021    import java.math.BigDecimal;
022    import java.sql.Date;
023    import java.sql.Timestamp;
024    
025    public class GracePeriodServiceImpl implements GracePeriodService {
026            private GracePeriodDao gracePeriodDao;
027    
028            public GracePeriodDao getGracePeriodDao() {
029                    return gracePeriodDao;
030            }
031    
032            public void setGracePeriodDao(GracePeriodDao gracePeriodDao) {
033                    this.gracePeriodDao = gracePeriodDao;
034            }
035    
036        @Override
037            public GracePeriodRule getGracePeriodRule(Date asOfDate){
038                    return gracePeriodDao.getGracePeriodRule(asOfDate);
039            }
040    
041            
042            @SuppressWarnings("deprecation")
043            public Timestamp processGracePeriodRule(Timestamp actualTime, Date asOfDate){
044                    GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
045                    if(gracePeriodRule!=null){
046                            //go ahead and round off seconds
047                            actualTime.setSeconds(0);
048                            
049                            BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
050                            BigDecimal minutes = new BigDecimal(actualTime.getMinutes());
051                            int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
052                            BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
053                    BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
054                    if(bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs().intValue()){
055                            actualTime.setMinutes(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
056                    } else {
057                            if(topInterval.equals(new BigDecimal(60))){
058                                    actualTime.setHours(actualTime.getHours()+1);
059                                    actualTime.setMinutes(0);
060                            } else {
061                                    actualTime.setMinutes(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
062                            }
063                    }
064                    }
065                    return actualTime;
066            }
067    
068            @Override
069            public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
070                    return gracePeriodDao.getGracePeriodRule(tkGracePeriodId);
071            }
072    }