1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.rules.graceperiod.service;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.joda.time.DateTime;
23 import org.joda.time.Interval;
24 import org.joda.time.LocalDate;
25 import org.kuali.kpme.tklm.time.rules.graceperiod.GracePeriodRule;
26 import org.kuali.kpme.tklm.time.rules.graceperiod.dao.GracePeriodDao;
27
28 public class GracePeriodServiceImpl implements GracePeriodService {
29 private GracePeriodDao gracePeriodDao;
30
31 public GracePeriodDao getGracePeriodDao() {
32 return gracePeriodDao;
33 }
34
35 public void setGracePeriodDao(GracePeriodDao gracePeriodDao) {
36 this.gracePeriodDao = gracePeriodDao;
37 }
38
39 @Override
40 public GracePeriodRule getGracePeriodRule(LocalDate asOfDate){
41 return gracePeriodDao.getGracePeriodRule(asOfDate);
42 }
43
44 public DateTime processGracePeriodRule(DateTime actualDateTime, LocalDate asOfDate) {
45 DateTime gracePeriodDateTime = actualDateTime;
46
47 GracePeriodRule gracePeriodRule = getGracePeriodRule(asOfDate);
48 if (gracePeriodRule!=null) {
49
50 gracePeriodDateTime = gracePeriodDateTime.withSecondOfMinute(0);
51 gracePeriodDateTime = gracePeriodDateTime.withMillisOfSecond(0);
52
53 BigDecimal minuteIncrement = gracePeriodRule.getHourFactor();
54 if(minuteIncrement.compareTo(BigDecimal.ZERO) == 0) {
55 return gracePeriodDateTime;
56 }
57 else {
58 BigDecimal minutes = new BigDecimal(gracePeriodDateTime.getMinuteOfHour());
59 int bottomIntervalFactor = minutes.divide(minuteIncrement, 0, BigDecimal.ROUND_FLOOR).intValue();
60 BigDecimal bottomInterval = new BigDecimal(bottomIntervalFactor).multiply(minuteIncrement);
61 BigDecimal topInterval = new BigDecimal(bottomIntervalFactor + 1).multiply(minuteIncrement);
62 if (bottomInterval.subtract(minutes).abs().intValue() < topInterval.subtract(minutes).abs().intValue()) {
63 gracePeriodDateTime = gracePeriodDateTime.withMinuteOfHour(bottomInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
64 } else {
65 if (topInterval.compareTo(BigDecimal.valueOf(60)) == 0) {
66 BigDecimal hour = new BigDecimal(gracePeriodDateTime.getHourOfDay());
67 if (hour.compareTo(BigDecimal.valueOf(23)) == 0) {
68 gracePeriodDateTime = gracePeriodDateTime.plusDays(1).withHourOfDay(0).withMinuteOfHour(0);
69 } else {
70 gracePeriodDateTime = gracePeriodDateTime.withHourOfDay(gracePeriodDateTime.getHourOfDay() + 1).withMinuteOfHour(0);
71 }
72 } else {
73 gracePeriodDateTime = gracePeriodDateTime.withMinuteOfHour(topInterval.setScale(0, BigDecimal.ROUND_HALF_UP).intValue());
74 }
75 }
76 }
77 }
78 return gracePeriodDateTime;
79 }
80
81 @Override
82 public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
83 return gracePeriodDao.getGracePeriodRule(tkGracePeriodId);
84 }
85
86 @Override
87 public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active, String showHistory) {
88 return gracePeriodDao.getGracePeriodRules(hourFactor, active, showHistory);
89 }
90 }