001package org.kuali.ole.batch.helper; 002 003import org.apache.commons.lang.StringUtils; 004import org.kuali.ole.OLEConstants; 005import org.kuali.ole.batch.bo.OLEBatchProcessScheduleBo; 006import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument; 007 008import java.util.Date; 009import java.util.regex.Matcher; 010import java.util.regex.Pattern; 011 012/** 013 * Created with IntelliJ IDEA. 014 * User: rajeshbabuk 015 * Date: 7/20/13 016 * Time: 3:32 PM 017 * To change this template use File | Settings | File Templates. 018 */ 019public class OLESchedulerHelper { 020 021 private static volatile OLESchedulerHelper helper = new OLESchedulerHelper(); 022 023 private OLESchedulerHelper(){ 024 025 } 026 027 public static synchronized OLESchedulerHelper getInstance(){ 028 return helper; 029 } 030 031 public String getCronExpression(OLEBatchProcessScheduleBo oleBatchProcessScheduleBo){ 032 String cronExpression=null; 033 String oneTimeOrRecur = oleBatchProcessScheduleBo.getOneTimeOrRecurring(); 034 if(oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_ONETIME)){ 035 Date oneDate = oleBatchProcessScheduleBo.getOneTimeDate(); 036 String oneTime = oleBatchProcessScheduleBo.getOneTimeStartTime(); 037 if (oneDate != null) { 038 cronExpression = getCronExpressionForOneTime(oneDate.toString(), oneTime); 039 } else { 040 cronExpression = getCronExpressionForOneTime(oleBatchProcessScheduleBo.getOneTimeStartDate(), oneTime); 041 } 042 } 043 044 if(oneTimeOrRecur.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_RECURRING)){ 045 String schduleType = oleBatchProcessScheduleBo.getScheduleType(); 046 if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_DAILY)){ 047 String startTimeDaily = oleBatchProcessScheduleBo.getStartTime(); 048 cronExpression = getCronExpressionForDaily(startTimeDaily); 049 } 050 if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_WEEKLY)){ 051 String startTimeWeekly = oleBatchProcessScheduleBo.getStartTime(); 052 String weekDays = oleBatchProcessScheduleBo.getWeekDays(); 053 cronExpression = getCronExpressionForWeekly(startTimeWeekly, weekDays); 054 } 055 if(schduleType.equalsIgnoreCase(OLEConstants.OLEBatchProcess.SCHEDULE_TYPE_MONTHLY)){ 056 String startTimeMonthly = oleBatchProcessScheduleBo.getStartTime(); 057 String dayNumberMonthly = oleBatchProcessScheduleBo.getDayNumber(); 058 String monthNumberMonthly = oleBatchProcessScheduleBo.getMonthNumber(); 059 cronExpression = getCronExpressionForMonthly(startTimeMonthly, dayNumberMonthly, monthNumberMonthly); 060 } 061 } 062 063 boolean validCronExpression = org.quartz.CronExpression.isValidExpression(cronExpression); 064 if(validCronExpression){ 065 return cronExpression; 066 }else{ 067 return null; 068 } 069 } 070 071 public String getCronExpressionForOneTime(String startDate, String oneTime) { 072 String[] oneTimeArray = oneTime.split(":"); 073 String hour = oneTimeArray[0]; 074 String minutes = oneTimeArray[1]; 075 String[] dateArray = startDate.split("\\-"); 076 String year = dateArray[0]; 077 String month = dateArray[1]; 078 if (month.charAt(0) == '0') { 079 month = month.substring(1); 080 } 081 String day = dateArray[2]; 082 if (day.charAt(0) == '0') { 083 day = day.substring(1); 084 } 085 String cronExp = "0 " + minutes + " " + hour + " " + day + " " + month 086 + " ? " + year; 087 return cronExp; 088 } 089 090 public String getCronExpressionForDaily(String startTime) { 091 String[] startTimeArray = startTime.split(":"); 092 String hour = startTimeArray[0]; 093 String minutes = startTimeArray[1]; 094 String cronExp = "0 " + minutes + " " + hour + " " + "1/1 " + "* " + "? " + "*"; 095 return cronExp; 096 } 097 098 public String getCronExpressionForWeekly(String startTime, String weekdays) { 099 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}