1 package org.oddjob.schedules.schedules; 2 3 import java.io.Serializable ; 4 import java.text.ParseException ; 5 import java.util.Date ; 6 import java.util.TimeZone ; 7 8 import org.apache.log4j.Logger; 9 import org.oddjob.schedules.AbstractSchedule; 10 import org.oddjob.schedules.DateUtils; 11 import org.oddjob.schedules.Interval; 12 import org.oddjob.schedules.Schedule; 13 import org.oddjob.schedules.ScheduleContext; 14 import org.oddjob.util.DateHelper; 15 import org.oddjob.util.OddjobConfigException; 16 17 30 31 32 final public class DateSchedule extends AbstractSchedule implements Serializable { 33 34 private static final long serialVersionUID = 20050226; 35 36 private static Logger logger = Logger.getLogger(DateSchedule.class); 37 38 39 private String startDate; 40 private String endDate; 41 42 52 public void setFrom(String startDateString) { 53 startDate = startDateString; 54 } 55 56 61 public String getFrom() { 62 return startDate; 63 } 64 65 74 public void setTo(String endDateString) { 75 endDate = endDateString; 76 } 77 78 83 public String getTo() { 84 return endDate; 85 } 86 87 95 public void setOn(String on) { 96 setFrom(on); 97 setTo(on); 98 } 99 100 101 106 public Date getStartDate(ScheduleContext context) { 107 if (startDate == null) { 108 return new Date (Interval.ALWAYS); 109 } 110 111 TimeZone timeZone = context.getTimeZone(); 112 113 try { 114 return DateHelper.parseDate(startDate, timeZone); 115 } catch (ParseException e) { 116 throw new OddjobConfigException("Failed to parse start date [" 117 + startDate + "]"); 118 } 119 120 } 121 122 127 public Date getEndDate(ScheduleContext context) { 128 if (endDate == null) { 129 return new Date (Interval.NEVER_AGAIN); 130 } 131 132 TimeZone timeZone = context.getTimeZone(); 133 134 try { 135 return DateUtils.endOfDay(DateHelper.parseDate(endDate, timeZone), 136 timeZone); 137 } catch (ParseException e) { 138 throw new OddjobConfigException("Failed to parse end date [" 139 + endDate + "]"); 140 } 141 } 142 143 147 public Interval nextDue(ScheduleContext context) { 148 Date now = context.getDate(); 149 if (now == null) { 150 return null; 151 } 152 if (now.compareTo(getEndDate(context)) > 0) { 153 return null; 154 } 155 Interval nextInterval = new Interval( 156 getStartDate(context), 157 getEndDate(context)); 158 Schedule child = getChildSchedule(); 159 if (child == null) { 160 return nextInterval; 161 } 162 child.setLimits(nextInterval); 163 Interval childInterval = null; 164 if (now.compareTo(nextInterval.getFromDate()) < 0) { 167 childInterval = child.nextDue( 168 context.spawn(nextInterval.getFromDate())); 169 } 170 else { 171 childInterval = child.nextDue(context); 172 } 173 return childInterval; 174 } 175 178 public String toString() { 179 180 return this.getClass().getName() + " from " + getFrom() + " to " + getTo(); 181 } 182 } 183 | Popular Tags |