From: Banana Date: Tue, 5 Jul 2022 10:45:01 +0000 (+0200) Subject: some magnolia code examples and stuff X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=bfb7c72b74a66fa6f8fe1d21174485b7496cc3f7;p=klimbim.git some magnolia code examples and stuff --- diff --git a/magnolia/README b/magnolia/README new file mode 100644 index 0000000..8f2b7b2 --- /dev/null +++ b/magnolia/README @@ -0,0 +1 @@ +Some examples based on Magnolia 6.2.15 \ No newline at end of file diff --git a/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidator.java b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidator.java new file mode 100644 index 0000000..6b8f02c --- /dev/null +++ b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidator.java @@ -0,0 +1,42 @@ +package net.bananasplayground.validator; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.apache.commons.lang3.StringUtils; + +import com.vaadin.data.ValidationResult; +import com.vaadin.data.ValueContext; +import com.vaadin.data.validator.AbstractValidator; + +/** + * @see net.bananasplayground.validator.TimeFrameValidatorDefinition + */ + +public class TimeFrameValidator extends AbstractValidator { + + private final String errorMessage; + private final TimeFrameValidatorDefinition definition; + + protected TimeFrameValidator(String errorMessage, TimeFrameValidatorDefinition definition) { + super(errorMessage); + this.errorMessage = errorMessage; + this.definition = definition; + } + + @Override + public ValidationResult apply(LocalDateTime value, ValueContext context) { + if(StringUtils.isAnyBlank(definition.getTimeTo(), definition.getTimeFrom())) return ValidationResult.ok(); + boolean isValid = false; + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateTime timeFrom = LocalDateTime.parse(value.format(formatter) +"T"+definition.getTimeFrom()); + LocalDateTime timeTo = LocalDateTime.parse(value.format(formatter) +"T"+definition.getTimeTo()); + + if((value.isEqual(timeFrom) || value.isAfter(timeFrom)) && (value.isEqual(timeTo) || value.isBefore(timeTo))) { + isValid = true; + } + + return isValid ? ValidationResult.ok() : ValidationResult.error(this.errorMessage); + } +} diff --git a/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorDefinition.java b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorDefinition.java new file mode 100644 index 0000000..2c66afc --- /dev/null +++ b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorDefinition.java @@ -0,0 +1,31 @@ +package net.bananasplayground.validator; + +import info.magnolia.ui.field.ConfiguredFieldValidatorDefinition; +import info.magnolia.ui.field.ValidatorType; +import lombok.Getter; +import lombok.Setter; + +/** + * Check if given datetime is between given timeframe of that date(day) + * + * works on LocalDateTime object + * + * timeframe: + * $type: timeframe + * timeFrom: "HH:MM" + * timeTo: "HH:MM" + * errorMessage: bla + */ + +@Getter +@Setter +@ValidatorType("timeframe") +public class TimeFrameValidatorDefinition extends ConfiguredFieldValidatorDefinition { + + private String timeTo; + private String timeFrom; + + public TimeFrameValidatorDefinition() { + setFactoryClass(TimeFrameValidatorFactory.class); + } +} diff --git a/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorFactory.java b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorFactory.java new file mode 100644 index 0000000..2b0788e --- /dev/null +++ b/magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorFactory.java @@ -0,0 +1,26 @@ +package net.bananasplayground.validator; + +import java.time.LocalDateTime; + +import javax.inject.Inject; + +import com.vaadin.data.Validator; + +import info.magnolia.ui.field.AbstractFieldValidatorFactory; + +/** + * @see net.bananasplayground.validator.TimeFrameValidatorDefinition + */ + +public class TimeFrameValidatorFactory extends AbstractFieldValidatorFactory { + + @Inject + public TimeFrameValidatorFactory(TimeFrameValidatorDefinition definition) { + super(definition); + } + + @Override + public Validator createValidator() { + return new TimeFrameValidator(getI18nErrorMessage(), definition); + } +}