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
14
15
16
17
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 }