Author: fdesbois Date: 2010-06-04 00:12:56 +0200 (Fri, 04 Jun 2010) New Revision: 1994 Url: http://nuiton.org/repositories/revision/topia/1994 Log: - Evo #660 : Introduce EntityFilter + TopiaFilter as implementation + add method addFilter in TopiaQuery - Add method createQuery in TopiaContext 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/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-06-03 17:28:18 UTC (rev 1993) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/TopiaContext.java 2010-06-03 22:12:56 UTC (rev 1994) @@ -186,6 +186,16 @@ List findByQuery(TopiaQuery query) throws TopiaException; /** + * Instantiate a new TopiaQuery. + * + * @param entityClass main entity class for the Query + * @param alias alias of the entity in the Query + * @return a new TopiaQuery + * @see TopiaQuery + */ + TopiaQuery createQuery(Class<?> entityClass, String alias); + + /** * 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-06-03 17:28:18 UTC (rev 1993) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2010-06-03 22:12:56 UTC (rev 1994) @@ -925,6 +925,11 @@ return query.execute(this); } + @Override + public TopiaQuery createQuery(Class<?> entityClass, String alias) { + return new TopiaQuery((Class<? extends TopiaEntity>)entityClass, alias); + } + /* * (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-06-03 17:28:18 UTC (rev 1993) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-06-03 22:12:56 UTC (rev 1994) @@ -1131,6 +1131,99 @@ } /** + * Add a {@code filter} to the query that contains limit indexes, + * orderBy condition and referenceId if needed. The referenceProperty is + * necessary to use the referenceId of the {@code filter}. The filter will + * be applied on the main entity in the query (using the mainAlias if + * necessary). + * <p/> + * Note : the default orderBy is the topiaCreateDate ordered desc (the most + * recent in first) + * + * @param filter Filter to apply on the query + * @see #addFilter(EntityFilter, String) + * @return the TopiaQuery + * @throws IllegalArgumentException if referenceId is defined but no + * referenceProperty was set + */ + public TopiaQuery addFilter(EntityFilter filter) + throws IllegalArgumentException { + return addFilter(filter, null); + } + + /** + * Add a {@code filter} to the query that contains limit indexes, + * orderBy condition and referenceId if needed. In some case it's necessary + * to specify explicitely the {@code propertyToFilter} in complex queries. + * The referenceProperty need to be specifie in {@code filter} to have a + * correspondance between the referenceId and it's property in the query. By + * default, the {@code propertyToFilter} is the mainAlias of the query. + * <p/> + * Note : the default orderBy is the topiaCreateDate ordered desc (the most + * recent in first) + * + * @param filter Filter to apply on the query + * @param propertyToFilter Explicit property to filter + * @return the TopiaQuery + * @throws IllegalArgumentException if referenceId is defined but no + * referenceProperty was set + */ + public TopiaQuery addFilter(EntityFilter filter, + String propertyToFilter) + throws IllegalArgumentException { + + if (propertyToFilter == null) { + propertyToFilter = mainAlias; + } + + Integer startIndex = filter.getStartIndex(); + Integer endIndex = filter.getEndIndex(); + String orderBy = filter.getOrderBy(); + String referenceId = filter.getReferenceId(); + String referenceProperty = filter.getReferenceProperty(); + + if (log.isDebugEnabled()) { + log.debug("Filter added to the query : " + filter); + } + + // Add limits. Only startIndex do nothing. + // startIndex + endIndex provides the limit + if (startIndex != null && endIndex != null) { + setLimit(startIndex, endIndex); + + // endIndex only provides the maxResults wanted + } else if (endIndex != null) { + setMaxResults(endIndex); + } + + // Add order to the main entity in the query, splitted by comma + if (orderBy != null) { + List<String> order = new ArrayList<String>(); + for (String elmt : orderBy.split(",")) { + String property = + TopiaQuery.getProperty(propertyToFilter, elmt.trim()); + order.add(property); + } + addOrder(order.toArray(new String[order.size()])); + + // Default order by creation date + } else { + addOrderDesc(getPropertyCreateDate(propertyToFilter)); + } + + if (filter.hasReference()) { + if (referenceProperty == null) { + throw new IllegalArgumentException("Reference property need" + + " to be defined in filter to use referenceId = " + + referenceId); + } + addEquals(getPropertyId(referenceProperty), referenceId); + } + + return this; + } + + /** * Simple execution of the query. This method use directly the find method * in TopiaContext interface. * @@ -1559,6 +1652,18 @@ return result; } + public String getPropertyId(String alias) { + return getProperty(alias, TopiaEntity.TOPIA_ID); + } + + public String getPropertyCreateDate(String alias) { + return getProperty(alias, TopiaEntity.TOPIA_CREATE_DATE); + } + + public String getPropertyVersion(String alias) { + return getProperty(alias, TopiaEntity.TOPIA_VERSION); + } + @Override protected void finalize() throws Throwable { // Clean StringBuilder statements 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-06-03 17:28:18 UTC (rev 1993) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-06-03 22:12:56 UTC (rev 1994) @@ -29,6 +29,7 @@ import org.apache.commons.logging.LogFactory; import org.junit.Assert; import org.junit.Test; +import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topiatest.QueriedEntity; /** @author fdesbois */ @@ -125,4 +126,36 @@ } + /** + * Test of addFilter method, of class TopiaQuery. + */ + @Test + public void testAddFilter() { + log.info("testAddFilter"); + + EntityFilter filter = new TopiaFilter(); + filter.setStartIndex(1); + filter.setEndIndex(40); + filter.setOrderBy(QueriedEntity.TEST_ADD); + + TopiaQuery query = new TopiaQuery(QueriedEntity.class).addFilter(filter); + + log.debug("Query : " + query); + + Assert.assertEquals(query.fullQuery(), + "FROM " + QueriedEntity.class.getName() + + " ORDER BY " + QueriedEntity.TEST_ADD); + + filter.setOrderBy(null); + + query = new TopiaQuery(QueriedEntity.class).addFilter(filter); + + log.debug("Query : " + query); + + Assert.assertEquals(query.fullQuery(), + "FROM " + QueriedEntity.class.getName() + + " ORDER BY " + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); + + } + }