From 3e39adbd0858fb762ce5a11fb4b2ae149833c2d3 Mon Sep 17 00:00:00 2001 From: maica Date: Wed, 6 Nov 2019 22:15:44 +0800 Subject: [PATCH 1/3] * Add LocalDate and LocalTime Binder * Add LocalDate and LocalDateTime InFutureCheck --- framework/src/play/data/binding/Binder.java | 4 ++++ .../data/binding/types/LocalDateBinder.java | 15 +++++++++++++++ .../data/binding/types/LocalTimeBinder.java | 15 +++++++++++++++ .../src/play/data/validation/InFutureCheck.java | 17 +++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 framework/src/play/data/binding/types/LocalDateBinder.java create mode 100644 framework/src/play/data/binding/types/LocalTimeBinder.java diff --git a/framework/src/play/data/binding/Binder.java b/framework/src/play/data/binding/Binder.java index 6bf216c87a..d274e1ee23 100644 --- a/framework/src/play/data/binding/Binder.java +++ b/framework/src/play/data/binding/Binder.java @@ -16,7 +16,9 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.text.ParseException; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.*; /** @@ -36,6 +38,8 @@ public abstract class Binder { supportedTypes.put(File.class, new FileBinder()); supportedTypes.put(File[].class, new FileArrayBinder()); supportedTypes.put(LocalDateTime.class, new LocalDateTimeBinder()); + supportedTypes.put(LocalDate.class, new LocalDateBinder()); + supportedTypes.put(LocalTime.class, new LocalTimeBinder()); supportedTypes.put(Model.BinaryField.class, new BinaryBinder()); supportedTypes.put(Upload.class, new UploadBinder()); supportedTypes.put(Upload[].class, new UploadArrayBinder()); diff --git a/framework/src/play/data/binding/types/LocalDateBinder.java b/framework/src/play/data/binding/types/LocalDateBinder.java new file mode 100644 index 0000000000..5ab529f6c5 --- /dev/null +++ b/framework/src/play/data/binding/types/LocalDateBinder.java @@ -0,0 +1,15 @@ +package play.data.binding.types; + +import play.data.binding.TypeBinder; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.time.LocalDate; + +public class LocalDateBinder implements TypeBinder { + + @Override + public LocalDate bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) { + return value != null && !value.trim().isEmpty() ? LocalDate.parse(value) : null; + } +} diff --git a/framework/src/play/data/binding/types/LocalTimeBinder.java b/framework/src/play/data/binding/types/LocalTimeBinder.java new file mode 100644 index 0000000000..1af6b00cae --- /dev/null +++ b/framework/src/play/data/binding/types/LocalTimeBinder.java @@ -0,0 +1,15 @@ +package play.data.binding.types; + +import play.data.binding.TypeBinder; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.time.LocalTime; + +public class LocalTimeBinder implements TypeBinder { + + @Override + public LocalTime bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) { + return value != null && !value.trim().isEmpty() ? LocalTime.parse(value) : null; + } +} diff --git a/framework/src/play/data/validation/InFutureCheck.java b/framework/src/play/data/validation/InFutureCheck.java index 27e11a403f..e31d970568 100644 --- a/framework/src/play/data/validation/InFutureCheck.java +++ b/framework/src/play/data/validation/InFutureCheck.java @@ -2,6 +2,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -53,6 +56,20 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con return false; } } + if (value instanceof LocalDate) { + try { + return reference.before(Date.from(((LocalDate) value).atStartOfDay(ZoneId.systemDefault()).toInstant())); + } catch (Exception e) { + return false; + } + } + if (value instanceof LocalDateTime) { + try { + return reference.before(Date.from(((LocalDateTime) value).atZone(ZoneId.systemDefault()).toInstant())); + } catch (Exception e) { + return false; + } + } return false; } From 6637265640eaba1b1d2913d3f40b11c6578c3f4a Mon Sep 17 00:00:00 2001 From: maica Date: Wed, 6 Nov 2019 22:26:16 +0800 Subject: [PATCH 2/3] * Add Lightbend copyright header --- framework/src/play/data/binding/types/LocalDateBinder.java | 3 +++ framework/src/play/data/binding/types/LocalTimeBinder.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/framework/src/play/data/binding/types/LocalDateBinder.java b/framework/src/play/data/binding/types/LocalDateBinder.java index 5ab529f6c5..b620a4bb4a 100644 --- a/framework/src/play/data/binding/types/LocalDateBinder.java +++ b/framework/src/play/data/binding/types/LocalDateBinder.java @@ -1,3 +1,6 @@ +/* + * Copyright (C) 2009-2019 Lightbend Inc. . + */ package play.data.binding.types; import play.data.binding.TypeBinder; diff --git a/framework/src/play/data/binding/types/LocalTimeBinder.java b/framework/src/play/data/binding/types/LocalTimeBinder.java index 1af6b00cae..d3caab52f9 100644 --- a/framework/src/play/data/binding/types/LocalTimeBinder.java +++ b/framework/src/play/data/binding/types/LocalTimeBinder.java @@ -1,3 +1,6 @@ +/* + * Copyright (C) 2009-2019 Lightbend Inc. . + */ package play.data.binding.types; import play.data.binding.TypeBinder; From 9ac2ddda7f5626c2c5fbd4ced81d8b10797a89f4 Mon Sep 17 00:00:00 2001 From: maica Date: Sun, 17 Nov 2019 23:51:11 +0800 Subject: [PATCH 3/3] * Add LocalDate and LocalTime binder --- .../binding/types/LocalDateBinderTest.java | 43 +++++++++++++++++++ .../binding/types/LocalTimeBinderTest.java | 43 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 framework/test-src/play/data/binding/types/LocalDateBinderTest.java create mode 100644 framework/test-src/play/data/binding/types/LocalTimeBinderTest.java diff --git a/framework/test-src/play/data/binding/types/LocalDateBinderTest.java b/framework/test-src/play/data/binding/types/LocalDateBinderTest.java new file mode 100644 index 0000000000..f2b9302fe9 --- /dev/null +++ b/framework/test-src/play/data/binding/types/LocalDateBinderTest.java @@ -0,0 +1,43 @@ +package play.data.binding.types; + +import org.junit.Before; +import org.junit.Test; +import play.PlayBuilder; + +import java.time.LocalDate; +import java.time.format.DateTimeParseException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class LocalDateBinderTest { + + private LocalDateBinder binder = new LocalDateBinder(); + + @Before + public void setup() { + new PlayBuilder().build(); + } + + @Test + public void nullLocalDate() { + assertNull(binder.bind("event.start", null, null, LocalDate.class, null)); + } + + @Test + public void emptyLocalDate() { + assertNull(binder.bind("event.start", null, "", LocalDate.class, null)); + } + + @Test + public void validLocalDate() { + LocalDate expected = LocalDate.parse("2007-12-03"); + LocalDate actual = binder.bind("event.start", null, "2007-12-03", LocalDate.class, null); + assertEquals(expected, actual); + } + + @Test(expected = DateTimeParseException.class) + public void invalidLocalDate() throws Exception { + binder.bind("event.start", null, "2007-13-03", LocalDate.class, null); + } +} \ No newline at end of file diff --git a/framework/test-src/play/data/binding/types/LocalTimeBinderTest.java b/framework/test-src/play/data/binding/types/LocalTimeBinderTest.java new file mode 100644 index 0000000000..9539105e26 --- /dev/null +++ b/framework/test-src/play/data/binding/types/LocalTimeBinderTest.java @@ -0,0 +1,43 @@ +package play.data.binding.types; + +import org.junit.Before; +import org.junit.Test; +import play.PlayBuilder; + +import java.time.LocalTime; +import java.time.format.DateTimeParseException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class LocalTimeBinderTest { + + private LocalTimeBinder binder = new LocalTimeBinder(); + + @Before + public void setup() { + new PlayBuilder().build(); + } + + @Test + public void nullLocalTime() { + assertNull(binder.bind("event.start", null, null, LocalTime.class, null)); + } + + @Test + public void emptyLocalTime() { + assertNull(binder.bind("event.start", null, "", LocalTime.class, null)); + } + + @Test + public void validLocalTime() { + LocalTime expected = LocalTime.parse("10:15:30"); + LocalTime actual = binder.bind("event.start", null, "10:15:30", LocalTime.class, null); + assertEquals(expected, actual); + } + + @Test(expected = DateTimeParseException.class) + public void invalidLocalTime() throws Exception { + binder.bind("event.start", null, "25:15:30", LocalTime.class, null); + } +} \ No newline at end of file