Skip to content

Commit

Permalink
Merge pull request #15 from kumuluz/feature/localdate-support
Browse files Browse the repository at this point in the history
Added support for local date
  • Loading branch information
zvonegit authored Nov 5, 2020
2 parents 15f708c + b1d29bc commit c03c1b8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 19 deletions.
38 changes: 36 additions & 2 deletions core/src/main/java/com/kumuluz/ee/rest/utils/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.format.DateTimeParseException;
import java.util.AbstractMap;
import java.util.Date;
Expand Down Expand Up @@ -114,6 +113,14 @@ protected static Object getValueForField(Field field, String value) {
if (c.equals(Instant.class))
return ZonedDateTime.parse(value).toInstant();

if (c.equals(LocalDate.class)) {
return LocalDate.parse(value);
}

if (c.equals(LocalDateTime.class)) {
return LocalDateTime.parse(value);
}

if (c.equals(Boolean.class))
return Boolean.parseBoolean(value);

Expand All @@ -130,4 +137,31 @@ protected static Object getValueForField(Field field, String value) {
return value;
}

protected static Object getTargetDateTypeValue(Date value, Class<?> clazzType) {

if (value == null) return null;

try {

if (clazzType.equals(Date.class))
return value;

if (clazzType.equals(Instant.class))
return value.toInstant();

if (clazzType.equals(LocalDate.class)) {
return value.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

if (clazzType.equals(LocalDateTime.class)) {
return value.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
} catch (IllegalArgumentException | DateTimeParseException e) {

throw new InvalidFieldValueException(e.getMessage(), "", value.toString());
}

return value;
}

}
66 changes: 49 additions & 17 deletions core/src/main/java/com/kumuluz/ee/rest/utils/StreamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -341,8 +342,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
switch (f.getOperation()) {

case EQ:
if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.EQ);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.EQ);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.EQ);
}
Expand All @@ -353,8 +354,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
}
break;
case NEQ:
if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.NEQ);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.NEQ);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.NEQ);
}
Expand Down Expand Up @@ -388,10 +389,12 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {

if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.GT);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.GT);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.GT);
}
Expand All @@ -401,10 +404,12 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {

if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.GTE);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.GTE);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.GTE);
}
Expand All @@ -414,10 +419,12 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {

if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.LT);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.LT);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.LT);
}
Expand All @@ -427,10 +434,12 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {

if (f.getDateValue() != null && clazzTarget.equals(Date.class)) {
np = filter(clazz, entityField, f.getDateValue(), FilterOperation.LTE);
if (f.getDateValue() != null && (clazzTarget.equals(Date.class) || clazzTarget.equals(Instant.class) || clazzTarget.equals(LocalDate.class) || clazzTarget.equals(LocalDateTime.class))) {
np = filter(clazz, entityField, getTargetDateTypeValue(f.getDateValue(), clazzTarget), FilterOperation.LTE);
} else if (f.getValue() != null) {
np = filter(clazz, entityField, getValueForField(field, f.getValue()), FilterOperation.LTE);
}
Expand Down Expand Up @@ -552,9 +561,9 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
} else if (value instanceof BigDecimal) {
return value.equals(new BigDecimal((String) fieldValue));
} else if (value instanceof LocalDate) {
return value.equals(LocalDate.parse((String) fieldValue));
return value.equals((LocalDate) fieldValue);
} else if (value instanceof LocalDateTime) {
return value.equals(LocalDateTime.parse((String) fieldValue));
return value.equals((LocalDateTime) fieldValue);
} else if (value instanceof Double) {
return value.equals(Double.parseDouble((String) fieldValue));
} else if (value instanceof Float) {
Expand Down Expand Up @@ -615,6 +624,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return !value.equals(Float.parseFloat((String) fieldValue));
} else if (value instanceof Long) {
return !value.equals(Long.parseLong((String) fieldValue));
} else if (value instanceof LocalDate) {
return !value.equals((LocalDate) fieldValue);
} else if (value instanceof LocalDateTime) {
return !value.equals((LocalDateTime) fieldValue);
} else if (value instanceof Boolean) {
return !value.equals(fieldValue);
} else if (value instanceof Byte) {
Expand Down Expand Up @@ -758,6 +771,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return ((Long) value).compareTo(Long.parseLong((String) fieldValue)) > 0;
} else if (value instanceof Boolean) {
return ((Boolean) value).compareTo((Boolean) fieldValue) > 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) > 0;
} else if (value instanceof LocalDateTime) {
return ((LocalDateTime) value).compareTo((LocalDateTime) fieldValue) > 0;
} else if (value instanceof Byte) {
return ((Byte) value).compareTo(Byte.parseByte((String) fieldValue)) > 0;
} else if (value instanceof Short) {
Expand Down Expand Up @@ -789,6 +806,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return ((Long) value).compareTo(Long.parseLong((String) fieldValue)) >= 0;
} else if (value instanceof Boolean) {
return ((Boolean) value).compareTo((Boolean) fieldValue) >= 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) >= 0;
} else if (value instanceof LocalDateTime) {
return ((LocalDateTime) value).compareTo((LocalDateTime) fieldValue) >= 0;
} else if (value instanceof Byte) {
return ((Byte) value).compareTo(Byte.parseByte((String) fieldValue)) >= 0;
} else if (value instanceof Short) {
Expand Down Expand Up @@ -821,6 +842,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return ((Long) value).compareTo(Long.parseLong((String) fieldValue)) < 0;
} else if (value instanceof Boolean) {
return ((Boolean) value).compareTo((Boolean) fieldValue) < 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) < 0;
} else if (value instanceof LocalDateTime) {
return ((LocalDateTime) value).compareTo((LocalDateTime) fieldValue) < 0;
} else if (value instanceof Byte) {
return ((Byte) value).compareTo(Byte.parseByte((String) fieldValue)) < 0;
} else if (value instanceof Short) {
Expand Down Expand Up @@ -854,6 +879,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return ((Long) value).compareTo(Long.parseLong((String) fieldValue)) <= 0;
} else if (value instanceof Boolean) {
return ((Boolean) value).compareTo((Boolean) fieldValue) <= 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) <= 0;
} else if (value instanceof LocalDateTime) {
return ((LocalDateTime) value).compareTo((LocalDateTime) fieldValue) <= 0;
} else if (value instanceof Byte) {
return ((Byte) value).compareTo(Byte.parseByte((String) fieldValue)) <= 0;
} else if (value instanceof Short) {
Expand Down Expand Up @@ -1010,7 +1039,8 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
private static <T> Comparator<T> comparator(Class<T> clazz, String fieldName, OrderDirection orderDirection, OrderNulls orderNulls) {
//optimization for sorting by children value
Map<Collection, Optional<Object>> minCollectionValCache = new HashMap<>();
return (T instance1, T instance2) -> compareInstanceFields(clazz, fieldName, instance1, instance2, orderDirection, orderNulls, minCollectionValCache);
return (T instance1, T instance2) -> compareInstanceFields(clazz, fieldName, instance1, instance2, orderDirection, orderNulls,
minCollectionValCache);
}

private static <T> int compareInstanceFields(Class<T> clazz, String fieldName, T instance1, T instance2, OrderDirection orderDirection,
Expand All @@ -1032,7 +1062,8 @@ private static <T> int compareInstanceFields(Class<T> clazz, String fieldName, T
Collection c1 = (Collection) value1;
Collection c2 = (Collection) value2;
if (c1 == null || c1.isEmpty() || c2 == null || c2.isEmpty()) {
return compare(c1 == null || c1.isEmpty() ? null : c1.size(), c2 == null || c2.isEmpty() ? null : c2.size(), orderDirection,
return compare(c1 == null || c1.isEmpty() ? null : c1.size(), c2 == null || c2.isEmpty() ? null : c2.size(),
orderDirection,
orderNulls);
}
if (fieldNames.length > 1) {
Expand Down Expand Up @@ -1067,7 +1098,8 @@ private static <T> int compareInstanceFields(Class<T> clazz, String fieldName, T

if (fieldNames.length > 1) {
String nextLevelFieldName = String.join(".", Arrays.copyOfRange(fieldNames, 1, fieldNames.length));
return compareInstanceFields(fieldClass, nextLevelFieldName, value1, value2, orderDirection, orderNulls, minCollectionValCache);
return compareInstanceFields(fieldClass, nextLevelFieldName, value1, value2, orderDirection, orderNulls,
minCollectionValCache);
}

return compare(value1, value2, orderDirection, orderNulls);
Expand Down

0 comments on commit c03c1b8

Please sign in to comment.