001    /**
002     * Copyright 2004-2013 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    import java.util.List;
025    
026    public class GracePeriodServiceImpl implements GracePeriodService {
027            private GracePeriodDao gracePeriodDao;
028    
029            public GracePeriodDao getGracePeriodDao() {
030                    return gracePeriodDao;
031            }
032    
033            public void setGracePeriodDao(GracePeriodDao gracePeriodDao) {
034                    this.gracePeriodDao = gracePeriodDao;
035            }
036    
037        @Override
038            public GracePeriodRule getGracePeriodRule(Date asOfDate){
039                    return gracePeriodDao.getGracePeriodRule(asOfDate);
040            }
041    
042            
043            @SuppressWarnings("deprecation")
044            public Timestamp processGracePeriodRule(Timestamp actualTime, Date asOfDate){
045                    GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
046                    if(gracePeriodRule!=null){
047                            //go ahead and round off seconds
048                            actualTime.setSeconds(0);
049                            
050                            BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
051                            BigDecimal minutes = new BigDecimal(actualTime.getMinutes());
052                            int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
053                            BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
054                    BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
055                    if(bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs().intValue()){
056                            actualTime.setMinutes(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
057                    } else {
058                            if(topInterval.equals(new BigDecimal(60))){
059                                    actualTime.setHours(actualTime.getHours()+1);
060                                    actualTime.setMinutes(0);
061                            } else {
062                                    actualTime.setMinutes(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
063                            }
064                    }
065                    }
066                    return actualTime;
067            }
068    
069            @Override
070            public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
071                    return gracePeriodDao.getGracePeriodRule(tkGracePeriodId);
072            }
073    
074        @Override
075        public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active) {
076            return gracePeriodDao.getGracePeriodRules(hourFactor, active);
077        }
078    }