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