1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch.service.impl;
17
18 import java.util.Calendar;
19 import java.util.Date;
20 import java.util.StringTokenizer;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.ole.gl.GeneralLedgerConstants;
24 import org.kuali.ole.gl.batch.ScrubberStep;
25 import org.kuali.ole.gl.batch.service.RunDateService;
26 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
27
28
29
30
31 public class RunDateServiceImpl implements RunDateService {
32 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RunDateServiceImpl.class);
33
34 private ParameterService parameterService;
35
36
37
38
39
40 public Date calculateRunDate(Date executionDate) {
41 Calendar currentCal = Calendar.getInstance();
42 currentCal.setTime(executionDate);
43
44 CutoffTime cutoffTime = parseCutoffTime(retrieveCutoffTimeValue());
45
46 if (isCurrentDateBeforeCutoff(currentCal, cutoffTime)) {
47
48 currentCal.add(Calendar.DAY_OF_MONTH, -1);
49
50
51 currentCal.set(Calendar.HOUR_OF_DAY, 23);
52 currentCal.set(Calendar.MINUTE, 59);
53 currentCal.set(Calendar.SECOND, 59);
54 currentCal.set(Calendar.MILLISECOND, 0);
55 return new Date(currentCal.getTimeInMillis());
56 }
57 return new Date(executionDate.getTime());
58 }
59
60
61
62
63
64
65
66
67 protected boolean isCurrentDateBeforeCutoff(Calendar currentCal, CutoffTime cutoffTime) {
68 if (cutoffTime != null) {
69
70
71
72
73
74
75 Calendar cutoffCal = (Calendar) currentCal.clone();
76 cutoffCal.setLenient(false);
77 cutoffCal.set(Calendar.HOUR_OF_DAY, cutoffTime.hour);
78 cutoffCal.set(Calendar.MINUTE, cutoffTime.minute);
79 cutoffCal.set(Calendar.SECOND, cutoffTime.second);
80 cutoffCal.set(Calendar.MILLISECOND, 0);
81
82 return currentCal.before(cutoffCal);
83 }
84
85 return false;
86 }
87
88
89
90
91 protected class CutoffTime {
92
93
94
95 protected int hour;
96
97
98
99
100 protected int minute;
101
102
103
104
105 protected int second;
106
107
108
109
110
111
112
113 protected CutoffTime(int hour, int minute, int second) {
114 this.hour = hour;
115 this.minute = minute;
116 this.second = second;
117 }
118 }
119
120
121
122
123
124
125
126 protected CutoffTime parseCutoffTime(String cutoffTime) {
127 if (StringUtils.isBlank(cutoffTime)) {
128 return new CutoffTime(0, 0, 0);
129 }
130 else {
131 cutoffTime = cutoffTime.trim();
132 if (LOG.isDebugEnabled()) {
133 LOG.debug("Cutoff time value found: " + cutoffTime);
134 }
135 StringTokenizer st = new StringTokenizer(cutoffTime, ":", false);
136
137 try {
138 String hourStr = st.nextToken();
139 String minuteStr = st.nextToken();
140 String secondStr = st.nextToken();
141
142 int hourInt = Integer.parseInt(hourStr, 10);
143 int minuteInt = Integer.parseInt(minuteStr, 10);
144 int secondInt = Integer.parseInt(secondStr, 10);
145
146 if (hourInt < 0 || hourInt > 23 || minuteInt < 0 || minuteInt > 59 || secondInt < 0 || secondInt > 59) {
147 throw new IllegalArgumentException("Cutoff time must be in the format \"HH:mm:ss\", where HH, mm, ss are defined in the java.text.SimpleDateFormat class. In particular, 0 <= hour <= 23, 0 <= minute <= 59, and 0 <= second <= 59");
148 }
149 return new CutoffTime(hourInt, minuteInt, secondInt);
150 }
151 catch (Exception e) {
152 throw new IllegalArgumentException("Cutoff time should either be null, or in the format \"HH:mm:ss\", where HH, mm, ss are defined in the java.text.SimpleDateFormat class.");
153 }
154 }
155 }
156
157
158
159
160
161
162
163 protected String retrieveCutoffTimeValue() {
164 String value = parameterService.getParameterValueAsString(ScrubberStep.class, GeneralLedgerConstants.GlScrubberGroupParameters.SCRUBBER_CUTOFF_TIME);
165 if (StringUtils.isBlank(value)) {
166 LOG.error("Unable to retrieve parameter for GL process cutoff date. Defaulting to no cutoff time (i.e. midnight)");
167 value = null;
168 }
169 return value;
170 }
171
172 public void setParameterService(ParameterService parameterService) {
173 this.parameterService = parameterService;
174 }
175
176 }