From 312eceba1f5742b564cb4252057e286047cf4fd4 Mon Sep 17 00:00:00 2001 From: Sascha Bieberstein Date: Mon, 16 Dec 2024 23:11:46 +0100 Subject: [PATCH 1/2] Add a new job parameter for setting local date times This is based on the LocalDateParameter and the relatively new t:localDateTime tag. Fixes: SIRI-929 --- .../jobs/params/LocalDateTimeParameter.java | 73 +++++++++++++++++++ .../biz/jobs/params/date-time.html.pasta | 23 ++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java create mode 100644 src/main/resources/default/templates/biz/jobs/params/date-time.html.pasta diff --git a/src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java b/src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java new file mode 100644 index 000000000..51d2eff03 --- /dev/null +++ b/src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java @@ -0,0 +1,73 @@ +/* + * Made with all the love in the world + * by scireum in Remshalden, Germany + * + * Copyright by scireum GmbH + * http://www.scireum.de - info@scireum.de + */ + +package sirius.biz.jobs.params; + +import sirius.kernel.commons.Value; +import sirius.kernel.nls.NLS; + +import java.time.LocalDateTime; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +/** + * Provides a parameter which accepts local date times. + */ +public class LocalDateTimeParameter extends ParameterBuilder { + + private Supplier defaultValueSupplier; + + /** + * Creates a new parameter with the given name and label. + * + * @param name the name of the parameter + * @param label the label of the parameter, which will be {@link NLS#smartGet(String) auto translated} + */ + public LocalDateTimeParameter(String name, String label) { + super(name, label); + } + + /** + * Specifies the default value to use. + *

+ * A Supplier is used instead of a constant value as most probably this parameter is + * only declared once but has to be able to provide an "up-to-date" value like "now". + * + * @param defaultValueSupplier a supplier which returns a default value to use. + * @return the parameter itself for fluent method calls + */ + public LocalDateTimeParameter withDefault(Supplier defaultValueSupplier) { + this.defaultValueSupplier = defaultValueSupplier; + return this; + } + + @Override + public String getTemplateName() { + return "/templates/biz/jobs/params/date-time.html.pasta"; + } + + @Override + protected String checkAndTransformValue(Value input) { + if (input.isEmptyString()) { + return NLS.toMachineString(defaultValueSupplier != null ? defaultValueSupplier.get() : null); + } + + return NLS.toMachineString(NLS.parseUserString(LocalDateTime.class, input.asString())); + } + + @Override + public Optional computeValueUpdate(Map parameterContext) { + return super.computeValueUpdate(parameterContext).map(NLS::toUserString); + } + + @Override + protected Optional resolveFromString(Value input) { + return Optional.ofNullable(NLS.parseMachineString(LocalDateTime.class, input.getString())); + } +} diff --git a/src/main/resources/default/templates/biz/jobs/params/date-time.html.pasta b/src/main/resources/default/templates/biz/jobs/params/date-time.html.pasta new file mode 100644 index 000000000..50107346f --- /dev/null +++ b/src/main/resources/default/templates/biz/jobs/params/date-time.html.pasta @@ -0,0 +1,23 @@ + + + + + + + From f3002c32f2618eca9520102c4b5a08801b7bf11a Mon Sep 17 00:00:00 2001 From: Sascha Bieberstein Date: Mon, 16 Dec 2024 23:13:50 +0100 Subject: [PATCH 2/2] Use the new local date time parameter in the delete files job This increases the flexibility and usefulness of the job, as a specific time can be entered for which older files should be deleted (like the point of time a zip file was extracted to delete old leftover files). Fixes: SIRI-929 --- .../java/sirius/biz/storage/layer3/DeleteFilesJob.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/sirius/biz/storage/layer3/DeleteFilesJob.java b/src/main/java/sirius/biz/storage/layer3/DeleteFilesJob.java index 97df7bf8b..040d4153d 100644 --- a/src/main/java/sirius/biz/storage/layer3/DeleteFilesJob.java +++ b/src/main/java/sirius/biz/storage/layer3/DeleteFilesJob.java @@ -13,7 +13,7 @@ import sirius.biz.jobs.batch.DefaultBatchProcessFactory; import sirius.biz.jobs.params.BooleanParameter; import sirius.biz.jobs.params.FileParameter; -import sirius.biz.jobs.params.LocalDateParameter; +import sirius.biz.jobs.params.LocalDateTimeParameter; import sirius.biz.jobs.params.Parameter; import sirius.biz.jobs.params.StringParameter; import sirius.biz.process.PersistencePeriod; @@ -33,7 +33,6 @@ import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.PathMatcher; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -69,8 +68,8 @@ public class DeleteFilesJob extends BatchJob { new StringParameter("filter", "$DeleteFilesJob.pathFilter").withDescription( "$DeleteFilesJob.pathFilter.help").build(); - private static final Parameter LAST_MODIFIED_BEFORE_PARAMETER = - new LocalDateParameter("lastModifiedBefore", "$DeleteFilesJob.lastModifiedBefore").withDescription( + private static final Parameter LAST_MODIFIED_BEFORE_PARAMETER = + new LocalDateTimeParameter("lastModifiedBefore", "$DeleteFilesJob.lastModifiedBefore").withDescription( "$DeleteFilesJob.lastModifiedBefore.help").build(); private static final Parameter ONLY_UNUSED_PARAMETER = @@ -116,8 +115,7 @@ public void execute() throws Exception { deleteEmpty = process.require(DELETE_EMPTY_DIRECTORIES_PARAMETER); onlyUnused = process.require(ONLY_UNUSED_PARAMETER); process.getParameter(PATH_FILTER_PARAMETER).ifPresent(this::initializePathMatcher); - process.getParameter(LAST_MODIFIED_BEFORE_PARAMETER) - .ifPresent(date -> lastModifiedBefore = date.atStartOfDay()); + process.getParameter(LAST_MODIFIED_BEFORE_PARAMETER).ifPresent(date -> lastModifiedBefore = date); handleDirectory(sourcePath); }