View Javadoc
1   package org.kuali.ole.batch.helper;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo;
6   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
7   
8   import java.util.Date;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  /**
13   * Created with IntelliJ IDEA.
14   * User: rajeshbabuk
15   * Date: 7/20/13
16   * Time: 3:32 PM
17   * To change this template use File | Settings | File Templates.
18   */
19  public class OLESchedulerHelper {
20  
21      private static volatile OLESchedulerHelper helper = new OLESchedulerHelper();
22  
23      private OLESchedulerHelper(){
24  
25      }
26  
27      public static synchronized OLESchedulerHelper getInstance(){
28          return helper;
29      }
30  
31      public String getCronExpression(OLEBatchProcessScheduleBo oleBatchProcessScheduleBo){
32          String cronExpression=null;
33          String oneTimeOrRecur = oleBatchProcessScheduleBo.getOneTimeOrRecurring();
34          if(oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_ONETIME)){
35              Date oneDate = oleBatchProcessScheduleBo.getOneTimeDate();
36              String oneTime = oleBatchProcessScheduleBo.getOneTimeStartTime();
37              if (oneDate != null) {
38                  cronExpression = getCronExpressionForOneTime(oneDate.toString(), oneTime);
39              } else {
40                  cronExpression = getCronExpressionForOneTime(oleBatchProcessScheduleBo.getOneTimeStartDate(), oneTime);
41              }
42          }
43  
44          if(oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_RECURRING)){
45              String schduleType = oleBatchProcessScheduleBo.getScheduleType();
46              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_DAILY)){
47                  String startTimeDaily = oleBatchProcessScheduleBo.getStartTime();
48                  cronExpression = getCronExpressionForDaily(startTimeDaily);
49              }
50              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_WEEKLY)){
51                  String startTimeWeekly = oleBatchProcessScheduleBo.getStartTime();
52                  String weekDays = oleBatchProcessScheduleBo.getWeekDays();
53                  cronExpression = getCronExpressionForWeekly(startTimeWeekly, weekDays);
54              }
55              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_MONTHLY)){
56                  String startTimeMonthly = oleBatchProcessScheduleBo.getStartTime();
57                  String dayNumberMonthly = oleBatchProcessScheduleBo.getDayNumber();
58                  String monthNumberMonthly = oleBatchProcessScheduleBo.getMonthNumber();
59                  cronExpression = getCronExpressionForMonthly(startTimeMonthly, dayNumberMonthly, monthNumberMonthly);
60              }
61          }
62  
63          boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExpression);
64              if(validCronExpression){
65                 return cronExpression;
66              }else{
67                  return null;
68              }
69      }
70  
71      public String getCronExpressionForOneTime(String startDate, String oneTime) {
72          String[] oneTimeArray = oneTime.split(":");
73          String hour = oneTimeArray[0];
74          String minutes = oneTimeArray[1];
75          String[] dateArray = startDate.split("\\-");
76          String year = dateArray[0];
77          String month = dateArray[1];
78          if (month.charAt(0) == '0') {
79              month = month.substring(1);
80          }
81          String day = dateArray[2];
82          if (day.charAt(0) == '0') {
83              day = day.substring(1);
84          }
85          String cronExp = "0 " + minutes + " " + hour + " " + day + " " + month
86                  + " ? " + year;
87          return cronExp;
88      }
89  
90      public String getCronExpressionForDaily(String startTime) {
91          String[] startTimeArray = startTime.split(":");
92          String hour = startTimeArray[0];
93          String minutes = startTimeArray[1];
94          String cronExp = "0 " + minutes + " " + hour + " " + "1/1 " + "* " + "? " + "*";
95          return cronExp;
96      }
97  
98      public String getCronExpressionForWeekly(String startTime, String weekdays) {
99          String[] startTimeArray = startTime.split(":");
100         String hour = startTimeArray[0];
101         String minutes = startTimeArray[1];
102         String cronExp = "0 " + minutes + " " + hour + " " + "? " + "* " + weekdays + " *";
103         return cronExp;
104     }
105 
106     public String getCronExpressionForMonthly(String startTime, String dayNumber, String monthNumber) {
107         String[] startTimeArray = startTime.split(":");
108         String hour = startTimeArray[0];
109         String minutes = startTimeArray[1];
110         String cronExp = "0 " + minutes + " " + hour + " " + dayNumber + " " + "1/" + monthNumber + " " + "?" + " " + "*";
111         return cronExp;
112     }
113 
114     /*public String getCronExpressionForDaily24Hr(String startTime) {
115         String[] time = startTime.split(":");
116         String minutes = time[1];
117         String hour = time[0];
118         String cronExp = "0 " + minutes + " " + hour + " " + "* * ?";
119         boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExp);
120         if(validCronExpression){
121             return cronExp;
122         }else{
123             return null;
124         }
125     }*/
126 
127 
128     /*private String[] splitTime(String time){
129         String[] minsAndHour = new String[2];
130         String[] time1 = time.split(":");
131         String[] time2 = new String[2];
132         Matcher spaceMatch = Pattern.compile("\\s").matcher(time1[1]);
133         if(spaceMatch.matches()){
134         time2 = time1[1].split("\\ ");
135         }else{
136             time2[0] = time1[1].substring(0,2);
137             time2[1] = time1[1].substring(2);
138         }
139         String hour;
140         String minutes;
141         if (time2[1].equalsIgnoreCase(OLEConstants.OLEBatchProcess.TIME_PM)) {
142             Integer hourInt = Integer.parseInt(time1[0]) + 12;
143             if (hourInt == 24) {
144                 hourInt = 0;
145             }
146             hour = hourInt.toString();
147         } else {
148             hour = time1[0];
149         }
150         minutes = time2[0];
151         minsAndHour[0] = hour;
152         minsAndHour[1] = minutes;
153         return minsAndHour;
154     }*/
155 
156 }