Fixes ISSUE-44827: Allow scheduling a process without start date or time
authorCristian Berner <cristian.berner@openbravo.com>
Fri, 21 Aug 2020 14:19:34 +0200
changeset 37515 45d74fc33c46
parent 37514 36f6383d31fc
child 37516 743260848809
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 dded7dec5f17b8b1fc8f6c118af985c5274670a5
src/org/openbravo/scheduling/SchedulerTimeUtils.java
src/org/openbravo/scheduling/trigger/LaterTriggerGenerator.java
src/org/openbravo/scheduling/trigger/ScheduledTriggerGenerator.java
--- a/src/org/openbravo/scheduling/SchedulerTimeUtils.java	Wed Aug 19 14:42:22 2020 +0200
+++ b/src/org/openbravo/scheduling/SchedulerTimeUtils.java	Fri Aug 21 14:19:34 2020 +0200
@@ -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 @@
   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 @@
    * 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 @@
    *           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) {
@@ -83,6 +93,31 @@
   }
 
   /**
+   * 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.
    * 
    * @param format
--- a/src/org/openbravo/scheduling/trigger/LaterTriggerGenerator.java	Wed Aug 19 14:42:22 2020 +0200
+++ b/src/org/openbravo/scheduling/trigger/LaterTriggerGenerator.java	Fri Aug 21 14:19:34 2020 +0200
@@ -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 @@
   }
 
   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);
   }
 
--- a/src/org/openbravo/scheduling/trigger/ScheduledTriggerGenerator.java	Wed Aug 19 14:42:22 2020 +0200
+++ b/src/org/openbravo/scheduling/trigger/ScheduledTriggerGenerator.java	Fri Aug 21 14:19:34 2020 +0200
@@ -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 @@
   }
 
   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);
   }