Skip to content

Commit 7841a02

Browse files
wog48GitHub Enterprise
authored andcommitted
Additional standard conversions (#382)
1 parent 3361789 commit 7841a02

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

jpa/odata-jpa-processor-cb/src/main/java/com/sap/olingo/jpa/processor/cb/impl/TypeConverter.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ static Object convert(final Object source, final Class<?> target) { // NOSONAR
4646
}
4747
if (target == Duration.class) {
4848
return convertDuration(source);
49+
}
50+
if (target == Timestamp.class) {
51+
return convertTimestamp(source);
4952
} else {
5053
LOGGER.debug("No converter found to convert " + source.getClass().getSimpleName() + " to " + target
5154
.getSimpleName());
@@ -61,7 +64,7 @@ private static Character convertToCharacter(final String source) {
6164
LOGGER.debug("Implicit conversion to Character from String only supported if String not longer than 1");
6265
throw new IllegalArgumentException("String to long");
6366
}
64-
if (source.length() == 0)
67+
if (source.isEmpty())
6568
return ' ';
6669
return source.charAt(0);
6770
}
@@ -138,6 +141,17 @@ private static OffsetDateTime convertTemporalToOffsetDateTime(final Object sourc
138141
return OffsetDateTime.parse((String) source);
139142
}
140143

144+
private static Timestamp convertTimestamp(Object source) {
145+
if (source instanceof LocalDateTime ldt)
146+
return Timestamp.valueOf(ldt);
147+
if (source instanceof LocalDate ld)
148+
return Timestamp.valueOf(LocalDateTime.of(ld, LocalTime.of(0, 0)));
149+
if (source instanceof String s)
150+
return Timestamp.valueOf(s);
151+
LOGGER.debug("No converter found to convert " + source.getClass().getSimpleName() + " to Timestamp");
152+
throw new IllegalArgumentException(createCastException(source, Timestamp.class));
153+
}
154+
141155
public static Class<?> boxed(final Class<?> javaType) {
142156
if (javaType == int.class) return Integer.class;
143157
if (javaType == long.class) return Long.class;

jpa/odata-jpa-processor-cb/src/test/java/com/sap/olingo/jpa/processor/cb/impl/TypeConverterTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ static Stream<Arguments> temporalConversion() {
152152
arguments(LocalDateTime.parse("2007-12-03T10:15:30.05"), "2007-12-03T10:15:30.05", LocalDateTime.class),
153153
arguments(OffsetTime.parse("10:15:30+01:00"), "10:15:30+01:00", OffsetTime.class),
154154
arguments(OffsetDateTime.parse("2007-12-03T10:15:30+01:00"), "2007-12-03T10:15:30+01:00",
155-
OffsetDateTime.class));
155+
OffsetDateTime.class),
156+
arguments(Timestamp.valueOf("2007-12-03 10:15:30.05"), LocalDateTime.parse("2007-12-03T10:15:30.05"),
157+
Timestamp.class),
158+
arguments(Timestamp.valueOf("2007-12-03 00:00:00"), LocalDate.parse("2007-12-03"),
159+
Timestamp.class),
160+
arguments(Timestamp.valueOf("2007-12-03 10:15:30.05"), "2007-12-03 10:15:30.05", Timestamp.class));
156161
}
157162

158163
static Stream<Arguments> durationConversion() {

0 commit comments

Comments
 (0)