Skip to content

Commit

Permalink
[Blazebit#1904] LocalDateBasicUserType correctly converting from stri…
Browse files Browse the repository at this point in the history
…ng to LocalDate

Update LocalDateBasicUserType.java

Revert "Update LocalDateBasicUserType.java"

This reverts commit 8a9d6d9.

Removed override - address code review items

Update entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java

Co-authored-by: Christian Beikov <[email protected]>

Update entity-view/impl/src/main/java/com/blazebit/persistence/view/impl/type/LocalDateBasicUserType.java

Co-authored-by: Christian Beikov <[email protected]>
  • Loading branch information
dsarlo and beikov committed Jun 5, 2024
1 parent e5f6eb7 commit 7d99f03
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDate> {

public static final BasicUserType<LocalDate> 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: " + input, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +49,7 @@ public class DocumentForCollections implements Serializable {

private Long id;
private String name;
private LocalDate dateCollected;
private PersonForCollections owner;
private Set<PersonForCollections> partners = new HashSet<PersonForCollections>();
private Map<Integer, PersonForCollections> contacts = new HashMap<Integer, PersonForCollections>();
Expand Down Expand Up @@ -119,6 +121,14 @@ public void setPersonList(List<PersonForCollections> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,8 @@ public interface SubviewDocumentMultisetFetchView extends SubviewSimpleDocumentM

public String getName();

public LocalDate getDateCollected();

@Mapping(value = "partners", fetch = FetchStrategy.MULTISET)
public Set<SubviewPersonForCollectionsMultisetFetchView> getMultisetPartners();

Expand Down

0 comments on commit 7d99f03

Please sign in to comment.