Skip to content

Commit

Permalink
Adding YEAR, WEEK and MONTH on system date variable. Added better err…
Browse files Browse the repository at this point in the history
…or message when syntax in not correct.
  • Loading branch information
vertigo17 committed Jul 24, 2024
1 parent ca5d33f commit 27ddc8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public class VariableService implements IVariableService {
private static final Logger LOG = LogManager.getLogger(VariableService.class);

private static final String VALUE_WHEN_NULL = "<null>";
public static final Pattern SYSTEM_VARIABLE_DATE_PATTERN = Pattern.compile("%system.([a-zA-Z0-9-+]*)-([a-z A-Z0-9]*)%");
// TIPS : Test online your java regex using : https://www.regexplanet.com/advanced/java/index.html
public static final Pattern SYSTEM_VARIABLE_DATE_PATTERN = Pattern.compile("%system.([a-zA-Z0-9-+]*)-([a-z A-Z0-9.,:;'\"$]*)%");
public static final Pattern SYSTEM_SUBVARIABLE_DATE_PATTERN = Pattern.compile("([a-zA-Z]*)([-+])([0-9]*)");

@Autowired
Expand Down Expand Up @@ -427,9 +428,21 @@ public String decodeStringWithDateVariable(String stringToDecode, String locale)
cal.add(Calendar.HOUR, -24);
break;
default:
try {

subVariableMatcher = SYSTEM_SUBVARIABLE_DATE_PATTERN.matcher(variableMatcher.group(1));
if (subVariableMatcher.matches()) {
LOG.debug("Time Unit " + subVariableMatcher.group(1) + " Offset " + subVariableMatcher.group(2) + subVariableMatcher.group(3));
switch (subVariableMatcher.group(1)) {
case "YEAR":
cal.add(Calendar.YEAR, Integer.parseInt(subVariableMatcher.group(2) + subVariableMatcher.group(3)));
break;
case "MONTH":
cal.add(Calendar.MONTH, Integer.parseInt(subVariableMatcher.group(2) + subVariableMatcher.group(3)));
break;
case "WEEK":
cal.add(Calendar.WEEK_OF_YEAR, Integer.parseInt(subVariableMatcher.group(2) + subVariableMatcher.group(3)));
break;
case "DAY":
cal.add(Calendar.DAY_OF_YEAR, Integer.parseInt(subVariableMatcher.group(2) + subVariableMatcher.group(3)));
break;
Expand All @@ -440,19 +453,31 @@ public String decodeStringWithDateVariable(String stringToDecode, String locale)
cal.add(Calendar.MINUTE, Integer.parseInt(subVariableMatcher.group(2) + subVariableMatcher.group(3)));
break;
default:
return stringToDecode.replace(variableMatcher.group(0), "[!!System Date decode error - Unknown Time Unit in '" + subVariableMatcher.group(1) + "' adding '" + subVariableMatcher.group(2) + subVariableMatcher.group(3) + "'!!]");
}
}

} catch (Exception e) {
LOG.warn("Warning when trying to decode a date system variable.", e);
return stringToDecode.replace(variableMatcher.group(0), "[!!System Date decode error - " + e.getMessage() + " in '" + subVariableMatcher.group(1) + "' adding '" + subVariableMatcher.group(2) + subVariableMatcher.group(3) + "'!!]");
}
}

String errorMess = "";
try {
if (StringUtil.isNotEmpty(locale)) {
errorMess = " in '" + variableMatcher.group(2) + "' with locale " + locale.split("-")[0];
LOG.debug("Decode Date Format : " + variableMatcher.group(2) + " with locale " + locale.split("-")[0]);
formater = new SimpleDateFormat(variableMatcher.group(2), new Locale(locale.split("-")[0]));
} else {
errorMess = " in '" + variableMatcher.group(2) + "'";
LOG.debug("Decode Date Format : " + variableMatcher.group(2));
formater = new SimpleDateFormat(variableMatcher.group(2));
}
stringToDecode = stringToDecode.replace(variableMatcher.group(0), formater.format(cal.getTime()));
} catch (Exception e) {
LOG.error(e, e);
stringToDecode = stringToDecode.replace(variableMatcher.group(0), "[!!System Date decode error - " + e.getMessage() + errorMess + "!!]");
LOG.warn("Warning when trying to decode a date system variable.", e);
}

} while (variableMatcher.find());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ NOTE: You can also get the nb of rows your variable has by using the `**nbrows**
|===

| *Date System Variables* |
| *XXXX* | Any of those key word: TODAY, YESTERDAY, TOMORROW, DAY-n, DAY+n, HOUR-n, HOUR+n, MINUTE-n, MINUTE+n
| *XXXX* | Any of those key word: TODAY, YESTERDAY, TOMORROW, YEAR-n, YEAR+n, MONTH-n, MONTH+n, WEEK-n, WEEK+n, DAY-n, DAY+n, HOUR-n, HOUR+n, MINUTE-n, MINUTE+n
| *YYYY* | Any of the https://docs.oracle.com/en%2Fjava%2Fjavase%2F11%2Fdocs%2Fapi%2F%2F/java.base/java/text/SimpleDateFormat.html[JAVA Date Patern] such as: *W* (Week in year), *W* (Week in month), *y* (Year), *M* (Month in year), *d* (Day in month), *E* (Day name in week), *H* (Hour in day (0-23)), *m* (Minute in hour), *s* (Second in minute),....

|===
Expand Down

0 comments on commit 27ddc8c

Please sign in to comment.