Author: fdesbois Date: 2010-05-26 11:59:37 +0200 (Wed, 26 May 2010) New Revision: 1975 Url: http://nuiton.org/repositories/revision/topia/1975 Log: - Evo #637 : deprecate all execute methods with no topiaContext in argument + constructor with DAO. Add method findByQuery in TopiaContext. - For subQuery, no need of flag in argument for SELECT or WHERE statement, only WHERE statement support subqueries. Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java 2010-05-26 09:15:19 UTC (rev 1974) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java 2010-05-26 09:59:37 UTC (rev 1975) @@ -31,6 +31,7 @@ import org.nuiton.topia.event.TopiaEntityVetoable; import org.nuiton.topia.event.TopiaTransactionListener; import org.nuiton.topia.event.TopiaTransactionVetoable; +import org.nuiton.topia.framework.TopiaQuery; import org.nuiton.topia.framework.TopiaService; import org.nuiton.topia.persistence.TopiaEntity; @@ -174,6 +175,17 @@ TopiaEntity findByTopiaId(String topiaId) throws TopiaException; /** + * Retrieve results executing a simple {@code query}. Generally this method + * is used for complex query where output type is specific (more than one + * element in the SELECT). + * + * @param query TopiaQuery to execute + * @return a List of results as hibernate give us + * @throws TopiaException + */ + List findByQuery(TopiaQuery query) throws TopiaException; + + /** * Permet de faire une requete HQL hibernate directement sur la base. * * @param hql la requete a faire Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2010-05-26 09:15:19 UTC (rev 1974) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2010-05-26 09:59:37 UTC (rev 1975) @@ -920,6 +920,11 @@ return result; } + @Override + public List findByQuery(TopiaQuery query) throws TopiaException { + return query.execute(this); + } + /* * (non-Javadoc) * @see TopiaContext#find(java.lang.String, java.lang.Object[]) Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 09:15:19 UTC (rev 1974) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 09:59:37 UTC (rev 1975) @@ -265,6 +265,11 @@ protected String mainAlias; + /** + * @deprecated since 2.4 : need calling right execute method directly from + * DAO + */ + @Deprecated protected TopiaDAO<? extends TopiaEntity> dao; /** Enum to simmplify using operation in query */ @@ -340,7 +345,10 @@ * added to the select part of the query if it is needed. * * @param dao DAO linked to the entity to threat + * @deprecated since 2.4 : instantiate the query from DAO {@link + * TopiaDAO#createQuery()} */ + @Deprecated public TopiaQuery(TopiaDAO<? extends TopiaEntity> dao) { this(); setFrom(dao.getEntityClass()); @@ -353,7 +361,10 @@ * * @param dao DAO linked to the entity to threat * @param alias of the main entity in the query + * @deprecated since 2.4 : instantiate the query from DAO {@link + * TopiaDAO#createQuery(String)} */ + @Deprecated public TopiaQuery(TopiaDAO<? extends TopiaEntity> dao, String alias) { this(); setFrom(dao.getEntityClass(), alias); @@ -402,7 +413,7 @@ /** * Add an other entity type to the from in the query. * - * @param entityClass different from the mainEntity in the DAO + * @param entityClass different from the mainEntity * @return the TopiaQuery */ public TopiaQuery addFrom(Class<? extends TopiaEntity> entityClass) { @@ -412,7 +423,7 @@ /** * Add an other entity type to the from in the query with an alias. * - * @param entityClass different from the mainEntity in the DAO + * @param entityClass different from the mainEntity * @param alias of the entity in the query * @return the TopiaQuery */ @@ -486,31 +497,23 @@ /** * Method used to add a subquery in an existing query. The params will be * automatically checked and copied from the subquery to the current one. - * This method is used to inject {@code subquery} in SELECT or WHERE parts - * of the query. The flag {@code select} is used to tell the method that the - * element will be added to the select (if false, the element will be added - * to the where). The {@code queryPart} is the element in the query to bind - * with the {@code subquery}. The ? character is used to inject the subquery + * This method is used to inject {@code subquery} in WHERE part of the query. + * The {@code queryPart} is the element in the query to bind with the {@code + * subquery}. The ? character is used to inject the subquery * into the {@code queryPart}. Ex : * <pre> - * // Add a SUM(subquery) into the SELECT of the query - * query.addSubQuery("SUM(?)", subquery, true); - * // Add a SUB_ELMT = (subquery) into the WHERE of the query + * // Add a SUB_ELMT = (subquery) into the query * query.addSubQuery("SUB_ELMT = (?)", subquery, false); * </pre> * * @param queryPart part of the query where subquery need to be injected * @param subquery existing topiaQuery as subquery - * @param select If true, the queryPart will be added to the SELECT - * statement, if false the default query statement is the - * WHERE * @return the TopiaQuery * @see TopiaQuery#getValueName(String) * @since 2.4 */ public TopiaQuery addSubQuery(String queryPart, - TopiaQuery subquery, - boolean select) { + TopiaQuery subquery) { List<Object> subqueryParams = subquery.getParams(); String subqueryString = subquery.fullQuery(); @@ -548,9 +551,6 @@ // Replace ? injection by the subquery String result = queryPart.replace("?", subqueryString); - if (select) { - return addSelect(result); - } return add(result); } @@ -973,7 +973,9 @@ * @return a List of results * @throws TopiaException for error during execution * @see #execute(TopiaContext) + * @deprecated since 2.4, need transaction, use {@link #execute(TopiaContext)} */ + @Deprecated public List execute() throws TopiaException { validateDAO(); return execute(dao.getContext()); @@ -1134,7 +1136,10 @@ * @return an Object * @throws TopiaException for error on query execution * @see #executeToObject(TopiaContext, String) + * @deprecated since 2.4, need transaction, use {@link #executeToObject(TopiaContext, + * String)} */ + @Deprecated public Object executeToObject(String select) throws TopiaException { validateDAO(); return executeToObject(dao.getContext(), select); @@ -1163,7 +1168,10 @@ * @return an Integer * @throws TopiaException for error on query execution * @see #executeToInteger(TopiaContext, String) + * @deprecated since 2.4, need transaction, use {@link #executeToInteger(TopiaContext, + * String)} */ + @Deprecated public int executeToInteger(String select) throws TopiaException { validateDAO(); return executeToInteger(dao.getContext(), select); @@ -1191,7 +1199,10 @@ * @return a String result * @throws TopiaException for error on query execution * @see #executeToString(TopiaContext, String) + * @deprecated since 2.4, need transaction, use {@link #executeToString(TopiaContext, + * String)} */ + @Deprecated public String executeToString(String select) throws TopiaException { validateDAO(); return executeToString(dao.getContext(), select); @@ -1243,6 +1254,7 @@ return executeCount(dao.getContext()); } + @Deprecated protected boolean validateDAO() throws TopiaException { if (dao == null) { throw new TopiaException( 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 2010-05-26 09:15:19 UTC (rev 1974) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-26 09:59:37 UTC (rev 1975) @@ -455,17 +455,23 @@ @Override public TopiaQuery createQuery() { + // Need to be change by constructor with entity only, only when + // method from TopiaQuery will be removed return new TopiaQuery(this); } @Override public TopiaQuery createQuery(String entityAlias) { + // Need to be change by constructor with entity only, only when + // method from TopiaQuery will be removed return new TopiaQuery(this, entityAlias); } @Override public E findByTopiaId(String id) throws TopiaException { - return findByQuery(createQuery().add(TopiaEntity.TOPIA_ID, id)); + TopiaQuery query = createQuery().add(TopiaEntity.TOPIA_ID, id); + E result = findByQuery(query); + return result; } @Override @@ -480,26 +486,6 @@ @Override public E findByProperties(String propertyName, Object value, Object... others) throws TopiaException { -// Map<String, Object> properties = new HashMap<String, Object>(); -// properties.put(propertyName, value); -// Object name = null; -// for (int i = 0; i < others.length;) { -// try { -// name = others[i++]; -// value = others[i++]; -// properties.put((String) name, value); -// } catch (ClassCastException eee) { -// throw new IllegalArgumentException( -// "Les noms des propriétés doivent être des chaines et " + -// "non pas " + propertyName.getClass().getName(), -// eee); -// } catch (ArrayIndexOutOfBoundsException eee) { -// throw new IllegalArgumentException( -// "Le nombre d'argument n'est pas un nombre pair: " -// + (others.length + 2) -// + " La dernière propriété était: " + name, eee); -// } -// } Map<String, Object> properties = convertPropertiesArrayToMap(propertyName, value, others); E result = findByProperties(properties); @@ -509,22 +495,28 @@ @Override public E findByProperties(Map<String, Object> properties) throws TopiaException { - return findByQuery(createQuery().add(properties)); + TopiaQuery query = createQuery().add(properties); + E result = findByQuery(query); + return result; } @Override public E findByQuery(TopiaQuery query) throws TopiaException { - return query.executeToEntity(context, getEntityClass()); + E result = query.executeToEntity(context, getEntityClass()); + return result; } @Override public List<E> findAll() throws TopiaException { - return findAllByQuery(createQuery()); + List<E> results = findAllByQuery(createQuery()); + return results; } @Override public List<String> findAllIds() throws TopiaException { - return createQuery().setSelect(TopiaEntity.TOPIA_ID).execute(); + TopiaQuery query = createQuery().setSelect(TopiaEntity.TOPIA_ID); + List<String> results = context.findByQuery(query); + return results; } @Override @@ -539,26 +531,6 @@ @Override public List<E> findAllByProperties(String propertyName, Object value, Object... others) throws TopiaException { -// Map<String, Object> properties = new HashMap<String, Object>(); -// properties.put(propertyName, value); -// Object name = null; -// for (int i = 0; i < others.length;) { -// try { -// name = others[i++]; -// value = others[i++]; -// properties.put((String) name, value); -// } catch (ClassCastException eee) { -// throw new IllegalArgumentException( -// "Les noms des propriétés doivent être des chaines et " + -// "non pas " + propertyName.getClass().getName(), -// eee); -// } catch (ArrayIndexOutOfBoundsException eee) { -// throw new IllegalArgumentException( -// "Le nombre d'argument n'est pas un nombre pair: " -// + (others.length + 2) -// + " La dernière propriété était: " + name, eee); -// } -// } Map<String, Object> properties = convertPropertiesArrayToMap(propertyName, value, others); List<E> result = findAllByProperties(properties); @@ -568,33 +540,41 @@ @Override public List<E> findAllByProperties(Map<String, Object> properties) throws TopiaException { - return findAllByQuery(createQuery().add(properties)); + TopiaQuery query = createQuery().add(properties); + List<E> results = findAllByQuery(query); + return results; } @Override public List<E> findAllByQuery(TopiaQuery query) throws TopiaException { - return query.executeToEntityList(context, getEntityClass()); + List<E> results = query.executeToEntityList(context, getEntityClass()); + return results; } @Override public Map<String, E> findAllMappedByQuery(TopiaQuery query) throws TopiaException { - return query.executeToEntityMap(context, getEntityClass()); + Map<String, E> results = + query.executeToEntityMap(context, getEntityClass()); + return results; } @Override public <K> Map<K, E> findAllMappedByQuery(TopiaQuery query, String keyName, Class<K> keyClass) throws TopiaException { - return query.executeToEntityMap(context, getEntityClass(), keyName, - keyClass); + Map<K, E> results = + query.executeToEntityMap(context, getEntityClass(), + keyName, keyClass); + return results; } @Override public List<E> findAllWithOrder(String... propertyNames) throws TopiaException { TopiaQuery query = createQuery().addOrder(propertyNames); - return findAllByQuery(query); + List<E> results = findAllByQuery(query); + return results; } @Override @@ -615,7 +595,7 @@ @Override public boolean existByQuery(TopiaQuery query) throws TopiaException { - int count = query.executeCount(); + int count = countByQuery(query); boolean result = count > 0; return result; } @@ -629,17 +609,20 @@ @Override @Deprecated public long size() throws TopiaException { - return count(); + long result = count(); + return result; } @Override public int count() throws TopiaException { - return countByQuery(createQuery()); + int result = countByQuery(createQuery()); + return result; } @Override public int countByQuery(TopiaQuery query) throws TopiaException { - return query.executeCount(context); + int result = query.executeCount(context); + return result; } /** @@ -778,8 +761,10 @@ * @param properties * @return the list of entities corresponding to the request * @throws TopiaException if any pb + * @deprecated since 2.4 */ @Override + @Deprecated public List<E> findAllContainsProperties(Map<String, Collection<?>> properties) throws TopiaException { List<E> all = findAll(); @@ -833,9 +818,12 @@ return result; } + /** @deprecated since 2.4 */ @Override + @Deprecated public List<E> findAllContainsProperties(String propertyName, - Collection<?> values, Object... others) throws TopiaException { + Collection<?> values, Object... others) + throws TopiaException { Map<String, Collection<?>> properties = new HashMap<String, Collection<?>>(); properties.put(propertyName, values); Object name = null; Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 09:15:19 UTC (rev 1974) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 09:59:37 UTC (rev 1975) @@ -27,18 +27,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.nuiton.topiatest.QueriedEntity; -/** - * - * @author fdesbois - */ +/** @author fdesbois */ public class TopiaQueryTest { private static final Log log = LogFactory.getLog(TopiaQueryTest.class); @@ -52,7 +45,7 @@ query.add(QueriedEntity.TEST_ADD, value); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE testAdd = :testAdd", + "WHERE testAdd = :testAdd", query.fullQuery()); // Test with null paramValue @@ -61,7 +54,7 @@ query.add(QueriedEntity.TEST_ADD, nullValue); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE testAdd IS NULL", + "WHERE testAdd IS NULL", query.fullQuery()); // Test with two paramValues @@ -70,7 +63,7 @@ query.add(QueriedEntity.TEST_ADD, value, value2); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE testAdd IN(:testAdd1, :testAdd2)", + "WHERE testAdd IN(:testAdd1, :testAdd2)", query.fullQuery()); // Test with two paramValues + null @@ -78,14 +71,14 @@ query.add(QueriedEntity.TEST_ADD, value, value2, null); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE (testAdd IN(:testAdd1, :testAdd2) OR testAdd IS NULL)", + "WHERE (testAdd IN(:testAdd1, :testAdd2) OR testAdd IS NULL)", query.fullQuery()); } @Test public void testAddSubQuery() { - // Test 1 : Subquery in select part with two params with different values + // Test 1 : Subquery with two params with different values TopiaQuery query = new TopiaQuery(QueriedEntity.class). add(QueriedEntity.TEST_ADD, "value1"); // Exist 2 params @@ -94,14 +87,14 @@ TopiaQuery subquery = new TopiaQuery(QueriedEntity.class). add(QueriedEntity.TEST_ADD, "value2"); - query.addSubQuery("SUM(?)", subquery, true); + query.addSubQuery("Q1 = (?)", subquery); log.debug(query); // Add other params from subquery Assert.assertEquals(4, query.getParams().size()); - // Test 2 : Subquery in select part with two params with different values - // one of both is null + // Test 2 : Subquery with two params with different values + // one of them is null query = new TopiaQuery(QueriedEntity.class). add(QueriedEntity.TEST_ADD, TopiaQuery.Op.EQ, null); // Exist 0 param (null value) @@ -110,13 +103,13 @@ subquery = new TopiaQuery(QueriedEntity.class). add(QueriedEntity.TEST_ADD, "value1"); - query.addSubQuery("SUM(?)", subquery, true); + query.addSubQuery("Q1 = (?)", subquery); log.debug(query); // Add 2 params from subquery Assert.assertEquals(2, query.getParams().size()); - // Test 3 : Subquery in where part with two params with same value + // Test 3 : Subquery with two params with same value query = new TopiaQuery(QueriedEntity.class, "Q1"). add(QueriedEntity.TEST_ADD, "value1"); // Exist 2 params @@ -125,7 +118,7 @@ subquery = new TopiaQuery(QueriedEntity.class, "Q2"). add(QueriedEntity.TEST_ADD, "value1"); - query.addSubQuery("Q1 = (?)", subquery, false); + query.addSubQuery("Q1 = (?)", subquery); log.debug(query); // Still 2 params Assert.assertEquals(2, query.getParams().size());