Commit dded7dec authored by Carlos Aristu's avatar Carlos Aristu
Browse files

[quartz-upgrade] Use Java 8 Date/Time API

parent aac918f7
......@@ -21,7 +21,7 @@ package org.openbravo.scheduling;
import static org.quartz.TriggerBuilder.newTrigger;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
......@@ -33,10 +33,10 @@ class LaterTriggerGenerator extends TriggerGenerator {
@Override
public TriggerBuilder<Trigger> getBuilder(TriggerData data) throws ParseException {
return newTrigger().startAt(getStartDate(data).getTime());
return newTrigger().startAt(getStartDate(data));
}
private Calendar getStartDate(TriggerData data) throws ParseException {
private Date getStartDate(TriggerData data) throws ParseException {
return timestamp(data.startDate, data.startTime);
}
......
......@@ -22,7 +22,8 @@ import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import java.text.ParseException;
import java.util.Calendar;
import java.time.LocalDateTime;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronTrigger;
......@@ -42,36 +43,36 @@ abstract class ScheduledTriggerGenerator extends TriggerGenerator {
public TriggerBuilder<?> getBuilder(TriggerData data) throws ParseException {
TriggerBuilder<?> triggerBuilder = getScheduledBuilder(data);
if (StringUtils.isEmpty(data.nextFireTime)) {
triggerBuilder.startAt(getStartDate(data).getTime());
triggerBuilder.startAt(getStartDate(data));
} else {
triggerBuilder.startAt(getNextFireDate(data).getTime());
triggerBuilder.startAt(getNextFireDate(data));
}
if (FINISHES.equals(data.finishes)) {
triggerBuilder.endAt(getFinishDate(data).getTime());
triggerBuilder.endAt(getFinishDate(data));
}
return triggerBuilder;
}
private Calendar getStartDate(TriggerData data) throws ParseException {
private Date getStartDate(TriggerData data) throws ParseException {
return timestamp(data.startDate, data.startTime);
}
private Calendar getFinishDate(TriggerData data) throws ParseException {
private Date getFinishDate(TriggerData data) throws ParseException {
return timestamp(data.finishesDate, data.finishesTime);
}
private Calendar getNextFireDate(TriggerData data) throws ParseException {
private Date getNextFireDate(TriggerData data) throws ParseException {
return timestamp(data.nextFireTime, data.nextFireTime);
}
protected String getCronTime(TriggerData data) throws ParseException {
Calendar start = getStartDate(data);
LocalDateTime localDateTime = parse(data.startDate, data.startTime);
int second = start.get(Calendar.SECOND);
int minute = start.get(Calendar.MINUTE);
int hour = start.get(Calendar.HOUR_OF_DAY);
int second = localDateTime.getSecond();
int minute = localDateTime.getMinute();
int hour = localDateTime.getHour();
return second + " " + minute + " " + hour;
}
......
......@@ -21,10 +21,12 @@ package org.openbravo.scheduling;
import static org.openbravo.scheduling.OBScheduler.OB_GROUP;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
......@@ -34,7 +36,7 @@ import org.quartz.TriggerBuilder;
*/
abstract class TriggerGenerator {
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
/**
* Provides the TriggerBuilder used by the {@link #generate(String, TriggerData)} method to create
......@@ -77,40 +79,50 @@ abstract class TriggerGenerator {
}
/**
* Utility method to parse a start date string and a start time string into a date.
* Utility method to parse a start date string and a start time string into a {@link Date}.
*
* @param date
* A date as a String. Expected format: 'dd-MM-yyyy'
*
* @param time
* A time as a String. Expected format: 'HH24:MI:SS'
* A time as a String. Expected format: 'HH:mm:ss'
*
* @return a {@link Calendar} with the provided date and time.
* @return a {@link Date} with the provided date and time.
*
* @throws ParseException
* if the provided date and time can not be parsed to create the {@link Calendar}
* instance.
* if the provided date and time can not be parsed to create the {@link Date} instance.
*/
protected Calendar timestamp(String date, String time) throws ParseException {
Calendar cal = Calendar.getInstance();
if (StringUtils.isNotBlank(date)) {
synchronized (dateFormat) {
cal.setTime(dateFormat.parse(date));
}
protected Date timestamp(String date, String time) throws ParseException {
LocalDateTime localDateTime = parse(date, time);
try {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} catch (Exception ex) {
throw new ParseException("Could not parse date " + date + " " + time, -1);
}
}
if (StringUtils.isNotBlank(time)) {
int hour = Integer.parseInt(time.substring(time.indexOf(' ') + 1, time.indexOf(':')));
int minute = Integer.parseInt(time.substring(time.indexOf(':') + 1, time.lastIndexOf(':')));
int second = Integer.parseInt(time.substring(time.lastIndexOf(':') + 1, time.length()));
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
/**
* Utility method to parse a start date string and a start time string into a
* {@link LocalDateTime}.
*
* @param date
* A date as a String. Expected format: 'dd-MM-yyyy'
*
* @param time
* A time as a String. Expected format: 'HH:mm:ss'
*
* @return a {@link LocalDateTime} with the provided date and time.
*
* @throws ParseException
* if the provided date and time can not be parsed to create the {@link LocalDateTime}
* instance.
*/
protected LocalDateTime parse(String date, String time) throws ParseException {
try {
return LocalDateTime.parse(date + " " + time, formatter);
} catch (DateTimeParseException ex) {
throw new ParseException("Could not parse date " + date + " " + time, -1);
}
return cal;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment