Author: tchemit Date: 2012-08-18 14:11:22 +0200 (Sat, 18 Aug 2012) New Revision: 2632 Url: http://nuiton.org/repositories/revision/topia/2632 Log: refs #2271: Deprecate TopiaQuery refs #2272: Add new methods to TopiaDAO to help hql query Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2012-08-18 10:37:13 UTC (rev 2631) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2012-08-18 12:11:22 UTC (rev 2632) @@ -226,10 +226,12 @@ * @author fdesbois * @version $Revision$ * @since 2.3.0 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 * <p/> * Mise a jour: $Date$ par * : $Author$ */ +@Deprecated public class TopiaQuery { private static final Log log = LogFactory.getLog(TopiaQuery.class); Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-08-18 10:37:13 UTC (rev 2631) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-08-18 12:11:22 UTC (rev 2632) @@ -42,9 +42,7 @@ import org.nuiton.topia.event.TopiaEntityVetoable; import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.topia.generator.DAOAbstractTransformer; -import org.nuiton.topia.generator.DAOImplTransformer; -import org.nuiton.topia.generator.DAOTransformer; +import org.nuiton.topia.generator.EntityDAOTransformer; import java.security.Permission; import java.util.List; @@ -56,9 +54,7 @@ * TopiaQuery}. * <p/> * This interface is implemented by {@link TopiaDAOImpl} overridden by generation - * from {@link DAOTransformer} for specific entity interface, {@link - * DAOAbstractTransformer} for abstract implementation and {@link - * DAOImplTransformer} for final implementation class. + * from {@link EntityDAOTransformer}. * <p/> * * @param <E> the entity type linked with the dao @@ -223,7 +219,9 @@ * @return une nouvelle TopiaQuery vide. (uniquement avec le From sur le * type d'entité) * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated TopiaQuery createQuery(); /** @@ -236,7 +234,9 @@ * requête * @return une nouvelle TopiaQuery * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated TopiaQuery createQuery(String entityAlias); /** @@ -275,7 +275,9 @@ * @throws TopiaException if any pb while getting datas * @see TopiaQuery#executeToEntity(TopiaContext, Class) * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated E findByQuery(TopiaQuery query) throws TopiaException; List<E> findAll() throws TopiaException; @@ -314,7 +316,9 @@ * @throws TopiaException if any pb while getting datas * @see TopiaQuery#executeToEntityList(TopiaContext, Class) * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated List<E> findAllByQuery(TopiaQuery query) throws TopiaException; /** @@ -327,7 +331,9 @@ * @throws TopiaException if any pb while getting datas * @see TopiaQuery#executeToEntityMap(TopiaContext, Class) * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated Map<String, E> findAllMappedByQuery(TopiaQuery query) throws TopiaException; /** @@ -343,7 +349,9 @@ * @throws TopiaException if any pb while getting datas * @see TopiaQuery#executeToEntityMap(TopiaContext, Class) * @since 2.3 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated <K> Map<K, E> findAllMappedByQuery(TopiaQuery query, String keyName, Class<K> keyClass) throws TopiaException; @@ -411,7 +419,9 @@ * @return true if entity exists, false otherwise * @throws TopiaException * @since 2.3.4 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated boolean existByQuery(TopiaQuery query) throws TopiaException; /** @@ -430,7 +440,9 @@ * @return number of entities filtered by the query * @throws TopiaException if any pb while getting datas * @since 2.3.4 + * @deprecated since 2.6.12, {@link TopiaQuery} will be removed in version 3.0 */ + @Deprecated int countByQuery(TopiaQuery query) throws TopiaException; /** @@ -454,5 +466,38 @@ */ E findByPrimaryKey(Object... k) throws TopiaException; + boolean existsByQuery(String hql, + Object... params) throws TopiaException; + + long countByQuery(String hql, + Object... params) throws TopiaException; + + E findUniqueByQuery(String hql, + Object... params) throws TopiaException; + + <R> R findUniqueByQuery(Class<R> type, + String hql, + Object... params) throws TopiaException; + + List<E> findAllByQuery(String hql, + Object... params) throws TopiaException; + + <R> List<R> findAllByQuery(Class<R> type, + String hql, + Object... params) throws TopiaException; + + List<E> findAllByQueryWithBound(String hql, + int startIndex, + int endIndex, + Object... params) throws TopiaException; + + List<E> findByQueryAndPager(String hql, + TopiaFilterPagerUtil.FilterPagerBean pager, + Object... params) throws TopiaException; + + void computeAndAddRecordsToPager(String hql, + TopiaFilterPagerUtil.FilterPagerBean pager, + Object... params) throws TopiaException; + } //TopiaDAO Modified: branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-08-18 10:37:13 UTC (rev 2631) +++ branches/topia-2.6.x/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2012-08-18 12:11:22 UTC (rev 2632) @@ -37,7 +37,9 @@ package org.nuiton.topia.persistence; +import com.google.common.base.Preconditions; import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.HibernateException; @@ -48,6 +50,7 @@ import org.nuiton.topia.event.TopiaEntityVetoable; import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.util.PagerUtil; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; @@ -237,7 +240,7 @@ Object propertyName = null; Object value; try { - for (int i = 0; i < properties.length;) { + for (int i = 0; i < properties.length; ) { propertyName = properties[i++]; value = properties[i++]; map.put((String) propertyName, value); @@ -517,7 +520,7 @@ Map<String, Object> properties = new HashMap<String, Object>(); properties.put(propertyName, propertyValue); Object name = null; - for (int i = 0; i < others.length;) { + for (int i = 0; i < others.length; ) { try { name = others[i++]; propertyValue = others[i++]; @@ -580,9 +583,98 @@ } throw new TopiaException("La classe " + entityClass.getName() + " n'a pas de cle primaire naturelle"); + } + public boolean existsByQuery(String hql, + Object... params) throws TopiaException { + long count = countByQuery(hql, params); + return count > 0; } + public long countByQuery(String hql, + Object... params) throws TopiaException { + + Preconditions.checkNotNull(hql); + Preconditions.checkArgument(hql.startsWith("SELECT COUNT(")); + + return findUniqueByQuery(Long.class, hql, params); + } + + public E findUniqueByQuery(String hql, + Object... params) throws TopiaException { + return findUniqueByQuery(getEntityClass(), hql, params); + } + + public <R> R findUniqueByQuery(Class<R> type, + String hql, + Object... params) throws TopiaException { + + Preconditions.checkNotNull(type); + Preconditions.checkNotNull(hql); + + Object unique = getContext().findUnique(hql, params); + Preconditions.checkState(unique == null || + type.isAssignableFrom(unique.getClass())); + return (R) unique; + } + + public List<E> findAllByQuery(String hql, + Object... params) throws TopiaException { + + return findAllByQuery(getEntityClass(), hql, params); + } + + public <R> List<R> findAllByQuery(Class<R> type, + String hql, + Object... params) throws TopiaException { + + Preconditions.checkNotNull(type); + Preconditions.checkNotNull(hql); + + List<R> result = getContext().findAll(hql, params); + return result; + } + + public List<E> findAllByQueryWithBound(String hql, + int startIndex, + int endIndex, + Object... params) throws TopiaException { + + Preconditions.checkNotNull(hql); + + List<E> result = getContext().find(hql, startIndex, endIndex, params); + return result; + } + + public List<E> findByQueryAndPager(String hql, + TopiaFilterPagerUtil.FilterPagerBean pager, + Object... params) throws TopiaException { + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(hql); + + if (StringUtils.isNotBlank(pager.getSortColumn())) { + hql += " ORDER BY " + pager.getSortColumn(); + if (!pager.isSortAscendant()) { + hql += " DESC"; + } + } + List<E> result = findAllByQueryWithBound(hql, + pager.getRecordStartIndex(), + pager.getRecordEndIndex() - 1, + params); + return result; + } + + public void computeAndAddRecordsToPager(String hql, + TopiaFilterPagerUtil.FilterPagerBean pager, + Object... params) throws TopiaException { + + long records = countByQuery(hql, params); + + pager.setRecords((int) records); + PagerUtil.computeRecordIndexesAndPagesNumber(pager); + } + /** * Renvoie la Session contenue dans le contexte * @@ -609,5 +701,5 @@ } return meta; } - + } //TopiaDAOImpl