1 /** 2 * Copyright 2005-2014 The Kuali Foundation 3 * 4 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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.kuali.rice.krad.data.jpa; 17 18 import javax.persistence.EntityManager; 19 import javax.persistence.Query; 20 21 /** 22 * Defines a criteria-based query. 23 * 24 * @author Kuali Rice Team (rice.collab@kuali.org) 25 */ 26 @Deprecated 27 class QueryByCriteria { 28 29 private EntityManager entityManager; 30 private Criteria criteria; 31 private QueryByCriteriaType type; 32 33 /** 34 * Defines the type of query to run (SELECT, UPDATE, DELETE, etc.). 35 */ 36 public enum QueryByCriteriaType {SELECT, UPDATE, DELETE} 37 38 /** 39 * Creates a criteria-based query. 40 * 41 * @param entityManager the entity manager for interacting with the database. 42 * @param criteria the criteria to convert into a query. 43 */ 44 QueryByCriteria(EntityManager entityManager, Criteria criteria) { 45 this(entityManager, criteria, QueryByCriteriaType.SELECT); 46 } 47 48 /** 49 * Creates a criteria-based query. 50 * 51 * @param entityManager the entity manager for interacting with the database. 52 * @param criteria the criteria to convert into a query. 53 * @param type the type of query to run. 54 */ 55 QueryByCriteria(EntityManager entityManager, Criteria criteria, QueryByCriteriaType type) { 56 this.entityManager = entityManager; 57 this.criteria = criteria; 58 this.type = type; 59 } 60 61 /** 62 * Converts the current {@link Criteria} to a {@link Query}. 63 * 64 * @return a {@link Query} associated with the current {@link Criteria}. 65 */ 66 public Query toQuery() { 67 Query query = entityManager.createQuery(criteria.toQuery(type)); 68 if (criteria.getSearchLimit() != null) { 69 query.setMaxResults(criteria.getSearchLimit()); 70 } 71 criteria.prepareParameters(query); 72 return query; 73 } 74 75 /** 76 * Converts the current {@link Criteria} to a count {@link Query}. 77 * 78 * @return a count {@link Query} associated with the current {@link Criteria}. 79 */ 80 public Query toCountQuery() { 81 Query query = entityManager.createQuery(criteria.toCountQuery()); 82 if (criteria.getSearchLimit() != null) { 83 query.setMaxResults(criteria.getSearchLimit()); 84 } 85 criteria.prepareParameters(query); 86 return query; 87 } 88 89 }