Refactor extract ebean-joda-time module - move joda ScalarTypes

Moves the joda-time ScalarTypes into a new ebean-joda-time module. Include
the ebean-joda-time module in the classpath, and the extra types will be
service loaded (by DefaultTypeManager).
This commit is contained in:
Rob Bygrave committed 2022-08-26 17:32:31 +12:00
1 parent 7b3405ca25
commit b7e62df371
22 files changed
+146 -108

No files matched your search

@@ -147,7 +147,6 @@ public final class DefaultTypeManager implements TypeManager {
initialiseStandard(config);
initialiseJavaTimeTypes(config);
initialiseJodaTypes(config);
initialiseJacksonTypes(config);
loadTypesFromProviders(config, objectMapper);
loadGeoTypeBinder(config);
@@ -261,7 +260,7 @@ public final class DefaultTypeManager implements TypeManager {
if (found == null) {
if (type.getName().equals("org.joda.time.LocalTime")) {
throw new IllegalStateException(
"ScalarType of Joda LocalTime not defined. You need to set DatabaseConfig.jodaLocalTimeMode to"
"ScalarType of Joda LocalTime not defined. 1) Check ebean-joda-time dependency has been added 2) Check DatabaseConfig.jodaLocalTimeMode is set to"
+ " either 'normal' or 'utc'. UTC is the old mode using UTC timezone but local time zone is now preferred as 'normal' mode.");
}
found = checkInheritedTypes(type);
@@ -798,36 +797,6 @@ public final class DefaultTypeManager implements TypeManager {
logicalMap.putIfAbsent(clazz.getSimpleName(), scalarType);
}
/**
* Detect if Joda classes are in the classpath and if so register the Joda data types.
*/
@SuppressWarnings("deprecation")
private void initialiseJodaTypes(DatabaseConfig config) {
// detect if Joda classes are in the classpath
if (config.getClassLoadConfig().isJodaTimePresent()) {
// Joda classes are in the classpath so register the types
log.log(DEBUG, "Registering Joda data types");
addType(LocalDateTime.class, new ScalarTypeJodaLocalDateTime(jsonDateTime));
addType(DateTime.class, new ScalarTypeJodaDateTime(jsonDateTime));
if (config.getDatabasePlatform().supportsNativeJavaTime()) {
addType(LocalDate.class, new ScalarTypeJodaLocalDateNative(jsonDate));
} else {
addType(LocalDate.class, new ScalarTypeJodaLocalDate(jsonDate));
}
addType(org.joda.time.DateMidnight.class, new ScalarTypeJodaDateMidnight(jsonDate));
addType(org.joda.time.Period.class, new ScalarTypeJodaPeriod());
String jodaLocalTimeMode = config.getJodaLocalTimeMode();
if ("normal".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the expected/normal local time zone
addType(LocalTime.class, new ScalarTypeJodaLocalTime());
} else if ("utc".equalsIgnoreCase(jodaLocalTimeMode)) {
// use the old UTC based
addType(LocalTime.class, new ScalarTypeJodaLocalTimeUTC());
}
}
}
/**
* Register all the standard types supported. This is the standard JDBC types
* plus some other common types such as java.util.Date and java.util.Calendar.
@@ -1,59 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDate;
import io.ebean.core.type.BasicTypeConverter;
import org.joda.time.DateMidnight;
import java.sql.Date;
import java.sql.Types;
/**
* ScalarType for Joda DateMidnight. This maps to a JDBC Date.
*/
@SuppressWarnings("deprecation")
final class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate<DateMidnight> {
/**
* Instantiates a new scalar type joda date midnight.
*/
ScalarTypeJodaDateMidnight(JsonConfig.Date mode) {
super(mode, org.joda.time.DateMidnight.class, false, Types.DATE);
}
@Override
protected String toIsoFormat(DateMidnight value) {
return value.toString();
}
@Override
public long convertToMillis(org.joda.time.DateMidnight value) {
return value.getMillis();
}
@Override
public org.joda.time.DateMidnight convertFromDate(Date ts) {
return new org.joda.time.DateMidnight(ts.getTime());
}
@Override
public Date convertToDate(org.joda.time.DateMidnight t) {
return new Date(t.getMillis());
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof org.joda.time.DateMidnight) {
return new Date(((org.joda.time.DateMidnight) value).getMillis());
}
return BasicTypeConverter.toDate(value);
}
@Override
public org.joda.time.DateMidnight toBeanType(Object value) {
if (value instanceof java.util.Date) {
return new org.joda.time.DateMidnight(((java.util.Date) value).getTime());
}
return (org.joda.time.DateMidnight) value;
}
}
@@ -1,72 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebean.core.type.BasicTypeConverter;
import org.joda.time.DateTime;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
/**
* ScalarType for Joda DateTime. This maps to a JDBC Timestamp.
*/
final class ScalarTypeJodaDateTime extends ScalarTypeBaseDateTime<DateTime> {
ScalarTypeJodaDateTime(JsonConfig.DateTime mode) {
super(mode, DateTime.class, false, Types.TIMESTAMP);
}
@Override
public long convertToMillis(DateTime value) {
return value.getMillis();
}
@Override
protected String toJsonNanos(DateTime value) {
return String.valueOf(value.toDateTime().getMillis());
}
@Override
protected String toJsonISO8601(DateTime value) {
return value.toString();
}
@Override
public DateTime convertFromMillis(long systemTimeMillis) {
return new DateTime(systemTimeMillis);
}
@Override
public DateTime convertFromTimestamp(Timestamp ts) {
return new DateTime(ts.getTime());
}
@Override
public DateTime convertFromInstant(Instant ts) {
return new DateTime(ts.toEpochMilli());
}
@Override
public Timestamp convertToTimestamp(DateTime t) {
return new Timestamp(t.getMillis());
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof DateTime) {
return new Timestamp(((DateTime) value).getMillis());
}
return BasicTypeConverter.toTimestamp(value);
}
@Override
public DateTime toBeanType(Object value) {
if (value instanceof java.util.Date) {
return new DateTime(((java.util.Date) value).getTime());
}
return (DateTime) value;
}
}
@@ -1,63 +0,0 @@
package io.ebeaninternal.server.type;
import java.sql.Date;
import java.sql.Types;
import io.ebean.core.type.ScalarTypeBaseDate;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.BasicTypeConverter;
/**
* ScalarType for Joda LocalDate. This maps to a JDBC Date.
*/
class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
ScalarTypeJodaLocalDate(JsonConfig.Date mode) {
super(mode, LocalDate.class, false, Types.DATE);
}
@Override
protected String toIsoFormat(LocalDate value) {
return value.toString();
}
@Override
public LocalDate convertFromMillis(long systemTimeMillis) {
return new LocalDate(systemTimeMillis);
}
@Override
public long convertToMillis(LocalDate value) {
return value.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();
}
@Override
public LocalDate convertFromDate(Date date) {
return LocalDate.fromDateFields(date);
}
@SuppressWarnings("deprecation")
@Override
public Date convertToDate(LocalDate value) {
return new Date(value.getYear() - 1900, value.getMonthOfYear() - 1, value.getDayOfMonth());
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof LocalDate) {
return convertToDate((LocalDate) value);
}
return BasicTypeConverter.toDate(value);
}
@Override
public LocalDate toBeanType(Object value) {
if (value instanceof java.util.Date) {
return LocalDate.fromDateFields((java.util.Date) value);
}
return (LocalDate) value;
}
}
@@ -1,35 +0,0 @@
package io.ebeaninternal.server.type;
import java.sql.SQLException;
import java.sql.Types;
import org.joda.time.LocalDate;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
/**
* ScalarType for Joda LocalDate. This maps to a LocalDate. Not all drivers/platforms may support this.
*/
final class ScalarTypeJodaLocalDateNative extends ScalarTypeJodaLocalDate{
ScalarTypeJodaLocalDateNative(JsonConfig.Date mode) {
super(mode);
}
@Override
public void bind(DataBinder binder, LocalDate value) throws SQLException {
if (value == null) {
binder.setNull(Types.DATE);
} else {
binder.setObject(java.time.LocalDate.of(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth()));
}
}
@Override
public LocalDate read(DataReader reader) throws SQLException {
java.time.LocalDate jtDate = reader.getObject(java.time.LocalDate.class);
return jtDate == null ? null : new org.joda.time.LocalDate(jtDate.getYear(), jtDate.getMonthValue(), jtDate.getDayOfMonth());
}
}
@@ -1,72 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.ScalarTypeBaseDateTime;
import io.ebean.core.type.BasicTypeConverter;
import org.joda.time.LocalDateTime;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
/**
* ScalarType for Joda LocalDateTime. This maps to a JDBC Timestamp.
*/
final class ScalarTypeJodaLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime> {
ScalarTypeJodaLocalDateTime(JsonConfig.DateTime mode) {
super(mode, LocalDateTime.class, false, Types.TIMESTAMP);
}
@Override
protected String toJsonNanos(LocalDateTime value) {
return String.valueOf(value.toDateTime().getMillis());
}
@Override
protected String toJsonISO8601(LocalDateTime value) {
return value.toString();
}
@Override
public long convertToMillis(LocalDateTime value) {
return value.toDateTime().getMillis();
}
@Override
public LocalDateTime convertFromMillis(long systemTimeMillis) {
return new LocalDateTime(systemTimeMillis);
}
@Override
public LocalDateTime convertFromTimestamp(Timestamp ts) {
return new LocalDateTime(ts.getTime());
}
@Override
public LocalDateTime convertFromInstant(Instant ts) {
return new LocalDateTime(ts.toEpochMilli());
}
@Override
public Timestamp convertToTimestamp(LocalDateTime t) {
return new Timestamp(t.toDateTime().getMillis());
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof LocalDateTime) {
return new Timestamp(((LocalDateTime) value).toDateTime().getMillis());
}
return BasicTypeConverter.toTimestamp(value);
}
@Override
public LocalDateTime toBeanType(Object value) {
if (value instanceof java.util.Date) {
return new LocalDateTime(((java.util.Date) value).getTime());
}
return (LocalDateTime) value;
}
}
@@ -1,118 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.DocPropertyType;
import io.ebean.core.type.ScalarTypeBase;
import io.ebean.core.type.BasicTypeConverter;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Types;
/**
* ScalarType for Joda LocalTime. This maps to a JDBC Time.
*/
class ScalarTypeJodaLocalTime extends ScalarTypeBase<LocalTime> {
ScalarTypeJodaLocalTime() {
super(LocalTime.class, false, Types.TIME);
}
@Override
public void bind(DataBinder binder, LocalTime value) throws SQLException {
if (value == null) {
binder.setNull(Types.TIME);
} else {
binder.setTime(new Time(value.toDateTimeToday().getMillis()));
}
}
@Override
public LocalTime read(DataReader reader) throws SQLException {
Time sqlTime = reader.getTime();
if (sqlTime == null) {
return null;
} else {
return new LocalTime(sqlTime, DateTimeZone.getDefault());
}
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof LocalTime) {
LocalTime lt = (LocalTime) value;
return new Time(lt.toDateTimeToday().getMillis());
}
return BasicTypeConverter.toTime(value);
}
@Override
public LocalTime toBeanType(Object value) {
if (value instanceof java.util.Date) {
return new LocalTime(value, DateTimeZone.getDefault());
}
return (LocalTime) value;
}
@Override
public String formatValue(LocalTime v) {
return v.toString();
}
@Override
public LocalTime parse(String value) {
return new LocalTime(value);
}
public LocalTime convertFromMillis(long systemTimeMillis) {
return new LocalTime(systemTimeMillis, DateTimeZone.getDefault());
}
@Override
public LocalTime readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
String val = dataInput.readUTF();
return parse(val);
}
}
@Override
public void writeData(DataOutput dataOutput, LocalTime value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
dataOutput.writeBoolean(true);
dataOutput.writeUTF(format(value));
}
}
@Override
public void jsonWrite(JsonGenerator writer, LocalTime value) throws IOException {
writer.writeString(value.toString());
}
@Override
public LocalTime jsonRead(JsonParser parser) throws IOException {
if (JsonToken.VALUE_NUMBER_INT == parser.getCurrentToken()) {
return convertFromMillis(parser.getLongValue());
} else {
return parse(parser.getValueAsString());
}
}
@Override
public DocPropertyType docType() {
return DocPropertyType.KEYWORD;
}
}
@@ -1,63 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebean.core.type.BasicTypeConverter;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Types;
/**
* ScalarType for Joda LocalTime. This maps to a JDBC Time.
*/
final class ScalarTypeJodaLocalTimeUTC extends ScalarTypeJodaLocalTime {
ScalarTypeJodaLocalTimeUTC() {
super();
}
@Override
public void bind(DataBinder binder, LocalTime value) throws SQLException {
if (value == null) {
binder.setNull(Types.TIME);
} else {
Time sqlTime = new Time(value.getMillisOfDay());
binder.setTime(sqlTime);
}
}
@Override
public LocalTime read(DataReader reader) throws SQLException {
Time sqlTime = reader.getTime();
if (sqlTime == null) {
return null;
} else {
return new LocalTime(sqlTime, DateTimeZone.UTC);
}
}
@Override
public Object toJdbcType(Object value) {
if (value instanceof LocalTime) {
return new Time(((LocalTime) value).getMillisOfDay());
}
return BasicTypeConverter.toTime(value);
}
@Override
public LocalTime toBeanType(Object value) {
if (value instanceof java.util.Date) {
return new LocalTime(value, DateTimeZone.UTC);
}
return (LocalTime) value;
}
@Override
public LocalTime convertFromMillis(long systemTimeMillis) {
return new LocalTime(systemTimeMillis);
}
}
@@ -1,40 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.core.type.ScalarTypeBaseVarchar;
import org.joda.time.Period;
/**
* ScalarType for Joda Period stored as DB VARCHAR
*/
final class ScalarTypeJodaPeriod extends ScalarTypeBaseVarchar<Period> {
ScalarTypeJodaPeriod() {
super(Period.class);
}
@Override
public int length() {
return 50;
}
@Override
public String formatValue(Period v) {
return v.toString();
}
@Override
public Period parse(String value) {
return Period.parse(value);
}
@Override
public Period convertFromDbString(String dbValue) {
return Period.parse(dbValue);
}
@Override
public String convertToDbString(Period beanValue) {
return beanValue.toString();
}
}
@@ -1,92 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.joda.time.LocalDate;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.sql.Date;
import java.util.TimeZone;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaLocalDateTest {
private ScalarTypeJodaLocalDate type = new ScalarTypeJodaLocalDate(JsonConfig.Date.MILLIS);
@Test
public void convertToMillis_convertFromMillis() {
LocalDate localDate = new LocalDate();
long millis = type.convertToMillis(localDate);
LocalDate localDate1 = type.convertFromMillis(millis);
assertThat(localDate).isEqualTo(localDate1);
}
@Test
public void convertToDate_convertFromDate_westOfUtc() {
final TimeZone originaDefaultTimezone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
convertDate(new LocalDate());
convertDate(new LocalDate(1899, 12, 1));
convertDate(new LocalDate(1900, 1, 1));
convertDate(new LocalDate(2021, 2, 8));
} finally {
TimeZone.setDefault(originaDefaultTimezone);
}
}
@Test
public void convertToDate_convertFromDate() {
convertDate(new LocalDate());
convertDate(new LocalDate(1899, 12, 1));
convertDate(new LocalDate(1900, 1, 1));
convertDate(new LocalDate(2021, 2, 8));
}
private void convertDate(LocalDate localDate) {
Date dateValue = type.convertToDate(localDate);
LocalDate localDate1 = type.convertFromDate(dateValue);
assertThat(localDate).isEqualTo(localDate1);
}
@Test
public void toJdbcType() {
LocalDate localDate = new LocalDate();
Object jdbcType = type.toJdbcType(localDate);
Date dateValue = type.convertToDate(localDate);
assertThat(jdbcType).isEqualTo(dateValue);
}
@Test
public void toBeanType() {
LocalDate localDate = new LocalDate();
Date dateValue = type.convertToDate(localDate);
LocalDate beanType = type.toBeanType(dateValue);
assertThat(beanType).isEqualTo(localDate);
}
@Test
public void json() throws IOException {
LocalDate val = new LocalDate(2019, 5, 9);
JsonTester<LocalDate> jsonMillis = new JsonTester<>(type);
assertThat(jsonMillis.test(val)).isEqualTo("{\"key\":1557360000000}");
JsonTester<LocalDate> jsonIso = new JsonTester<>(new ScalarTypeJodaLocalDate(JsonConfig.Date.ISO8601) );
assertThat(jsonIso.test(val)).isEqualTo("{\"key\":\"2019-05-09\"}");
}
}
@@ -1,43 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.junit.jupiter.api.Test;
import java.sql.Timestamp;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScalarTypeJodaLocalDateTimeTest {
ScalarTypeJodaLocalDateTime type = new ScalarTypeJodaLocalDateTime(JsonConfig.DateTime.ISO8601);
@Test
public void testConvertFromTimestamp() throws Exception {
long now = System.currentTimeMillis();
Timestamp nowTs = new Timestamp(now);
LocalDateTime ldt1 = type.convertFromTimestamp(nowTs);
LocalDateTime ldt2 = localConvertFromTimestamp(nowTs);
assertEquals(ldt1, ldt2);
Timestamp ts1 = type.convertToTimestamp(ldt1);
Timestamp ts2 = localConvertToTimestamp(ldt2);
assertEquals(ts1, ts2);
}
LocalDateTime localConvertFromTimestamp(Timestamp ts) {
return new LocalDateTime(ts.getTime(), DateTimeZone.getDefault());
}
Timestamp localConvertToTimestamp(LocalDateTime t) {
return new Timestamp(t.toDateTime(DateTimeZone.getDefault()).getMillis());
}
}
@@ -1,64 +0,0 @@
package io.ebeaninternal.server.type;
import io.ebean.DB;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tests.model.basic.TJodaEntity;
import java.sql.Timestamp;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScalarTypeJodaLocalTimeTest {
ScalarTypeJodaLocalTime type = new ScalarTypeJodaLocalTime();
@Test
public void toJdbcType_toBeanType() {
LocalTime localTime0 = new LocalTime().withMillisOfSecond(0);
Object time = type.toJdbcType(localTime0);
LocalTime localTime1 = type.toBeanType(time);
assertThat(localTime0).isEqualTo(localTime1);
}
@Test
public void test() {
long now = System.currentTimeMillis();
//DateTimeZone timeZone = DateTimeZone.getDefault();
//ISOChronology instance = ISOChronology.getInstance();
LocalDateTime ldt1 = new LocalDateTime(now, DateTimeZone.getDefault());
LocalDateTime ldt2 = new LocalDateTime(now);
assertEquals(ldt1, ldt2);
Timestamp ts1 = new Timestamp(ldt1.toDateTime(DateTimeZone.getDefault()).getMillis());
Timestamp ts2 = new Timestamp(ldt2.toDateTime().getMillis());
assertEquals(ts1, ts2);
}
@Test
public void toJson() {
LocalTime now = new LocalTime();
TJodaEntity bean = new TJodaEntity();
bean.setId(42);
bean.setLocalTime(now);
String json = DB.json().toJson(bean);
TJodaEntity bean1 = DB.json().toBean(TJodaEntity.class, json);
Assertions.assertEquals(bean1.getLocalTime(), now);
}
}
@@ -1,43 +0,0 @@
package io.ebeaninternal.server.type;
import org.joda.time.Period;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ScalarTypeJodaPeriodTest {
private ScalarTypeJodaPeriod scalarType = new ScalarTypeJodaPeriod();
@Test
public void getLength() {
assertThat(scalarType.length()).isEqualTo(50);
}
@Test
public void formatAndParse() {
Period original = Period.years(1).plusMonths(2).plusDays(4)
.plusHours(12).plusMinutes(19).plusSeconds(20);
String value = scalarType.formatValue(original);
assertThat(value).isEqualTo("P1Y2M4DT12H19M20S");
Period period = scalarType.parse(value);
assertThat(period).isEqualTo(original);
}
@Test
public void convertFromDbString() {
Period original = Period.years(1).plusMonths(2).plusDays(4)
.plusHours(23).plusMinutes(19).plusSeconds(20).plusMillis(987);
String stringVal = scalarType.convertToDbString(original);
Period period = scalarType.convertFromDbString(stringVal);
assertThat(period).isEqualTo(original);
}
}
@@ -1,32 +0,0 @@
package org.tests.model.basic;
import org.joda.time.LocalTime;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class TJodaEntity {
@Id
Integer id;
LocalTime localTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LocalTime getLocalTime() {
return localTime;
}
public void setLocalTime(LocalTime localTime) {
this.localTime = localTime;
}
}