-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2066 from scireum/feature/sbi/SIRI-929
Jobs Add a new parameter for setting local date times
- Loading branch information
Showing
3 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/sirius/biz/jobs/params/LocalDateTimeParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/resources/default/templates/biz/jobs/params/date-time.html.pasta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |