|
| 1 | +/* |
| 2 | + * Copyright 2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.convert; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import javax.persistence.criteria.CriteriaBuilder; |
| 25 | +import javax.persistence.criteria.Expression; |
| 26 | +import javax.persistence.criteria.From; |
| 27 | +import javax.persistence.criteria.Path; |
| 28 | +import javax.persistence.criteria.Predicate; |
| 29 | +import javax.persistence.criteria.Root; |
| 30 | +import javax.persistence.metamodel.Attribute; |
| 31 | +import javax.persistence.metamodel.Attribute.PersistentAttributeType; |
| 32 | +import javax.persistence.metamodel.ManagedType; |
| 33 | +import javax.persistence.metamodel.SingularAttribute; |
| 34 | + |
| 35 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 36 | +import org.springframework.data.domain.Example; |
| 37 | +import org.springframework.data.domain.Example.NullHandler; |
| 38 | +import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; |
| 39 | +import org.springframework.orm.jpa.JpaSystemException; |
| 40 | +import org.springframework.util.Assert; |
| 41 | +import org.springframework.util.ClassUtils; |
| 42 | +import org.springframework.util.ObjectUtils; |
| 43 | +import org.springframework.util.StringUtils; |
| 44 | + |
| 45 | +/** |
| 46 | + * {@link QueryByExamplePredicateBuilder} creates a single {@link CriteriaBuilder#and(Predicate...)} combined |
| 47 | + * {@link Predicate} for a given {@link Example}. <br /> |
| 48 | + * The builder includes any {@link SingularAttribute} of the {@link Example#getProbe()} applying {@link String} and |
| 49 | + * {@literal null} matching strategies configured on the {@link Example}. Ignored paths are no matter of their actual |
| 50 | + * value not considered. <br /> |
| 51 | + * |
| 52 | + * @author Christoph Strobl |
| 53 | + * @since 1.10 |
| 54 | + */ |
| 55 | +public class QueryByExamplePredicateBuilder { |
| 56 | + |
| 57 | + private static final Set<PersistentAttributeType> ASSOCIATION_TYPES; |
| 58 | + |
| 59 | + static { |
| 60 | + ASSOCIATION_TYPES = new HashSet<PersistentAttributeType>(Arrays.asList(PersistentAttributeType.MANY_TO_MANY, |
| 61 | + PersistentAttributeType.MANY_TO_ONE, PersistentAttributeType.ONE_TO_MANY, PersistentAttributeType.ONE_TO_ONE)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Extract the {@link Predicate} representing the {@link Example}. |
| 66 | + * |
| 67 | + * @param root must not be {@literal null}. |
| 68 | + * @param cb must not be {@literal null}. |
| 69 | + * @param example must not be {@literal null}. |
| 70 | + * @return never {@literal null}. |
| 71 | + */ |
| 72 | + public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) { |
| 73 | + |
| 74 | + Assert.notNull(root, "Root must not be null!"); |
| 75 | + Assert.notNull(cb, "CriteriaBuilder must not be null!"); |
| 76 | + Assert.notNull(example, "Root must not be null!"); |
| 77 | + |
| 78 | + List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getSampleObject(), example, |
| 79 | + new PathNode("root", null, example.getSampleObject())); |
| 80 | + |
| 81 | + if (predicates.isEmpty()) { |
| 82 | + return cb.isTrue(cb.literal(false)); |
| 83 | + } |
| 84 | + |
| 85 | + if (predicates.size() == 1) { |
| 86 | + return predicates.iterator().next(); |
| 87 | + } |
| 88 | + |
| 89 | + return cb.and(predicates.toArray(new Predicate[predicates.size()])); |
| 90 | + } |
| 91 | + |
| 92 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 93 | + static List<Predicate> getPredicates(String path, CriteriaBuilder cb, Path<?> from, ManagedType<?> type, |
| 94 | + Object value, Example<?> example, PathNode currentNode) { |
| 95 | + |
| 96 | + List<Predicate> predicates = new ArrayList<Predicate>(); |
| 97 | + DirectFieldAccessFallbackBeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(value); |
| 98 | + |
| 99 | + for (SingularAttribute attribute : type.getSingularAttributes()) { |
| 100 | + |
| 101 | + String currentPath = !StringUtils.hasText(path) ? attribute.getName() : path + "." + attribute.getName(); |
| 102 | + |
| 103 | + if (example.isIgnoredPath(currentPath)) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + Object attributeValue = example.getValueTransformerForPath(currentPath).convert( |
| 108 | + beanWrapper.getPropertyValue(attribute.getName())); |
| 109 | + |
| 110 | + if (attributeValue == null) { |
| 111 | + |
| 112 | + if (example.getNullHandler().equals(NullHandler.INCLUDE)) { |
| 113 | + predicates.add(cb.isNull(from.get(attribute))); |
| 114 | + } |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)) { |
| 119 | + |
| 120 | + predicates.addAll(getPredicates(currentPath, cb, from.get(attribute.getName()), |
| 121 | + (ManagedType<?>) attribute.getType(), attributeValue, example, currentNode)); |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + if (isAssociation(attribute)) { |
| 126 | + |
| 127 | + if (!(from instanceof From)) { |
| 128 | + throw new JpaSystemException(new IllegalArgumentException(String.format( |
| 129 | + "Unexpected path type for %s. Found % where From.class was expected.", currentPath, from))); |
| 130 | + } |
| 131 | + |
| 132 | + PathNode node = currentNode.add(attribute.getName(), attributeValue); |
| 133 | + if (node.spansCycle()) { |
| 134 | + throw new InvalidDataAccessApiUsageException(String.format( |
| 135 | + "Path '%s' from root %s must not span a cyclic property reference!\r\n%s", currentPath, |
| 136 | + ClassUtils.getShortName(example.getSampleType()), node)); |
| 137 | + } |
| 138 | + |
| 139 | + predicates.addAll(getPredicates(currentPath, cb, ((From<?, ?>) from).join(attribute.getName()), |
| 140 | + (ManagedType<?>) attribute.getType(), attributeValue, example, node)); |
| 141 | + |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + if (attribute.getJavaType().equals(String.class)) { |
| 146 | + |
| 147 | + Expression<String> expression = from.get(attribute); |
| 148 | + if (example.isIgnoreCaseForPath(currentPath)) { |
| 149 | + expression = cb.lower(expression); |
| 150 | + attributeValue = attributeValue.toString().toLowerCase(); |
| 151 | + } |
| 152 | + |
| 153 | + switch (example.getStringMatcherForPath(currentPath)) { |
| 154 | + |
| 155 | + case DEFAULT: |
| 156 | + case EXACT: |
| 157 | + predicates.add(cb.equal(expression, attributeValue)); |
| 158 | + break; |
| 159 | + case CONTAINING: |
| 160 | + predicates.add(cb.like(expression, "%" + attributeValue + "%")); |
| 161 | + break; |
| 162 | + case STARTING: |
| 163 | + predicates.add(cb.like(expression, attributeValue + "%")); |
| 164 | + break; |
| 165 | + case ENDING: |
| 166 | + predicates.add(cb.like(expression, "%" + attributeValue)); |
| 167 | + break; |
| 168 | + default: |
| 169 | + throw new IllegalArgumentException("Unsupported StringMatcher " |
| 170 | + + example.getStringMatcherForPath(currentPath)); |
| 171 | + } |
| 172 | + } else { |
| 173 | + predicates.add(cb.equal(from.get(attribute), attributeValue)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return predicates; |
| 178 | + } |
| 179 | + |
| 180 | + private static boolean isAssociation(Attribute<?, ?> attribute) { |
| 181 | + return ASSOCIATION_TYPES.contains(attribute.getPersistentAttributeType()); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * {@link PathNode} is used to dynamically grow a directed graph structure that allows to detect cycles within its |
| 186 | + * direct predecessor nodes by comparing parent node values using {@link System#identityHashCode(Object)}. |
| 187 | + * |
| 188 | + * @author Christoph Strobl |
| 189 | + */ |
| 190 | + private static class PathNode { |
| 191 | + |
| 192 | + String name; |
| 193 | + PathNode parent; |
| 194 | + List<PathNode> siblings = new ArrayList<PathNode>();; |
| 195 | + Object value; |
| 196 | + |
| 197 | + public PathNode(String edge, PathNode parent, Object value) { |
| 198 | + |
| 199 | + this.name = edge; |
| 200 | + this.parent = parent; |
| 201 | + this.value = value; |
| 202 | + } |
| 203 | + |
| 204 | + PathNode add(String attribute, Object value) { |
| 205 | + |
| 206 | + PathNode node = new PathNode(attribute, this, value); |
| 207 | + siblings.add(node); |
| 208 | + return node; |
| 209 | + } |
| 210 | + |
| 211 | + boolean spansCycle() { |
| 212 | + |
| 213 | + if (value == null) { |
| 214 | + return false; |
| 215 | + } |
| 216 | + |
| 217 | + String identityHex = ObjectUtils.getIdentityHexString(value); |
| 218 | + PathNode tmp = parent; |
| 219 | + |
| 220 | + while (tmp != null) { |
| 221 | + |
| 222 | + if (ObjectUtils.getIdentityHexString(tmp.value).equals(identityHex)) { |
| 223 | + return true; |
| 224 | + } |
| 225 | + tmp = tmp.parent; |
| 226 | + } |
| 227 | + |
| 228 | + return false; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public String toString() { |
| 233 | + |
| 234 | + StringBuilder sb = new StringBuilder(); |
| 235 | + if (parent != null) { |
| 236 | + sb.append(parent.toString()); |
| 237 | + sb.append(" -"); |
| 238 | + sb.append(name); |
| 239 | + sb.append("-> "); |
| 240 | + } |
| 241 | + |
| 242 | + sb.append("[{ "); |
| 243 | + sb.append(ObjectUtils.nullSafeToString(value)); |
| 244 | + sb.append(" }]"); |
| 245 | + return sb.toString(); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
0 commit comments