Adding converter for Instants to Dates

This commit is contained in:
Thomas Georgios Giannos 2023-11-20 17:11:57 +02:00
parent 3a033a655b
commit caf7ec09c4
2 changed files with 27 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import eu.eudat.commons.enums.DmpStatus;
import eu.eudat.commons.enums.DmpVersionStatus; import eu.eudat.commons.enums.DmpVersionStatus;
import eu.eudat.commons.enums.IsActive; import eu.eudat.commons.enums.IsActive;
import eu.eudat.data.converters.DateToUTCConverter; 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.DmpAccessTypeConverter;
import eu.eudat.data.converters.enums.DmpStatusConverter; import eu.eudat.data.converters.enums.DmpStatusConverter;
import eu.eudat.data.converters.enums.IsActiveConverter; import eu.eudat.data.converters.enums.IsActiveConverter;
@ -63,13 +64,13 @@ public class DmpEntity implements DataEntity<DmpEntity, UUID> {
public static final String _description = "description"; public static final String _description = "description";
@Column(name = "created_at") @Column(name = "created_at")
@Convert(converter = DateToUTCConverter.class) @Convert(converter = InstantToUTCConverter.class)
private Instant createdAt; private Instant createdAt;
public static final String _createdAt = "createdAt"; public static final String _createdAt = "createdAt";
@Column(name = "updated_at") @Column(name = "updated_at")
@Convert(converter = DateToUTCConverter.class) @Convert(converter = InstantToUTCConverter.class)
private Instant updatedAt; private Instant updatedAt;
public static final String _updatedAt = "updatedAt"; public static final String _updatedAt = "updatedAt";
@ -81,6 +82,7 @@ public class DmpEntity implements DataEntity<DmpEntity, UUID> {
public static final String _isActive = "isActive"; public static final String _isActive = "isActive";
@Column(name = "finalized_at") @Column(name = "finalized_at")
@Convert(converter = InstantToUTCConverter.class)
private Instant finalizedAt; private Instant finalizedAt;
public static final String _finalizedAt = "finalizedAt"; public static final String _finalizedAt = "finalizedAt";

View File

@ -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<Instant, Date> {
@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();
}
}