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              cronExpression = getCronExpressionForOneTime(oneDate, oneTime);
38          }
39  
40          if(oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_RECURRING)){
41              String schduleType = oleBatchProcessScheduleBo.getScheduleType();
42              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_DAILY)){
43                  String startTimeDaily = oleBatchProcessScheduleBo.getStartTime();
44                  cronExpression = getCronExpressionForDaily(startTimeDaily);
45              }
46              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_WEEKLY)){
47                  String startTimeWeekly = oleBatchProcessScheduleBo.getStartTime();
48                  String weekDays = oleBatchProcessScheduleBo.getWeekDays();
49                  cronExpression = getCronExpressionForWeekly(startTimeWeekly, weekDays);
50              }
51              if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_MONTHLY)){
52                  String startTimeMonthly = oleBatchProcessScheduleBo.getStartTime();
53                  String dayNumberMonthly = oleBatchProcessScheduleBo.getDayNumber();
54                  String monthNumberMonthly = oleBatchProcessScheduleBo.getMonthNumber();
55                  cronExpression = getCronExpressionForMonthly(startTimeMonthly, dayNumberMonthly, monthNumberMonthly);
56              }
57          }
58  
59          boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExpression);
60              if(validCronExpression){
61                 return cronExpression;
62              }else{
63                  return null;
64              }
65      }
66  
67      public String getCronExpressionForOneTime(Date oneTimeDate, String oneTime) {
68          String[] oneTimeArray = oneTime.split(":");
69          String hour = oneTimeArray[0];
70          String minutes = oneTimeArray[1];
71          String startDate = oneTimeDate.toString();
72          String[] dateArray = startDate.split("\\-");
73          String year = dateArray[0];
74          String month = dateArray[1];
75          if (month.charAt(0) == '0') {
76              month = month.substring(1);
77          }
78          String day = dateArray[2];
79          if (day.charAt(0) == '0') {
80              day = day.substring(1);
81          }
82          String cronExp = "0 " + minutes + " " + hour + " " + day + " " + month
83                  + " ? " + year;
84          return cronExp;
85      }
86  
87      public String getCronExpressionForDaily(String startTime) {
88          String[] startTimeArray = startTime.split(":");
89          String hour = startTimeArray[0];
90          String minutes = startTimeArray[1];
91          String cronExp = "0 " + minutes + " " + hour + " " + "1/1 " + "* " + "? " + "*";
92          return cronExp;
93      }
94  
95      public String getCronExpressionForWeekly(String startTime, String weekdays) {
96          String[] startTimeArray = startTime.split(":");
97          String hour = startTimeArray[0];
98          String minutes = startTimeArray[1];
99          String cronExp = "0 " + minutes + " " + hour + " " + "? " + "* " + weekdays + " *";
100         return cronExp;
101     }
102 
103     public String getCronExpressionForMonthly(String startTime, String dayNumber, String monthNumber) {
104         String[] startTimeArray = startTime.split(":");
105         String hour = startTimeArray[0];
106         String minutes = startTimeArray[1];
107         String cronExp = "0 " + minutes + " " + hour + " " + dayNumber + " " + "1/" + monthNumber + " " + "?" + " " + "*";
108         return cronExp;
109     }
110 
111     /*public String getCronExpressionForDaily24Hr(String startTime) {
112         String[] time = startTime.split(":");
113         String minutes = time[1];
114         String hour = time[0];
115         String cronExp = "0 " + minutes + " " + hour + " " + "* * ?";
116         boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExp);
117         if(validCronExpression){
118             return cronExp;
119         }else{
120             return null;
121         }
122     }*/
123 
124 
125     /*private String[] splitTime(String time){
126         String[] minsAndHour = new String[2];
127         String[] time1 = time.split(":");
128         String[] time2 = new String[2];
129         Matcher spaceMatch = Pattern.compile("\\s").matcher(time1[1]);
130         if(spaceMatch.matches()){
131         time2 = time1[1].split("\\ ");
132         }else{
133             time2[0] = time1[1].substring(0,2);
134             time2[1] = time1[1].substring(2);
135         }
136         String hour;
137         String minutes;
138         if (time2[1].equalsIgnoreCase(OLEConstants.OLEBatchProcess.TIME_PM)) {
139             Integer hourInt = Integer.parseInt(time1[0]) + 12;
140             if (hourInt == 24) {
141                 hourInt = 0;
142             }
143             hour = hourInt.toString();
144         } else {
145             hour = time1[0];
146         }
147         minutes = time2[0];
148         minsAndHour[0] = hour;
149         minsAndHour[1] = minutes;
150         return minsAndHour;
151     }*/
152 
153 }