You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by mw...@apache.org on 2025/08/07 19:49:36 UTC

(incubator-kie-kogito-runtimes) branch main updated: Add support for simple time definitions in task notifications (#4015)

This is an automated email from the ASF dual-hosted git repository.

mweiler pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-kogito-runtimes.git


The following commit(s) were added to refs/heads/main by this push:
     new f595552037 Add support for simple time definitions in task notifications (#4015)
f595552037 is described below

commit f59555203779d1cd0c9fd6987c042beb74c72619
Author: Alisha Mohamed Ali <33...@users.noreply.github.com>
AuthorDate: Fri Aug 8 01:19:33 2025 +0530

    Add support for simple time definitions in task notifications (#4015)
    
    Co-authored-by: Alisha Mohamed Ali <Al...@ibm.com>
---
 .../kogito/usertask/impl/model/DeadlineHelper.java | 86 +++++++++++++++++++++-
 .../usertask/impl/model/DeadlineHelperTest.java    | 56 ++++++++++++++
 2 files changed, 140 insertions(+), 2 deletions(-)

diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/model/DeadlineHelper.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/model/DeadlineHelper.java
index e492bfcc73..8218a8e978 100644
--- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/model/DeadlineHelper.java
+++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/model/DeadlineHelper.java
@@ -31,6 +31,7 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import java.util.function.Function;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
@@ -53,6 +54,7 @@ public class DeadlineHelper {
 
     private static final Pattern deadLineSeparatorPattern = Pattern.compile("\\^");
     private static final Pattern listSeparatorPattern = Pattern.compile(",");
+    private static final Pattern shorthandDurationPattern = Pattern.compile("^(\\d+)(\\w+)$");
 
     public static ExpirationTime getExpirationTime(ScheduleInfo info) {
         ExpirationTime expirationTime;
@@ -172,8 +174,18 @@ public class DeadlineHelper {
             // not repeatable duration
             scheduleInfo.setDuration(parseDuration(dateComponents[0]));
         } else {
-            // not repeatable exact date
-            scheduleInfo.setEndDate(ZonedDateTime.parse(dateComponents[0]));
+            Duration shorthandDuration = parseShorthandDuration(dateComponents[0]);
+            if (shorthandDuration != null) {
+                //  Support shorthand durations like "1m" or "2h"
+                scheduleInfo.setDuration(shorthandDuration);
+            } else {
+                // not repeatable exact date
+                try {
+                    scheduleInfo.setEndDate(ZonedDateTime.parse(dateComponents[0]));
+                } catch (Exception e) {
+                    throw new IllegalArgumentException("Invalid time format: " + timeStr, e);
+                }
+            }
         }
         return scheduleInfo;
     }
@@ -237,4 +249,74 @@ public class DeadlineHelper {
         }
         return info.getDuration().toMillis();
     }
+
+    private static Duration parseShorthandDuration(String timeStr) {
+        Matcher matcher = shorthandDurationPattern.matcher(timeStr);
+        if (matcher.matches()) {
+            long value = Long.parseLong(matcher.group(1));
+            String unitInput = matcher.group(2).toLowerCase();
+            DurationUnit unit = DurationUnit.fromString(unitInput);
+            if (unit == null) {
+                throw new IllegalArgumentException("Unknown shorthand duration unit: " + unitInput);
+            }
+            return unit.toDuration(value);
+        }
+        return null;
+    }
+
+    private enum DurationUnit {
+        SECONDS("s") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofSeconds(value);
+            }
+        },
+        MINUTES("m") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofMinutes(value);
+            }
+        },
+        HOURS("h") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofHours(value);
+            }
+        },
+        DAYS("d") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofDays(value);
+            }
+        },
+        WEEKS("w") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofDays(value * 7);
+            }
+        },
+        YEARS("y") {
+            @Override
+            Duration toDuration(long value) {
+                return Duration.ofDays(value * 365);
+            }
+        };
+
+        private final String alias;
+
+        DurationUnit(String alias) {
+            this.alias = alias;
+        }
+
+        abstract Duration toDuration(long value);
+
+        static DurationUnit fromString(String input) {
+            for (DurationUnit unit : values()) {
+                if (unit.alias.equals(input)) {
+                    return unit;
+                }
+            }
+            return null;
+        }
+    }
 }
