Skip to content

Commit

Permalink
Merge pull request #23 from kumuluz/fix/issue-22
Browse files Browse the repository at this point in the history
Fix/issue 22
  • Loading branch information
osbeorn authored Nov 22, 2021
2 parents 8561656 + a50f56b commit 94d453e
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 201 deletions.
22 changes: 17 additions & 5 deletions core/src/main/java/com/kumuluz/ee/rest/utils/JPAUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static <T> Long queryEntitiesCount(EntityManager em, Class<T> entity, Que
cq.where(wherePredicate);
}

cq.select(cb.count(r)).distinct(requiresDistinct);
cq.select(requiresDistinct ? cb.countDistinct(r) : cb.count(r));

return em.createQuery(cq).getSingleResult();
}
Expand Down Expand Up @@ -1028,13 +1028,22 @@ private static Object getValueForPath(Path path, String value) {

try {

if (c.equals(Date.class))
if (c.equals(Date.class)) {
return Date.from(ZonedDateTime.parse(value).toInstant());
}

if (c.equals(Instant.class)) {
return ZonedDateTime.parse(value).toInstant();
}

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

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

if (c.equals(LocalDate.class)) {
return LocalDate.parse(value);
}
Expand All @@ -1051,14 +1060,17 @@ private static Object getValueForPath(Path path, String value) {
return ZonedDateTime.parse(value);
}

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

if (c.isEnum())
if (c.isEnum()) {
return Enum.valueOf(c, value);
}

if (c.equals(UUID.class))
if (c.equals(UUID.class)) {
return UUID.fromString(value);
}
} catch (IllegalArgumentException | DateTimeParseException e) {

throw new InvalidFieldValueException(e.getMessage(), path.getAlias(), value);
Expand Down
37 changes: 33 additions & 4 deletions core/src/main/java/com/kumuluz/ee/rest/utils/StreamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.*;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -389,6 +386,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalTime.class.isAssignableFrom(clazz) ||
OffsetTime.class.isAssignableFrom(clazz) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {
Expand All @@ -404,6 +403,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalTime.class.isAssignableFrom(clazz) ||
OffsetTime.class.isAssignableFrom(clazz) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {
Expand All @@ -419,6 +420,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalTime.class.isAssignableFrom(clazz) ||
OffsetTime.class.isAssignableFrom(clazz) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {
Expand All @@ -434,6 +437,8 @@ private static StreamCriteriaWhereQuery createWhereQueryInternal(Class<?> clazz,
if (Date.class.isAssignableFrom(clazzTarget) ||
Instant.class.isAssignableFrom(clazzTarget) ||
Number.class.isAssignableFrom(clazzTarget) ||
LocalTime.class.isAssignableFrom(clazz) ||
OffsetTime.class.isAssignableFrom(clazz) ||
LocalDate.class.isAssignableFrom(clazzTarget) ||
LocalDateTime.class.isAssignableFrom(clazzTarget) ||
String.class.isAssignableFrom(clazzTarget)) {
Expand Down Expand Up @@ -560,6 +565,10 @@ private static <T> Predicate<T> filter(Class<T> clazz, String fieldName, Object
return value.equals(fieldValue);
} else if (value instanceof BigDecimal) {
return value.equals(new BigDecimal((String) fieldValue));
} else if (value instanceof LocalTime) {
return value.equals((LocalTime) fieldValue);
} else if (value instanceof OffsetTime) {
return value.equals((OffsetTime) fieldValue);
} else if (value instanceof LocalDate) {
return value.equals((LocalDate) fieldValue);
} else if (value instanceof LocalDateTime) {
Expand Down Expand Up @@ -624,6 +633,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 LocalTime) {
return !value.equals((LocalTime) fieldValue);
} else if (value instanceof OffsetTime) {
return !value.equals((OffsetTime) fieldValue);
} else if (value instanceof LocalDate) {
return !value.equals((LocalDate) fieldValue);
} else if (value instanceof LocalDateTime) {
Expand Down Expand Up @@ -771,6 +784,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 LocalTime) {
return ((LocalTime) value).compareTo((LocalTime) fieldValue) > 0;
} else if (value instanceof OffsetTime) {
return ((OffsetTime) value).compareTo((OffsetTime) fieldValue) > 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) > 0;
} else if (value instanceof LocalDateTime) {
Expand Down Expand Up @@ -806,6 +823,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 LocalTime) {
return ((LocalTime) value).compareTo((LocalTime) fieldValue) >= 0;
} else if (value instanceof OffsetTime) {
return ((OffsetTime) value).compareTo((OffsetTime) fieldValue) >= 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) >= 0;
} else if (value instanceof LocalDateTime) {
Expand Down Expand Up @@ -842,6 +863,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 LocalTime) {
return ((LocalTime) value).compareTo((LocalTime) fieldValue) >= 0;
} else if (value instanceof OffsetTime) {
return ((OffsetTime) value).compareTo((OffsetTime) fieldValue) >= 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) < 0;
} else if (value instanceof LocalDateTime) {
Expand Down Expand Up @@ -879,6 +904,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 LocalTime) {
return ((LocalTime) value).compareTo((LocalTime) fieldValue) >= 0;
} else if (value instanceof OffsetTime) {
return ((OffsetTime) value).compareTo((OffsetTime) fieldValue) >= 0;
} else if (value instanceof LocalDate) {
return ((LocalDate) value).compareTo((LocalDate) fieldValue) <= 0;
} else if (value instanceof LocalDateTime) {
Expand Down
17 changes: 17 additions & 0 deletions core/src/test/java/com/kumuluz/ee/rest/test/JPAUtilsCountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,21 @@ public void testQueryCount() {
Assert.assertNotNull(count);
Assert.assertEquals(2, count.longValue());
}

@Test
public void testQueryCount2() {

QueryParameters q = new QueryParameters();

QueryFilter qf = new QueryFilter();
qf.setField("projects.status");
qf.setOperation(FilterOperation.EQ);
qf.setValue("ACTIVE");
q.getFilters().add(qf);

Long count = JPAUtils.queryEntitiesCount(em, User.class, q);

Assert.assertNotNull(count);
Assert.assertEquals(39, count.longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kumuluz.ee.rest.enums.FilterOperation;
import com.kumuluz.ee.rest.enums.OrderDirection;
import com.kumuluz.ee.rest.test.entities.Event;
import com.kumuluz.ee.rest.test.entities.User;
import com.kumuluz.ee.rest.test.utils.JpaUtil;
import com.kumuluz.ee.rest.utils.JPAUtils;
import org.junit.Assert;
Expand Down Expand Up @@ -208,4 +209,23 @@ public void testFilterEQZonedDateTime() {
Assert.assertEquals(1, events.size());
Assert.assertEquals(Integer.valueOf(1), events.get(0).getId());
}

@Test
public void testFilterEQTime() {

//Match with 06:20:11
QueryFilter qf = new QueryFilter();
qf.setField("registrationTime");
qf.setOperation(FilterOperation.EQ);
qf.setValue("06:20:11");

QueryParameters q = new QueryParameters();
q.getFilters().add(qf);

List<User> users = JPAUtils.queryEntities(em, User.class, q);

Assert.assertNotNull(users);
Assert.assertEquals(1, users.size());
Assert.assertEquals(Integer.valueOf(2), users.get(0).getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Collection<EntityManager> data() {
@Test
public void testEmptyQueryCount() {

List<User> users = em.createNamedQuery("User.getAll").getResultList();
List<User> users = em.createNamedQuery("User.getAll", User.class).getResultList();

Long count = StreamUtils.queryEntitiesCount(users);

Expand Down Expand Up @@ -67,11 +67,30 @@ public void testQueryCount() {
qf.setValue("%ina");
q.getFilters().add(qf);

List<User> users = em.createNamedQuery("User.getAll").getResultList();
List<User> users = em.createNamedQuery("User.getAll", User.class).getResultList();

Long count = StreamUtils.queryEntitiesCount(users, q);

Assert.assertNotNull(count);
Assert.assertEquals( 2L, count.longValue());
}

@Test
public void testQueryCount2() {

QueryParameters q = new QueryParameters();

QueryFilter qf = new QueryFilter();
qf.setField("projects.status");
qf.setOperation(FilterOperation.EQ);
qf.setValue("ACTIVE");
q.getFilters().add(qf);

List<User> users = em.createNamedQuery("User.getAll", User.class).getResultList();

Long count = StreamUtils.queryEntitiesCount(users, q);

Assert.assertNotNull(count);
Assert.assertEquals(39, count.longValue());
}
}
Loading

0 comments on commit 94d453e

Please sign in to comment.