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 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
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
154
155
156 }