Author: athimel Date: 2013-10-30 18:42:49 +0100 (Wed, 30 Oct 2013) New Revision: 2852 Url: http://nuiton.org/projects/topia/repository/revisions/2852 Log: Add findByTopiaId methods Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.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-30 11:16:15 UTC (rev 2851) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/AbstractTopiaPersistenceContext.java 2013-10-30 17:42:49 UTC (rev 2852) @@ -292,7 +292,9 @@ @Override public <E extends TopiaEntity, D extends TopiaDAO<E>> D getDao(Class<E> entityClass, Class<D> daoClass) { - return (D) getDao(entityClass); + TopiaDAO<E> dao = getDao(entityClass); + D result = (D) dao; + return result; } @Override Modified: 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-30 11:16:15 UTC (rev 2851) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/AbstractTopiaDao.java 2013-10-30 17:42:49 UTC (rev 2852) @@ -311,7 +311,8 @@ } protected HqlAndParametersBuilder<E> newHqlAndParametersBuilder() { - return new HqlAndParametersBuilder<E>(getEntityClass()); + HqlAndParametersBuilder<E> result = new HqlAndParametersBuilder<E>(getEntityClass()); + return result; } protected HqlAndParametersBuilder<E> getHqlForProperties(String propertyName, @@ -319,29 +320,33 @@ Object... otherPropertyNamesAndValues) { Map<String, Object> properties = TopiaUtil.convertPropertiesArrayToMap(propertyName, propertyValue, otherPropertyNamesAndValues); - return getHqlForProperties(properties); + HqlAndParametersBuilder<E> result = getHqlForProperties(properties); + return result; } protected HqlAndParametersBuilder<E> getHqlForNoConstraint() { Map<String, Object> properties = Collections.emptyMap(); - return getHqlForProperties(properties); + HqlAndParametersBuilder<E> result = getHqlForProperties(properties); + return result; } protected HqlAndParametersBuilder<E> getHqlForProperties(Map<String, Object> properties) { - HqlAndParametersBuilder<E> hqlAndParametersBuilder = newHqlAndParametersBuilder(); + HqlAndParametersBuilder<E> result = newHqlAndParametersBuilder(); for (Map.Entry<String, Object> property : properties.entrySet()) { - hqlAndParametersBuilder.addEquals(property.getKey(), property.getValue()); + result.addEquals(property.getKey(), property.getValue()); } - return hqlAndParametersBuilder; + return result; } protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql) { Map<String, Object> properties = Collections.emptyMap(); - return forHql(hql, properties); + TopiaQueryBuilderRunQueryStep<E> result = forHql(hql, properties); + return result; } protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, Map<String, Object> hqlParameters) { - return new TopiaQueryBuilderRunQueryStep(this, hql, hqlParameters); + TopiaQueryBuilderRunQueryStep result = new TopiaQueryBuilderRunQueryStep(this, hql, hqlParameters); + return result; } protected TopiaQueryBuilderRunQueryStep<E> forHql(String hql, String parameterName, @@ -349,14 +354,15 @@ Object... otherParameterNamesAndValues) { Map<String, Object> hqlParameters = TopiaUtil.convertPropertiesArrayToMap(parameterName, parameterValue, otherParameterNamesAndValues); - return forHql(hql, hqlParameters); + TopiaQueryBuilderRunQueryStep<E> result = forHql(hql, hqlParameters); + return result; } @Override public TopiaQueryBuilderAddCriteriaStep<E> forProperties(Map<String, Object> properties) { HqlAndParametersBuilder<E> hqlAndParametersBuilder = getHqlForProperties(properties); - TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); - return nextStep; + TopiaQueryBuilderAddCriteriaStep result = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return result; } @Override @@ -364,101 +370,105 @@ Object propertyValue, Object... otherPropertyNamesAndValues) { HqlAndParametersBuilder<E> hqlAndParametersBuilder = getHqlForProperties(propertyName, propertyValue, otherPropertyNamesAndValues); - TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); - return nextStep; + TopiaQueryBuilderAddCriteriaStep result = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return result; } @Override public TopiaQueryBuilderAddCriteriaStep<E> newQueryBuilder() { HqlAndParametersBuilder<E> hqlAndParametersBuilder = newHqlAndParametersBuilder(); - TopiaQueryBuilderAddCriteriaStep nextStep = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); - return nextStep; + TopiaQueryBuilderAddCriteriaStep result = new TopiaQueryBuilderAddCriteriaStep(this, hqlAndParametersBuilder); + return result; } @Override public TopiaQueryBuilderRunQueryStep<E> forContains(String propertyName, Object propertyValue) { - return newQueryBuilder().addContains(propertyName, propertyValue).getNextStep(); + TopiaQueryBuilderRunQueryStep<E> result = newQueryBuilder().addContains(propertyName, propertyValue).getNextStep(); + return result; } @Override public TopiaQueryBuilderRunQueryStep<E> forEquals(String propertyName, Object propertyValue) { - return newQueryBuilder().addEquals(propertyName, propertyValue).getNextStep(); + TopiaQueryBuilderRunQueryStep<E> result = newQueryBuilder().addEquals(propertyName, propertyValue).getNextStep(); + return result; } @Override public TopiaQueryBuilderRunQueryStep<E> forIn(String propertyName, Iterable<Object> propertyValues) { - return newQueryBuilder().addIn(propertyName, propertyValues).getNextStep(); + TopiaQueryBuilderRunQueryStep<E> result = newQueryBuilder().addIn(propertyName, propertyValues).getNextStep(); + return result; } protected boolean exists(String hql, Map<String, Object> hqlParameters) { List<E> entities = topiaJpaSupport.find(hql, 0, 0, hqlParameters); - boolean exists = !entities.isEmpty(); - return exists; + boolean result = !entities.isEmpty(); + return result; } 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; + Long result = findUnique(hql, hqlParameters, Long.class); + return result; } protected E findUnique(String hql, Map<String, Object> hqlParameters) { - E unique = findUnique(hql, hqlParameters, getEntityClass()); - return unique; + E result = findUnique(hql, hqlParameters, getEntityClass()); + return result; } protected E findUniqueOrNull(String hql, Map<String, Object> hqlParameters) { - E uniqueOrNull = findUniqueOrNull(hql, hqlParameters, getEntityClass()); - return uniqueOrNull; + E result = findUniqueOrNull(hql, hqlParameters, getEntityClass()); + return result; } protected Optional<E> tryFindUnique(String hql, Map<String, Object> hqlParameters) { - Optional<E> uniqueOrNull = tryFindUnique(hql, hqlParameters, getEntityClass()); - return uniqueOrNull; + Optional<E> result = tryFindUnique(hql, hqlParameters, getEntityClass()); + return result; } protected E findFirst(String hql, Map<String, Object> hqlParameters) { - E firstOrNull = findFirst(hql, hqlParameters, getEntityClass()); - return firstOrNull; + E result = findFirst(hql, hqlParameters, getEntityClass()); + return result; } protected E findFirstOrNull(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findFirstOrNull(hql, hqlParameters, getEntityClass()); - return anyOrNull; + E result = findFirstOrNull(hql, hqlParameters, getEntityClass()); + return result; } protected Optional<E> tryFindFirst(String hql, Map<String, Object> hqlParameters) { - Optional<E> anyOrNull = tryFindFirst(hql, hqlParameters, getEntityClass()); - return anyOrNull; + Optional<E> result = tryFindFirst(hql, hqlParameters, getEntityClass()); + return result; } protected E findAny(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findAny(hql, hqlParameters, getEntityClass()); - return anyOrNull; + E result = findAny(hql, hqlParameters, getEntityClass()); + return result; } protected E findAnyOrNull(String hql, Map<String, Object> hqlParameters) { - E anyOrNull = findAnyOrNull(hql, hqlParameters, getEntityClass()); - return anyOrNull; + E result = findAnyOrNull(hql, hqlParameters, getEntityClass()); + return result; } protected Optional<E> tryFindAny(String hql, Map<String, Object> hqlParameters) { - Optional<E> anyOrNull = tryFindAny(hql, hqlParameters, getEntityClass()); - return anyOrNull; + Optional<E> result = tryFindAny(hql, hqlParameters, getEntityClass()); + return result; } protected <R> R findUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); - if (uniqueOrNull == null) { + R result = findUniqueOrNull(hql, hqlParameters, type); + if (result == null) { // TODO brendan 30/09/13 throw another exception if no result throw new TopiaException("query " + hql + " returns no elements"); } - return uniqueOrNull; + return result; } protected <R> Optional<R> tryFindUnique(String hql, Map<String, Object> hqlParameters, Class<R> type) { R uniqueOrNull = findUniqueOrNull(hql, hqlParameters, type); - return Optional.fromNullable(uniqueOrNull); + Optional<R> result = Optional.fromNullable(uniqueOrNull); + return result; } protected <R> R findUniqueOrNull(String hql, Map<String, Object> hqlParameters, Class<R> type) { @@ -471,85 +481,87 @@ throw new TopiaException(message); } // otherwise return the first one, or null - R uniqueOrNull = Iterables.getOnlyElement(results, null); - return uniqueOrNull; + R result = Iterables.getOnlyElement(results, null); + return result; } protected <R> R findFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R firstOrNull = findFirstOrNull(hql, hqlParameters, type); - if (firstOrNull == null) { + R result = findFirstOrNull(hql, hqlParameters, type); + if (result == null) { // TODO brendan 30/09/13 throw another exception if no result throw new TopiaException("query " + hql + " returns no elements"); } - return firstOrNull; + return result; } protected <R> Optional<R> tryFindFirst(String hql, Map<String, Object> hqlParameters, Class<R> type) { R firstOrNull = findFirstOrNull(hql, hqlParameters, type); - return Optional.fromNullable(firstOrNull); + Optional<R> result = Optional.fromNullable(firstOrNull); + return result; } 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; + R result = findAnyOrNull(hql, hqlParameters, type); + return result; } protected <R> R findAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { - R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - if (anyOrNull == null) { + R result = findAnyOrNull(hql, hqlParameters, type); + if (result == null) { // TODO brendan 30/09/13 throw another exception if no result - throw new TopiaException("query " + hql + " returns no elements"); + throw new TopiaException(String.format("Query '%s' returns no elements", hql)); } - return anyOrNull; + return result; } protected <R> Optional<R> tryFindAny(String hql, Map<String, Object> hqlParameters, Class<R> type) { R anyOrNull = findAnyOrNull(hql, hqlParameters, type); - return Optional.fromNullable(anyOrNull); + Optional<R> result = Optional.fromNullable(anyOrNull); + return result; } 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; + R result = Iterables.getOnlyElement(results, null); + return result; } 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; + List<E> result = topiaJpaSupport.findAll(hql, hqlParameters); + return result; } 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; + List<E> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + return result; } protected List<E> findAll(String hql, Map<String, Object> hqlParameters, TopiaPagerBean pager) { - List<E> all = findAll(hql, hqlParameters, getEntityClass(), pager); - return all; + List<E> result = findAll(hql, hqlParameters, getEntityClass(), pager); + return result; } 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; + List<R> result = topiaJpaSupport.findAll(hql, hqlParameters); + return result; } 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; + List<R> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + return result; } protected <R> List<R> findAll(String hql, Map<String, Object> hqlParameters, Class<R> type, TopiaPagerBean pager) { @@ -575,8 +587,8 @@ } protected Iterable<E> findAllLazy(String hql, Map<String, Object> hqlParameters) { - Iterable<E> allLazy = findAllLazy(hql, hqlParameters, getEntityClass()); - return allLazy; + Iterable<E> result = findAllLazy(hql, hqlParameters, getEntityClass()); + return result; } protected <R> Iterable<R> findAllLazy(String hql, Map<String, Object> hqlParameters, Class<R> type) { @@ -604,45 +616,60 @@ @Override public TopiaQueryBuilderRunQueryStep<E> forTopiaIdEquals(String topiaId) { Preconditions.checkNotNull(topiaId, "given topiaId is null"); - return forEquals(TopiaEntity.PROPERTY_TOPIA_ID, topiaId); + TopiaQueryBuilderRunQueryStep<E> result = forEquals(TopiaEntity.PROPERTY_TOPIA_ID, topiaId); + return result; } @Override public TopiaQueryBuilderRunQueryStep<E> forTopiaIdIn(Iterable<String> topiaIds) { Preconditions.checkNotNull(topiaIds, "given topiaIds is null"); - return forIn(TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); + TopiaQueryBuilderRunQueryStep<E> result = forIn(TopiaEntity.PROPERTY_TOPIA_ID, (Iterable) topiaIds); + return result; } @Override + public E findByTopiaId(String id) { + // AThimel 30/10/13 Not using findUnique to avoid querying several elements (cf. findUnique implementation) + E result = forTopiaIdEquals(id).findAny(); + return result; + } + + @Override + public Optional<E> tryFindByTopiaId(String topiaId) { + Optional<E> result = forTopiaIdEquals(topiaId).tryFindAny(); + return result; + } + + @Override public List<String> findAllIds() { - List<String> find = newQueryBuilder().findAllIds(); - return find; + List<String> result = newQueryBuilder().findAllIds(); + return result; } @Override public List<E> findAll() { - List<E> all = newQueryBuilder().findAll(); - return all; + List<E> result = newQueryBuilder().findAll(); + return result; } @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; + Iterable<E> result = findAllLazy(hql, hqlParameters); + return result; } @Override public Iterator<E> iterator() { - Iterator<E> iterator = findAllLazy().iterator(); - return iterator; + Iterator<E> result = findAllLazy().iterator(); + return result; } @Override public long count() { - long count = newQueryBuilder().count(); - return count; + long result = newQueryBuilder().count(); + return result; } /** 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-30 11:16:15 UTC (rev 2851) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2013-10-30 17:42:49 UTC (rev 2852) @@ -37,6 +37,7 @@ package org.nuiton.topia.persistence; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.Iterators; @@ -973,6 +974,11 @@ } @Override + public Optional<E> tryFindByTopiaId(String topiaId) { + throw new UnsupportedOperationException("Please use new Dao implementations"); + } + + @Override public TopiaQueryBuilderRunQueryStep<E> forIn(String propertyName, Iterable<Object> propertyValues) { throw new UnsupportedOperationException("Please use new Dao implementations"); } @@ -1043,17 +1049,17 @@ @Override public <E1> List<E1> findAll(String hql, Object... propertyNamesAndValues) throws TopiaException { - throw new UnsupportedOperationException("Do not use this DAO implementation"); + throw new UnsupportedOperationException("Please use new Dao implementations"); } @Override public <E1> List<E1> find(String hql, int startIndex, int endIndex, Object... propertyNamesAndValues) throws TopiaException { - throw new UnsupportedOperationException("Do not use this DAO implementation"); + throw new UnsupportedOperationException("Please use new Dao implementations"); } @Override public <E1> E1 findUnique(String hql, Object... propertyNamesAndValues) throws TopiaException { - throw new UnsupportedOperationException("Do not use this DAO implementation"); + throw new UnsupportedOperationException("Please use new Dao implementations"); } } //TopiaDAOImpl 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-30 11:16:15 UTC (rev 2851) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDao.java 2013-10-30 17:42:49 UTC (rev 2852) @@ -1,10 +1,12 @@ package org.nuiton.topia.persistence; +import java.util.List; +import java.util.Map; + import org.nuiton.topia.event.TopiaEntityListener; import org.nuiton.topia.event.TopiaEntityVetoable; -import java.util.List; -import java.util.Map; +import com.google.common.base.Optional; /** * This contract represents the common operation any DAO should @@ -24,7 +26,7 @@ /** * Obtains the batch size used to load data. - * + * <p/> * Default value if 1000. * * @return the batch size. @@ -148,8 +150,9 @@ * @since 3.0 */ TopiaQueryBuilderAddCriteriaOrRunQueryStep<E> forProperties(String propertyName, - Object propertyValue, - Object... otherPropertyNamesAndValues); + Object propertyValue, + Object... otherPropertyNamesAndValues); + /** * @since 3.0 */ @@ -176,8 +179,28 @@ TopiaQueryBuilderRunQueryStep<E> forTopiaIdEquals(String topiaId); /** + * Tries to find the entity with the given topiaId. If not found, an exception will be thrown. + * <p/> + * IMPORTANT : The behavior of the method changes in ToPIA 3.0 because an exception is thrown if no entity found. + * + * @param topiaId the identifier of the entity to look for + * @return The entity found + * @throws SomeException when not result is found // TODO AThimel 30/10/13 Find the right exception + */ + E findByTopiaId(String topiaId); + + /** + * Tries to find the entity with the given topiaId. If not found, the result.isPresent() will be <code>false</code>. + * + * @param topiaId the identifier of the entity to look for + * @return The entity found wrapped by an Optional * @since 3.0 */ + Optional<E> tryFindByTopiaId(String topiaId); + + /** + * @since 3.0 + */ TopiaQueryBuilderRunQueryStep<E> forTopiaIdIn(Iterable<String> topiaIds); void addTopiaEntityListener(TopiaEntityListener listener); 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-30 11:16:15 UTC (rev 2851) +++ trunk/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java 2013-10-30 17:42:49 UTC (rev 2852) @@ -96,7 +96,7 @@ log.debug("DELETE PERSONNE"); dao.delete(personne); transaction.commitTransaction(); - Personne res = dao.findByTopiaId(idPersonne); + Personne res = dao.forTopiaIdEquals(idPersonne).findAnyOrNull(); assertNull(res); log.debug("ENTITY PERSONNE DELETED !"); @@ -113,7 +113,7 @@ log.debug("DELETE PERSONNE with PartyDAO"); dao2.delete(personne2); transaction.commitTransaction(); - Party2 res2 = dao2.findByTopiaId(idPersonne2); + Party2 res2 = dao2.forTopiaIdEquals(idPersonne2).findAnyOrNull(); assertNull(res2); log.debug("ENTITY PERSONNE DELETED !"); @@ -161,7 +161,7 @@ log.debug("DELETE PERSONNE"); dao.delete(personne); transaction.commitTransaction(); - Personne res = dao.findByTopiaId(idPersonne); + Personne res = dao.forTopiaIdEquals(idPersonne).findAnyOrNull(); assertNull(res); log.debug("ENTITY PERSONNE DELETED !"); @@ -170,7 +170,7 @@ log.debug("DELETE CONTACT"); contactDAO.delete(contact); transaction.commitTransaction(); - Contact2 res2 = contactDAO.findByTopiaId(idContact); + Contact2 res2 = contactDAO.forTopiaIdEquals(idContact).findAnyOrNull(); assertNull(res2); log.debug("ENTITY PERSONNE DELETED !");