Skip to content

Commit

Permalink
Merge pull request #2066 from scireum/feature/sbi/SIRI-929
Browse files Browse the repository at this point in the history
Jobs Add a new parameter for setting local date times
  • Loading branch information
idlira authored Dec 17, 2024
2 parents 728a253 + f3002c3 commit 63b0dad
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
73 changes: 73 additions & 0 deletions src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java
Original file line number Diff line number Diff line change
@@ -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<LocalDateTime, LocalDateTimeParameter> {

private Supplier<LocalDateTime> 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.
* <p>
* A <tt>Supplier</tt> 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<LocalDateTime> 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<String, String> parameterContext) {
return super.computeValueUpdate(parameterContext).map(NLS::toUserString);
}

@Override
protected Optional<LocalDateTime> resolveFromString(Value input) {
return Optional.ofNullable(NLS.parseMachineString(LocalDateTime.class, input.getString()));
}
}
10 changes: 4 additions & 6 deletions src/main/java/sirius/biz/storage/layer3/DeleteFilesJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -69,8 +68,8 @@ public class DeleteFilesJob extends BatchJob {
new StringParameter("filter", "$DeleteFilesJob.pathFilter").withDescription(
"$DeleteFilesJob.pathFilter.help").build();

private static final Parameter<LocalDate> LAST_MODIFIED_BEFORE_PARAMETER =
new LocalDateParameter("lastModifiedBefore", "$DeleteFilesJob.lastModifiedBefore").withDescription(
private static final Parameter<LocalDateTime> LAST_MODIFIED_BEFORE_PARAMETER =
new LocalDateTimeParameter("lastModifiedBefore", "$DeleteFilesJob.lastModifiedBefore").withDescription(
"$DeleteFilesJob.lastModifiedBefore.help").build();

private static final Parameter<Boolean> ONLY_UNUSED_PARAMETER =
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<i:arg type="sirius.biz.jobs.params.Parameter" name="param"/>
<i:arg type="Map" name="context"/>

<i:local name="additionalClasses" value="@apply('param-%s %s', param.getName(), param.isRequired() ? 'required' : '')"/>
<t:dateTimeField class="@apply('col-md-12 col-xs-12 %s', additionalClasses)"
name="@param.getName()"
label="@param.getLabel()"
step="1"
help="@param.getDescription()"
value="@param.get(context).orElse(null).as(LocalDateTime.class)"/>

<script>
sirius.ready(function () {
const _elem = sirius.querySelector(".param-___param.getName()");
const _input = _elem.querySelector("input");
_elem.addEventListener("clear", function () {
_input.value = null;
});
_elem.addEventListener("updated-value", function (event) {
_input.value = event.detail;
});
});
</script>

0 comments on commit 63b0dad

Please sign in to comment.