Skip to content

Commit

Permalink
Merge pull request #3 from d-lobo/add-resolution-support-in-date-fiel…
Browse files Browse the repository at this point in the history
…d-definition-builder

Add resolution support in DateFieldDefinitionBuilder
  • Loading branch information
bjoern42 authored Sep 16, 2024
2 parents 9259fdb + cd01c41 commit 6aec24a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.merkle.oss.magnolia.definition.builder.simple;

import info.magnolia.ui.field.DateFieldDefinition;
import info.magnolia.ui.field.TextFieldDefinition;

import javax.annotation.Nullable;
import java.util.Optional;

import javax.annotation.Nullable;

import com.vaadin.shared.ui.datefield.DateResolution;
import com.vaadin.shared.ui.datefield.DateTimeResolution;

/**
* builds a {@link DateFieldDefinition}
* @see <a href="https://docs.magnolia-cms.com/product-docs/6.2/Developing/Templating/Dialog-definition/Field-definition/List-of-fields/Date-field.html">magnolia Docs - Date field </a>
Expand All @@ -20,6 +23,8 @@ public class DateFieldDefinitionBuilder extends AbstractConfiguredFieldDefinitio
private String dateFormat;
@Nullable
private String timeFormat;
@Nullable
private String resolution;

public DateFieldDefinitionBuilder() {}
public DateFieldDefinitionBuilder(final DateFieldDefinition definition) {
Expand All @@ -28,6 +33,7 @@ public DateFieldDefinitionBuilder(final DateFieldDefinition definition) {
inISO8061Format(definition.isInISO8061Format());
dateFormat(definition.getDateFormat());
timeFormat(definition.getTimeFormat());
resolution(definition.getResolution());
}

public DateFieldDefinitionBuilder time(final boolean time) {
Expand All @@ -50,13 +56,27 @@ public DateFieldDefinitionBuilder timeFormat(final String timeFormat) {
return self();
}

private DateFieldDefinitionBuilder resolution(final String resolution) {
this.resolution = resolution;
return self();
}

public DateFieldDefinitionBuilder resolution(final DateTimeResolution resolution) {
return resolution(resolution.name());
}

public DateFieldDefinitionBuilder resolution(final DateResolution resolution) {
return resolution(resolution.name());
}

public DateFieldDefinition build(final String name) {
final DateFieldDefinition definition = new DateFieldDefinition();
super.populate(definition, name);
Optional.ofNullable(time).ifPresent(definition::setTime);
Optional.ofNullable(inISO8061Format).ifPresent(definition::setInISO8061Format);
Optional.ofNullable(dateFormat).ifPresent(definition::setDateFormat);
Optional.ofNullable(timeFormat).ifPresent(definition::setTimeFormat);
Optional.ofNullable(resolution).ifPresent(definition::setResolution);
return definition;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.merkle.oss.magnolia.definition.builder.simple;

import com.merkle.oss.magnolia.definition.builder.AbstractFieldDefinitionBuilderTestCase;
import com.vaadin.shared.ui.datefield.DateTimeResolution;

import info.magnolia.ui.field.DateFieldDefinition;
import org.junit.jupiter.api.Test;

Expand All @@ -16,16 +18,19 @@ void testBuilder() {
.timeFormat("timeFormat")
.time(true)
.inISO8061Format(true)
.resolution(DateTimeResolution.MINUTE)
.build("date");
assertEquals("dateFormat", definition.getDateFormat());
assertEquals("timeFormat", definition.getTimeFormat());
assertEquals("MINUTE", definition.getResolution());
assertTrue(definition.isTime());
assertTrue(definition.isInISO8061Format());

final DateFieldDefinition emptyDefinition = new DateFieldDefinitionBuilder().build("date");
assertEquals(Date.class, emptyDefinition.getType());
assertEquals("yyyy-MM-dd", emptyDefinition.getDateFormat());
assertEquals("HH:mm", emptyDefinition.getTimeFormat());
assertNull(emptyDefinition.getResolution());
assertFalse(emptyDefinition.isTime());
assertFalse(emptyDefinition.isInISO8061Format());
}
Expand Down

0 comments on commit 6aec24a

Please sign in to comment.