Author: bleny Date: 2013-10-25 14:03:15 +0200 (Fri, 25 Oct 2013) New Revision: 2847 Url: http://nuiton.org/projects/topia/repository/revisions/2847 Log: extract contract TopiaDao, breking the build Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaDao.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaOrRunQueryStep.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderRunQueryStep.java Removed: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDAO.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/AbstractTopiaContext.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaHibernateEventListener.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityAbstract.java trunk/topia-persistence/src/test/java/org/nuiton/topia/GeneratedPersonneTopiaDao.java trunk/topia-persistence/src/test/java/org/nuiton/topia/TopiaJpaSupportTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaContextReplicateTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaTestCase.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/EntityVisitorExportXmlTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java trunk/topia-persistence/src/test/java/org/nuiton/topia/test/ano1882/DAOAbstractTransformerTest.java trunk/topia-persistence/src/test/java/org/nuiton/topiatest/EnumTest.java trunk/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -35,7 +35,7 @@ import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.nuiton.topia.framework.TopiaFiresSupport; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaIdFactory; @@ -278,9 +278,9 @@ throw new TopiaException("unable to find DAO class " + daoClassName, e); } - if (dao instanceof AbstractTopiaDAO) { - AbstractTopiaDAO abstractTopiaDAO = (AbstractTopiaDAO) dao; - abstractTopiaDAO.init(hibernateSupport, jpaSupport, sqlSupport, listenableSupport, topiaIdFactory, firesSupport, this); + if (dao instanceof AbstractTopiaDao) { + AbstractTopiaDao abstractTopiaDao = (AbstractTopiaDao) dao; + abstractTopiaDao.init(hibernateSupport, jpaSupport, sqlSupport, listenableSupport, topiaIdFactory, firesSupport, this); } daoCache.put(entityClass, dao); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/AbstractTopiaContext.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/AbstractTopiaContext.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/AbstractTopiaContext.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -59,7 +59,7 @@ import org.nuiton.topia.event.TopiaSchemaListener; import org.nuiton.topia.event.TopiaTransactionListener; import org.nuiton.topia.event.TopiaTransactionVetoable; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.DefaultTopiaIdFactory; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaDAOImpl; @@ -751,8 +751,8 @@ if (result instanceof TopiaDAOImpl) { // backward compatibility ((TopiaDAOImpl)result).init(this, entityClass, getFiresSupport()); - } else if (result instanceof AbstractTopiaDAO) { - ((AbstractTopiaDAO)result).init(this, this, this, this, getTopiaIdFactory(), getFiresSupport(), this); + } else if (result instanceof AbstractTopiaDao) { + ((AbstractTopiaDao)result).init(this, this, this, this, getTopiaIdFactory(), getFiresSupport(), this); } daoCache.put(entityClass, result); } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaFiresSupport.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -54,7 +54,7 @@ import org.nuiton.topia.event.TopiaTransactionEvent; import org.nuiton.topia.event.TopiaTransactionListener; import org.nuiton.topia.event.TopiaTransactionVetoable; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.util.CategorisedListenerSet; import org.nuiton.util.ListenerSet; @@ -282,7 +282,7 @@ } } - public void fireOnPostLoad(AbstractTopiaDAO context, + public void fireOnPostLoad(AbstractTopiaDao context, TopiaEntity entity, Object[] state) { if (log.isDebugEnabled()) { log.debug("fireOnPostLoad"); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaHibernateEventListener.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaHibernateEventListener.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaHibernateEventListener.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -49,7 +49,7 @@ import org.hibernate.event.spi.SaveOrUpdateEventListener; import org.nuiton.topia.TopiaDaoSupplier; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaEntityContextable; @@ -188,7 +188,7 @@ if (daoSupplier != null && event.getEntity() instanceof TopiaEntity) { attachContext(event.getEntity(), daoSupplier); TopiaEntity entity = (TopiaEntity) event.getEntity(); - AbstractTopiaDAO<? extends TopiaEntity> dao = (AbstractTopiaDAO) daoSupplier.getDao(entity.getClass()); + AbstractTopiaDao<? extends TopiaEntity> dao = (AbstractTopiaDao) daoSupplier.getDao(entity.getClass()); dao.getTopiaFiresSupport().fireOnPostLoad(dao, (TopiaEntity) event.getEntity(), new Object[]{}); } } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -44,7 +44,7 @@ import org.nuiton.eugene.models.object.ObjectModelInterface; import org.nuiton.eugene.models.object.ObjectModelOperation; import org.nuiton.eugene.models.object.ObjectModelParameter; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.TopiaDAOImpl; import java.util.ArrayList; @@ -1042,7 +1042,7 @@ if (StringUtils.isEmpty(daoImpl)) { // use the default dao implementation of topia - result = AbstractTopiaDAO.class; + result = AbstractTopiaDao.class; } else { try { result = Class.forName(daoImpl); Deleted: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDAO.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDAO.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -1,1307 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -/* * - * TopiaDAOAbstract.java - * - * Created: 31 déc. 2005 13:10:34 - * - * @author poussin <poussin@codelutin.com> - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - -package org.nuiton.topia.persistence; - -import com.google.common.base.Optional; -import com.google.common.base.Preconditions; -import com.google.common.collect.Iterables; -import com.google.common.collect.Iterators; -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; -import org.hibernate.metadata.ClassMetadata; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaDaoSupplier; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.TopiaHibernateSupport; -import org.nuiton.topia.TopiaJpaSupport; -import org.nuiton.topia.TopiaListenableSupport; -import org.nuiton.topia.TopiaSqlSupport; -import org.nuiton.topia.event.TopiaEntityListener; -import org.nuiton.topia.event.TopiaEntityVetoable; -import org.nuiton.topia.framework.TopiaContextImplementor; -import org.nuiton.topia.framework.TopiaFiresSupport; -import org.nuiton.topia.framework.TopiaUtil; -import org.nuiton.topia.persistence.pager.TopiaPagerBean; -import org.nuiton.util.PagerBeanUtil; - -import java.lang.reflect.InvocationTargetException; -import java.security.Permission; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; - -/** - * Cette classe permet d'avoir un ensemble de méthode implantée de façon - * standard et plus spécifiquement pour Hibernate. - * <p/> - * Certains accès à Hibernate sont tout de même fait ici, car on a pris le choix - * de se baser entièrement sur hibernate pour la persistence, et il est ainsi - * possible d'accèder au meta information hibernate sur les classes lorque l'on - * en a besoin. - * - * @param <E> le type de l'entite - * @author bpoussin <poussin@codelutin.com> - * @version $Id$ - */ - -public abstract class AbstractTopiaDAO<E extends TopiaEntity> implements TopiaDAO<E> { - - /** to use log facility, just put in your code: log.info(\"...\"); */ - private static Log log = LogFactory.getLog(AbstractTopiaDAO.class); - - /** - * Default batch size used to iterate on data. - * - * @since 2.6.14 - */ - protected int batchSize = 1000; - - protected TopiaHibernateSupport topiaHibernateSupport; - - protected TopiaJpaSupport topiaJpaSupport; - - protected TopiaSqlSupport topiaSqlSupport; - - protected TopiaListenableSupport topiaListenableSupport; - - protected TopiaIdFactory topiaIdFactory; - - protected TopiaFiresSupport topiaFiresSupport; - - protected TopiaDaoSupplier topiaDaoSupplier; - - public abstract TopiaEntityEnum getTopiaEntityEnum(); - - public abstract Class<E> getEntityClass(); - - public Iterator<E> iterator() { - - Iterator<E> iterator = new FindAllIterator<E, E>( - this, - getEntityClass(), - batchSize, - "FROM " + getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", - Collections. <String, Object> emptyMap()); - - return iterator; - - } - -// /** -// * Retourne l'id de l'entity -// * -// * @param e l'entity -// * @return l'id de l'entity ou null si pas trouvé -// * @throws org.nuiton.topia.TopiaException Si une erreur survient durant la recherche -// */ -// protected Serializable getId(E e) { -// ClassMetadata meta = getClassMetadata(); -// String idPropName = meta.getIdentifierPropertyName(); -// -// try { -// Serializable result; -// result = (Serializable) PropertyUtils.getSimpleProperty(e, -// idPropName); -// return result; -// } catch (Exception eee) { -// throw new TopiaException("Impossible de récuperer l'identifiant " -// + idPropName + " de l'entite: " + e); -// } -// } - -// /** -// * Retourne l'id de l'entity representer comme une map -// * -// * @param map l'entity en representation map -// * @return l'id de l'entity ou null si pas trouvé -// * @throws org.nuiton.topia.TopiaException Si une erreur survient durant la recherche -// */ -// protected Serializable getId(Map map) { -// try { -// ClassMetadata meta = getClassMetadata(); -// String idPropName = meta.getIdentifierPropertyName(); -// -// Serializable id = (Serializable) map.get(idPropName); -// return id; -// } catch (HibernateException eee) { -// throw new TopiaException(eee); -// } -// } - - /** - * When AbstractTopiaContext create the TopiaDAOHibernate, it must call this - * method just after. - */ - public void init( - TopiaHibernateSupport topiaHibernateSupport, - TopiaJpaSupport topiaJpaSupport, - TopiaSqlSupport topiaSqlSupport, - TopiaListenableSupport topiaListenableSupport, - TopiaIdFactory topiaIdFactory, - TopiaFiresSupport topiaFiresSupport, - TopiaDaoSupplier topiaDaoSupplier) { - log.debug("init dao for " + getEntityClass()); - this.topiaHibernateSupport = topiaHibernateSupport; - this.topiaJpaSupport = topiaJpaSupport; - this.topiaSqlSupport = topiaSqlSupport; - this.topiaListenableSupport = topiaListenableSupport; - this.topiaIdFactory = topiaIdFactory; - this.topiaFiresSupport = topiaFiresSupport; - this.topiaDaoSupplier = topiaDaoSupplier; - } - - public TopiaFiresSupport getTopiaFiresSupport() { - return topiaFiresSupport; - } - - @Override - public int getBatchSize() { - return batchSize; - } - - @Override - public void setBatchSize(int batchSize) { - this.batchSize = batchSize; - } - - protected String createSimpleQuery() { - return newFromClause(null); - } - - protected String newFromClause(String alias) { - String hql = "from " + getTopiaEntityEnum().getImplementationFQN(); - if (StringUtils.isNotBlank(alias)) { - hql += " " + alias; - } - return hql; - } - - @Deprecated - public String createSimpleQuery(String alias) { - return newFromClause(alias); - } - - public E newInstance() { - if (log.isTraceEnabled()) { - log.trace("entityClass = " + getEntityClass()); - } - Class<E> implementation = (Class<E>) - getTopiaEntityEnum().getImplementation(); - try { - E newInstance = implementation.newInstance(); - return newInstance; - } catch (InstantiationException e) { - throw new TopiaException( - "Impossible de trouver ou d'instancier la classe " - + implementation); - } catch (IllegalAccessException e) { - throw new TopiaException( - "Impossible de trouver ou d'instancier la classe " - + implementation); - } - } - - public void addTopiaEntityListener(TopiaEntityListener listener) { - topiaListenableSupport.addTopiaEntityListener(getEntityClass(), listener); - } - - public void addTopiaEntityVetoable(TopiaEntityVetoable vetoable) { - topiaListenableSupport.addTopiaEntityVetoable(getEntityClass(), vetoable); - } - - public void removeTopiaEntityListener(TopiaEntityListener listener) { - topiaListenableSupport.removeTopiaEntityListener(getEntityClass(), listener); - } - - public void removeTopiaEntityVetoable(TopiaEntityVetoable vetoable) { - topiaListenableSupport.removeTopiaEntityVetoable(getEntityClass(), vetoable); - } - - public E create(E entity) { - try { - // first set topiaId - if (StringUtils.isBlank(entity.getTopiaId())) { - - // only set id if not already on - String topiaId = topiaIdFactory.newTopiaId(getEntityClass(), entity); - entity.setTopiaId(topiaId); - } - - if (entity instanceof TopiaEntityContextable) { - TopiaEntityContextable contextable = (TopiaEntityContextable) entity; - contextable.setTopiaDAOSupplier(this.topiaDaoSupplier); - } - - // save entity - topiaHibernateSupport.getHibernateSession().save(entity); - topiaFiresSupport.warnOnCreateEntity(entity); - return entity; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - public E create(String propertyName, Object propertyValue, Object... otherPropertyNamesAndValues) { - Map<String, Object> properties = - TopiaUtil.convertPropertiesArrayToMap(propertyName, propertyValue, otherPropertyNamesAndValues); - E result = create(properties); - return result; - } - - @Override - public E create(Object... propertyNamesAndValues) { - Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - E result = create(properties); - return result; - } - - public E create(Map<String, Object> properties) { - - E result = newInstance(); - - try { - for (Map.Entry<String, Object> e : properties.entrySet()) { - String propertyName = e.getKey(); - Object value = e.getValue(); - PropertyUtils.setProperty(result, propertyName, value); - } - } catch (IllegalAccessException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } catch (InvocationTargetException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } catch (NoSuchMethodException eee) { - throw new IllegalArgumentException( - "Can't put properties on new Object", eee); - } - - create(result); - - return result; - } - - public E create() { - E result = newInstance(); - create(result); - return result; - } - - public E update(E e) { - try { - topiaHibernateSupport.getHibernateSession().saveOrUpdate(e); - topiaFiresSupport.warnOnUpdateEntity(e); - return e; - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - - public void delete(E e) { - try { - topiaHibernateSupport.getHibernateSession().delete(e); - e.notifyDeleted(); - topiaFiresSupport.warnOnDeleteEntity(e); - } catch (HibernateException eee) { - throw new TopiaException(eee); - } - } - - - public void deleteAll(Iterable<E> entities) { - for (E entity : entities) { - delete(entity); - } - } - - protected HqlAndParametersBuilder<E> newHqlAndParametersBuilder() { - return new HqlAndParametersBuilder<E>(getEntityClass()); - } - - protected HqlAndParametersBuilder<E> getHqlForProperties(String propertyName, - Object propertyValue, - Object... otherPropertyNamesAndValues) { - Map<String, Object> properties = - TopiaUtil.convertPropertiesArrayToMap(propertyName, propertyValue, otherPropertyNamesAndValues); - return getHqlForProperties(properties); - } - - protected HqlAndParametersBuilder<E> getHqlForNoConstraint() { - Map<String, Object> properties = Collections.emptyMap(); - return getHqlForProperties(properties); - } - - protected HqlAndParametersBuilder<E> getHqlForProperties(Map<String, Object> properties) { - HqlAndParametersBuilder<E> hqlAndParametersBuilder = newHqlAndParametersBuilder(); - for (Map.Entry<String, Object> property : properties.entrySet()) { - hqlAndParametersBuilder.addEquals(property.getKey(), property.getValue()); - } - return hqlAndParametersBuilder; - } - - protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql) { - Map<String, Object> properties = Collections.emptyMap(); - return forHql(hql, properties); - } - - protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, Map<String, Object> hqlParameters) { - return new TopiaQueryBuilderRunQueryStep(this, hql, hqlParameters); - } - - protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, String parameterName, - Object parameterValue, - Object... otherParameterNamesAndValues) { - Map<String, Object> hqlParameters = - TopiaUtil.convertPropertiesArrayToMap(parameterName, parameterValue, otherParameterNamesAndValues); - return forHql(hql, hqlParameters); - } - - public TopiaQueryBuilderCreateQueryStep<E> newQueryBuilder() { - return new TopiaQueryBuilderCreateQueryStep(this); - } - - // shortcuts for first step of builder - - public TopiaQueryBuilderAddCriteriaStep<E> forProperties(Map<String, Object> properties) { - return newQueryBuilder().forProperties(properties); - } - - public TopiaQueryBuilderAddCriteriaStep<E> forProperties(String propertyName, - Object propertyValue, - Object... otherPropertyNamesAndValues) { - return newQueryBuilder().forProperties(propertyName, propertyValue, otherPropertyNamesAndValues); - } - - public TopiaQueryBuilderAddCriteriaStep<E> forAll() { - return newQueryBuilder().forAll(); - } - - public TopiaQueryBuilderRunQueryStep<E> forContains(String propertyName, Object propertyValue) { - return forAll().addContains(propertyName, propertyValue).getNextStep(); - } - - public TopiaQueryBuilderRunQueryStep<E> forEquals(String propertyName, Object propertyValue) { - return forAll().addEquals(propertyName, propertyValue).getNextStep(); - } - - public TopiaQueryBuilderRunQueryStep<E> forIn(String propertyName, Iterable<Object> propertyValues) { - return forAll().addIn(propertyName, propertyValues).getNextStep(); - } - - public static class TopiaQueryBuilderCreateQueryStep<E extends TopiaEntity> { - - protected AbstractTopiaDAO<E> topiaDAO; - - protected TopiaQueryBuilderCreateQueryStep(AbstractTopiaDAO<E> topiaDAO) { - this.topiaDAO = topiaDAO; - } - - public TopiaQueryBuilderAddCriteriaStep<E> forProperties(Map<String, Object> properties) { - HqlAndParametersBuilder<E> hqlAndParametersBuilder = topiaDAO.getHqlForProperties(properties); - return getNextStep(hqlAndParametersBuilder); - } - - public TopiaQueryBuilderAddCriteriaStep<E> forProperties(String propertyName, - Object propertyValue, - Object... otherPropertyNamesAndValues) { - HqlAndParametersBuilder<E> hqlAndParametersBuilder = topiaDAO.getHqlForProperties(propertyName, propertyValue, otherPropertyNamesAndValues); - return getNextStep(hqlAndParametersBuilder); - } - - public TopiaQueryBuilderAddCriteriaStep<E> forAll() { - HqlAndParametersBuilder<E> hqlAndParametersBuilder = topiaDAO.getHqlForNoConstraint(); - return getNextStep(hqlAndParametersBuilder); - } - - protected TopiaQueryBuilderAddCriteriaStep<E> getNextStep(HqlAndParametersBuilder<E> hqlAndParametersBuilder) { - TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(topiaDAO, hqlAndParametersBuilder); - return nextStep; - } - - // shortcuts to next step - - public TopiaQueryBuilderAddCriteriaStep<E> addEquals(String property, Object value) { - return forAll().addEquals(property, value); - } - - public TopiaQueryBuilderAddCriteriaStep<E> addIn(String property, Iterable<Object> values) { - return forAll().addIn(property, values); - } - - public TopiaQueryBuilderAddCriteriaStep<E> addContains(String property, Object value) { - return forAll().addContains(property, value); - } - - public TopiaQueryBuilderAddCriteriaStep<E> addNull(String property) { - return forAll().addNull(property); - } - - public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(Set<String> orderByArguments) { - return forAll().setOrderByArguments(orderByArguments); - } - - public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(String... orderByArguments) { - return forAll().setOrderByArguments(orderByArguments); - } - - } - - public static class TopiaQueryBuilderAddCriteriaStep<E extends TopiaEntity> { - - protected AbstractTopiaDAO<E> topiaDAO; - - protected HqlAndParametersBuilder<E> hqlAndParametersBuilder; - - protected boolean hasOrderByClause = false; - - protected TopiaQueryBuilderAddCriteriaStep(AbstractTopiaDAO<E> topiaDAO, HqlAndParametersBuilder<E> hqlAndParametersBuilder) { - this.topiaDAO = topiaDAO; - this.hqlAndParametersBuilder = hqlAndParametersBuilder; - } - - public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(Set<String> orderByArguments) { - hasOrderByClause = ! Iterables.isEmpty(orderByArguments); - hqlAndParametersBuilder.setOrderByArguments(orderByArguments); - return getNextStep(); - } - - public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(String... orderByArguments) { - hqlAndParametersBuilder.setOrderByArguments(orderByArguments); - return getNextStep(); - } - - public TopiaQueryBuilderAddCriteriaStep<E> addEquals(String property, Object value) { - hqlAndParametersBuilder.addEquals(property, value); - return this; - } - - public TopiaQueryBuilderAddCriteriaStep<E> addIn(String property, Iterable<Object> values) { - hqlAndParametersBuilder.addIn(property, values); - return this; - } - - public TopiaQueryBuilderAddCriteriaStep<E> addContains(String property, Object value) { - hqlAndParametersBuilder.addContains(property, value); - return this; - } - - public TopiaQueryBuilderAddCriteriaStep<E> addNull(String property) { - hqlAndParametersBuilder.addNull(property); - return this; - } - - public void addTopiaIdEquals(String property, String topiaId) { - hqlAndParametersBuilder.addTopiaIdEquals(property, topiaId); - } - - public void addTopiaIdIn(String property, Iterable<String> topiaIds) { - hqlAndParametersBuilder.addTopiaIdIn(property, topiaIds); - } - - // shortcuts to next step - - public boolean exists() { - return getNextStep().exists(); - } - - public E findAnyOrNull() { - return getNextStep().findAnyOrNull(); - } - - public E findUniqueOrNull() { - return getNextStep().findUniqueOrNull(); - } - - public E findAny() { - return getNextStep().findAny(); - } - - public E findUnique() { - return getNextStep().findUnique(); - } - - public E findFirst() { - return getNextStep().findFirst(); - } - - public E findFirstOrNull() { - return getNextStep().findFirstOrNull(); - } - - public Optional<E> tryFindAny() { - return getNextStep().tryFindAny(); - } - - public Optional<E> tryFindFirst() { - return getNextStep().tryFindFirst(); - } - - public Optional<E> tryFindUnique() { - return getNextStep().tryFindUnique(); - } - - public List<E> findAll() { - return getNextStep().findAll(); - } - - public List<E> findAll(int startIndex, int endIndex) { - return getNextStep().findAll(startIndex, endIndex); - } - - public Iterable<E> findAllLazy() { - return getNextStep().findAllLazy(); - } - - public long count() { - return getNextStep().count(); - } - - public List<String> findAllIds(int startIndex, int endIndex) { - return getNextStep().findAllIds(startIndex, endIndex); - } - - public List<String> findAllIds() { - return getNextStep().findAllIds(); - } - - protected TopiaQueryBuilderRunQueryStep<E> getNextStep() { - String hql = hqlAndParametersBuilder.getHql(); - Map<String, Object> hqlParameters = hqlAndParametersBuilder.getHqlParameters(); - TopiaQueryBuilderRunQueryStep nextStep = new TopiaQueryBuilderRunQueryStep(topiaDAO, hql, hqlParameters); - return nextStep; - } - - } - - public static class TopiaQueryBuilderRunQueryStep<E extends TopiaEntity> { - - protected final String hql; - - protected final Map<String, Object> hqlParameters; - - protected AbstractTopiaDAO<E> topiaDAO; - - protected TopiaQueryBuilderRunQueryStep(AbstractTopiaDAO<E> topiaDAO, String hql, Map<String, Object> hqlParameters) { - this.hql = hql; - this.hqlParameters = hqlParameters; - this.topiaDAO = topiaDAO; - } - - public boolean exists() { - return topiaDAO.exists(hql, hqlParameters); - } - - public long count() { - String hqlWithSelectClause = "select count(topiaId) " + hql; - return topiaDAO.count(hqlWithSelectClause, hqlParameters); - } - - public E findUnique() { - return topiaDAO.findUnique(hql, hqlParameters); - } - - public E findUniqueOrNull() { - return topiaDAO.findUniqueOrNull(hql, hqlParameters); - } - - public Optional<E> tryFindUnique() { - return topiaDAO.tryFindUnique(hql, hqlParameters); - } - - public E findFirst() { - return topiaDAO.findFirst(hql, hqlParameters); - } - - public E findFirstOrNull() { - return topiaDAO.findFirstOrNull(hql, hqlParameters); - } - - public Optional<E> tryFindFirst() { - return topiaDAO.tryFindFirst(hql, hqlParameters); - } - - public E findAny() { - return topiaDAO.findAny(hql, hqlParameters); - } - - public E findAnyOrNull() { - return topiaDAO.findAnyOrNull(hql, hqlParameters); - } - - public Optional<E> tryFindAny() { - return topiaDAO.tryFindAny(hql, hqlParameters); - } - - public List<E> findAll() { - return topiaDAO.findAll(hql, hqlParameters); - } - - public Iterable<E> findAllLazy() { - return topiaDAO.findAllLazy(hql, hqlParameters); - } - - public List<E> findAll(int startIndex, int endIndex) { - return topiaDAO.findAll(hql, hqlParameters, startIndex, endIndex); - } - - public List<String> findAllIds() { - // XXX brendan 30/09/13 does this truely work ? - String hqlWithSelectClause = "select topiaId " + hql; - return topiaDAO.findAll(hqlWithSelectClause, hqlParameters, String.class); - } - - public List<String> findAllIds(int startIndex, int endIndex) { - // XXX brendan 30/09/13 does this truely work ? - String hqlWithSelectClause = "select topiaId " + hql; - return topiaDAO.findAll(hqlWithSelectClause, hqlParameters, String.class, startIndex, endIndex); - } - - } - - protected boolean exists(String hql, Map<String, Object> hqlParameters) { - List<E> entities = topiaJpaSupport.find(hql, 0, 0, hqlParameters); - boolean exists = !entities.isEmpty(); - return exists; - } - - protected long count(String hql, Map<String, Object> hqlParameters) { - Preconditions.checkArgument(hql.toLowerCase().trim().startsWith("select count(")); - Long count = findUnique(hql, hqlParameters, Long.class); - return count; - } - - protected E findUnique(String hql, Map<String, Object> hqlParameters) { - E unique = findUnique(hql, hqlParameters, getEntityClass()); - return unique; - } - - protected E findUniqueOrNull(String hql, Map<String, Object> hqlParameters) { - E uniqueOrNull = findUniqueOrNull(hql, hqlParameters, getEntityClass()); - return uniqueOrNull; - } - - protected Optional<E> tryFindUnique(String hql, Map<String, Object> hqlParameters) { - Optional<E> uniqueOrNull = tryFindUnique(hql, hqlParameters, getEntityClass()); - return uniqueOrNull; - } - - protected E findFirst(String hql, Map<String, Object> hqlParameters) { - E firstOrNull = findFirst(hql, hqlParameters, getEntityClass()); - return firstOrNull; - } - - protected E findFirstOrNull(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findFirstOrNull(hql, hqlParameters, getEntityClass()); - return anyOrNull; - } - - protected Optional<E> tryFindFirst(String hql, Map<String, Object> hqlParameters) { - Optional<E> anyOrNull = tryFindFirst(hql, hqlParameters, getEntityClass()); - return anyOrNull; - } - - protected E findAny(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findAny(hql, hqlParameters, getEntityClass()); - return anyOrNull; - } - - protected E findAnyOrNull(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findAnyOrNull(hql, hqlParameters, getEntityClass()); - return anyOrNull; - } - - protected Optional<E> tryFindAny(String hql, Map<String, Object> hqlParameters) { - Optional<E> anyOrNull = tryFindAny(hql, hqlParameters, getEntityClass()); - return anyOrNull; - } - - protected <R> R findUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); - if (uniqueOrNull == null) { - // TODO brendan 30/09/13 throw another exception if no result - throw new TopiaException("query " + hql + " returns no elements"); - } - return uniqueOrNull; - } - - protected <R> Optional<R> tryFindUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); - return Optional.fromNullable(uniqueOrNull); - } - - protected <R> R findUniqueOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { - List<R> results = findAll(hql, hqlParameters, type, 0, 1); - // If there is more than 1 result, throw an exception - if (results.size() > 1) { - String message = String.format( - "The query '%s' returns more than 1 unique result", hql); - // TODO AThimel 02/08/13 Throw another exception if more than 1 result is found - throw new TopiaException(message); - } - // otherwise return the first one, or null - R uniqueOrNull = Iterables.getOnlyElement(results, null); - return uniqueOrNull; - } - - protected <R> R findFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R firstOrNull = findFirstOrNull(hql, hqlParameters, type); - if (firstOrNull == null) { - // TODO brendan 30/09/13 throw another exception if no result - throw new TopiaException("query " + hql + " returns no elements"); - } - return firstOrNull; - } - - protected <R> Optional<R> tryFindFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R firstOrNull = findFirstOrNull(hql, hqlParameters, type); - return Optional.fromNullable(firstOrNull); - } - - protected <R> R findFirstOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { - Preconditions.checkArgument(hql.toLowerCase().contains("order by")); - R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - return anyOrNull; - } - - protected <R> R findAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - if (anyOrNull == null) { - // TODO brendan 30/09/13 throw another exception if no result - throw new TopiaException("query " + hql + " returns no elements"); - } - return anyOrNull; - } - - protected <R> Optional<R> tryFindAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - return Optional.fromNullable(anyOrNull); - } - - protected <R> R findAnyOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - List<R> results = findAll(hql, hqlParameters, type, 0, 0); - R anyOrNull = Iterables.getOnlyElement(results, null); - return anyOrNull; - } - - protected List<E> findAll(String hql, Map<String, Object> hqlParameters) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - List<E> all = topiaJpaSupport.findAll(hql, hqlParameters); - return all; - } - - protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - List<R> all = topiaJpaSupport.findAll(hql, hqlParameters); - return all; - } - - protected List<E> findAll(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - List<E> all = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); - return all; - } - - protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, int startIndex, int endIndex) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - List<R> all = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); - return all; - } - - protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, TopiaPagerBean pager) { - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - Preconditions.checkNotNull(pager); - - if (StringUtils.isNotBlank(pager.getSortColumn())) { - hql += " order by " + pager.getSortColumn(); - if (!pager.isSortAscendant()) { - hql += " desc"; - } - } - - List<R> result = topiaJpaSupport.find( - hql, - (int) pager.getRecordStartIndex(), - (int) pager.getRecordEndIndex() - 1, - hqlParameters); - - return result; - - } - - protected Iterable<E> findAllLazy(String hql, Map<String, Object> hqlParameters) { - - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - - final Iterator<E> iterator = new FindAllIterator<E, E>(this, - getEntityClass(), - batchSize, - hql, - hqlParameters); - Iterable<E> result = new Iterable<E>() { - @Override - public Iterator<E> iterator() { - return iterator; - } - }; - return result; - } - - protected <R> Iterable<R> findAllLazy(String hql, Map<String, Object> hqlParameters, Class<R> type) { - - Preconditions.checkNotNull(hql); - Preconditions.checkNotNull(hqlParameters); - Preconditions.checkNotNull(type); - - final Iterator<R> iterator = new FindAllIterator<E, R>(this, - type, - batchSize, - hql, - hqlParameters); - Iterable<R> result = new Iterable<R>() { - @Override - public Iterator<R> iterator() { - return iterator; - } - }; - return result; - } - - public TopiaQueryBuilderRunQueryStep<E> forTopiaId(String topiaId) { - Preconditions.checkNotNull(topiaId, "given topiaId is null"); - return forEquals(TopiaEntity.PROPERTY_TOPIA_ID, topiaId); - } - - public TopiaQueryBuilderRunQueryStep<E> forTopiaIds(Iterable<String> topiaIds) { - Preconditions.checkNotNull(topiaIds, "given topiaIds is null"); - return forIn(TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); - } - - @Deprecated - public E findByTopiaId(String id) { - return forTopiaId(id).findUniqueOrNull(); - } - - @Deprecated - public E findByProperty(String propertyName, Object value) { - return newQueryBuilder().forProperties(propertyName, value).findAnyOrNull(); - } - - @Override - public <R> R findByQuery(Class<R> type, String hql, Object... propertyNamesAndValues) { - Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - return findAny(hql, properties, type); - } - - @Override - public E findByPrimaryKey(Map<String, Object> keys) { - return newQueryBuilder().forProperties(keys).findUniqueOrNull(); - } - - @Override - @Deprecated - public E findByPrimaryKey(Object... propertyNamesAndValues) { - Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - E byPrimaryKey = findByPrimaryKey(properties); - return byPrimaryKey; - } - - @Override - public List<E> findAllWithOrder(String... propertyNames) { - return newQueryBuilder().forAll().setOrderByArguments(propertyNames).findAll(); - } - - @Override - public <R> List<R> findAllByQuery(Class<R> type, String hql, Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - List<R> all = findAll(hql, hqlParameters, type); - return all; - } - - @Deprecated - public E findByProperties(String propertyName, Object value, - Object... propertyNamesAndValues) { - return newQueryBuilder().forProperties(propertyName, value, propertyNamesAndValues).findUniqueOrNull(); - } - - @Deprecated - public E findByProperties(Map<String, Object> properties) { - return newQueryBuilder().forProperties(properties).findUniqueOrNull(); - } - - public List<E> findAll() { - return newQueryBuilder().forAll().findAll(); - } - - public List<String> findAllIds() { - List<String> find = newQueryBuilder().forAll().findAllIds(); - return find; - } - - @Deprecated - public List<E> findAllByProperty(String propertyName, Object value) { - List<E> all = newQueryBuilder().forProperties(propertyName, value).findAll(); - return all; - } - - @Deprecated - public List<E> findAllByProperties(String propertyName, Object value, Object... propertyNamesAndValues) { - List<E> all = newQueryBuilder().forProperties(propertyName, value, propertyNamesAndValues).findAll(); - return all; - } - - @Deprecated - public List<E> findAllByProperties(Map<String, Object> properties) { - List<E> all = newQueryBuilder().forProperties(properties).findAll(); - return all; - } - - - public E findContains(String propertyName, Object value) { - E find = newQueryBuilder().forAll().addContains(propertyName, value).findAny(); - return find; - } - - - public List<E> findAllContains(String propertyName, - Object value) { - List<E> find = newQueryBuilder().forAll().addContains(propertyName, value).findAll(); - return find; - } - - @Deprecated - public boolean existByTopiaId(String topiaId) { - boolean exists = forTopiaId(topiaId).exists(); - return exists; - } - - @Deprecated - public boolean existByProperties(String propertyName, Object propertyValue, - Object... propertyNamesAndValues) { - boolean exists = newQueryBuilder().forProperties(propertyName, propertyValue, propertyNamesAndValues).exists(); - return exists; - } - - public long count() { - long count = newQueryBuilder().forAll().count(); - return count; - } - - - @Deprecated - public boolean existsByQuery(String hql, Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - boolean exists = exists(hql, hqlParameters); - return exists; - } - - - @Deprecated - public long countByQuery(String hql, - Object... propertyNamesAndValues) { - - Preconditions.checkNotNull(StringUtils.isNotBlank(hql)); - Preconditions.checkArgument(hql.toUpperCase().trim().startsWith("SELECT COUNT(")); - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - long count = forHql(hql, hqlParameters).count(); - return count; - } - - - @Deprecated - public E findByQuery(String hql, - Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - E uniqueOrNull = forHql(hql, hqlParameters).findUniqueOrNull(); - return uniqueOrNull; - } - - - @Deprecated - public List<E> findAllByQuery(String hql, - Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - List<E> all = forHql(hql, hqlParameters).findAll(); - return all; - } - - - @Deprecated - public Iterable<E> findAllLazyByQuery(String hql, - Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - Iterable<E> result = findAllLazy(hql, hqlParameters); - return result; - } - - - @Deprecated - public <R> Iterable<R> findAllLazyByQuery(Class<R> type, - String hql, - Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - Iterable<R> result = findAllLazy(hql, hqlParameters, type); - return result; - } - - - @Deprecated - public Iterable<E> findAllLazyByQuery(int batchSize, - String hql, - Object... propertyNamesAndValues) { - return findAllLazyByQuery(getEntityClass(), batchSize, hql, propertyNamesAndValues); - } - - - /** - * @deprecated use {@link #findAllLazy(String, java.util.Map, Class)} - */ - @Deprecated - public <R> Iterable<R> findAllLazyByQuery(Class<R> type, - int batchSize, - String hql, - Object... propertyNamesAndValues) { - setBatchSize(batchSize); - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - Iterable<R> allLazy = findAllLazy(hql, hqlParameters, type); - return allLazy; - } - - - @Deprecated - public <R> List<R> findAllByQueryWithBound(Class<R> type, - String hql, - int startIndex, - int endIndex, - Object... propertyNamesAndValues) { - Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); - List<R> all = findAll(hql, hqlParameters, type, startIndex, endIndex); - return all; - } - - - @Deprecated - public List<E> findAllByQueryWithBound(String hql, - int startIndex, - int endIndex, - Object... propertyNamesAndValues) { - List<E> result = findAllByQueryWithBound(getEntityClass(), - hql, - startIndex, - endIndex, - propertyNamesAndValues); - return result; - } - - - @Deprecated - public <R> List<R> findAllByQueryAndPager(Class<R> type, - String hql, - TopiaPagerBean pager, - Object... propertyNamesAndValues) { - Preconditions.checkNotNull(pager); - Preconditions.checkNotNull(hql); - - if (StringUtils.isNotBlank(pager.getSortColumn())) { - hql += " ORDER BY " + pager.getSortColumn(); - if (!pager.isSortAscendant()) { - hql += " DESC"; - } - } - List<R> result = findAllByQueryWithBound(type, hql, - (int) pager.getRecordStartIndex(), - (int) pager.getRecordEndIndex() - 1, - propertyNamesAndValues); - return result; - } - - - @Deprecated - public List<E> findAllByQueryAndPager(String hql, - TopiaPagerBean pager, - Object... propertyNamesAndValues) { - - List<E> result = findAllByQueryAndPager(getEntityClass(), - hql, - pager, - propertyNamesAndValues); - return result; - } - - - public void computeAndAddRecordsToPager(String hql, - TopiaPagerBean pager, - Object... propertyNamesAndValues) { - - long records = countByQuery(hql, propertyNamesAndValues); - - pager.setRecords(records); - PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); - } - - /** - * package locale method because this is hibernate specific method and - * we don't want expose it. - * - * @return the meta-data of the entity - * @throws org.nuiton.topia.TopiaException if any pb - */ - protected ClassMetadata getClassMetadata() { - ClassMetadata meta = topiaHibernateSupport.getHibernateFactory() - .getClassMetadata(getEntityClass()); - if (meta == null) { - meta = topiaHibernateSupport.getHibernateFactory().getClassMetadata( - getTopiaEntityEnum().getImplementationFQN()); - } - return meta; - } - - public static class FindAllIterator<E extends TopiaEntity, R> implements Iterator<R> { - - protected Iterator<R> data; - - protected final AbstractTopiaDAO<E> dao; - - protected final Class<R> type; - - protected final String hql; - - protected final Map<String, Object> params; - - protected TopiaPagerBean pager; - - public FindAllIterator(AbstractTopiaDAO<E> dao, - Class<R> type, - int batchSize, - String hql, - Map<String, Object> params) { - this.dao = dao; - this.type = type; - this.hql = hql; - this.params = params; - - String hql2 = hql.toLowerCase(); - int i = hql2.indexOf("order by"); - if (i == -1) { - throw new IllegalStateException( - "must have a *order by* in hql, " + - "but did not find it in " + hql); - } - - // get the count (removing the order-by) - long count2 = dao.count("SELECT COUNT(*) " + - hql.substring(0, i), params); - pager = new TopiaPagerBean(); - pager.setRecords(count2); - pager.setPageSize(batchSize); - PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); - - // empty iterator (will be changed at first next call) - data = Iterators.emptyIterator(); - } - - - public boolean hasNext() { - boolean result = data.hasNext() || // no more data - pager.getPageIndex() < pager.getPagesNumber(); - return result; - } - - - public R next() { - if (!hasNext()) { - throw new NoSuchElementException(); - } - - if (!data.hasNext()) { - - // must load iterator - - // increments page index - pager.setPageIndex(pager.getPageIndex() + 1); - PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); - - // load new window of data - data = dao.findAll(hql, params, type, pager).iterator(); - - } - - R next = data.next(); - return next; - } - - - public void remove() { - throw new UnsupportedOperationException( - "This iterator does not support remove operation."); - } - } - - @Override - public List<Permission> getRequestPermission(String topiaId, int actions) { - throw new UnsupportedOperationException(); - } - - @Override - public TopiaContextImplementor getContext() { - throw new UnsupportedOperationException(); - } - - @Override - public TopiaContext getTopiaContext() { - throw new UnsupportedOperationException(); - } - -} //TopiaDAOImpl Copied: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java (from rev 2846, trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDAO.java) =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,1000 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/* * + * TopiaDAOAbstract.java + * + * Created: 31 déc. 2005 13:10:34 + * + * @author poussin <poussin@codelutin.com> + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ + +package org.nuiton.topia.persistence; + +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterables; +import com.google.common.collect.Iterators; +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; +import org.hibernate.metadata.ClassMetadata; +import org.nuiton.topia.TopiaDaoSupplier; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaHibernateSupport; +import org.nuiton.topia.TopiaJpaSupport; +import org.nuiton.topia.TopiaListenableSupport; +import org.nuiton.topia.TopiaSqlSupport; +import org.nuiton.topia.event.TopiaEntityListener; +import org.nuiton.topia.event.TopiaEntityVetoable; +import org.nuiton.topia.framework.TopiaFiresSupport; +import org.nuiton.topia.framework.TopiaUtil; +import org.nuiton.topia.persistence.pager.TopiaPagerBean; +import org.nuiton.util.PagerBeanUtil; + +import java.lang.reflect.InvocationTargetException; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; + +/** + * Cette classe permet d'avoir un ensemble de méthode implantée de façon + * standard et plus spécifiquement pour Hibernate. + * <p/> + * Certains accès à Hibernate sont tout de même fait ici, car on a pris le choix + * de se baser entièrement sur hibernate pour la persistence, et il est ainsi + * possible d'accèder au meta information hibernate sur les classes lorque l'on + * en a besoin. + * + * @param <E> le type de l'entite + * @author bpoussin <poussin@codelutin.com> + * @version $Id$ + */ +public abstract class AbstractTopiaDao<E extends TopiaEntity> extends LegacyTopiaDao<E> { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + private static Log log = LogFactory.getLog(AbstractTopiaDao.class); + + /** + * Default batch size used to iterate on data. + * + * @since 2.6.14 + */ + protected int batchSize = 1000; + + protected TopiaHibernateSupport topiaHibernateSupport; + + protected TopiaJpaSupport topiaJpaSupport; + + protected TopiaSqlSupport topiaSqlSupport; + + protected TopiaListenableSupport topiaListenableSupport; + + protected TopiaIdFactory topiaIdFactory; + + protected TopiaFiresSupport topiaFiresSupport; + + protected TopiaDaoSupplier topiaDaoSupplier; + + public abstract TopiaEntityEnum getTopiaEntityEnum(); + + public abstract Class<E> getEntityClass(); + + /** + * When AbstractTopiaContext create the TopiaDAOHibernate, it must call this + * method just after. + */ + public void init( + TopiaHibernateSupport topiaHibernateSupport, + TopiaJpaSupport topiaJpaSupport, + TopiaSqlSupport topiaSqlSupport, + TopiaListenableSupport topiaListenableSupport, + TopiaIdFactory topiaIdFactory, + TopiaFiresSupport topiaFiresSupport, + TopiaDaoSupplier topiaDaoSupplier) { + if (log.isDebugEnabled()) { + log.debug("init dao for " + getEntityClass()); + } + this.topiaHibernateSupport = topiaHibernateSupport; + this.topiaJpaSupport = topiaJpaSupport; + this.topiaSqlSupport = topiaSqlSupport; + this.topiaListenableSupport = topiaListenableSupport; + this.topiaIdFactory = topiaIdFactory; + this.topiaFiresSupport = topiaFiresSupport; + this.topiaDaoSupplier = topiaDaoSupplier; + } + + public TopiaFiresSupport getTopiaFiresSupport() { + return topiaFiresSupport; + } + + @Override + public int getBatchSize() { + return batchSize; + } + + @Override + public void setBatchSize(int batchSize) { + this.batchSize = batchSize; + } + + protected String newFromClause(String alias) { + String hql = "from " + getTopiaEntityEnum().getImplementationFQN(); + if (StringUtils.isNotBlank(alias)) { + hql += " " + alias; + } + return hql; + } + + @Override + public E newInstance() { + if (log.isTraceEnabled()) { + log.trace("entityClass = " + getEntityClass()); + } + Class<E> implementation = (Class<E>) + getTopiaEntityEnum().getImplementation(); + try { + E newInstance = implementation.newInstance(); + return newInstance; + } catch (InstantiationException e) { + throw new TopiaException( + "Impossible de trouver ou d'instancier la classe " + + implementation); + } catch (IllegalAccessException e) { + throw new TopiaException( + "Impossible de trouver ou d'instancier la classe " + + implementation); + } + } + + @Override + public void addTopiaEntityListener(TopiaEntityListener listener) { + topiaListenableSupport.addTopiaEntityListener(getEntityClass(), listener); + } + + @Override + public void addTopiaEntityVetoable(TopiaEntityVetoable vetoable) { + topiaListenableSupport.addTopiaEntityVetoable(getEntityClass(), vetoable); + } + + @Override + public void removeTopiaEntityListener(TopiaEntityListener listener) { + topiaListenableSupport.removeTopiaEntityListener(getEntityClass(), listener); + } + + @Override + public void removeTopiaEntityVetoable(TopiaEntityVetoable vetoable) { + topiaListenableSupport.removeTopiaEntityVetoable(getEntityClass(), vetoable); + } + + @Override + public E create(E entity) { + try { + // first set topiaId + if (StringUtils.isBlank(entity.getTopiaId())) { + + // only set id if not already on + String topiaId = topiaIdFactory.newTopiaId(getEntityClass(), entity); + entity.setTopiaId(topiaId); + } + + if (entity instanceof TopiaEntityContextable) { + TopiaEntityContextable contextable = (TopiaEntityContextable) entity; + contextable.setTopiaDAOSupplier(this.topiaDaoSupplier); + } + + // save entity + topiaHibernateSupport.getHibernateSession().save(entity); + topiaFiresSupport.warnOnCreateEntity(entity); + return entity; + } catch (HibernateException eee) { + throw new TopiaException(eee); + } + } + + @Override + public E create(String propertyName, Object propertyValue, Object... otherPropertyNamesAndValues) { + Map<String, Object> properties = + TopiaUtil.convertPropertiesArrayToMap(propertyName, propertyValue, otherPropertyNamesAndValues); + E result = create(properties); + return result; + } + + @Override + public E create(Map<String, Object> properties) { + + E result = newInstance(); + + try { + for (Map.Entry<String, Object> e : properties.entrySet()) { + String propertyName = e.getKey(); + Object value = e.getValue(); + PropertyUtils.setProperty(result, propertyName, value); + } + } catch (IllegalAccessException eee) { + throw new IllegalArgumentException( + "Can't put properties on new Object", eee); + } catch (InvocationTargetException eee) { + throw new IllegalArgumentException( + "Can't put properties on new Object", eee); + } catch (NoSuchMethodException eee) { + throw new IllegalArgumentException( + "Can't put properties on new Object", eee); + } + + create(result); + + return result; + } + + @Override + public E create() { + E result = newInstance(); + create(result); + return result; + } + + @Override + public Iterable<E> createAll(Iterable<E> entities) { + for (E entity : entities) { + create(entity); + } + return entities; + } + + @Override + public E update(E entity) { + try { + topiaHibernateSupport.getHibernateSession().saveOrUpdate(entity); + topiaFiresSupport.warnOnUpdateEntity(entity); + return entity; + } catch (HibernateException eee) { + throw new TopiaException(eee); + } + } + + @Override + public Iterable<E> updateAll(Iterable<E> entities) { + for (E entity : entities) { + update(entity); + } + return entities; + } + + @Override + public void delete(E entity) { + try { + topiaHibernateSupport.getHibernateSession().delete(entity); + entity.notifyDeleted(); + topiaFiresSupport.warnOnDeleteEntity(entity); + } catch (HibernateException eee) { + throw new TopiaException(eee); + } + } + + @Override + public void deleteAll(Iterable<E> entities) { + for (E entity : entities) { + delete(entity); + } + } + + protected HqlAndParametersBuilder<E> newHqlAndParametersBuilder() { + return new HqlAndParametersBuilder<E>(getEntityClass()); + } + + protected HqlAndParametersBuilder<E> getHqlForProperties(String propertyName, + Object propertyValue, + Object... otherPropertyNamesAndValues) { + Map<String, Object> properties = + TopiaUtil.convertPropertiesArrayToMap(propertyName, propertyValue, otherPropertyNamesAndValues); + return getHqlForProperties(properties); + } + + protected HqlAndParametersBuilder<E> getHqlForNoConstraint() { + Map<String, Object> properties = Collections.emptyMap(); + return getHqlForProperties(properties); + } + + protected HqlAndParametersBuilder<E> getHqlForProperties(Map<String, Object> properties) { + HqlAndParametersBuilder<E> hqlAndParametersBuilder = newHqlAndParametersBuilder(); + for (Map.Entry<String, Object> property : properties.entrySet()) { + hqlAndParametersBuilder.addEquals(property.getKey(), property.getValue()); + } + return hqlAndParametersBuilder; + } + + protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql) { + Map<String, Object> properties = Collections.emptyMap(); + return forHql(hql, properties); + } + + protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, Map<String, Object> hqlParameters) { + return new TopiaQueryBuilderRunQueryStep(this, hql, hqlParameters); + } + + protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, String parameterName, + Object parameterValue, + Object... otherParameterNamesAndValues) { + Map<String, Object> hqlParameters = + TopiaUtil.convertPropertiesArrayToMap(parameterName, parameterValue, otherParameterNamesAndValues); + return forHql(hql, hqlParameters); + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> forProperties(Map<String, Object> properties) { + HqlAndParametersBuilder<E> hqlAndParametersBuilder = getHqlForProperties(properties); + TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return nextStep; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> forProperties(String propertyName, + Object propertyValue, + Object... otherPropertyNamesAndValues) { + HqlAndParametersBuilder<E> hqlAndParametersBuilder = getHqlForProperties(propertyName, propertyValue, otherPropertyNamesAndValues); + TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return nextStep; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> newQueryBuilder() { + HqlAndParametersBuilder<E> hqlAndParametersBuilder = newHqlAndParametersBuilder(); + TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return nextStep; + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> forContains(String propertyName, Object propertyValue) { + return newQueryBuilder().addContains(propertyName, propertyValue).getNextStep(); + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> forEquals(String propertyName, Object propertyValue) { + return newQueryBuilder().addEquals(propertyName, propertyValue).getNextStep(); + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> forIn(String propertyName, Iterable<Object> propertyValues) { + return newQueryBuilder().addIn(propertyName, propertyValues).getNextStep(); + } + + protected boolean exists(String hql, Map<String, Object> hqlParameters) { + List<E> entities = topiaJpaSupport.find(hql, 0, 0, hqlParameters); + boolean exists = !entities.isEmpty(); + return exists; + } + + protected long count(String hql, Map<String, Object> hqlParameters) { + Preconditions.checkArgument(hql.toLowerCase().trim().startsWith("select count(")); + Long count = findUnique(hql, hqlParameters, Long.class); + return count; + } + + protected E findUnique(String hql, Map<String, Object> hqlParameters) { + E unique = findUnique(hql, hqlParameters, getEntityClass()); + return unique; + } + + protected E findUniqueOrNull(String hql, Map<String, Object> hqlParameters) { + E uniqueOrNull = findUniqueOrNull(hql, hqlParameters, getEntityClass()); + return uniqueOrNull; + } + + protected Optional<E> tryFindUnique(String hql, Map<String, Object> hqlParameters) { + Optional<E> uniqueOrNull = tryFindUnique(hql, hqlParameters, getEntityClass()); + return uniqueOrNull; + } + + protected E findFirst(String hql, Map<String, Object> hqlParameters) { + E firstOrNull = findFirst(hql, hqlParameters, getEntityClass()); + return firstOrNull; + } + + protected E findFirstOrNull(String hql, Map<String, Object> hqlParameters) { + E anyOrNull = findFirstOrNull(hql, hqlParameters, getEntityClass()); + return anyOrNull; + } + + protected Optional<E> tryFindFirst(String hql, Map<String, Object> hqlParameters) { + Optional<E> anyOrNull = tryFindFirst(hql, hqlParameters, getEntityClass()); + return anyOrNull; + } + + protected E findAny(String hql, Map<String, Object> hqlParameters) { + E anyOrNull = findAny(hql, hqlParameters, getEntityClass()); + return anyOrNull; + } + + protected E findAnyOrNull(String hql, Map<String, Object> hqlParameters) { + E anyOrNull = findAnyOrNull(hql, hqlParameters, getEntityClass()); + return anyOrNull; + } + + protected Optional<E> tryFindAny(String hql, Map<String, Object> hqlParameters) { + Optional<E> anyOrNull = tryFindAny(hql, hqlParameters, getEntityClass()); + return anyOrNull; + } + + protected <R> R findUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); + if (uniqueOrNull == null) { + // TODO brendan 30/09/13 throw another exception if no result + throw new TopiaException("query " + hql + " returns no elements"); + } + return uniqueOrNull; + } + + protected <R> Optional<R> tryFindUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); + return Optional.fromNullable(uniqueOrNull); + } + + protected <R> R findUniqueOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { + List<R> results = findAll(hql, hqlParameters, type, 0, 1); + // If there is more than 1 result, throw an exception + if (results.size() > 1) { + String message = String.format( + "The query '%s' returns more than 1 unique result", hql); + // TODO AThimel 02/08/13 Throw another exception if more than 1 result is found + throw new TopiaException(message); + } + // otherwise return the first one, or null + R uniqueOrNull = Iterables.getOnlyElement(results, null); + return uniqueOrNull; + } + + protected <R> R findFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R firstOrNull = findFirstOrNull(hql, hqlParameters, type); + if (firstOrNull == null) { + // TODO brendan 30/09/13 throw another exception if no result + throw new TopiaException("query " + hql + " returns no elements"); + } + return firstOrNull; + } + + protected <R> Optional<R> tryFindFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R firstOrNull = findFirstOrNull(hql, hqlParameters, type); + return Optional.fromNullable(firstOrNull); + } + + protected <R> R findFirstOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { + Preconditions.checkArgument(hql.toLowerCase().contains("order by")); + R anyOrNull = findAnyOrNull(hql, hqlParameters, type); + return anyOrNull; + } + + protected <R> R findAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R anyOrNull = findAnyOrNull(hql, hqlParameters, type); + if (anyOrNull == null) { + // TODO brendan 30/09/13 throw another exception if no result + throw new TopiaException("query " + hql + " returns no elements"); + } + return anyOrNull; + } + + protected <R> Optional<R> tryFindAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { + R anyOrNull = findAnyOrNull(hql, hqlParameters, type); + return Optional.fromNullable(anyOrNull); + } + + protected <R> R findAnyOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + List<R> results = findAll(hql, hqlParameters, type, 0, 0); + R anyOrNull = Iterables.getOnlyElement(results, null); + return anyOrNull; + } + + protected List<E> findAll(String hql, Map<String, Object> hqlParameters) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + List<E> all = topiaJpaSupport.findAll(hql, hqlParameters); + return all; + } + + protected List<E> findAll(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + List<E> all = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + return all; + } + + protected List<E> findAll(String hql, Map<String, Object> hqlParameters, TopiaPagerBean pager) { + List<E> all = findAll(hql, hqlParameters, getEntityClass(), pager); + return all; + } + + protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + Preconditions.checkNotNull(type); + List<R> all = topiaJpaSupport.findAll(hql, hqlParameters); + return all; + } + + protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, int startIndex, int endIndex) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + Preconditions.checkNotNull(type); + List<R> all = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + return all; + } + + protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, TopiaPagerBean pager) { + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + Preconditions.checkNotNull(type); + Preconditions.checkNotNull(pager); + + if (StringUtils.isNotBlank(pager.getSortColumn())) { + hql += " order by " + pager.getSortColumn(); + if (!pager.isSortAscendant()) { + hql += " desc"; + } + } + + List<R> result = topiaJpaSupport.find( + hql, + (int) pager.getRecordStartIndex(), + (int) pager.getRecordEndIndex() - 1, + hqlParameters); + + return result; + } + + protected Iterable<E> findAllLazy(String hql, Map<String, Object> hqlParameters) { + Iterable<E> allLazy = findAllLazy(hql, hqlParameters, getEntityClass()); + return allLazy; + } + + protected <R> Iterable<R> findAllLazy(String hql, Map<String, Object> hqlParameters, Class<R> type) { + + Preconditions.checkNotNull(hql); + Preconditions.checkNotNull(hqlParameters); + Preconditions.checkNotNull(type); + + final Iterator<R> iterator = new FindAllIterator<E, R>(this, + type, + batchSize, + hql, + hqlParameters); + + Iterable<R> result = new Iterable<R>() { + @Override + public Iterator<R> iterator() { + return iterator; + } + }; + + return result; + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> forTopiaIdEquals(String topiaId) { + Preconditions.checkNotNull(topiaId, "given topiaId is null"); + return forEquals(TopiaEntity.PROPERTY_TOPIA_ID, topiaId); + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> forTopiaIdIn(Iterable<String> topiaIds) { + Preconditions.checkNotNull(topiaIds, "given topiaIds is null"); + return forIn(TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); + } + + @Override + public List<String> findAllIds() { + List<String> find = newQueryBuilder().findAllIds(); + return find; + } + + @Override + public List<E> findAll() { + List<E> all = newQueryBuilder().findAll(); + return all; + } + + @Override + public Iterable<E> findAllLazy() { + String hql = "from " + getTopiaEntityEnum().getImplementationFQN() + " order by id"; + Map<String, Object> hqlParameters = Collections.emptyMap(); + Iterable<E> allLazy = findAllLazy(hql, hqlParameters); + return allLazy; + } + + @Override + public Iterator<E> iterator() { + Iterator<E> iterator = findAllLazy().iterator(); + return iterator; + } + + @Override + public long count() { + long count = newQueryBuilder().count(); + return count; + } + + /** + * package locale method because this is hibernate specific method and + * we don't want expose it. + * + * @return the meta-data of the entity + * @throws org.nuiton.topia.TopiaException if any pb + */ + protected ClassMetadata getClassMetadata() { + ClassMetadata meta = topiaHibernateSupport.getHibernateFactory() + .getClassMetadata(getEntityClass()); + if (meta == null) { + meta = topiaHibernateSupport.getHibernateFactory().getClassMetadata( + getTopiaEntityEnum().getImplementationFQN()); + } + return meta; + } + + public static class FindAllIterator<E extends TopiaEntity, R> implements Iterator<R> { + + protected Iterator<R> data; + + protected final AbstractTopiaDao<E> dao; + + protected final Class<R> type; + + protected final String hql; + + protected final Map<String, Object> params; + + protected TopiaPagerBean pager; + + public FindAllIterator(AbstractTopiaDao<E> dao, + Class<R> type, + int batchSize, + String hql, + Map<String, Object> params) { + this.dao = dao; + this.type = type; + this.hql = hql; + this.params = params; + + String hql2 = hql.toLowerCase(); + int i = hql2.indexOf("order by"); + if (i == -1) { + throw new IllegalStateException( + "must have a *order by* in hql, " + + "but did not find it in " + hql); + } + + // get the count (removing the order-by) + long count2 = dao.count("SELECT COUNT(*) " + + hql.substring(0, i), params); + pager = new TopiaPagerBean(); + pager.setRecords(count2); + pager.setPageSize(batchSize); + PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); + + // empty iterator (will be changed at first next call) + data = Iterators.emptyIterator(); + } + + + public boolean hasNext() { + boolean result = data.hasNext() || // no more data + pager.getPageIndex() < pager.getPagesNumber(); + return result; + } + + + public R next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + if (!data.hasNext()) { + + // must load iterator + + // increments page index + pager.setPageIndex(pager.getPageIndex() + 1); + PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); + + // load new window of data + data = dao.findAll(hql, params, type, pager).iterator(); + + } + + R next = data.next(); + return next; + } + + + public void remove() { + throw new UnsupportedOperationException( + "This iterator does not support remove operation."); + } + } + + public static class TopiaQueryBuilderAddCriteriaStep<E extends TopiaEntity> implements TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> { + + protected AbstractTopiaDao<E> topiaDAO; + + protected HqlAndParametersBuilder<E> hqlAndParametersBuilder; + + protected boolean hasOrderByClause = false; + + protected TopiaQueryBuilderAddCriteriaStep(AbstractTopiaDao<E> topiaDAO, HqlAndParametersBuilder<E> hqlAndParametersBuilder) { + this.topiaDAO = topiaDAO; + this.hqlAndParametersBuilder = hqlAndParametersBuilder; + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(Set<String> orderByArguments) { + hasOrderByClause = ! Iterables.isEmpty(orderByArguments); + hqlAndParametersBuilder.setOrderByArguments(orderByArguments); + return getNextStep(); + } + + @Override + public TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(String... orderByArguments) { + hqlAndParametersBuilder.setOrderByArguments(orderByArguments); + return getNextStep(); + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addEquals(String property, Object value) { + hqlAndParametersBuilder.addEquals(property, value); + return this; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addIn(String property, Iterable<Object> values) { + hqlAndParametersBuilder.addIn(property, values); + return this; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addContains(String property, Object value) { + hqlAndParametersBuilder.addContains(property, value); + return this; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addNull(String property) { + hqlAndParametersBuilder.addNull(property); + return this; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addTopiaIdEquals(String property, String topiaId) { + hqlAndParametersBuilder.addTopiaIdEquals(property, topiaId); + return this; + } + + @Override + public TopiaQueryBuilderAddCriteriaStep<E> addTopiaIdIn(String property, Iterable<String> topiaIds) { + hqlAndParametersBuilder.addTopiaIdIn(property, topiaIds); + return this; + } + + // shortcuts to next step + + @Override + public boolean exists() { + return getNextStep().exists(); + } + + @Override + public E findAnyOrNull() { + return getNextStep().findAnyOrNull(); + } + + @Override + public E findUniqueOrNull() { + return getNextStep().findUniqueOrNull(); + } + + @Override + public E findAny() { + return getNextStep().findAny(); + } + + @Override + public E findUnique() { + return getNextStep().findUnique(); + } + + @Override + public E findFirst() { + return getNextStep().findFirst(); + } + + @Override + public E findFirstOrNull() { + return getNextStep().findFirstOrNull(); + } + + @Override + public Optional<E> tryFindAny() { + return getNextStep().tryFindAny(); + } + + @Override + public Optional<E> tryFindFirst() { + return getNextStep().tryFindFirst(); + } + + @Override + public Optional<E> tryFindUnique() { + return getNextStep().tryFindUnique(); + } + + @Override + public List<E> findAll() { + return getNextStep().findAll(); + } + + @Override + public List<E> findAll(int startIndex, int endIndex) { + return getNextStep().findAll(startIndex, endIndex); + } + + @Override + public Iterable<E> findAllLazy() { + return getNextStep().findAllLazy(); + } + + @Override + public long count() { + return getNextStep().count(); + } + + @Override + public List<String> findAllIds(int startIndex, int endIndex) { + return getNextStep().findAllIds(startIndex, endIndex); + } + + @Override + public List<String> findAllIds() { + return getNextStep().findAllIds(); + } + + protected TopiaQueryBuilderRunQueryStep<E> getNextStep() { + String hql = hqlAndParametersBuilder.getHql(); + Map<String, Object> hqlParameters = hqlAndParametersBuilder.getHqlParameters(); + TopiaQueryBuilderRunQueryStep nextStep = new TopiaQueryBuilderRunQueryStep(topiaDAO, hql, hqlParameters); + return nextStep; + } + + } + + public static class TopiaQueryBuilderRunQueryStep<E extends TopiaEntity> implements org.nuiton.topia.persistence.TopiaQueryBuilderRunQueryStep<E> { + + protected final String hql; + + protected final Map<String, Object> hqlParameters; + + protected AbstractTopiaDao<E> topiaDAO; + + protected TopiaQueryBuilderRunQueryStep(AbstractTopiaDao<E> topiaDAO, String hql, Map<String, Object> hqlParameters) { + this.hql = hql; + this.hqlParameters = hqlParameters; + this.topiaDAO = topiaDAO; + } + + @Override + public boolean exists() { + return topiaDAO.exists(hql, hqlParameters); + } + + @Override + public long count() { + String hqlWithSelectClause = "select count(topiaId) " + hql; + return topiaDAO.count(hqlWithSelectClause, hqlParameters); + } + + @Override + public E findUnique() { + return topiaDAO.findUnique(hql, hqlParameters); + } + + @Override + public E findUniqueOrNull() { + return topiaDAO.findUniqueOrNull(hql, hqlParameters); + } + + @Override + public Optional<E> tryFindUnique() { + return topiaDAO.tryFindUnique(hql, hqlParameters); + } + + @Override + public E findFirst() { + return topiaDAO.findFirst(hql, hqlParameters); + } + + @Override + public E findFirstOrNull() { + return topiaDAO.findFirstOrNull(hql, hqlParameters); + } + + @Override + public Optional<E> tryFindFirst() { + return topiaDAO.tryFindFirst(hql, hqlParameters); + } + + @Override + public E findAny() { + return topiaDAO.findAny(hql, hqlParameters); + } + + @Override + public E findAnyOrNull() { + return topiaDAO.findAnyOrNull(hql, hqlParameters); + } + + @Override + public Optional<E> tryFindAny() { + return topiaDAO.tryFindAny(hql, hqlParameters); + } + + @Override + public List<E> findAll() { + return topiaDAO.findAll(hql, hqlParameters); + } + + @Override + public Iterable<E> findAllLazy() { + return topiaDAO.findAllLazy(hql, hqlParameters); + } + + @Override + public List<E> findAll(int startIndex, int endIndex) { + return topiaDAO.findAll(hql, hqlParameters, startIndex, endIndex); + } + + @Override + public List<String> findAllIds() { + // XXX brendan 30/09/13 does this truely work ? + String hqlWithSelectClause = "select topiaId " + hql; + return topiaDAO.findAll(hqlWithSelectClause, hqlParameters, String.class); + } + + @Override + public List<String> findAllIds(int startIndex, int endIndex) { + // XXX brendan 30/09/13 does this truely work ? + String hqlWithSelectClause = "select topiaId " + hql; + return topiaDAO.findAll(hqlWithSelectClause, hqlParameters, String.class, startIndex, endIndex); + } + + } + +} Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaDao.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaDao.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/LegacyTopiaDao.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,309 @@ +package org.nuiton.topia.persistence; + +import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaUtil; +import org.nuiton.topia.persistence.pager.TopiaPagerBean; +import org.nuiton.util.PagerBeanUtil; + +import java.security.Permission; +import java.util.List; +import java.util.Map; + +/** + * Implements deprecated method from {@link TopiaDAO}, should be deleted. + */ +public abstract class LegacyTopiaDao<E extends TopiaEntity> implements TopiaDAO<E> { + + @Override + public E create(Object... propertyNamesAndValues) { + Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + E result = create(properties); + return result; + } + + @Deprecated + public boolean existByTopiaId(String topiaId) { + return forTopiaIdEquals(topiaId).exists(); + } + + @Deprecated + public boolean existByProperties(String propertyName, Object propertyValue, + Object... propertyNamesAndValues) { + return forProperties(propertyName, propertyValue, propertyNamesAndValues).exists(); + } + + @Deprecated + protected String createSimpleQuery() { + return newFromClause(null); + } + + @Deprecated + public String createSimpleQuery(String alias) { + return newFromClause(alias); + } + + protected abstract String newFromClause(String alias); + + @Deprecated + public E findByTopiaId(String id) { + return forTopiaIdEquals(id).findUniqueOrNull(); + } + + @Deprecated + public E findByProperty(String propertyName, Object value) { + return forProperties(propertyName, value).findAnyOrNull(); + } + + @Override + @Deprecated + public <R> R findByQuery(Class<R> type, String hql, Object... propertyNamesAndValues) { + Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + return findAny(hql, properties, type); + } + + @Override + @Deprecated + public E findByPrimaryKey(Map<String, Object> keys) { + return forProperties(keys).findUniqueOrNull(); + } + + @Override + @Deprecated + public E findByPrimaryKey(Object... propertyNamesAndValues) { + Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + E byPrimaryKey = findByPrimaryKey(properties); + return byPrimaryKey; + } + + @Override + @Deprecated + public List<E> findAllWithOrder(String... propertyNames) { + return newQueryBuilder().setOrderByArguments(propertyNames).findAll(); + } + + @Override + @Deprecated + public <R> List<R> findAllByQuery(Class<R> type, String hql, Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + List<R> all = findAll(hql, hqlParameters, type); + return all; + } + + @Deprecated + public E findByProperties(String propertyName, Object value, + Object... propertyNamesAndValues) { + return forProperties(propertyName, value, propertyNamesAndValues).findUniqueOrNull(); + } + + @Deprecated + public E findByProperties(Map<String, Object> properties) { + return forProperties(properties).findUniqueOrNull(); + } + + @Deprecated + public List<E> findAllByProperty(String propertyName, Object value) { + List<E> all = forProperties(propertyName, value).findAll(); + return all; + } + + @Deprecated + public List<E> findAllByProperties(String propertyName, Object value, Object... propertyNamesAndValues) { + List<E> all = forProperties(propertyName, value, propertyNamesAndValues).findAll(); + return all; + } + + @Deprecated + public List<E> findAllByProperties(Map<String, Object> properties) { + return forProperties(properties).findAll(); + } + + @Deprecated + public E findContains(String propertyName, Object value) { + return newQueryBuilder().addContains(propertyName, value).findAny(); + } + + @Deprecated + public List<E> findAllContains(String propertyName, + Object value) { + return newQueryBuilder().addContains(propertyName, value).findAll(); + } + + @Deprecated + public boolean existsByQuery(String hql, Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + boolean exists = exists(hql, hqlParameters); + return exists; + } + + @Deprecated + public long countByQuery(String hql, + Object... propertyNamesAndValues) { + + Preconditions.checkNotNull(StringUtils.isNotBlank(hql)); + Preconditions.checkArgument(hql.toUpperCase().trim().startsWith("SELECT COUNT(")); + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + long count = forHql(hql, hqlParameters).count(); + return count; + } + + @Deprecated + public E findByQuery(String hql, + Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + E uniqueOrNull = forHql(hql, hqlParameters).findUniqueOrNull(); + return uniqueOrNull; + } + + @Deprecated + public List<E> findAllByQuery(String hql, + Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + List<E> all = forHql(hql, hqlParameters).findAll(); + return all; + } + + @Deprecated + public Iterable<E> findAllLazyByQuery(String hql, + Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + Iterable<E> result = findAllLazy(hql, hqlParameters); + return result; + } + + @Deprecated + public <R> Iterable<R> findAllLazyByQuery(Class<R> type, + String hql, + Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + Iterable<R> result = findAllLazy(hql, hqlParameters, type); + return result; + } + + @Deprecated + public Iterable<E> findAllLazyByQuery(int batchSize, + String hql, + Object... propertyNamesAndValues) { + return findAllLazyByQuery(getEntityClass(), batchSize, hql, propertyNamesAndValues); + } + + + /** + * @deprecated use {@link #findAllLazy(String, java.util.Map, Class)} + */ + @Deprecated + public <R> Iterable<R> findAllLazyByQuery(Class<R> type, + int batchSize, + String hql, + Object... propertyNamesAndValues) { + setBatchSize(batchSize); + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + Iterable<R> allLazy = findAllLazy(hql, hqlParameters, type); + return allLazy; + } + + @Deprecated + public <R> List<R> findAllByQueryWithBound(Class<R> type, + String hql, + int startIndex, + int endIndex, + Object... propertyNamesAndValues) { + Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(propertyNamesAndValues); + List<R> all = findAll(hql, hqlParameters, type, startIndex, endIndex); + return all; + } + + @Deprecated + public List<E> findAllByQueryWithBound(String hql, + int startIndex, + int endIndex, + Object... propertyNamesAndValues) { + List<E> result = findAllByQueryWithBound(getEntityClass(), + hql, + startIndex, + endIndex, + propertyNamesAndValues); + return result; + } + + + @Deprecated + public <R> List<R> findAllByQueryAndPager(Class<R> type, + String hql, + TopiaPagerBean pager, + Object... propertyNamesAndValues) { + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(hql); + + if (StringUtils.isNotBlank(pager.getSortColumn())) { + hql += " ORDER BY " + pager.getSortColumn(); + if (!pager.isSortAscendant()) { + hql += " DESC"; + } + } + List<R> result = findAllByQueryWithBound(type, hql, + (int) pager.getRecordStartIndex(), + (int) pager.getRecordEndIndex() - 1, + propertyNamesAndValues); + return result; + } + + @Deprecated + public List<E> findAllByQueryAndPager(String hql, + TopiaPagerBean pager, + Object... propertyNamesAndValues) { + + List<E> result = findAllByQueryAndPager(getEntityClass(), + hql, + pager, + propertyNamesAndValues); + return result; + } + + @Deprecated + public void computeAndAddRecordsToPager(String hql, + TopiaPagerBean pager, + Object... propertyNamesAndValues) { + + long records = countByQuery(hql, propertyNamesAndValues); + + pager.setRecords(records); + PagerBeanUtil.computeRecordIndexesAndPagesNumber(pager); + } + + @Override + public List<Permission> getRequestPermission(String topiaId, int actions) { + throw new UnsupportedOperationException(); + } + + @Override + public TopiaContextImplementor getContext() { + throw new UnsupportedOperationException(); + } + + + @Override + public TopiaContext getTopiaContext() { + throw new UnsupportedOperationException(); + } + + + // let's use method written in AbstractTopiaDao + + protected abstract Iterable<E> findAllLazy(String hql, Map<String, Object> hqlParameters); + + protected abstract <R> R findAny(String hql, Map<String, Object> properties, Class<R> type); + + protected abstract TopiaQueryBuilderRunQueryStep<E> forHql(String hql, Map<String, Object> hqlParameters); + + protected abstract <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type); + + protected abstract <R> Iterable<R> findAllLazy(String hql, Map<String, Object> hqlParameters, Class<R> type); + + protected abstract <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, int startIndex, int endIndex); + + protected abstract boolean exists(String hql, Map<String, Object> hqlParameters); + +} Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -38,8 +38,6 @@ import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.event.TopiaEntityListener; -import org.nuiton.topia.event.TopiaEntityVetoable; import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.persistence.pager.TopiaPagerBean; @@ -60,23 +58,16 @@ * @author fdesbois <fdesbois@codelutin.com> * @author tchemit <chemit@codelutin.com> * @version $Id$ + * @deprecated prefer {@link TopiaDao} */ -public interface TopiaDAO<E extends TopiaEntity> extends Iterable<E> { +@Deprecated +public interface TopiaDAO<E extends TopiaEntity> extends TopiaDao<E> { //------------------------------------------------------------------------// //-- Create - update - delete methods ------------------------------------// //------------------------------------------------------------------------// /** - * Create a new instance of managed entity <strong>not persisted</strong>. - * - * @return new entity instance - * @throws TopiaException if any pb while creating the entity - * @since 2.3.1 - */ - E newInstance() throws TopiaException; - - /** * Creates a new instance of the entity managed by the DAO * * @param propertyNamesAndValues the list of properties that the created entity will have. Arguments are key-value @@ -87,57 +78,10 @@ * if some property type is not the * expected one * @see #create(Map) + * @deprecated use {@link TopiaDao#create(String, Object, Object...)} */ E create(Object... propertyNamesAndValues) throws TopiaException; - /** - * Creates a new instance of the entity managed by the DAO - * - * @param properties the key-value list of properties that the created entity will have. - * @return the newly created entity - * @throws TopiaException if any problem during instantiation - * @throws IllegalArgumentException if some property type is not the - * expected one - */ - E create(Map<String, Object> properties) throws TopiaException; - - /** - * Creates an entity not created without the DAO. Generally used when DAO - * is layered after a service layer where the {@link #create(Object...)} - * would be to onerous. - * - * @param e the instance to persist - * @return the persisted entity (with its topiaId valued) - * @throws TopiaException if any problem while creating data - * @since 2.3.1 - */ - E create(E e) throws TopiaException; - - /** - * Update an entity. May be used for an entity coming from another context. - * - * @param e the entity to create or update - * @return the given entity - * @throws TopiaException if any problem while updating datas - */ - E update(E e) throws TopiaException; - - /** - * Removes the given entity from the storage - * - * @param e the entity to remove - * @throws TopiaException if any problem while deleting datas - */ - void delete(E e) throws TopiaException; - - /** - * Permet de supprimer des entités. - * - * @param entities les entités à supprimer - * @throws TopiaException if any pb while deleting datas - */ - void deleteAll(Iterable<E> entities) throws TopiaException; - //------------------------------------------------------------------------// //-- findByXXX methods ---------------------------------------------------// //------------------------------------------------------------------------// @@ -149,6 +93,7 @@ * @param id topiaId of the entity to found * @return the entity found or null if not * @throws TopiaException for Topia errors on query + * @deprecated ambiguous signature use new API and inline {@link TopiaDao#forTopiaIdEquals(String)} */ E findByTopiaId(String id) throws TopiaException; @@ -159,6 +104,7 @@ * @param value value of the property to match * @return the first entity matching the request * @throws TopiaException if any pb while getting datas + * @deprecated @deprecated use new API and inline {@link LegacyTopiaDao#findByProperties(String, Object, Object...)} */ E findByProperty(String propertyName, Object value) throws TopiaException; @@ -172,6 +118,7 @@ * [propertyName;value;propertyName;value;...] * @return the first entity matching the request * @throws TopiaException if any pb while getting datas + * @deprecated @deprecated use new API and inline {@link LegacyTopiaDao#findByProperties(String, Object, Object...)} */ E findByProperties(String propertyName, Object value, @@ -183,6 +130,7 @@ * @param properties the properties key + value to match * @return the first entity matching the request * @throws TopiaException if any pb while getting datas + * @deprecated @deprecated use new API and inline {@link LegacyTopiaDao#findByProperties(java.util.Map)} */ E findByProperties(Map<String, Object> properties) throws TopiaException; @@ -195,6 +143,7 @@ * @return the entity E found or null * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your code inside the DAO and use a suitable protected method */ E findByQuery(String hql, Object... propertyNamesAndValues) throws TopiaException; @@ -212,6 +161,7 @@ * @throws TopiaException if any pb while getting datas * @throws ClassCastException if the found type is not the expected one * @since 2.6.12 + * @deprecated move your code inside the DAO and use a suitable protected method */ <R> R findByQuery(Class<R> type, String hql, @@ -225,6 +175,7 @@ * @param keys Map with the natural id property name as Map.key, and value as Map.value * @return the entity E found or null * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findByPrimaryKey(java.util.Map)} */ E findByPrimaryKey(Map<String, Object> keys) throws TopiaException; @@ -236,6 +187,7 @@ * [propertyName;value;propertyName;value;...] * @return the entity E found or null * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findByPrimaryKey(Object...)} */ E findByPrimaryKey(Object... propertyNamesAndValues) throws TopiaException; @@ -248,6 +200,7 @@ * @return the entity E found or null * @throws TopiaException if any pb while getting datas * @since 2.5.4 + * @deprecated use new API and inline {@link LegacyTopiaDao#findContains(String, Object)} */ E findContains(String propertyName, Object value) throws TopiaException; @@ -257,14 +210,6 @@ //------------------------------------------------------------------------// /** - * Finds all the entities managed by this DAO. - * - * @return the full list of entities - * @throws TopiaException if any pb while getting datas - */ - List<E> findAll() throws TopiaException; - - /** * Finds all entites E managed by this DAO. The returned list will be ordered according to the given * {@code propertyNames}. * <p/> @@ -274,19 +219,11 @@ * @param propertyNames property names of order to apply * @return all entities E of the dao entity type with given order * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findAllWithOrder(String...)} */ List<E> findAllWithOrder(String... propertyNames) throws TopiaException; /** - * Find all the ids for the E entity type - * - * @return the full list of ids - * @throws TopiaException if any pb while getting datas - */ - // TODO AThimel 20/07/13 This method should return a Set ? - List<String> findAllIds() throws TopiaException; - - /** * Finds all entities E which value for the given {@code propertyName} is * {@code value} * @@ -294,6 +231,7 @@ * @param value value to expect * @return the list of entities E having the given value * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findAllByProperties(String, Object, Object...)} */ List<E> findAllByProperty(String propertyName, Object value) throws TopiaException; @@ -308,6 +246,7 @@ * [propertyName;value;propertyName;value;...] * @return the list of entities E having the given value * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findAllByProperties(String, Object, Object...)} */ List<E> findAllByProperties(String propertyName, Object value, @@ -319,6 +258,7 @@ * @param properties properties to match * @return the list of entities E having the given values * @throws TopiaException if any pb while getting datas + * @deprecated use new API and inline {@link LegacyTopiaDao#findAllByProperties(java.util.Map)} */ List<E> findAllByProperties(Map<String, Object> properties) throws TopiaException; @@ -331,6 +271,7 @@ * @return the list of entities E found by the query and parameters * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map)} */ List<E> findAllByQuery(String hql, Object... propertyNamesAndValues) throws TopiaException; @@ -348,6 +289,7 @@ * @return entites of the query result * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map, Class)} */ <R> List<R> findAllByQuery(Class<R> type, String hql, @@ -367,6 +309,7 @@ * @return entites E of the query result * @throws TopiaException if any pb while getting datas * @since 2.6.14 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAllLazy(String, java.util.Map, Class)} */ Iterable<E> findAllLazyByQuery(String hql, Object... propertyNamesAndValues) throws TopiaException; @@ -384,6 +327,7 @@ * @return entites R of the query result * @throws TopiaException if any pb while getting datas * @since 2.6.14 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAllLazy(String, java.util.Map, Class)} */ <R> Iterable<R> findAllLazyByQuery(Class<R> type, String hql, @@ -402,6 +346,7 @@ * @return entites E of the query result * @throws TopiaException if any pb while getting datas * @since 2.6.14 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAllLazy(String, java.util.Map)} */ Iterable<E> findAllLazyByQuery(int batchSize, String hql, @@ -421,6 +366,7 @@ * @return entites R of the query result * @throws TopiaException if any pb while getting datas * @since 2.6.14 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAllLazy(String, java.util.Map, Class)} */ <R> Iterable<R> findAllLazyByQuery(Class<R> type, int batchSize, @@ -441,6 +387,7 @@ * @return entites E of the paginated query result * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map, int, int)} */ List<E> findAllByQueryWithBound(String hql, int startIndex, @@ -462,6 +409,7 @@ * @return entites R of the paginated query result * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map, Class, int, int)} */ <R> List<R> findAllByQueryWithBound(Class<R> type, String hql, @@ -481,6 +429,7 @@ * @throws TopiaException if any pb while getting datas * @see TopiaPagerBean * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map, org.nuiton.topia.persistence.pager.TopiaPagerBean)} */ List<E> findAllByQueryAndPager(String hql, TopiaPagerBean pager, @@ -499,6 +448,7 @@ * @throws TopiaException if any pb while getting datas * @see TopiaPagerBean * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#findAll(String, java.util.Map, Class, org.nuiton.topia.persistence.pager.TopiaPagerBean)} */ <R> List<R> findAllByQueryAndPager(Class<R> type, String hql, @@ -514,6 +464,7 @@ * @return all the entities E found * @throws TopiaException if any pb while getting datas * @since 2.5.4 + * @deprecated use new API and inline {@link LegacyTopiaDao#findAllContains(String, Object)} */ List<E> findAllContains(String propertyName, Object value) throws TopiaException; @@ -529,6 +480,7 @@ * @return true if entity exists, false otherwise * @throws TopiaException for any error * @since 2.3.4 + * @deprecated use new API and inline {@link LegacyTopiaDao#existByTopiaId(String)} */ boolean existByTopiaId(String id) throws TopiaException; @@ -544,6 +496,7 @@ * @return true if entity exists, false otherwise * @throws TopiaException for Topia errors * @since 2.3.4 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#exists(String, java.util.Map)} */ boolean existByProperties(String propertyName, Object propertyValue, @@ -558,6 +511,7 @@ * @return true if entity exists, false otherwise * @throws TopiaException * @since 2.6.12 + * @deprecated move your code inside the DAO and use {@link AbstractTopiaDao#exists(String, java.util.Map)} */ boolean existsByQuery(String hql, Object... propertyNamesAndValues) throws TopiaException; @@ -567,15 +521,6 @@ //------------------------------------------------------------------------// /** - * Count the number of existing entities. - * - * @return number of total entities - * @throws TopiaException if any pb while getting datas - * @since 2.3.4 - */ - long count() throws TopiaException; - - /** * Count the number of entities based on a the given HQL query. * * @param hql the HQL query to use @@ -584,6 +529,7 @@ * @return number of entities filtered by the query * @throws TopiaException if any pb while getting datas * @since 2.6.12 + * @deprecated move your call to inside the DAO and use {@link AbstractTopiaDao#count(String, java.util.Map)} */ long countByQuery(String hql, Object... propertyNamesAndValues) throws TopiaException; @@ -593,33 +539,6 @@ //------------------------------------------------------------------------// /** - * Find usages of the given {@code entity} in the entities of the given - * {@code type}. - * - * @param type the type of entity to search - * @param entity the entity on which search is done - * @param <R> type of entity to search - * @return the list of entities R which uses the given entity - * @throws TopiaException if any problem while getting data - * @since 2.3.0 - */ - <R extends TopiaEntity> List<R> findUsages(Class<R> type, - E entity) throws TopiaException; - - /** - * Find all usages of the given {@code entity}. - * - * @param entity the entity - * @return the dictionnary of usages of the given entities (keys are entity - * usage container, values are the list of this type of entity to - * use the given entity). - * @throws TopiaException if any pb while getting data - * @since 2.3.0 - */ - - Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> findAllUsages(E entity) throws TopiaException; - - /** * Execute the count {@code hql} query and then synch the pager to this * result (says fill the * {@link TopiaPagerBean#records} field and then adapt @@ -632,6 +551,8 @@ * @throws TopiaException if any pb while getting datas * @see TopiaPagerBean * @since 2.6.12 + * // TODO brendan 17/10/13 really ?? + * @deprecated */ void computeAndAddRecordsToPager(String hql, TopiaPagerBean pager, @@ -642,13 +563,6 @@ //------------------------------------------------------------------------// /** - * Return the class of the entity managed by this DAO. - * - * @return this DAO's managed entity's class - */ - Class<E> getEntityClass(); - - /** * Returns the context used by this DAO. * * @return Returns the context. @@ -669,28 +583,12 @@ * Get the entityEnum of the type of entity managed by this DAO. * * @return entity type enum managed by this DAO + * @deprecated you should get it from persistence context or application context */ + @Deprecated TopiaEntityEnum getTopiaEntityEnum(); /** - * Obtains the batch size used to load data. - * <p/> - * Default value if 1000. - * - * @return the batch size. - * @since 2.6.14 - */ - int getBatchSize(); - - /** - * Set a new default batch size. - * - * @param batchSize new batch size to use when iterating. - * @since 2.6.14 - */ - void setBatchSize(int batchSize); - - /** * Create the simple HQL query for the entity managed by the dao. * <p/> * A optional alias can be passed: @@ -705,22 +603,6 @@ @Deprecated String createSimpleQuery(String alias); - //------------------------------------------------------------------------// - //-- Listener methods ----------------------------------------------------// - //------------------------------------------------------------------------// - - // TODO AThimel 20/07/13 Javadoc - void addTopiaEntityListener(TopiaEntityListener listener); - - // TODO AThimel 20/07/13 Javadoc - void addTopiaEntityVetoable(TopiaEntityVetoable vetoable); - - // TODO AThimel 20/07/13 Javadoc - void removeTopiaEntityListener(TopiaEntityListener listener); - - // TODO AThimel 20/07/13 Javadoc - void removeTopiaEntityVetoable(TopiaEntityVetoable vetoable); - /** * Retourne les permissions a verifier pour l'acces a l'entite pour le * service Taas. Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -85,8 +85,7 @@ * @version $Id$ */ -public class TopiaDAOImpl<E extends TopiaEntity> implements - TopiaDAO<E> { // TopiaDAOImpl +public class TopiaDAOImpl<E extends TopiaEntity> implements TopiaDAO<E> { // TopiaDAOImpl /** to use log facility, just put in your code: log.info(\"...\"); */ private static Log log = LogFactory.getLog(TopiaDAOImpl.class); Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,214 @@ +package org.nuiton.topia.persistence; + +import org.nuiton.topia.event.TopiaEntityListener; +import org.nuiton.topia.event.TopiaEntityVetoable; + +import java.util.List; +import java.util.Map; + +/** + * This contract represents the common operation any DAO should + * be able to provide as API. + * + * @author bleny + * @since 3.0 + */ +public interface TopiaDao<E extends TopiaEntity> extends Iterable<E> { + + /** + * Return the class of the entity managed by this DAO. + * + * @return this DAO's managed entity's class + */ + Class<E> getEntityClass(); + + /** + * Obtains the batch size used to load data. + * + * Default value if 1000. + * + * @return the batch size. + * @since 2.6.14 + */ + int getBatchSize(); + + /** + * Set a new default batch size. + * + * @param batchSize new batch size to use when iterating. + * @since 2.6.14 + */ + void setBatchSize(int batchSize); + + /** + * Create a new instance of managed entity <strong>not persisted</strong>. + * + * @since 2.3.1 + */ + E newInstance(); + + /** + * Creates an entity not created without the DAO using any of the others + * create methods. The instance may have been created elsewhere. + * + * @param entity the instance to persist + * @return the persisted entity (with its topiaId valued) + * @since 2.3.1 + */ + E create(E entity); + + /** + * @since 3.0 + */ + E create(String propertyName, Object propertyValue, Object... otherPropertyNamesAndValues); + + /** + * Creates a new instance of the entity managed by the DAO + * + * @param properties the key-value list of properties that the created entity will have. + * @return the newly created entity + * @throws IllegalArgumentException if some property type is not the + * expected one + */ + E create(Map<String, Object> properties); + + /** + * @since 3.0 + */ + E create(); + + /** + * Update an entity. May be used for an entity coming from another context. + * + * @param entity the entity to create or update + * @return the given entity + */ + E update(E entity); + + /** + * Deletes the given entity from the storage + * + * @param entity the entity to remove + */ + void delete(E entity); + + /** + * Deletes all given entities from the storage + * + * @param entities entities to delete + * @since 3.0 + */ + void deleteAll(Iterable<E> entities); + + /** + * Finds all the entities managed by this DAO. + * + * @return the full list of entities in no particular + * (non-determinisic) order + */ + List<E> findAll(); + + /** + * @since 3.0 + */ + Iterable<E> findAllLazy(); + + /** + * @since 3.0 + */ + Iterable<E> createAll(Iterable<E> entities); + + /** + * @since 3.0 + */ + Iterable<E> updateAll(Iterable<E> entities); + + /** + * Find all the ids for the entities managed by this DAO. + * + * @return the ids of all the entities + */ + // TODO AThimel 20/07/13 This method should return a Set ? + List<String> findAllIds(); + + /** + * Count the number of existing entities. + * + * @return number of total entities + * @since 2.3.4 + */ + long count(); + + /** + * @since 3.0 + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> forProperties(Map<String, Object> properties); + + /** + * @since 3.0 + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> forProperties(String propertyName, + Object propertyValue, + Object... otherPropertyNamesAndValues); + /** + * @since 3.0 + */ + TopiaQueryBuilderAddCriteriaStep<E> newQueryBuilder(); + + /** + * @since 3.0 + */ + TopiaQueryBuilderRunQueryStep<E> forContains(String propertyName, Object propertyValue); + + /** + * @since 3.0 + */ + TopiaQueryBuilderRunQueryStep<E> forEquals(String propertyName, Object propertyValue); + + /** + * @since 3.0 + */ + TopiaQueryBuilderRunQueryStep<E> forIn(String propertyName, Iterable<Object> propertyValues); + + /** + * @since 3.0 + */ + TopiaQueryBuilderRunQueryStep<E> forTopiaIdEquals(String topiaId); + + /** + * @since 3.0 + */ + TopiaQueryBuilderRunQueryStep<E> forTopiaIdIn(Iterable<String> topiaIds); + + void addTopiaEntityListener(TopiaEntityListener listener); + + void addTopiaEntityVetoable(TopiaEntityVetoable vetoable); + + void removeTopiaEntityListener(TopiaEntityListener listener); + + void removeTopiaEntityVetoable(TopiaEntityVetoable vetoable); + + /** + * Find usages of the given {@code entity} in the entities of the given + * {@code type}. + * + * @param type the type of entity to search + * @param entity the entity on which search is done + * @param <R> type of entity to search + * @return the list of entities R which uses the given entity + * @since 2.3.0 + */ + <R extends TopiaEntity> List<R> findUsages(Class<R> type, E entity); + + /** + * Find all usages of the given {@code entity}. + * + * @param entity the entity + * @return the dictionnary of usages of the given entities (keys are entity + * usage container, values are the list of this type of entity to + * use the given entity). + * @since 2.3.0 + */ + Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> findAllUsages(E entity); + +} Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityAbstract.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityAbstract.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityAbstract.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -208,7 +208,7 @@ if (fireSupport == null) { if (this instanceof TopiaEntityContextable) { TopiaEntityContextable contextable = (TopiaEntityContextable) this; - AbstractTopiaDAO topiaDAO = (AbstractTopiaDAO)contextable.getTopiaDAOSupplier().getDao(getClass()); + AbstractTopiaDao topiaDAO = (AbstractTopiaDao)contextable.getTopiaDAOSupplier().getDao(getClass()); fireSupport = topiaDAO.getTopiaFiresSupport(); } } Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaOrRunQueryStep.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaOrRunQueryStep.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaOrRunQueryStep.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,5 @@ +package org.nuiton.topia.persistence; + +public interface TopiaQueryBuilderAddCriteriaOrRunQueryStep<E extends TopiaEntity> extends TopiaQueryBuilderAddCriteriaStep<E>, TopiaQueryBuilderRunQueryStep<E> { + +} Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderAddCriteriaStep.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,59 @@ +package org.nuiton.topia.persistence; + +import java.util.Set; + +/** + * Represents a step when building a query to add a constraint. + * + * The builder implements the fluent interface DP, so you can add multiple + * constraints by chaining calls. + * + * @author bleny + * @since 3.0 + */ +public interface TopiaQueryBuilderAddCriteriaStep<E extends TopiaEntity> { + + /** + * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property. + * @param value the value the field of the entity must be equals to argument + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addEquals(String property, Object value); + + /** + * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property + * @param values the value the field of the entity must be equals to argument + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addIn(String property, Iterable<Object> values); + + /** + * @param property the name of a field of the queried entity, must be + * a one-to-many or a many-to-many property + * @param value the value the field of the entity must be equals to argument + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addContains(String property, Object value); + + /** + * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addNull(String property); + + /** + * This method has the same behavior as {@link #addEquals(String, Object)} but + * you don't need to have the entity but only the topiaId. + * + * @param property the name of a field of the queried entity, must be + * a one-to-one or a many-to-one property + * @param topiaId the + */ + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addTopiaIdEquals(String property, String topiaId); + + TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> addTopiaIdIn(String property, Iterable<String> topiaIds); + + TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(Set<String> orderByArguments); + + TopiaQueryBuilderRunQueryStep<E> setOrderByArguments(String... orderByArguments); + +} Added: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderRunQueryStep.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderRunQueryStep.java (rev 0) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaQueryBuilderRunQueryStep.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -0,0 +1,44 @@ +package org.nuiton.topia.persistence; + +import com.google.common.base.Optional; + +import java.util.List; + +/** + * @author bleny + */ +public interface TopiaQueryBuilderRunQueryStep<E extends TopiaEntity> { + + boolean exists(); + + long count(); + + E findUnique(); + + E findUniqueOrNull(); + + Optional<E> tryFindUnique(); + + E findFirst(); + + E findFirstOrNull(); + + Optional<E> tryFindFirst(); + + E findAny(); + + E findAnyOrNull(); + + Optional<E> tryFindAny(); + + List<E> findAll(); + + Iterable<E> findAllLazy(); + + List<E> findAll(int startIndex, int endIndex); + + List<String> findAllIds(); + + List<String> findAllIds(int startIndex, int endIndex); + +} Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/GeneratedPersonneTopiaDao.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/GeneratedPersonneTopiaDao.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/GeneratedPersonneTopiaDao.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -27,7 +27,7 @@ import java.util.List; import java.util.Map; -import org.nuiton.topia.persistence.AbstractTopiaDAO; +import org.nuiton.topia.persistence.AbstractTopiaDao; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaEntityEnum; import org.nuiton.topiatest.Personne; @@ -35,7 +35,7 @@ /** * @author Arnaud Thimel <thimel@codelutin.com> */ -public abstract class GeneratedPersonneTopiaDao<E extends Personne> extends AbstractTopiaDAO<E> implements PersonneDao { +public abstract class GeneratedPersonneTopiaDao<E extends Personne> extends AbstractTopiaDao<E> implements PersonneDao { @Override public TopiaEntityEnum getTopiaEntityEnum() { Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/TopiaJpaSupportTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/TopiaJpaSupportTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/TopiaJpaSupportTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -28,10 +28,10 @@ import org.junit.Rule; import org.junit.Test; import org.nuiton.topiatest.Address; -import org.nuiton.topiatest.AddressDAO; +import org.nuiton.topiatest.AddressTopiaDao; import org.nuiton.topiatest.Gender; import org.nuiton.topiatest.Personne; -import org.nuiton.topiatest.PersonneDAO; +import org.nuiton.topiatest.PersonneTopiaDao; import java.util.List; @@ -47,8 +47,8 @@ protected TopiaTestTopiaPersistenceContext persistenceContext; protected TopiaJpaSupport jpaSupport; - protected AddressDAO addressDAO; - protected PersonneDAO personneDAO; + protected AddressTopiaDao addressDAO; + protected PersonneTopiaDao personneDAO; protected Address address; @Before Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaConnectionProviderTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -33,7 +33,8 @@ import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topia.test.entities.Person; -import org.nuiton.topia.test.entities.PersonDAO; +import org.nuiton.topia.test.entities.PersonDao; +import org.nuiton.topia.test.entities.PersonTopiaDao; import org.nuiton.topiatest.Personne; import java.io.File; @@ -125,7 +126,7 @@ TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); try { - PersonDAO dao = TopiaTestDAOHelper.getPersonDAO(transaction); + PersonTopiaDao dao = TopiaTestDAOHelper.getPersonDAO(transaction); Person personne = dao.create(Personne.PROPERTY_NAME, "Jack Bauer"); transaction.commitTransaction(); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaContextReplicateTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaContextReplicateTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaContextReplicateTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -27,15 +27,14 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaDatabase; import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topia.test.entities.Person; -import org.nuiton.topia.test.entities.PersonDAO; +import org.nuiton.topia.test.entities.PersonDao; import org.nuiton.topia.test.entities.Pet; -import org.nuiton.topia.test.entities.PetDAO; +import org.nuiton.topia.test.entities.PetDao; import java.io.File; import java.util.Properties; @@ -93,8 +92,8 @@ TopiaTestTopiaPersistenceContext txSource; TopiaTestTopiaPersistenceContext txTarget; - PersonDAO daoSource, daoTarget; - PetDAO petDAOSource, petDAOTarget; + PersonDao daoSource, daoTarget; + PetDao petDAOSource, petDAOTarget; Person personSource, personTarget; Pet petSource, petTarget; Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaTestCase.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaTestCase.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/generator/TopiaTestCase.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -35,9 +35,9 @@ import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topiatest.Company; -import org.nuiton.topiatest.CompanyDAO; +import org.nuiton.topiatest.CompanyDao; import org.nuiton.topiatest.Department; -import org.nuiton.topiatest.DepartmentDAO; +import org.nuiton.topiatest.DepartmentDao; /** * TopiaTestCase. @@ -123,8 +123,8 @@ // try { TopiaTestTopiaPersistenceContext newContext = db.beginTransaction(); - CompanyDAO companyDAO = newContext.getCompanyDao(); - DepartmentDAO departmentDAO = newContext.getDepartmentDao(); + CompanyDao companyDAO = newContext.getCompanyDao(); + DepartmentDao departmentDAO = newContext.getDepartmentDao(); Company company = companyDAO.create(); company.setName("Ma société"); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/EntityVisitorExportXmlTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/EntityVisitorExportXmlTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/EntityVisitorExportXmlTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -34,13 +34,13 @@ import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topiatest.Address; -import org.nuiton.topiatest.AddressDAO; +import org.nuiton.topiatest.AddressTopiaDao; import org.nuiton.topiatest.Company; -import org.nuiton.topiatest.CompanyDAO; +import org.nuiton.topiatest.CompanyTopiaDao; import org.nuiton.topiatest.Department; -import org.nuiton.topiatest.DepartmentDAO; +import org.nuiton.topiatest.DepartmentTopiaDao; import org.nuiton.topiatest.Employe; -import org.nuiton.topiatest.EmployeDAO; +import org.nuiton.topiatest.EmployeTopiaDao; /** * Test de visitor. @@ -72,14 +72,14 @@ TopiaTestTopiaPersistenceContext newContext = db.beginTransaction(); try { // company - CompanyDAO companyDAO = newContext.getCompanyDao(); + CompanyTopiaDao companyDAO = newContext.getCompanyDao(); Company clCompany = companyDAO.create(Company.PROPERTY_NAME, "CodeLutin"); // employe - EmployeDAO employeDAO = newContext.getEmployeDao(); + EmployeTopiaDao employeDAO = newContext.getEmployeDao(); Employe empl1 = employeDAO.create(Employe.PROPERTY_NAME, "boss", Employe.PROPERTY_SALARY, 30000); - AddressDAO adressDAO = newContext.getAddressDao(); + AddressTopiaDao adressDAO = newContext.getAddressDao(); Address addr1 = adressDAO.create(Address.PROPERTY_CITY, "Nantes", Address.PROPERTY_ADRESS, "12 Avenue Jules Vernes"); empl1.setAddress(addr1); @@ -88,7 +88,7 @@ empl2.setAddress(addr2); // departement - DepartmentDAO departmentDAO = newContext.getDepartmentDao(); + DepartmentTopiaDao departmentDAO = newContext.getDepartmentDao(); Department depComm = departmentDAO.create(Department.PROPERTY_NAME, "Commercial"); depComm.setLeader(empl1); @@ -117,7 +117,7 @@ TopiaTestTopiaPersistenceContext context = db.beginTransaction(); - CompanyDAO companyDAO = context.getCompanyDao(); + CompanyTopiaDao companyDAO = context.getCompanyDao(); Company clCompany = companyDAO.findByName("CodeLutin"); EntityVisitor delegateVisitor = new ExportXMLVisitor(); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -36,7 +36,7 @@ import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topiatest.NaturalizedEntity; -import org.nuiton.topiatest.NaturalizedEntityDAO; +import org.nuiton.topiatest.NaturalizedEntityDao; /** * NaturalIdTest @@ -61,7 +61,7 @@ log.debug("Test naturalId : create succesfull"); TopiaTestTopiaPersistenceContext persistenceContext = db.beginTransaction(); - NaturalizedEntityDAO dao = + NaturalizedEntityDao dao = persistenceContext.getNaturalizedEntityDao(); // No exception will be thrown with the two properties @@ -82,7 +82,7 @@ log.debug("Test naturalId : create failed"); TopiaTestTopiaPersistenceContext persistenceContext = db.beginTransaction(); - NaturalizedEntityDAO dao = + NaturalizedEntityDao dao = persistenceContext.getNaturalizedEntityDao(); // Exception will be throw @@ -108,7 +108,7 @@ TopiaTestTopiaPersistenceContext persistenceContext = db.beginTransaction(); - NaturalizedEntityDAO dao = + NaturalizedEntityDao dao = persistenceContext.getNaturalizedEntityDao(); NaturalizedEntity entity = @@ -131,7 +131,7 @@ TopiaTestTopiaPersistenceContext persistenceContext = db.beginTransaction(); - NaturalizedEntityDAO dao = + NaturalizedEntityDao dao = persistenceContext.getNaturalizedEntityDao(); NaturalizedEntity entity = @@ -149,7 +149,7 @@ TopiaTestTopiaPersistenceContext persistenceContext = db.beginTransaction(); - NaturalizedEntityDAO dao = + NaturalizedEntityDao dao = persistenceContext.getNaturalizedEntityDao(); dao.createByNaturalId(5, "str"); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -35,7 +35,7 @@ import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topia.test.entities.Person; -import org.nuiton.topia.test.entities.PersonDAO; +import org.nuiton.topia.test.entities.PersonDao; import java.util.List; @@ -55,7 +55,7 @@ protected TopiaTestTopiaPersistenceContext context; - protected PersonDAO dao; + protected PersonDao dao; @Before public void setup() throws TopiaException { Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/test/ano1882/DAOAbstractTransformerTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/test/ano1882/DAOAbstractTransformerTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/test/ano1882/DAOAbstractTransformerTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -26,7 +26,6 @@ import org.junit.Rule; import org.junit.Test; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaDatabase; import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; @@ -42,7 +41,7 @@ public void testAno1882() throws Exception { TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); - FrenchCompanyDAO dao = TopiaTestDAOHelper.getFrenchCompanyDAO(transaction); + FrenchCompanyDao dao = TopiaTestDAOHelper.getFrenchCompanyDAO(transaction); SIRETDAO siretDAO = TopiaTestDAOHelper.getSIRETDAO(transaction); SIRET siret = siretDAO.create(); FrenchCompany entity = Modified: trunk/topia-persistence/src/test/java/org/nuiton/topiatest/EnumTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topiatest/EnumTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topiatest/EnumTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -27,7 +27,6 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaDatabase; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestDAOHelper; @@ -54,7 +53,7 @@ public void storeEntityWithEnumValue() throws TopiaException { TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); - PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction); + PersonneDao dao = TopiaTestDAOHelper.getPersonneDAO(transaction); Personne personne = new PersonneImpl(); personne.setGender(Gender.FEMALE); personne.setOtherGender(Gender.MALE); Modified: trunk/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java 2013-10-17 16:14:18 UTC (rev 2846) +++ trunk/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java 2013-10-25 12:03:15 UTC (rev 2847) @@ -43,14 +43,13 @@ import org.junit.Assert; import org.junit.Rule; import org.junit.Test; -import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaDatabase; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaTestDAOHelper; import org.nuiton.topia.TopiaTestTopiaPersistenceContext; import org.nuiton.topiatest.Gender; import org.nuiton.topiatest.Personne; -import org.nuiton.topiatest.PersonneDAO; +import org.nuiton.topiatest.PersonneDao; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -85,7 +84,7 @@ TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); log.debug("DAO : PersonneDAO"); - PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction); + PersonneDao dao = TopiaTestDAOHelper.getPersonneDAO(transaction); log.debug("CREATE PERSONNE : Bob Marley"); Personne personne = dao.create(Personne.PROPERTY_NAME, "Bob Marley"); @@ -135,7 +134,7 @@ TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); - PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction); + PersonneDao dao = TopiaTestDAOHelper.getPersonneDAO(transaction); log.debug("CREATE PERSONNE : Bob Marley"); Personne personne = dao.create(Personne.PROPERTY_NAME, "Bob Marley"); @@ -186,7 +185,7 @@ TopiaTestTopiaPersistenceContext transaction = db.beginTransaction(); - PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction); + PersonneDao dao = TopiaTestDAOHelper.getPersonneDAO(transaction); Personne person = dao.newInstance(); Assert.assertNull(person.getTopiaId());