diff --git a/jbpm/jbpm-usertask/src/test/java/org/kie/kogito/usertask/impl/model/DeadlineHelperTest.java b/jbpm/jbpm-usertask/src/test/java/org/kie/kogito/usertask/impl/model/DeadlineHelperTest.java
index 2f83ce3753..550f52633f 100644
--- a/jbpm/jbpm-usertask/src/test/java/org/kie/kogito/usertask/impl/model/DeadlineHelperTest.java
+++ b/jbpm/jbpm-usertask/src/test/java/org/kie/kogito/usertask/impl/model/DeadlineHelperTest.java
@@ -32,6 +32,7 @@ import org.kie.kogito.usertask.model.Reassignment;
 import org.kie.kogito.usertask.model.ScheduleInfo;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.jupiter.api.Assertions.fail;
 
 public class DeadlineHelperTest {
@@ -251,4 +252,59 @@ public class DeadlineHelperTest {
     private void assertEqualsDate(ZonedDateTime expectedDate, ZonedDateTime calculatedDate) {
         assertThat(calculatedDate.toInstant().getEpochSecond()).isEqualTo(expectedDate.toInstant().getEpochSecond());
     }
+
+    @Test
+    public void testStandaloneShorthandDuration() {
+        Collection<DeadlineInfo<Notification>> deadlines = DeadlineHelper.parseDeadlines(
+                "[subject:1 minute shorthand]@[1m]");
+        assertThat(deadlines).hasSize(1);
+        DeadlineInfo<Notification> deadlineInfo = deadlines.iterator().next();
+        assertThat(deadlineInfo.getNotification().getData()).containsEntry("subject", "1 minute shorthand");
+        Collection<ScheduleInfo> scheduling = deadlineInfo.getScheduleInfo();
+        assertThat(scheduling).hasSize(1);
+        ScheduleInfo scheduleInfo = scheduling.iterator().next();
+        assertThat(scheduleInfo.getDuration()).isEqualTo(Duration.ofMinutes(1));
+        assertThat(scheduleInfo.getNumRepetitions()).isZero();
+        assertThat(scheduleInfo.getEndDate()).isNull();
+        assertThat(scheduleInfo.getStartDate()).isNull();
+
+        ExpirationTime time = DeadlineHelper.getExpirationTime(scheduleInfo);
+        assertThat(time.repeatInterval()).isNull();
+        assertThat(time.repeatLimit()).isZero();
+        assertThat(ZonedDateTime.now().plus(Duration.ofMinutes(1)).isAfter(time.get())).isTrue();
+    }
+
+    @Test
+    public void testMultipleStandaloneShorthandDurations() {
+        Collection<DeadlineInfo<Notification>> deadlines = DeadlineHelper.parseDeadlines(
+                "[subject:1 min and 2 hours]@[1m,2h]");
+        assertThat(deadlines).hasSize(1);
+        DeadlineInfo<Notification> deadlineInfo = deadlines.iterator().next();
+        assertThat(deadlineInfo.getNotification().getData()).containsEntry("subject", "1 min and 2 hours");
+        Collection<ScheduleInfo> scheduling = deadlineInfo.getScheduleInfo();
+        assertThat(scheduling).hasSize(2);
+        assertThat(scheduling.stream().map(ScheduleInfo::getDuration))
+                .containsExactlyInAnyOrder(Duration.ofMinutes(1), Duration.ofHours(2));
+    }
+
+    @Test
+    public void testInvalidShorthandDuration() {
+        assertThatThrownBy(() -> DeadlineHelper.parseDeadlines("[subject:Invalid unit]@[1x]"))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("Unknown shorthand duration unit: x");
+    }
+
+    @Test
+    public void testZeroShorthandDuration() {
+        Collection<DeadlineInfo<Notification>> deadlines = DeadlineHelper.parseDeadlines(
+                "[subject:Zero seconds]@[0s]");
+        assertThat(deadlines).hasSize(1);
+        DeadlineInfo<Notification> deadlineInfo = deadlines.iterator().next();
+        assertThat(deadlineInfo.getNotification().getData()).containsEntry("subject", "Zero seconds");
+        Collection<ScheduleInfo> scheduling = deadlineInfo.getScheduleInfo();
+        assertThat(scheduling).hasSize(1);
+        ScheduleInfo scheduleInfo = scheduling.iterator().next();
+        assertThat(scheduleInfo.getDuration()).isEqualTo(Duration.ZERO);
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org