Author: fdesbois Date: 2010-06-24 18:58:30 +0200 (Thu, 24 Jun 2010) New Revision: 3034 Url: http://chorem.org/repositories/revision/pollen/3034 Log: Refactor queries to use PollenQueryHelper Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantDAOImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountDAOImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantDAOImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantDAOImpl.java 2010-06-24 16:34:31 UTC (rev 3033) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/FavoriteParticipantDAOImpl.java 2010-06-24 16:58:30 UTC (rev 3034) @@ -19,23 +19,35 @@ filter.checkReference(FavoriteList.class, true); - // ------------------------- PROPERTIES ----------------------------- // +// // ------------------------- PROPERTIES ----------------------------- // +// +// String participantAlias = PollenQueryHelper.ALIAS_FAVORITE_PARTICIPANT; +// String listAlias = PollenQueryHelper.ALIAS_FAVORITE_LIST; +// String listParticipantProperty = +// TopiaQuery.getProperty(listAlias, +// FavoriteList.PROPERTY_FAVORITE_PARTICIPANT); +// +// filter.setReferenceProperty(listAlias); +// +// // ----------------------- QUERY ------------------------------------ // +// +// TopiaQuery query = context.createQuery(FavoriteList.class, listAlias). +// setSelect(participantAlias). +// addJoin(listParticipantProperty, participantAlias, false). +// addFilter(filter, participantAlias); - String participantAlias = PollenQueryHelper.ALIAS_FAVORITE_PARTICIPANT; - String listAlias = PollenQueryHelper.ALIAS_FAVORITE_LIST; - String listParticipantProperty = - TopiaQuery.getProperty(listAlias, - FavoriteList.PROPERTY_FAVORITE_PARTICIPANT); + PollenQueryHelper.FavoriteListProperty listProperty = + PollenQueryHelper.newFavoriteListProperty(); + PollenQueryHelper.FavoriteParticipantProperty participantProperty = + PollenQueryHelper.newFavoriteParticipantProperty(); - filter.setReferenceProperty(listAlias); + filter.setReferenceProperty(listProperty.$alias()); - // ----------------------- QUERY ------------------------------------ // + TopiaQuery query = PollenQueryHelper.createQuery(listProperty). + setSelect(participantProperty.$alias()). + addJoin(listProperty.favoriteParticipant(), participantProperty.$alias(), false). + addFilter(filter, participantProperty.$alias()); - TopiaQuery query = context.createQuery(FavoriteList.class, listAlias). - setSelect(participantAlias). - addJoin(listParticipantProperty, participantAlias, false). - addFilter(filter, participantAlias); - return query; } } Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountDAOImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountDAOImpl.java 2010-06-24 16:34:31 UTC (rev 3033) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/entity/PollAccountDAOImpl.java 2010-06-24 16:58:30 UTC (rev 3034) @@ -18,54 +18,85 @@ filter.checkReference(Poll.class, true); - // ------------------------- PROPERTIES ----------------------------- // +// // ------------------------- PROPERTIES ----------------------------- // +// +// String pollAlias = PollenQueryHelper.ALIAS_POLL; +// String pollAccountProperty = +// TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_POLL_ACCOUNT); +// +// String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT; +// String participantVoteDateProperty = +// TopiaQuery.getProperty(participantAlias, +// PollAccount.PROPERTY_VOTE_DATE); +// +// filter.setReferenceProperty(pollAlias); +// +// // ----------------------- QUERY ------------------------------------ // +// +// // Will return lists in case of GROUP poll +// TopiaQuery query = context.createQuery(Poll.class, pollAlias). +// setSelect(participantAlias). +// addJoin(pollAccountProperty, participantAlias, false). +// addNotNull(participantVoteDateProperty). +// addFilter(filter, participantAlias); - String pollAlias = PollenQueryHelper.ALIAS_POLL; - String pollAccountProperty = - TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_POLL_ACCOUNT); + PollenQueryHelper.PollProperty pollProperty = + PollenQueryHelper.newPollProperty(); + PollenQueryHelper.PollAccountProperty participantProperty = + PollenQueryHelper.newPollAccountProperty(); - String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT; - String participantVoteDateProperty = - TopiaQuery.getProperty(participantAlias, - PollAccount.PROPERTY_VOTE_DATE); + filter.setReferenceProperty(pollProperty.$alias()); - filter.setReferenceProperty(pollAlias); + TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). + setSelect(participantProperty.$alias()). + addJoin(pollProperty.pollAccount(), participantProperty.$alias(), false). + addNotNull(participantProperty.voteDate()). + addFilter(filter, participantProperty.$alias()); - // ----------------------- QUERY ------------------------------------ // - - // Will return lists in case of GROUP poll - TopiaQuery query = context.createQuery(Poll.class, pollAlias). - setSelect(participantAlias). - addJoin(pollAccountProperty, participantAlias, false). - addNotNull(participantVoteDateProperty). - addFilter(filter, participantAlias); - return query; } @Override public TopiaQuery createQueryFindAllParticipantsByPoll(Poll poll) { - String pollAlias = PollenQueryHelper.ALIAS_POLL; - String pollAccountProperty = - TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_POLL_ACCOUNT); +// String pollAlias = PollenQueryHelper.ALIAS_POLL; +// String pollAccountProperty = +// TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_POLL_ACCOUNT); +// +// String listAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT; +// String listChildProperty = +// TopiaQuery.getProperty(listAlias, PollAccount.PROPERTY_CHILD); +// +// String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT_CHILD; +// +// TopiaQuery query = context.createQuery(Poll.class, pollAlias). +// setSelect(participantAlias). +// addEquals(pollAlias, poll); +// +// // Manage join depends on pollType +// if (poll.getPollType().isGroup()) { +// query.addJoin(pollAccountProperty, listAlias, false). +// addJoin(listChildProperty, participantAlias, false); +// } else { +// query.addJoin(pollAccountProperty, participantAlias, false); +// } + PollenQueryHelper.PollProperty pollProperty = + PollenQueryHelper.newPollProperty(); + PollenQueryHelper.PollAccountProperty listProperty = + PollenQueryHelper.newPollAccountProperty(); + PollenQueryHelper.PollAccountProperty participantProperty = + PollenQueryHelper.newPollAccountChildProperty(); - String listAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT; - String listChildProperty = - TopiaQuery.getProperty(listAlias, PollAccount.PROPERTY_CHILD); + TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). + setSelect(participantProperty.$alias()). + addEquals(pollProperty.$alias(), poll); - String participantAlias = PollenQueryHelper.ALIAS_POLL_ACCOUNT_CHILD; - - TopiaQuery query = context.createQuery(Poll.class, pollAlias). - setSelect(participantAlias). - addEquals(pollAlias, poll); - // Manage join depends on pollType if (poll.getPollType().isGroup()) { - query.addJoin(pollAccountProperty, listAlias, false). - addJoin(listChildProperty, participantAlias, false); + query.addJoin(pollProperty.pollAccount(), listProperty.$alias(), false). + addJoin(listProperty.child(), participantProperty.$alias(), false); } else { - query.addJoin(pollAccountProperty, participantAlias, false); + query.addJoin(pollProperty.pollAccount(), participantProperty.$alias(), false); } return query; Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java 2010-06-24 16:34:31 UTC (rev 3033) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceFavoriteImpl.java 2010-06-24 16:58:30 UTC (rev 3034) @@ -8,6 +8,7 @@ import org.chorem.pollen.PollenContext; import org.chorem.pollen.PollenDAOHelper; import org.chorem.pollen.PollenException; +import org.chorem.pollen.PollenQueryHelper; import org.chorem.pollen.entity.FavoriteList; import org.chorem.pollen.entity.FavoriteListDAO; import org.chorem.pollen.entity.FavoriteListImpl; @@ -76,10 +77,7 @@ // check favoriteList name exist for user UserAccount user = list.getUserAccount(); -// int count = dao.createQuery(). -// add(FavoriteList.USER_ACCOUNT, user). -// add(FavoriteList.NAME, list.getName()). -// executeCount(); + // existing list found if (dao.existByNaturalId(list.getName(), user)) { throw new PollenBusinessException( @@ -246,13 +244,16 @@ FavoriteParticipantDAO dao = PollenDAOHelper.getFavoriteParticipantDAO(transaction); + PollenQueryHelper.FavoriteParticipantProperty participantProperty = + PollenQueryHelper.newFavoriteParticipantProperty(); + TopiaQuery query = dao.createQueryFindAllByFavoriteList(filter); if (log.isDebugEnabled()) { log.debug("Query : " + query); } - query.addLoad(FavoriteParticipant.PROPERTY_FAVORITE_LIST); + query.addLoad(participantProperty.favoriteList()); Map<String, FavoriteParticipant> results = dao.findAllMappedByQuery(query); Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java 2010-06-24 16:34:31 UTC (rev 3033) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServicePollImpl.java 2010-06-24 16:58:30 UTC (rev 3034) @@ -357,10 +357,13 @@ PollDAO dao = PollenDAOHelper.getPollDAO(transaction); + PollenQueryHelper.PollAccountProperty participantProperty = + PollenQueryHelper.newPollAccountChildProperty(); + // Filter can be apply on userAccount as reference TopiaQuery query = dao.createQueryFindAllByUser(filter); - query.addSelect(PollenQueryHelper.ALIAS_POLL_ACCOUNT_CHILD); + query.addSelect(participantProperty.$alias()); log.debug("Query : " + query); @@ -436,18 +439,23 @@ CommentDAO dao = PollenDAOHelper.getCommentDAO(transaction); - String pollAlias = PollenQueryHelper.ALIAS_POLL; - String pollCommentProperty = - TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_COMMENT); - String commentAlias = PollenQueryHelper.ALIAS_COMMENT; +// String pollAlias = PollenQueryHelper.ALIAS_POLL; +// String pollCommentProperty = +// TopiaQuery.getProperty(pollAlias, Poll.PROPERTY_COMMENT); +// String commentAlias = PollenQueryHelper.ALIAS_COMMENT; + PollenQueryHelper.PollProperty pollProperty = + PollenQueryHelper.newPollProperty(); + PollenQueryHelper.CommentProperty commentProperty = + PollenQueryHelper.newCommentProperty(); + filter.checkReference(Poll.class, true); - filter.setReferenceProperty(pollAlias); + filter.setReferenceProperty(pollProperty.$alias()); - TopiaQuery query = transaction.createQuery(Poll.class, pollAlias). - setSelect(commentAlias). - addJoin(pollCommentProperty, commentAlias, false). - addFilter(filter); + TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). + setSelect(commentProperty.$alias()). + addJoin(pollProperty.comment(), commentProperty.$alias(), false). + addFilter(filter); List<Comment> comments = dao.findAllByQuery(query); @@ -537,32 +545,25 @@ String accountUid, String pollUid) throws TopiaException { - PollDAO dao = PollenDAOHelper.getPollDAO(transaction); - - // No need alias managment so, no need of queryBuilder -// Prop builder = context.newQueryBuilder(dao); +// String creatorUidProperty = +// TopiaQuery.getProperty(Poll.PROPERTY_CREATOR, +// PollAccount.PROPERTY_UID); // -// EntityQueryProperty creatorProperty = -// builder.getPollCreatorProperty(Poll.CREATOR); -// -// int count = builder.getQuery(). -// add(Poll.UID, pollUid). -// add(creatorProperty.nameProperty(PollAccount.UID), accountUid). -// executeCount(); +// TopiaQuery query = dao.createQuery(). +// addEquals(Poll.PROPERTY_UID, pollUid). +// addEquals(creatorUidProperty, accountUid); - String creatorUidProperty = - TopiaQuery.getProperty(Poll.PROPERTY_CREATOR, - PollAccount.PROPERTY_UID); + PollenQueryHelper.PollProperty pollProperty = PollenQueryHelper.newPollProperty(); - TopiaQuery query = dao.createQuery(). - addEquals(Poll.PROPERTY_UID, pollUid). - addEquals(creatorUidProperty, accountUid); + TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). + addEquals(pollProperty.uid(), pollUid). + addEquals(pollProperty.creatorProperty().uid(), accountUid); // Note a boolean admin exist in PollAccount, maybe // find the account in lists - boolean result = dao.existByQuery(query); + boolean result = PollenDAOHelper.getPollDAO(transaction).existByQuery(query); return result; } @@ -572,21 +573,26 @@ String pollUid, String... properties) throws TopiaException, PollenBusinessException { - PollDAO dao = PollenDAOHelper.getPollDAO(transaction); - // TopiaQuery query = dao.createQuery(). // addLoad(properties). // addEquals(Poll.UID, pollUid); - TopiaQuery query = dao.createQuery(). - addFetch(properties); +// TopiaQuery query = dao.createQuery(). +// addFetch(properties); +// +// String pollUidProperty = +// TopiaQuery.getProperty(query.getMainAlias(), Poll.PROPERTY_UID); +// +// query.addEquals(pollUidProperty, pollUid); - String pollUidProperty = - TopiaQuery.getProperty(query.getMainAlias(), Poll.PROPERTY_UID); + PollenQueryHelper.PollProperty pollProperty = + PollenQueryHelper.newPollProperty(); - query.addEquals(pollUidProperty, pollUid); + TopiaQuery query = PollenQueryHelper.createQuery(pollProperty). + addFetch(properties). + addEquals(pollProperty.uid(), pollUid); - Poll result = dao.findByQuery(query); + Poll result = PollenDAOHelper.getPollDAO(transaction).findByQuery(query); if (result == null) { throw new PollenBusinessException( PollenBusinessException.PollenExceptionType.POLL_NOT_EXIST); Modified: trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java =================================================================== --- trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-06-24 16:34:31 UTC (rev 3033) +++ trunk/pollen-business/src/main/java/org/chorem/pollen/service/ServiceVoteImpl.java 2010-06-24 16:58:30 UTC (rev 3034) @@ -177,6 +177,13 @@ addDistinct(). addFetch(participantChoiceProperty); +// PollenQueryHelper.PollAccountProperty accountProperty = +// PollenQueryHelper.newPollAccountProperty(); +// +// TopiaQuery query = dao.createQueryFindAllByPoll(filter). +// addDistinct(). +// addFetch(accountProperty.choiceVoteProperty().choice()); + List<PollAccount> results = dao.findAllByQuery(query); if (log.isDebugEnabled()) {