From caf7ec09c441a336ecf4e676bd63e2533718bebf Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Mon, 20 Nov 2023 17:11:57 +0200 Subject: [PATCH] Adding converter for Instants to Dates --- .../main/java/eu/eudat/data/DmpEntity.java | 6 +++-- .../converters/InstantToUTCConverter.java | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 dmp-backend/core/src/main/java/eu/eudat/data/converters/InstantToUTCConverter.java diff --git a/dmp-backend/core/src/main/java/eu/eudat/data/DmpEntity.java b/dmp-backend/core/src/main/java/eu/eudat/data/DmpEntity.java index 4c9e03fb0..08569d642 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/data/DmpEntity.java +++ b/dmp-backend/core/src/main/java/eu/eudat/data/DmpEntity.java @@ -5,6 +5,7 @@ import eu.eudat.commons.enums.DmpStatus; import eu.eudat.commons.enums.DmpVersionStatus; import eu.eudat.commons.enums.IsActive; import eu.eudat.data.converters.DateToUTCConverter; +import eu.eudat.data.converters.InstantToUTCConverter; import eu.eudat.data.converters.enums.DmpAccessTypeConverter; import eu.eudat.data.converters.enums.DmpStatusConverter; import eu.eudat.data.converters.enums.IsActiveConverter; @@ -63,13 +64,13 @@ public class DmpEntity implements DataEntity { public static final String _description = "description"; @Column(name = "created_at") - @Convert(converter = DateToUTCConverter.class) + @Convert(converter = InstantToUTCConverter.class) private Instant createdAt; public static final String _createdAt = "createdAt"; @Column(name = "updated_at") - @Convert(converter = DateToUTCConverter.class) + @Convert(converter = InstantToUTCConverter.class) private Instant updatedAt; public static final String _updatedAt = "updatedAt"; @@ -81,6 +82,7 @@ public class DmpEntity implements DataEntity { public static final String _isActive = "isActive"; @Column(name = "finalized_at") + @Convert(converter = InstantToUTCConverter.class) private Instant finalizedAt; public static final String _finalizedAt = "finalizedAt"; diff --git a/dmp-backend/core/src/main/java/eu/eudat/data/converters/InstantToUTCConverter.java b/dmp-backend/core/src/main/java/eu/eudat/data/converters/InstantToUTCConverter.java new file mode 100644 index 000000000..fdb54ad96 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/data/converters/InstantToUTCConverter.java @@ -0,0 +1,23 @@ +package eu.eudat.data.converters; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; + +import java.time.Instant; +import java.util.Date; + +@Converter +public class InstantToUTCConverter implements AttributeConverter { + + @Override + public Date convertToDatabaseColumn(Instant attribute) { + if(attribute == null) return null; + return Date.from(attribute); + } + + @Override + public Instant convertToEntityAttribute(Date dbData) { + if (dbData == null) return null; + return dbData.toInstant(); + } +}