Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Suporte a consulta por operadores relacionais (>,<,>=,<=) #124

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 177 additions & 15 deletions demoiselle-crud/src/main/java/org/demoiselle/jee/crud/AbstractDAO.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
Expand All @@ -27,19 +32,24 @@
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;
import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.time.DateUtils;
import org.demoiselle.jee.core.api.crud.Crud;
import org.demoiselle.jee.core.api.crud.Result;
import org.demoiselle.jee.crud.exception.DemoiselleCrudException;
import org.demoiselle.jee.crud.pagination.PaginationHelperConfig;
import org.demoiselle.jee.crud.pagination.ResultSet;
import org.demoiselle.jee.crud.sort.CrudSort;
import org.demoiselle.jee.crud.sort.SortModel;

@TransactionAttribute(TransactionAttributeType.MANDATORY)
public abstract class AbstractDAO<T, I> implements Crud<T, I> {
Expand All @@ -55,6 +65,12 @@ public abstract class AbstractDAO<T, I> implements Crud<T, I> {
protected abstract EntityManager getEntityManager();

private Logger logger = Logger.getLogger(this.getClass().getName());

private static final String ISO8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS";

private static final String ISO8601_UTC_PATTERN = ISO8601_PATTERN + "'Z'";

private static final TimeZone timeZoneUTC = TimeZone.getTimeZone("UTC");

@SuppressWarnings("unchecked")
public AbstractDAO() {
Expand Down Expand Up @@ -204,37 +220,47 @@ protected void configureOrder(CriteriaBuilder criteriaBuilder, CriteriaQuery<T>
drc.getSorts().stream().forEachOrdered(sortModel -> {

if (sortModel.getType().equals(CrudSort.ASC)) {
orders.add(criteriaBuilder.asc(root.get(sortModel.getField())));
orders.add(criteriaBuilder.asc(buildSortPath(criteriaBuilder, criteriaQuery, root, sortModel)));
} else {
orders.add(criteriaBuilder.desc(root.get(sortModel.getField())));
orders.add(criteriaBuilder.desc(buildSortPath(criteriaBuilder, criteriaQuery, root, sortModel)));
}
});

criteriaQuery.orderBy(orders);
}

}

protected Path<?> buildSortPath(CriteriaBuilder criteriaBuilder, CriteriaQuery<T> criteriaQuery, Root<T> root, SortModel model) {
if (CrudUtilHelper.hasSubField(model.getField())) {
String[] attrs = model.getField().split("[()]");
//
return root.get(attrs[0]).get(attrs[1]);
} else {
return root.get(model.getField());
}
}

protected Predicate[] buildPredicates(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, Root<T> root) {
List<Predicate> predicates = new LinkedList<>();

if (drc.getFilters() != null) {
drc.getFilters().getChildren().stream().forEach(child -> {

List<Predicate> predicatesToBuild = new LinkedList<>();

/*
* If the child doesnt child the element is on fist level.
*
* ?description=test
*
*/
if (child.getChildren().isEmpty()) {
List<Predicate> predicatesToBuild = new LinkedList<>();

child.getValue().stream().forEach(value -> {
fillPredicates(predicatesToBuild, root, criteriaBuilder, criteriaQuery, child, value, null);
});

predicates.add(criteriaBuilder.or(predicatesToBuild.toArray(new Predicate[]{})));
}
else{

Expand All @@ -243,23 +269,55 @@ protected Predicate[] buildPredicates(CriteriaBuilder criteriaBuilder, CriteriaQ
*
* ?category(description)=test
*/

Join<?, ?> join = root.join(child.getKey());
child.getChildren().stream().forEach( child2ndLevel -> {

child2ndLevel.getValue().stream().forEach(value -> {
fillPredicates(predicatesToBuild, join, criteriaBuilder, criteriaQuery, child2ndLevel, value, child);
});
});

if (isCollectionField(child.getKey())) {
/*
* Builds an Exists predicate for filters on entity collections fields.
*/
predicates.add(buildExistsPredicate(criteriaBuilder, criteriaQuery, root, child));
} else {
Join<?, ?> join = root.join(child.getKey());

child.getChildren().stream().forEach( child2ndLevel -> {
List<Predicate> predicatesToBuild = new LinkedList<>();

child2ndLevel.getValue().stream().forEach(value -> {
fillPredicates(predicatesToBuild, join, criteriaBuilder, criteriaQuery, child2ndLevel, value, child);
});

predicates.add(criteriaBuilder.or(predicatesToBuild.toArray(new Predicate[]{})));
});
}
}

predicates.add(criteriaBuilder.or(predicatesToBuild.toArray(new Predicate[]{})));
});
}

return predicates.toArray(new Predicate[]{});
}

@SuppressWarnings({"rawtypes", "unchecked"})
private Predicate buildExistsPredicate(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, Root<T> root, TreeNodeField<String, Set<String>> parent) {
Subquery<?> subquery = criteriaQuery.subquery(Integer.class);
Root<?> subqueryRoot = subquery.correlate(root);
Join<?, ?> join = subqueryRoot.join(parent.getKey());

List<Predicate> subqueryPredicates = new LinkedList<>();

parent.getChildren().stream().forEach(child2ndLevel -> {
List<Predicate> predicatesToBuild = new LinkedList<>();

child2ndLevel.getValue().stream().forEach(value -> {
fillPredicates(predicatesToBuild, join, criteriaBuilder, criteriaQuery, child2ndLevel, value, parent);
});

subqueryPredicates.add(criteriaBuilder.or(predicatesToBuild.toArray(new Predicate[] {})));
});

subquery.select((Expression)criteriaBuilder.literal(1)).where(subqueryPredicates.toArray(new Predicate[] {}));

return criteriaBuilder.exists(subquery);
}

private void fillPredicates(List<Predicate> predicates, From<?, ?> from, CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, TreeNodeField<String, Set<String>> child, String value, TreeNodeField<String, Set<String>> parent) {

if ("null".equals(value) || value == null) {
Expand All @@ -268,12 +326,18 @@ private void fillPredicates(List<Predicate> predicates, From<?, ?> from, Criter
predicates.add(criteriaBuilder.isEmpty(from.get(child.getKey())));
} else if (isLikeFilter(value)) {
predicates.add(buildLikePredicate(criteriaBuilder, criteriaQuery, from, child.getKey(), value));
} else if (isRelationalFilter(child.getKey(), value)) {
predicates.add(buildRelationalPredicate(criteriaBuilder, criteriaQuery, from, child.getKey(), value, parent));
} else if (isIntervalFilter(value)) {
predicates.add(buildIntervalPredicate(criteriaBuilder, criteriaQuery, from, child.getKey(), value, parent));
} else if ("isTrue".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value)) {
predicates.add(criteriaBuilder.isTrue(from.get(child.getKey())));
} else if ("isFalse".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
predicates.add(criteriaBuilder.isFalse(from.get(child.getKey())));
} else if (isEnumFilter(child.getKey(), value, parent)) {
predicates.add(criteriaBuilder.equal(from.get(child.getKey()), convertEnumToInt(child.getKey(), value, parent)));
} else if (isDateFilter(child.getKey(), value)) {
predicates.add(criteriaBuilder.equal(from.get(child.getKey()), convertStringToDate(child.getKey(), value)));
} else if (isUUIDFilter(child.getKey(), value, parent)) {
predicates.add(criteriaBuilder.equal(from.get(child.getKey()), UUID.fromString(value)));
} else {
Expand Down Expand Up @@ -399,10 +463,51 @@ private List<Field> getAllFields(Class<?> clazz){
List<Field> fields = new ArrayList<>();
return CrudUtilHelper.getAllFields(fields, clazz);
}

protected Date convertStringToDate(String key, String value) {
final String pattern = getDatePattern(value);
final SimpleDateFormat formatter = new SimpleDateFormat(pattern);
//
if (ISO8601_UTC_PATTERN.equals(pattern)) {
formatter.setTimeZone(timeZoneUTC);
}
//
try {
return formatter.parse(value);
} catch (final ParseException e) {
throw new DemoiselleCrudException(String.format("Não foi possível converter a string (%s) para uma data", value), e);
}
}

protected Boolean isLikeFilter(String value) {
return value.startsWith("*") || value.endsWith("*");
}

protected Boolean isIntervalFilter(String value) {
return value.matches("^(\\(|\\[).+\\.{3}.+(\\)|\\])$"); //e.g. [2018...2019]
}

protected boolean isRelationalFilter(String key, String value) {
return value.startsWith(">") || value.startsWith("<");
}

protected boolean isDateFilter(String key, String value) {
try {
DateUtils.parseDate(value, getDatePattern(value));
//
return true;
} catch (final ParseException e) {
return false;
}
}

protected String getDatePattern(String date) {
if (date.endsWith("Z")) {
return ISO8601_UTC_PATTERN;
} else {
return ISO8601_PATTERN;
}
}

protected Predicate buildLikePredicate(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, From<?, ?> root, String key, String value) {
String pattern = value.trim();
Expand All @@ -416,6 +521,59 @@ protected Predicate buildLikePredicate(CriteriaBuilder criteriaBuilder, Criteria
//
return criteriaBuilder.like(criteriaBuilder.lower(root.get(key)), pattern.toLowerCase());
}

@SuppressWarnings("unchecked")
protected Predicate buildIntervalPredicate(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, From<?, ?> root, String key, String value, TreeNodeField<String, Set<String>> tnf) {
final String pattern = value.trim();
final String op1 = pattern.substring(0, 1);
final String op2 = pattern.substring(pattern.length() -1);
final String value1 = pattern.substring(1, pattern.indexOf("..."));
final String value2 = pattern.substring(pattern.indexOf("...") + 3, pattern.length() -1);
//
final Predicate p1;
final Predicate p2;
//
if ("[".equals(op1)) {
p1 = criteriaBuilder.greaterThanOrEqualTo(root.get(key), (Comparable<Object>)convertToAppropriateType(key, value1, tnf));
} else {
p1 = criteriaBuilder.greaterThan(root.get(key), (Comparable<Object>)convertToAppropriateType(key, value1, tnf));
}
//
if ("]".equals(op2)) {
p2 = criteriaBuilder.lessThanOrEqualTo(root.get(key), (Comparable<Object>)convertToAppropriateType(key, value2, tnf));
} else {
p2 = criteriaBuilder.lessThan(root.get(key), (Comparable<Object>)convertToAppropriateType(key, value2, tnf));
}
//
return criteriaBuilder.and(p1, p2);
}

@SuppressWarnings("unchecked")
protected Predicate buildRelationalPredicate(CriteriaBuilder criteriaBuilder, CriteriaQuery<?> criteriaQuery, From<?, ?> root, String key, String value, TreeNodeField<String, Set<String>> tnf) {
final String pattern = value.trim();
//
if (pattern.startsWith(">=")) {
return criteriaBuilder.greaterThanOrEqualTo(root.get(key), (Comparable<Object>)convertToAppropriateType(key, pattern.substring(2), tnf));
} else if (pattern.startsWith("<=")) {
return criteriaBuilder.lessThanOrEqualTo(root.get(key), (Comparable<Object>)convertToAppropriateType(key, pattern.substring(2), tnf));
} else if (pattern.startsWith(">")) {
return criteriaBuilder.greaterThan(root.get(key), (Comparable<Object>)convertToAppropriateType(key, pattern.substring(1), tnf));
} else if (pattern.startsWith("<")) {
return criteriaBuilder.lessThan(root.get(key), (Comparable<Object>)convertToAppropriateType(key, pattern.substring(1), tnf));
} else {
throw new DemoiselleCrudException("Operador relacional não encontrado no filtro");
}
}

protected Comparable<?> convertToAppropriateType(String key, String value, TreeNodeField<String, Set<String>> tnf) {
if (isEnumFilter(key, value, tnf)) {
return convertEnumToInt(key, value, tnf);
} else if (isDateFilter(key, value)) {
return convertStringToDate(key, value);
} else {
return value;
}
}

protected Integer getMaxResult() {
if (drc.getLimit() == null || drc.getOffset() == null) {
Expand Down Expand Up @@ -454,5 +612,9 @@ public DemoiselleRequestContext getDrc() {
public Class<T> getEntityClass() {
return entityClass;
}


private boolean isCollectionField(String name) {
return getAllFields(getEntityClass()).stream()
.anyMatch(f -> f.getName().equalsIgnoreCase(name) && Collection.class.isAssignableFrom(f.getType()));
}
}
32 changes: 27 additions & 5 deletions demoiselle-crud/src/main/java/org/demoiselle/jee/crud/CrudUtilHelper.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.persistence.Id;

import javax.ws.rs.container.ResourceInfo;
Expand Down Expand Up @@ -114,13 +117,26 @@ public static List<String> extractFields(String fields) {
int lastComma = 0;
int lastPosition = 0;
String subField = "";

int escapedCommas = 0;

char fieldsArray[] = fields.toCharArray();

for (int i = 0; i < fieldsArray.length; i++) {

char letter = fieldsArray[i];

char nextLetter = i + 1 < fieldsArray.length ? fieldsArray[i + 1] : Character.MIN_VALUE;

if (letter == ',') {
if (nextLetter == ',' && escapedCommas == 0) {
escapedCommas++;
continue;
} else if (escapedCommas > 0) {
escapedCommas = 0;
continue;
}
}

// Find util the next ',' character
if (letter == ',') {
lastComma = i;
Expand Down Expand Up @@ -177,7 +193,7 @@ public static List<String> extractFields(String fields) {
results.add(subField);
}

return results;
return results.stream().map(s -> s.replace(",,", ",")).collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -315,7 +331,13 @@ public static void validateFields(TreeNodeField<String, Set<String>> tnf, Resour
throw new IllegalArgumentException(crudMessage.fieldRequestDoesNotExistsOnObject(leaf.getKey(), targetClass.getName()));
}

Class<?> fieldClazz = fieldMaster.getType();
Class<?> fieldClazz;

if (Collection.class.isAssignableFrom(fieldMaster.getType())) {
fieldClazz = (Class<?>)((ParameterizedType)fieldMaster.getGenericType()).getActualTypeArguments()[0];
} else {
fieldClazz = fieldMaster.getType();
}

leaf.getChildren().forEach((subLeaf) -> {
try {
Expand All @@ -338,7 +360,7 @@ public static void validateFields(TreeNodeField<String, Set<String>> tnf, Resour

}

private static Boolean hasSubField(String field) {
public static Boolean hasSubField(String field) {
Pattern patternLevels = Pattern.compile("\\([^)]*\\)*");
Matcher matcher = patternLevels.matcher(field);

Expand All @@ -358,4 +380,4 @@ public static String getMethodAnnotatedWithID(Class<?> targetClass) {



}
}
Loading