mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
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:
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;
|
||||
}
|
||||
}
|
||||
-35
@@ -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());
|
||||
}
|
||||
}
|
||||
-72
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user