View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
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   *	$Id$
17   *  $HeadURL$
18   */
19  package com.hack23.cia.service.data.impl;
20  
21  import java.util.Locale;
22  
23  import javax.persistence.criteria.CriteriaBuilder;
24  import javax.persistence.criteria.Expression;
25  import javax.persistence.criteria.Predicate;
26  import javax.persistence.criteria.Root;
27  import javax.persistence.metamodel.SingularAttribute;
28  
29  /**
30   * The Class QueryHelper.
31   */
32  public final class QueryHelper {
33  
34  	/**
35  	 * Equals ignore case if string predicate.
36  	 *
37  	 * @param <T>
38  	 *            the generic type
39  	 * @param criteriaBuilder
40  	 *            the criteria builder
41  	 * @param root
42  	 *            the root
43  	 * @param value
44  	 *            the value
45  	 * @param property
46  	 *            the property
47  	 * @return the predicate
48  	 */
49  	public static <T> Predicate equalsIgnoreCaseIfStringPredicate(final CriteriaBuilder criteriaBuilder,final Root<T> root,
50  			final Object value, final SingularAttribute<T, ? extends Object> property) {
51  		Predicate condition;
52  		if (value instanceof String) {
53  			final Expression<String> propertyObject = (Expression<String>) root.get(property);
54  			condition = criteriaBuilder.equal(criteriaBuilder.upper(propertyObject), ((String) value).toUpperCase(Locale.ENGLISH));
55  
56  		} else {
57  			condition = criteriaBuilder.equal(root.get(property), value);
58  		}
59  		return condition;
60  	}
61  
62  }