|
| 1 | +package com.perplexhub.rsql.jpa; |
| 2 | + |
| 3 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.EQUAL; |
| 4 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.GREATER_THAN; |
| 5 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.GREATER_THAN_OR_EQUAL; |
| 6 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.IN; |
| 7 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.LESS_THAN; |
| 8 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.LESS_THAN_OR_EQUAL; |
| 9 | +import static cz.jirutka.rsql.parser.ast.RSQLOperators.NOT_EQUAL; |
| 10 | + |
| 11 | +import java.lang.reflect.Constructor; |
| 12 | +import java.time.LocalDate; |
| 13 | +import java.time.LocalDateTime; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import javax.persistence.criteria.CriteriaBuilder; |
| 18 | +import javax.persistence.criteria.Path; |
| 19 | +import javax.persistence.criteria.Predicate; |
| 20 | +import javax.persistence.criteria.Root; |
| 21 | +import javax.persistence.metamodel.Attribute; |
| 22 | + |
| 23 | +import org.springframework.core.convert.ConversionService; |
| 24 | +import org.springframework.core.convert.support.DefaultConversionService; |
| 25 | +import org.springframework.util.StringUtils; |
| 26 | + |
| 27 | +import cz.jirutka.rsql.parser.ast.AndNode; |
| 28 | +import cz.jirutka.rsql.parser.ast.ComparisonNode; |
| 29 | +import cz.jirutka.rsql.parser.ast.ComparisonOperator; |
| 30 | +import cz.jirutka.rsql.parser.ast.Node; |
| 31 | +import cz.jirutka.rsql.parser.ast.OrNode; |
| 32 | +import cz.jirutka.rsql.parser.ast.RSQLVisitor; |
| 33 | + |
| 34 | +// clone from com.putracode.utils.JPARsqlConverter |
| 35 | +@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 36 | +public class RsqlJpaConverter implements RSQLVisitor<Predicate, Root> { |
| 37 | + private final CriteriaBuilder builder; |
| 38 | + private final ConversionService conversionService = new DefaultConversionService(); |
| 39 | + |
| 40 | + public RsqlJpaConverter(CriteriaBuilder builder) { |
| 41 | + this.builder = builder; |
| 42 | + } |
| 43 | + |
| 44 | + public Predicate visit(AndNode node, Root root) { |
| 45 | + |
| 46 | + return builder.and(processNodes(node.getChildren(), root)); |
| 47 | + } |
| 48 | + |
| 49 | + public Predicate visit(OrNode node, Root root) { |
| 50 | + |
| 51 | + return builder.or(processNodes(node.getChildren(), root)); |
| 52 | + } |
| 53 | + |
| 54 | + public Predicate visit(ComparisonNode node, Root root) { |
| 55 | + ComparisonOperator op = node.getOperator(); |
| 56 | + //Path attrPath = root.get(node.getSelector()); |
| 57 | + //Attribute attribute = root.getModel().getAttribute(node.getSelector()); |
| 58 | + RsqlJpaHolder holder = RsqlJpaSpecification.findPropertyPath(node.getSelector(), root); |
| 59 | + Path attrPath = holder.path; |
| 60 | + Attribute attribute = holder.attribute; |
| 61 | + Class type = attribute.getJavaType(); |
| 62 | + if (node.getArguments().size() > 1) { |
| 63 | + /** |
| 64 | + * Implementasi List |
| 65 | + */ |
| 66 | + List<Object> listObject = new ArrayList<>(); |
| 67 | + for (String argument : node.getArguments()) { |
| 68 | + listObject.add(castDynamicClass(type, argument)); |
| 69 | + } |
| 70 | + if (op.equals(IN)) { |
| 71 | + return attrPath.in(listObject); |
| 72 | + } else { |
| 73 | + return builder.not(attrPath.in(listObject)); |
| 74 | + } |
| 75 | + |
| 76 | + } else { |
| 77 | + /** |
| 78 | + * Searching With One Value |
| 79 | + */ |
| 80 | + Object argument = castDynamicClass(type, node.getArguments().get(0)); |
| 81 | + if (op.equals(EQUAL)) { |
| 82 | + if (type.equals(String.class)) { |
| 83 | + if (argument.toString().contains("*") && argument.toString().contains("^")) { |
| 84 | + return builder.like(builder.upper(attrPath), argument.toString().replace("*", "%").replace("^", "").toUpperCase()); |
| 85 | + } else if (argument.toString().contains("*")) { |
| 86 | + return builder.like(attrPath, argument.toString().replace('*', '%')); |
| 87 | + } else if (argument.toString().contains("^")) { |
| 88 | + |
| 89 | + return builder.equal(builder.upper(attrPath), argument.toString().replace("^", "").toUpperCase()); |
| 90 | + } else { |
| 91 | + return builder.equal(attrPath, argument); |
| 92 | + } |
| 93 | + } else if (argument == null) { |
| 94 | + return builder.isNull(attrPath); |
| 95 | + } else { |
| 96 | + return builder.equal(attrPath, argument); |
| 97 | + } |
| 98 | + } |
| 99 | + if (op.equals(NOT_EQUAL)) { |
| 100 | + if (type.equals(String.class)) { |
| 101 | + if (argument.toString().contains("*") && argument.toString().contains("^")) { |
| 102 | + return builder.notLike(builder.upper(attrPath), argument.toString().replace("*", "%").replace("^", "").toUpperCase()); |
| 103 | + } else if (argument.toString().contains("*")) { |
| 104 | + return builder.notLike(attrPath, argument.toString().replace('*', '%')); |
| 105 | + } else if (argument.toString().contains("^")) { |
| 106 | + return builder.notEqual(builder.upper(attrPath), argument.toString().replace("^", "").toUpperCase()); |
| 107 | + } else { |
| 108 | + return builder.notEqual(attrPath, argument); |
| 109 | + } |
| 110 | + } else if (argument == null) { |
| 111 | + return builder.isNotNull(attrPath); |
| 112 | + } else { |
| 113 | + return builder.notEqual(attrPath, argument); |
| 114 | + } |
| 115 | + } |
| 116 | + if (!Comparable.class.isAssignableFrom(type)) { |
| 117 | + throw new IllegalArgumentException(String.format( |
| 118 | + "Operator %s can be used only for Comparables", op)); |
| 119 | + } |
| 120 | + Comparable comparable = (Comparable) conversionService.convert(argument, type); |
| 121 | + |
| 122 | + if (op.equals(GREATER_THAN)) { |
| 123 | + return builder.greaterThan(attrPath, comparable); |
| 124 | + } |
| 125 | + if (op.equals(GREATER_THAN_OR_EQUAL)) { |
| 126 | + return builder.greaterThanOrEqualTo(attrPath, comparable); |
| 127 | + } |
| 128 | + if (op.equals(LESS_THAN)) { |
| 129 | + return builder.lessThan(attrPath, comparable); |
| 130 | + } |
| 131 | + if (op.equals(LESS_THAN_OR_EQUAL)) { |
| 132 | + return builder.lessThanOrEqualTo(attrPath, comparable); |
| 133 | + } |
| 134 | + } |
| 135 | + throw new IllegalArgumentException("Unknown operator: " + op); |
| 136 | + } |
| 137 | + |
| 138 | + private Predicate[] processNodes(List<Node> nodes, Root root) { |
| 139 | + |
| 140 | + Predicate[] predicates = new Predicate[nodes.size()]; |
| 141 | + |
| 142 | + for (int i = 0; i < nodes.size(); i++) { |
| 143 | + predicates[i] = nodes.get(i).accept(this, root); |
| 144 | + } |
| 145 | + return predicates; |
| 146 | + } |
| 147 | + |
| 148 | + public Object castDynamicClass(Class dynamicClass, String value) { |
| 149 | + Object object = null; |
| 150 | + try { |
| 151 | + if (dynamicClass.equals(LocalDate.class)) { |
| 152 | + object = LocalDate.parse(value); |
| 153 | + } else if (dynamicClass.equals(LocalDateTime.class)) { |
| 154 | + object = LocalDateTime.parse(value); |
| 155 | + } else if (dynamicClass.equals(Character.class)) { |
| 156 | + object = (!StringUtils.isEmpty(value) ? value.charAt(0) : null); |
| 157 | + } else { |
| 158 | + Constructor<?> cons = (Constructor<?>) dynamicClass.getConstructor(new Class<?>[] { String.class }); |
| 159 | + object = cons.newInstance(new Object[] { value }); |
| 160 | + } |
| 161 | + |
| 162 | + return object; |
| 163 | + } catch (Exception e) { |
| 164 | + e.printStackTrace(); |
| 165 | + } |
| 166 | + return null; |
| 167 | + } |
| 168 | +} |
0 commit comments