]> 91.132.146.200 Git - klimbim.git/commitdiff
some magnolia code examples and stuff
authorBanana <banana@starscream.de>
Tue, 5 Jul 2022 10:45:01 +0000 (12:45 +0200)
committerBanana <banana@starscream.de>
Tue, 5 Jul 2022 10:45:01 +0000 (12:45 +0200)
magnolia/README [new file with mode: 0644]
magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidator.java [new file with mode: 0644]
magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorDefinition.java [new file with mode: 0644]
magnolia/timeFrameValidator/net/bananasplayground/validator/TimeFrameValidatorFactory.java [new file with mode: 0644]

diff --git a/magnolia/README b/magnolia/README
new file mode 100644 (file)
index 0000000..8f2b7b2
--- /dev/null
@@ -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 (file)
index 0000000..6b8f02c
--- /dev/null
@@ -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<LocalDateTime> {
+
+    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 (file)
index 0000000..2c66afc
--- /dev/null
@@ -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 (file)
index 0000000..2b0788e
--- /dev/null
@@ -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<TimeFrameValidatorDefinition, LocalDateTime> {
+
+    @Inject
+    public TimeFrameValidatorFactory(TimeFrameValidatorDefinition definition) {
+        super(definition);
+    }
+
+    @Override
+    public Validator<LocalDateTime> createValidator() {
+        return new TimeFrameValidator(getI18nErrorMessage(), definition);
+    }
+}