Author: fdesbois Date: 2010-04-28 16:05:02 +0200 (Wed, 28 Apr 2010) New Revision: 2978 Log: - Clean old PropertyNameProvider - Add a new api for building query using QueryBuilder and EntityQueryProperty Added: trunk/pollen-business/src/main/java/org/chorem/pollen/EntityQueryProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/PollenQueryBuilder.java trunk/pollen-business/src/main/java/org/chorem/pollen/TopiaQueryBuilder.java trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java Removed: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantListProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountProperty.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProvider.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProviderImpl.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/bean/Filter.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceUserImpl.java Copied: trunk/pollen-business/src/main/java/org/chorem/pollen/EntityQueryProperty.java (from rev 2977, trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProviderImpl.java) =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/EntityQueryProperty.java (rev 0) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/EntityQueryProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -0,0 +1,209 @@ + +package org.chorem.pollen; + +import org.nuiton.topia.framework.*; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * EntityQueryProperty is used to simplify property concatenation in TopiaQuery. + * The usage is simple, the EntityQueryProperty need a mainAlias which represents + * the entity in a TopiaQuery. Then the {@link #nameProperty(String)} + * is called to retrieve the correct property name used for the query. + * Exemple : + * <pre> + * BoatDAO dao = AppDAOHelper.getBoatDAO(topiaContext); + * + * // Property used in the query + * EntityQueryProperty boatProperty = new EntityQueryProperty("B"); + * + * // It's better to create the query with the property name + * TopiaQuery query = dao.createQuery(boatProperty.name()); + * + * // The property NAME of the boat need to be find equals to "ULUBERLU" + * query.add(boatProperty.nameProperty(Boat.NAME), "ULUBERLU"); + * + * // It's avoid doing this : + * query.add("B." + Boat.NAME, "ULUBERLU"); + * </pre> + * This last exemple is a simple case but for a more complex query with more + * than one entity, it's obvious that concatenation using + is not a good + * practice.<br /> + * You can also use this class as a superclass for each entity, ex : + * <pre> + * public class BoatProperty extends EntityQueryProperty { + * + * public BoatProperty(String mainAlias) { + * super(mainAlias); + * } + * + * public String namePropertyName() { + * return nameProperty(Boat.NAME); + * } + * } + * </pre> + * + * Created: 14 avr. 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + */ +public class EntityQueryProperty { + + /** alias of the entity **/ + protected String mainAlias; + + /** name of the property used for join in query **/ + protected String propertyJoin; + + /** class of the entity **/ + protected Class<? extends TopiaEntity> entityClass; + + /** + * Default constructor. mainAlias, propertyJoin and entityClass are null. + * You can use setters to initalize these three properties. + * You can use {@link #nameProperty(String)} method with a mainAlias null. + */ + public EntityQueryProperty() { + } + + /** + * Constructor with mainAlias. + * + * @param mainAlias of the entity corresponding to this EntityQueryProperty + */ + public EntityQueryProperty(String mainAlias) { + setMainAlias(mainAlias); + } + + /** + * Constructor with mainAlias and entityClass. + * + * @param mainAlias alias of the entity corresponding to this + * EntityQueryProperty + * @param entityClass class of the entity corresponding to this + * EntityQueryProperty + */ + public EntityQueryProperty(String mainAlias, + Class<? extends TopiaEntity> entityClass) { + this(mainAlias); + setEntityClass(entityClass); + } + + /** + * Setter for mainAlias. + * + * @param mainAlias of the entity corresponding to this EntityQueryProperty + */ + public void setMainAlias(String mainAlias) { + this.mainAlias = mainAlias; + } + + /** + * Setter for propertyJoin. + * + * @param propertyName name of the property used for join + * @see #namePropertyJoin() + */ + public void setPropertyJoin(String propertyName) { + this.propertyJoin = propertyName; + } + + /** + * Setter for entityClass. + * + * @param entityClass class of the entity corresponding to this + * EntityQueryProperty + */ + public void setEntityClass(Class<? extends TopiaEntity> entityClass) { + this.entityClass = entityClass; + } + + /** + * Name the entity as a property for a TopiaQuery. Careful, this name can + * be null if no mainAlias is properly set. + * + * @return the entity name to use in a TopiaQuery + */ + public String name() { + return mainAlias; + } + + /** + * Name a property from the Entity for a TopiaQuery. No matter if the + * mainAlias is null or not, the property will be correctly named. + * + * @param propertyName to named in the TopiaQuery + * @return the property name to use in a TopiaQuery + */ + public String nameProperty(String propertyName) { + return TopiaQuery.getProperty(mainAlias, propertyName); + } + + /** + * Name the property Id (TOPIA_ID) of the entity. + * + * @return the property name to use in a TopiaQuery + */ + public String namePropertyId() { + return nameProperty(TopiaEntity.TOPIA_ID); + } + + /** + * Name the property CreateDate (TOPIA_CREATE_DATE) of the entity. + * + * @return the property name to use in a TopiaQuery + */ + public String namePropertyCreateDate() { + return nameProperty(TopiaEntity.TOPIA_CREATE_DATE); + } + + /** + * Name the property join. The propertyJoin attribute need to be set to + * avoid NullPointerException. + * + * @return the property name to use in a TopiaQuery + */ + public String namePropertyJoin() { + if (propertyJoin == null) { + throw new NullPointerException("propertyJoin is not set"); + } + return nameProperty(propertyJoin); + } + + /** + * Get the entityClass associated with this EntityQueryProperty. + * Can be useful in some case, to create other query for exemple or to + * use it in {@link TopiaQuery#addFrom(Class)} method. + * The entityClass need to be set before calling this method to avoid + * NullPointerException. + * + * @return the entity class corresponding to this EntityQueryProperty. + */ + public Class<? extends TopiaEntity> getEntityClass() { + if (entityClass == null) { + throw new NullPointerException("entitClass is not set"); + } + return entityClass; + } + + /** + * Instanciate a new {@link TopiaQuery} based on this EntityQueryProperty. + * The entityClass need to be set although the TopiaQuery can't be + * instantiate. + * + * @return a new TopiaQuery with the main entity as the EntityQueryProperty + * provides it. + */ + public TopiaQuery newQuery() { + return new TopiaQuery(getEntityClass(), name()); + } + + /** + * Simply the main name (mainAlias) of the EntityQueryProperty. + * + * @return the entity name associated to this EntityQueryProperty + */ + @Override + public String toString() { + return name(); + } +} Added: trunk/pollen-business/src/main/java/org/chorem/pollen/PollenQueryBuilder.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/PollenQueryBuilder.java (rev 0) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/PollenQueryBuilder.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -0,0 +1,57 @@ + +package org.chorem.pollen; + +import org.chorem.pollen.bean.Filter; +import org.chorem.pollen.entity.FavoriteList; +import org.chorem.pollen.entity.FavoriteParticipant; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaDAO; + +/** + * PollenQueryBuilder + * + * Created: 28 avr. 2010 + * + * @author fdesbois + * @version $Revision$ + * + * Mise a jour: $Date$ + * par : $Author$ + */ +public class PollenQueryBuilder extends TopiaQueryBuilder { + + public PollenQueryBuilder() { + super(); + } + + public PollenQueryBuilder(TopiaDAO<?> dao) { + super(dao); + } + + public EntityQueryProperty getFavoriteListProperty(String alias) { + return getEntityProperty(FavoriteList.class, alias); + } + + public EntityQueryProperty getFavoriteParticipantProperty( + String alias) { + return getEntityProperty(FavoriteParticipant.class, alias); + } + + public TopiaQuery createQueryFindFavoriteParticipantsByFavoriteList( + Filter filter) { + + EntityQueryProperty participantProperty = + getFavoriteParticipantProperty("P"); + + EntityQueryProperty listProperty = + getFavoriteListProperty("L"); + + listProperty.setPropertyJoin(FavoriteList.FAVORITE_PARTICIPANT); + + createQueryFindElementsByCollection( + participantProperty, listProperty, filter); + + return query; + } + +} Property changes on: trunk/pollen-business/src/main/java/org/chorem/pollen/PollenQueryBuilder.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Added: trunk/pollen-business/src/main/java/org/chorem/pollen/TopiaQueryBuilder.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/TopiaQueryBuilder.java (rev 0) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/TopiaQueryBuilder.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -0,0 +1,168 @@ + +package org.chorem.pollen; + +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.lang.StringUtils; +import org.chorem.pollen.bean.Filter; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaDAO; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * TopiaQueryBuilder is used to build a TopiaQuery. <br /> + * Convention rules : + * <ul> + * <li>method with prefix {@code prepare} will modify the current query</li> + * <li>method with prefix {@code create} will instantiate a new query</li> + * </ul> + * <br /> + * To use directly {@code prepare} methods, you have to set the query using + * {@link #setQuery(TopiaQuery)} or instantiate the builder with the entity + * dao using {@link #TopiaQueryBuilder(TopiaDAO)}, this will create a new query + * from dao. + * + * Created: 28 avr. 2010 + * + * @author fdesbois <fdesbois@codelutin.com> + */ +public class TopiaQueryBuilder { + + protected TopiaQuery query; + + public TopiaQueryBuilder() { + } + + /** + * This will create a new TopiaQuery from the {@code dao} in argument. + * + * @param dao used to create a new TopiaQuery in the builder + */ + public TopiaQueryBuilder(TopiaDAO<?> dao) { + setQuery(dao.createQuery()); + } + + public static TopiaQueryBuilder newInstance() { + return new TopiaQueryBuilder(); + } + + /** + * This will create a new TopiaQueryBuilder with a new TopiaQuery from + * the {@code dao} in argument. + * + * @param dao used to create a new TopiaQuery in the builder + * @return a new instance of TopiaQueryBuilder + */ + public static TopiaQueryBuilder newInstance(TopiaDAO<?> dao) { + return new TopiaQueryBuilder(dao); + } + + public void setQuery(TopiaQuery query) { + this.query = query; + } + + public TopiaQuery getQuery() { + return query; + } + + public EntityQueryProperty getEntityProperty(String alias) { + return new EntityQueryProperty(alias); + } + + public EntityQueryProperty getEntityProperty( + Class<? extends TopiaEntity> entityClass, String alias) { + return new EntityQueryProperty(alias, entityClass); + } + + public EntityQueryProperty getMainProperty() { + return getEntityProperty(query.getMainAlias()); + } + + /** + * Create a TopiaQuery to find all participants of a list with + * {@code filter} constraints (order, limit indexes, listId). + * + * @param filter used to add constraint in the query + * @param element + * @param collection + * @return the TopiaQuery created + * @see Filter#prepareQuery(TopiaQuery, boolean, PropertyUid) + */ + public TopiaQuery createQueryFindElementsByCollection( + EntityQueryProperty element, + EntityQueryProperty collection, + Filter filter) { + + // Instanciate a new query based on element + query = element.newQuery(); + + // Add the collection in the From of the query + query.addFrom(collection.getEntityClass(), collection.name()); + + // Add the link between the collection and the element + query.addInElements(element.name(), collection.namePropertyJoin()); + + if (filter != null) { + // Add constraints from filter, the collection is given to provide + // the name of its ID property. + prepareQuery(filter, collection); + } + + return query; + } + + /** + * Prepare a query with filter : adding startIndex and endIndex as limit + * and orderBy if defined. Need an existing query in builder. + * + * @param filter Filter to add in the query + * @see #prepareQuery(Filter filter, EntityQueryProperty) + */ + public void prepareQuery(Filter filter) { + prepareQuery(filter, null); + } + + /** + * Prepare a query with filter. The {@code reference} is used + * to retrieve the name of ID property in case of filter using referenceId. + * Need an existing query in builder. + * + * @param filter Filter to add in the query + * @param reference used to filter on referenceId + */ + public void prepareQuery(Filter filter, + EntityQueryProperty reference) { + + Integer startIndex = filter.getStartIndex(); + Integer endIndex = filter.getEndIndex(); + String orderBy = filter.getOrderBy(); + String referenceId = filter.getReferenceId(); + + // Add limits. Only startIndex do nothing. + // startIndex + endIndex provides the limit + if (filter.getStartIndex() != null && endIndex != null) { + query.setLimit(startIndex, endIndex); + + // endIndex only provides the maxResults wanted + } else if (endIndex != null) { + query.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(",")) { + order.add(getMainProperty().nameProperty(elmt.trim())); + } + query.addOrder(order.toArray(new String[order.size()])); + + // Default order by creation date + } else { + query.addOrderDesc(getMainProperty().namePropertyCreateDate()); + } + + if (StringUtils.isNotEmpty(referenceId) && reference != null) { + query.add(reference.namePropertyId(), referenceId); + } + } +} Property changes on: trunk/pollen-business/src/main/java/org/chorem/pollen/TopiaQueryBuilder.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/bean/Filter.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/bean/Filter.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/bean/Filter.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -3,10 +3,6 @@ import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; -import org.apache.commons.lang.StringUtils; -import org.chorem.pollen.entity.PropertyNameProviderImpl; -import org.chorem.pollen.entity.PropertyNameProvider; -import org.nuiton.topia.framework.TopiaQuery; import org.nuiton.topia.persistence.TopiaEntity; /** @@ -140,49 +136,4 @@ propertyChangeSupport.removePropertyChangeListener(listener); } - /** - * Prepare a query with filter : adding startIndex and endIndex as limit - * and orderBy if defined. - * - * @param query - * @see #prepareQuery(TopiaQuery, PropertyUid) - */ - public void prepareQuery(TopiaQuery query) { - prepareQuery(query, null); - } - - /** - * Prepare a query with filter. The {@code property} is used - * to retrieve the name of UID property in case of filter using uid. - * - * @param query to prepare - * @param property to provide UID property name (using uid filter) - */ - public void prepareQuery(TopiaQuery query, PropertyNameProvider property) { - // Add limits. Only startIndex do nothing. - // startIndex + endIndex provides the limit - if (startIndex != null && endIndex != null) { - query.setLimit(startIndex, endIndex); - - // endIndex only provides the maxResults wanted - } else if (endIndex != null) { - query.setMaxResults(endIndex); - } - - if (StringUtils.isNotEmpty(referenceId) && property != null) { - query.add(property.namePropertyId(), referenceId); - } - - // Add order to the query, splitted by comma and trimmed using - // {@link StringUtils#stripAll(String[])} - if (orderBy != null) { - String[] order = StringUtils.stripAll(orderBy.split(",")); - query.addOrder(order); - - // Default order by creation date - } else { - query.addOrderDesc(TopiaEntity.TOPIA_CREATE_DATE); - } - } - } Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListProperty.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListProperty.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteListProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,23 +0,0 @@ - -package org.chorem.pollen.entity; - -/** - * FavoriteListProperty - * - * Created: 28 avr. 2010 - * - * @author fdesbois - */ -public class FavoriteListProperty extends PropertyNameProviderImpl - implements ParticipantListProperty { - - public FavoriteListProperty(String mainAlias) { - super(FavoriteList.class, mainAlias); - } - - @Override - public String namePropertyParticipant() { - return nameProperty(FavoriteList.FAVORITE_PARTICIPANT); - } - -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantProperty.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantProperty.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,19 +0,0 @@ - -package org.chorem.pollen.entity; - -import org.nuiton.topia.persistence.TopiaEntity; - -/** - * FavoriteParticipant - * - * Created: 28 avr. 2010 - * - * @author fdesbois - */ -public class FavoriteParticipantProperty extends PropertyNameProviderImpl - implements ParticipantProperty { - - public FavoriteParticipantProperty(String mainAlias) { - super(FavoriteParticipant.class, mainAlias); - } -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantListProperty.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantListProperty.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantListProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,14 +0,0 @@ - -package org.chorem.pollen.entity; - -/** - * ParticipantListProperty - * - * Created: 28 avr. 2010 - * - * @author fdesbois - */ -public interface ParticipantListProperty extends PropertyNameProvider { - - String namePropertyParticipant(); -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantProperty.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantProperty.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/ParticipantProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,13 +0,0 @@ - -package org.chorem.pollen.entity; - -/** - * ParticipantProperty - * - * Created: 28 avr. 2010 - * - * @author fdesbois - */ -public interface ParticipantProperty extends PropertyNameProvider { - -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountProperty.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountProperty.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountProperty.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,21 +0,0 @@ - -package org.chorem.pollen.entity; - -/** - * PollAccountProperty - * - * Created: 27 avr. 2010 - * - * @author fdesbois - * @since 2.0 - */ -public class PollAccountProperty extends PropertyNameProviderImpl { - - public PollAccountProperty(String mainAlias) { - super(PollAccount.class, mainAlias); - } - - public String namePropertyChild() { - return nameProperty(PollAccount.CHILD); - } -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProvider.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProvider.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProvider.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,26 +0,0 @@ - -package org.chorem.pollen.entity; - -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.topia.persistence.TopiaEntity; - -/** - * PropertyUid - * - * Created: 27 avr. 2010 - * - * @author fdesbois - * @since 2.0 - */ -public interface PropertyNameProvider { - - String name(); - - String namePropertyId(); - - String nameProperty(String propertyName); - - Class<? extends TopiaEntity> getEntityClass(); - - TopiaQuery newQuery(); -} Deleted: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProviderImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProviderImpl.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PropertyNameProviderImpl.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -1,110 +0,0 @@ - -package org.chorem.pollen.entity; - -import org.nuiton.topia.framework.*; -import org.nuiton.topia.persistence.TopiaEntity; - -/** - * EntityProperty is used to simplify property concatenation in TopiaQuery. - * The usage is simple, the EntityProperty need a mainAlias which represents - * the entity in a TopiaQuery. Then the {@link #nameProperty(String)} - * is called to retrieve the correct property name used for the query. - * Exemple : - * <pre> - * BoatDAO dao = AppDAOHelper.getBoatDAO(topiaContext); - * - * // Property used in the query - * EntityProperty boatProperty = new EntityProperty("B"); - * - * // It's better to create the query with the property name - * TopiaQuery query = dao.createQuery(boatProperty.name()); - * - * // The property NAME of the boat need to be find equals to "ULUBERLU" - * query.add(boatProperty.nameProperty(Boat.NAME), "ULUBERLU"); - * - * // It's avoid doing this : - * query.add("B." + Boat.NAME, "ULUBERLU"); - * </pre> - * This last exemple is a simple case but for a more complex query with more - * than one entity, it's obvious that concatenation using + is not a good - * practice.<br /> - * You can also use this class as a superclass for each entity, ex : - * <pre> - * public class BoatProperty extends EntityProperty { - * - * public BoatProperty(String mainAlias) { - * super(mainAlias); - * } - * - * public String namePropertyName() { - * return nameProperty(Boat.NAME); - * } - * } - * </pre> - * - * Created: 14 avr. 2010 - * - * @author fdesbois <fdesbois@codelutin.com> - */ -public class PropertyNameProviderImpl implements PropertyNameProvider { - - /** alias of the entity **/ - protected String mainAlias; - - protected Class<? extends TopiaEntity> entityClass; - - public PropertyNameProviderImpl(Class<? extends TopiaEntity> entityClass, - String mainAlias) { - this.mainAlias = mainAlias; - this.entityClass = entityClass; - } - - public void setMainAlias(String mainAlias) { - this.mainAlias = mainAlias; - } - - public void setEntityClass(Class<? extends TopiaEntity> entityClass) { - this.entityClass = entityClass; - } - - /** - * Name the entity as a property for a TopiaQuery. - * - * @return the entity name used as a TopiaQuery property - */ - @Override - public String name() { - return mainAlias; - } - - /** - * Name a property from the Entity for a TopiaQuery. - * - * @param propertyName to named in the TopiaQuery - * @return the property name used as a TopiaQuery property - */ - @Override - public String nameProperty(String propertyName) { - return TopiaQuery.getProperty(mainAlias, propertyName); - } - - @Override - public String namePropertyId() { - return nameProperty(TopiaEntity.TOPIA_ID); - } - - @Override - public Class<? extends TopiaEntity> getEntityClass() { - return entityClass; - } - - @Override - public TopiaQuery newQuery() { - return new TopiaQuery(getEntityClass(), name()); - } - - @Override - public String toString() { - return name(); - } -} Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceUserImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceUserImpl.java 2010-04-28 08:47:44 UTC (rev 2977) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceUserImpl.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -16,14 +16,14 @@ import org.chorem.pollen.entity.FavoriteList; import org.chorem.pollen.entity.FavoriteListDAO; import org.chorem.pollen.entity.FavoriteListImpl; -import org.chorem.pollen.entity.FavoriteListProperty; import org.chorem.pollen.entity.FavoriteParticipant; import org.chorem.pollen.entity.FavoriteParticipantDAO; import org.chorem.pollen.entity.FavoriteParticipantImpl; -import org.chorem.pollen.entity.FavoriteParticipantProperty; -import org.chorem.pollen.entity.ParticipantListProperty; -import org.chorem.pollen.entity.ParticipantProperty; import org.chorem.pollen.entity.PollAccount; +import org.chorem.pollen.EntityQueryProperty; +import org.chorem.pollen.PollenQueryBuilder; +import org.chorem.pollen.TopiaQueryBuilder; +import org.chorem.pollen.entity.PollAccountDAO; import org.chorem.pollen.entity.UserAccount; import org.chorem.pollen.entity.UserAccountDAO; import org.chorem.pollen.entity.UserAccountImpl; @@ -280,12 +280,12 @@ Filter filter) throws TopiaException { UserAccountDAO dao = PollenDAOHelper.getUserAccountDAO(transaction); - TopiaQuery query = dao.createQuery(); - filter.prepareQuery(query); + TopiaQueryBuilder builder = new TopiaQueryBuilder(dao); + builder.prepareQuery(filter); - Map<String, UserAccount> results = dao.findAllMappedByQuery(query, - UserAccount.LOGIN, String.class); + Map<String, UserAccount> results = dao.findAllMappedByQuery( + builder.getQuery(), UserAccount.LOGIN, String.class); return results; } @@ -294,46 +294,14 @@ Filter filter) throws TopiaException { UserAccountDAO dao = PollenDAOHelper.getUserAccountDAO(transaction); - TopiaQuery query = dao.createQuery(); - filter.prepareQuery(query); + TopiaQueryBuilder builder = new TopiaQueryBuilder(dao); + builder.prepareQuery(filter); - int result = query.executeCount(); + int result = builder.getQuery().executeCount(); return result; } - /** - * Create a TopiaQuery to find all participants of a list with - * {@code filter} constraints (order, limit indexes, listId). - * - * @param filter used to add constraint in the query - * @param participantPropertyProvider - * @param listPropertyProvider - * @return the TopiaQuery created - * @see Filter#prepareQuery(TopiaQuery, boolean, PropertyUid) - */ - protected TopiaQuery createQueryFindParticipantsByList( - Filter filter, - ParticipantProperty participantPropertyProvider, - ParticipantListProperty listPropertyProvider) { - - // Instanciate a new query based on participant - TopiaQuery query = participantPropertyProvider.newQuery(); - - // Add the list in the From of the query - query.addFrom(listPropertyProvider.getEntityClass(), listPropertyProvider.name()); - - // Add the link between the list and the participant - query.addInElements(participantPropertyProvider.name(), - listPropertyProvider.namePropertyParticipant()); - - // Add constraints from filter, the listProperty is given to provide - // the name of its ID property. - filter.prepareQuery(query, listPropertyProvider); - - return query; - } - @Override protected FavoriteList executeGetNewFavoriteList(UserAccount user) { FavoriteList result = new FavoriteListImpl(); @@ -345,31 +313,31 @@ protected void executeCreateFavoriteList(TopiaContext transaction, FavoriteList list) throws TopiaException, PollenBusinessException { -// PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); -// -// // check favoriteList name exist for user -// UserAccount user = list.getUserAccount(); -// int count = dao.createQuery(). -// add(PollAccount.LIST, true). -// add(PollAccount.USER_ACCOUNT, user). -// add(PollAccount.NAME, list.getName()). -// executeCount(); -// // existing list found -// if (count > 0) { -// throw new PollenBusinessException( -// PollenExceptionType.FAVORITE_LIST_NAME_EXIST, -// list.getName(), user.getDisplayName()); -// } -// -// // Create unique Id for the new List -// String UId = context.createPollenUrlId(); -// PollAccount newList = dao.create(UId); -// BinderProvider.getBinder(PollAccount.class, -// BINDER_CONTEXT_FAVORITE_LIST). -// copy(list, newList); -// dao.update(newList); -// -// transaction.commitTransaction(); + FavoriteListDAO dao = PollenDAOHelper.getFavoriteListDAO(transaction); + + // check favoriteList name exist for user + UserAccount user = list.getUserAccount(); + int count = dao.createQuery(). + add(PollAccount.LIST, true). + add(PollAccount.USER_ACCOUNT, user). + add(PollAccount.NAME, list.getName()). + executeCount(); + // existing list found + if (count > 0) { + throw new PollenBusinessException( + PollenExceptionType.FAVORITE_LIST_NAME_EXIST, + list.getName(), user.getDisplayName()); + } + + // Create unique Id for the new List + String UId = context.createPollenUrlId(); + FavoriteList newList = dao.create(UId); + BinderProvider.getBinder(FavoriteList.class, + BINDER_CONTEXT_FAVORITE_LIST). + copy(list, newList); + dao.update(newList); + + transaction.commitTransaction(); } @Override @@ -420,17 +388,14 @@ @Override protected Map<String, FavoriteParticipant> executeGetFavoriteParticipants( TopiaContext transaction, Filter filter) throws TopiaException { - - FavoriteParticipantDAO dao = - PollenDAOHelper.getFavoriteParticipantDAO(transaction); - ParticipantProperty participantProperty = - new FavoriteParticipantProperty("P"); + PollenQueryBuilder builder = new PollenQueryBuilder(); - ParticipantListProperty listProperty = new FavoriteListProperty("L"); + TopiaQuery query = + builder.createQueryFindFavoriteParticipantsByFavoriteList(filter); - TopiaQuery query = createQueryFindParticipantsByList(filter, - participantProperty, listProperty); + FavoriteParticipantDAO dao = + PollenDAOHelper.getFavoriteParticipantDAO(transaction); Map<String, FavoriteParticipant> results = dao.findAllMappedByQuery( query, FavoriteParticipant.EMAIL, String.class); @@ -441,14 +406,14 @@ @Override protected int executeGetNbFavoriteParticipants(TopiaContext transaction, Filter filter) throws TopiaException { - throw new UnsupportedOperationException("Not supported yet."); -// PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(transaction); -// -// TopiaQuery query = createQueryFindAccountsByList(dao, filter); -// -// int result = query.executeCount(); -// return result; + PollenQueryBuilder builder = new PollenQueryBuilder(); + + TopiaQuery query = + builder.createQueryFindFavoriteParticipantsByFavoriteList(filter); + + int result = query.executeCount(transaction); + return result; } } Added: trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java =================================================================== --- trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java (rev 0) +++ trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java 2010-04-28 14:05:02 UTC (rev 2978) @@ -0,0 +1,121 @@ + +package org.chorem.pollen; + +import java.io.IOException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.bean.Filter; +import org.chorem.pollen.entity.FavoriteList; +import org.chorem.pollen.entity.FavoriteParticipant; +import org.chorem.pollen.entity.UserAccount; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaEntity; + +/** + * + * @author fdesbois + */ +public class TopiaQueryBuilderTest { + + private static final Log log = + LogFactory.getLog(TopiaQueryBuilderTest.class); + + /** + * Test of prepareQuery method, of class Filter. + */ + @Test + public void testPrepareQueryForFilter() { + log.info("testPrepareQueryForFilter"); + + Filter filter = new Filter(); + filter.setStartIndex(1); + filter.setEndIndex(40); + filter.setOrderBy(UserAccount.LOGIN); + + TopiaQueryBuilder builder = new TopiaQueryBuilder(); + builder.setQuery(new TopiaQuery(UserAccount.class)); + builder.prepareQuery(filter); + + log.debug("Query : " + builder.getQuery()); + + Assert.assertEquals(builder.getQuery().fullQuery(), + "FROM " + UserAccount.class.getName() + + " ORDER BY " + UserAccount.LOGIN); + + filter.setOrderBy(null); + + builder.setQuery(new TopiaQuery(UserAccount.class)); + builder.prepareQuery(filter); + + log.debug("Query : " + builder.getQuery()); + + Assert.assertEquals(builder.getQuery().fullQuery(), + "FROM " + UserAccount.class.getName() + + " ORDER BY " + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); + + } + + @Test + public void testCreateQueryFindElementsByCollection() throws IOException { + + TopiaQueryBuilder builder = new TopiaQueryBuilder(); + + Filter filter = new Filter(); + filter.setStartIndex(1); + filter.setEndIndex(40); + filter.setOrderBy(FavoriteParticipant.NAME); + + EntityQueryProperty participantPropertyProvider = + builder.getEntityProperty(FavoriteParticipant.class, "P"); + + EntityQueryProperty listPropertyProvider = + builder.getEntityProperty(FavoriteList.class, "L"); + listPropertyProvider.setPropertyJoin(FavoriteList.FAVORITE_PARTICIPANT); + + log.info("test1 : orderBy FavoriteParticipant name"); + TopiaQuery result = builder.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + FavoriteParticipant.NAME); + + log.info("test2 : orderBy not set (default to topiaCreateDate desc)"); + filter.setOrderBy(null); + + result = builder.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + TopiaEntity.TOPIA_CREATE_DATE + " DESC"); + + log.info("test3 : orderBy name and email desc"); + String orderBy = FavoriteParticipant.NAME + ", " + FavoriteParticipant.EMAIL + " desc"; + filter.setOrderBy(orderBy); + + result = builder.createQueryFindElementsByCollection( + participantPropertyProvider, listPropertyProvider, filter); + + log.debug("Query : " + result); + + Assert.assertEquals(result.fullQuery(), + "SELECT P FROM " + FavoriteParticipant.class.getName() + " P, " + + FavoriteList.class.getName() + " L" + + " WHERE P IN elements (L." + FavoriteList.FAVORITE_PARTICIPANT + + ") ORDER BY P." + FavoriteParticipant.NAME + ", P." + + FavoriteParticipant.EMAIL + " desc"); + + } + +} \ No newline at end of file Property changes on: trunk/pollen-business/src/test/java/org/chorem/pollen/TopiaQueryBuilderTest.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"