Commit 7246213c authored by Cristian Berner's avatar Cristian Berner 💎
Browse files

Fixes ISSUE-44827: Allow scheduling a process without start date or time

Previously it was possible to schedule a process without start date or
time, assuming current date and current time. This was lost in a
refactor.
A new method getCurrentDateTime has been added to SchedulerTimeUtils to
get the missing part of date/time provided assuming "now" when missing.
It will return the date + time in the expected format for timestamp and
parse.

Fixes regression introduced in commit dded7dec
parent 08236f77
......@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2019 Openbravo SLU
* All portions are Copyright (C) 2019-2020 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
......@@ -36,6 +36,10 @@ public class SchedulerTimeUtils {
private static final Logger log = LogManager.getLogger();
private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter
.ofPattern("dd-MM-yyyy HH:mm:ss");
private static final DateTimeFormatter DEFAULT_DATE_FORMATTER = DateTimeFormatter
.ofPattern("dd-MM-yyyy");
private static final DateTimeFormatter DEFAULT_TIME_FORMATTER = DateTimeFormatter
.ofPattern("HH:mm:ss");
private SchedulerTimeUtils() {
}
......@@ -44,7 +48,8 @@ public class SchedulerTimeUtils {
* Utility method to parse a date with time String into a {@link Date}.
*
* @param dateTime
* A date with time as a String. Expected format: 'dd-MM-yyyy HH:mm:ss'
* A date with time as a String. Expected format: 'dd-MM-yyyy HH:mm:ss' If missing,
* current date and time is returned.
*
* @return a {@link Date} with the provided date and time.
*
......@@ -52,7 +57,12 @@ public class SchedulerTimeUtils {
* if the provided date and time can not be parsed to create the {@link Date} instance.
*/
public static Date timestamp(String dateTime) throws ParseException {
LocalDateTime localDateTime = parse(dateTime);
LocalDateTime localDateTime;
if (dateTime.isEmpty()) {
localDateTime = LocalDateTime.now();
} else {
localDateTime = parse(dateTime);
}
try {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} catch (Exception ex) {
......@@ -82,6 +92,31 @@ public class SchedulerTimeUtils {
}
}
/**
* Returns current DateTime as formatted String to be used in the parse/timestamp method If date
* or time is missing, it will assume now for that part, if both are present it will keep them
*
* @param date
* Date in format dd-MM-yyyy
* @param time
* Time in format HH:mm:ss
* @return Current DateTime as String formatted as "dd-MM-yyyy HH:mm:ss", if any parameter
* missing, it will assume current
*/
public static String getCurrentDateTime(String date, String time) {
String resultDate = date;
if (resultDate.isEmpty()) {
resultDate = LocalDateTime.now().format(DEFAULT_DATE_FORMATTER);
}
String resultTime = time;
if (resultTime.isEmpty()) {
resultTime = LocalDateTime.now().format(DEFAULT_TIME_FORMATTER);
}
return resultDate + " " + resultTime;
}
/**
* Formats the current date using a specific format.
*
......
......@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2019 Openbravo SLU
* All portions are Copyright (C) 2019-2020 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
......@@ -38,7 +38,7 @@ class LaterTriggerGenerator extends TriggerGenerator {
}
private Date getStartDate(TriggerData data) throws ParseException {
String dateTime = data.startDate + " " + data.startTime;
String dateTime = SchedulerTimeUtils.getCurrentDateTime(data.startDate, data.startTime);
return SchedulerTimeUtils.timestamp(dateTime);
}
......
......@@ -11,7 +11,7 @@
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2019 Openbravo SLU
* All portions are Copyright (C) 2019-2020 Openbravo SLU
* All Rights Reserved.
* Contributor(s): ______________________________________.
************************************************************************
......@@ -57,12 +57,12 @@ abstract class ScheduledTriggerGenerator extends TriggerGenerator {
}
private Date getStartDate(TriggerData data) throws ParseException {
String dateTime = data.startDate + " " + data.startTime;
String dateTime = SchedulerTimeUtils.getCurrentDateTime(data.startDate, data.startTime);
return SchedulerTimeUtils.timestamp(dateTime);
}
private Date getFinishDate(TriggerData data) throws ParseException {
String dateTime = data.finishesDate + " " + data.finishesTime;
String dateTime = SchedulerTimeUtils.getCurrentDateTime(data.finishesDate, data.finishesTime);
return SchedulerTimeUtils.timestamp(dateTime);
}
......
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