From 0f8d95e2f47bf7d020d6d1789c885dbe14b69b69 Mon Sep 17 00:00:00 2001 From: Daniel Sarlo Date: Wed, 5 Jun 2024 11:59:53 -0400 Subject: [PATCH 1/2] [#1904] LocalDateBasicUserType correctly converting from string to LocalDate Update LocalDateBasicUserType.java Revert "Update LocalDateBasicUserType.java" This reverts commit 8a9d6d9b7b1d37d44424c75265f111b6a567e837. Removed override - address code review items --- .../impl/type/LocalDateBasicUserType.java | 34 ++++++++++++++++--- .../entity/simple/DocumentForCollections.java | 10 ++++++ .../subview/MultisetFetchCollectionsTest.java | 3 ++ .../SubviewDocumentMultisetFetchView.java | 3 ++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java index 791bb6d8b8..d008ab6b41 100644 --- a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java +++ b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java @@ -18,22 +18,46 @@ import com.blazebit.persistence.view.spi.type.BasicUserType; -import java.sql.Timestamp; import java.time.LocalDate; -import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; /** - * * @author Christian Beikov * @since 1.5.0 */ public class LocalDateBasicUserType extends TimestampishImmutableBasicUserType { public static final BasicUserType INSTANCE = new LocalDateBasicUserType(); + private static final DateTimeFormatter OPTIONAL_TIME_OFFSET_AND_ZONE_FORMATTER = new DateTimeFormatterBuilder() + .parseCaseInsensitive() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .optionalStart() + .optionalStart() + .appendLiteral(' ') + .optionalEnd() + .optionalStart() + .appendLiteral('T') + .optionalEnd() + .append(DateTimeFormatter.ISO_LOCAL_TIME) + .optionalStart() + .appendOffsetId() + .optionalEnd() + .optionalStart() + .appendLiteral('[') + .appendZoneRegionId() + .appendLiteral(']') + .optionalEnd() + .optionalEnd().toFormatter(); @Override public LocalDate fromString(CharSequence sequence) { - return Timestamp.valueOf(sequence.toString()).toInstant().atZone(ZoneOffset.UTC).toLocalDate(); + String input = sequence.toString(); + try { + return LocalDate.parse(input, OPTIONAL_TIME_OFFSET_AND_ZONE_FORMATTER); + } catch (DateTimeParseException e) { + throw new IllegalArgumentException("Invalid date or date-time format"); + } } - } diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java index 516b899648..aad77ad8f8 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/entity/simple/DocumentForCollections.java @@ -28,6 +28,7 @@ import javax.persistence.OrderColumn; import javax.persistence.Table; import java.io.Serializable; +import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -48,6 +49,7 @@ public class DocumentForCollections implements Serializable { private Long id; private String name; + private LocalDate dateCollected; private PersonForCollections owner; private Set partners = new HashSet(); private Map contacts = new HashMap(); @@ -119,6 +121,14 @@ public void setPersonList(List personList) { this.personList = personList; } + public LocalDate getDateCollected() { + return dateCollected; + } + + public void setDateCollected(LocalDate dateCollected) { + this.dateCollected = dateCollected; + } + @Override public int hashCode() { final int prime = 31; diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java index 1b036530a2..fe1c3b0b71 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/MultisetFetchCollectionsTest.java @@ -41,6 +41,7 @@ import org.junit.experimental.categories.Category; import javax.persistence.EntityManager; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.Set; @@ -91,6 +92,8 @@ public void work(EntityManager em) { doc1.getContacts().put(1, pers1); doc1.getContacts().put(2, pers2); + doc1.setDateCollected(LocalDate.now()); + em.persist(pers1); em.persist(pers2); diff --git a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java index 21292f70db..e2021e6529 100644 --- a/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java +++ b/entity-view/testsuite/src/test/java/com/blazebit/persistence/view/testsuite/collections/subview/model/SubviewDocumentMultisetFetchView.java @@ -23,6 +23,7 @@ import com.blazebit.persistence.view.UpdatableEntityView; import com.blazebit.persistence.view.testsuite.collections.entity.simple.DocumentForCollections; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.Set; @@ -41,6 +42,8 @@ public interface SubviewDocumentMultisetFetchView extends SubviewSimpleDocumentM public String getName(); + public LocalDate getDateCollected(); + @Mapping(value = "partners", fetch = FetchStrategy.MULTISET) public Set getMultisetPartners(); From 7e5c5d9fddb03bfc2d6eee1c780890c044a46fc1 Mon Sep 17 00:00:00 2001 From: Daniel Sarlo Date: Wed, 5 Jun 2024 13:00:43 -0400 Subject: [PATCH 2/2] Update entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java Co-authored-by: Christian Beikov --- .../persistence/view/impl/type/LocalDateBasicUserType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java index d008ab6b41..9273a7d167 100644 --- a/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java +++ b/entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java @@ -57,7 +57,7 @@ public LocalDate fromString(CharSequence sequence) { try { return LocalDate.parse(input, OPTIONAL_TIME_OFFSET_AND_ZONE_FORMATTER); } catch (DateTimeParseException e) { - throw new IllegalArgumentException("Invalid date or date-time format"); + throw new IllegalArgumentException("Invalid date or date-time format: " + input, e); } } }