r3182 - in branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services: exceptions impl
Author: tchemit Date: 2012-03-14 15:40:57 +0100 (Wed, 14 Mar 2012) New Revision: 3182 Url: http://chorem.org/repositories/revision/pollen/3182 Log: - all queries are in persistence layer - implements delete poll action Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/PollAccountNotFound.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UnauthorizedPollAccessException.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/PollAccountNotFound.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/PollAccountNotFound.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/PollAccountNotFound.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -0,0 +1,34 @@ +/* + * #%L + * Pollen :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.services.exceptions; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class PollAccountNotFound extends Exception { + private static final long serialVersionUID = 1L; +} Property changes on: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/PollAccountNotFound.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UnauthorizedPollAccessException.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UnauthorizedPollAccessException.java (rev 0) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UnauthorizedPollAccessException.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -0,0 +1,34 @@ +/* + * #%L + * Pollen :: Services + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * #L% + */ +package org.chorem.pollen.services.exceptions; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 1.2.6 + */ +public class UnauthorizedPollAccessException extends Exception { + private static final long serialVersionUID = 1L; +} Property changes on: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UnauthorizedPollAccessException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java 2012-03-14 14:40:02 UTC (rev 3181) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -43,8 +43,7 @@ import org.chorem.pollen.services.exceptions.ParticipantNotFoundInListException; import org.chorem.pollen.services.exceptions.UserNotFoundException; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.web.struts2.FilterPagerUtil; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import java.util.List; @@ -54,23 +53,13 @@ private static final Log log = LogFactory.getLog(FavoriteService.class); public List<PersonList> getFavoriteLists(UserAccount user, - FilterPagerUtil.FilterPagerBean pager) { + TopiaFilterPagerUtil.FilterPagerBean pager) { Preconditions.checkNotNull(user); - PersonListDAO dao = getDAO(PersonList.class); - try { - - TopiaQuery countQuery = dao.createQuery("e"); - countQuery.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); - long records = dao.countByQuery(countQuery); - - pager.setRecords((int) records); - - TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, pager); - - List<PersonList> result = dao.findAllByQuery(query); + PersonListDAO dao = getDAO(PersonList.class); + List<PersonList> result = dao.getFavoriteLists(user, pager); return result; } catch (TopiaException e) { throw new PollenTechnicalException(e); @@ -81,13 +70,9 @@ Preconditions.checkNotNull(user); - PersonListDAO dao = getDAO(PersonList.class); - try { - - TopiaQuery query = dao.createQuery("e"); - query.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); - List<PersonList> result = dao.findAllByQuery(query); + PersonListDAO dao = getDAO(PersonList.class); + List<PersonList> result = dao.getFavoriteLists(user); return result; } catch (TopiaException e) { throw new PollenTechnicalException(e); @@ -100,9 +85,8 @@ Preconditions.checkNotNull(user); Preconditions.checkNotNull(favoriteListId); - PersonListDAO dao = getDAO(PersonList.class); - try { + PersonListDAO dao = getDAO(PersonList.class); PersonList favoriteList = dao.findByTopiaId(favoriteListId); @@ -124,23 +108,15 @@ } public List<PollAccount> getFavoriteListUsers(PersonList favoriteList, - FilterPagerUtil.FilterPagerBean pager) { + TopiaFilterPagerUtil.FilterPagerBean pager) { Preconditions.checkNotNull(favoriteList); Preconditions.checkNotNull(pager); - PollAccountDAO dao = getDAO(PollAccount.class); - try { - int records = favoriteList.sizePollAccount(); - pager.setRecords(records); - - TopiaQuery query = dao.createQuery("e"); - FilterPagerUtil.addPagerToQuery(query, pager); - query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, favoriteList); - - List<PollAccount> result = dao.findAllByQuery(query); + PollAccountDAO dao = getDAO(PollAccount.class); + List<PollAccount> result = dao.getFavoriteListUsers(favoriteList, pager); return result; } catch (TopiaException e) { @@ -171,13 +147,8 @@ // check list does not already exists - TopiaQuery query = dao.createQuery("e"); + boolean exist = dao.isPersonListExist(user, name); - query.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); - query.addWhere("e." + PersonList.PROPERTY_NAME, TopiaQuery.Op.EQ, name); - - boolean exist = dao.existByQuery(query); - if (exist) { throw new FavoriteListAlreadyExistException(); @@ -258,10 +229,7 @@ } // check there is other poll account in this list with same id - TopiaQuery query = dao.createQuery("e"); - query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, personListToUpdate); - query.addWhere("e." + PollAccount.PROPERTY_VOTING_ID, TopiaQuery.Op.EQ, pollAccount.getVotingId()); - boolean pollAccountExists = dao.existByQuery(query); + boolean pollAccountExists = dao.isPollAccountExist(personListToUpdate, pollAccount); if (pollAccountExists) { throw new ParticipantAlreadyFoundInListException(); @@ -314,11 +282,7 @@ } // check there is another poll account in this list with same id - TopiaQuery query = dao.createQuery("e"); - query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, personListToUpdate); - query.addWhere("e." + PollAccount.PROPERTY_VOTING_ID, TopiaQuery.Op.EQ, pollAccount.getVotingId()); - query.addWhere("e." + PollAccount.TOPIA_ID, TopiaQuery.Op.NEQ, pollAccount.getTopiaId()); - boolean pollAccountExists = dao.existByQuery(query); + boolean pollAccountExists = dao.isPollAccountAlreadyExist(personListToUpdate, pollAccount); if (pollAccountExists) { throw new ParticipantAlreadyFoundInListException(); @@ -384,208 +348,4 @@ PollAccount pollAccount = newInstance(dao); return pollAccount; } - -// public void createFavoriteParticipant(FavoriteParticipant participant) throws ParticipantExistWithoutMailException, ParticipantAlreadyFoundInListException { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// checkFavoriteParticipant(dao, participant); -// -// // Create newParticipant with naturalId -// FavoriteParticipant result = -// dao.create(participant.getName(), -// participant.getEmail(), -// participant.getFavoriteList()); -// -// // Set other field -// result.setWeight(participant.getWeight()); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void deleteFavoriteParticipant(String id) { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// FavoriteParticipant participantLoaded = dao.findByTopiaId(id); -// dao.delete(participantLoaded); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public FavoriteParticipant getFavoriteParticipant(String id) { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// FavoriteParticipant result = dao.findByTopiaId(id); -// //No more needed -//// // Load parent list -//// result.getFavoriteList(); -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public Map<String, FavoriteParticipant> getFavoriteParticipants(EntityFilter filter) { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// PollenQueryHelper.FavoriteParticipantProperty participantProperty = -// PollenQueryHelper.newFavoriteParticipantProperty(); -// -// TopiaQuery query = dao.createQueryFindAllByFavoriteList(filter); -// -// if (log.isDebugEnabled()) { -// log.debug("Query : " + query); -// } -// -// query.addLoad(participantProperty.favoriteList()); -// -// Map<String, FavoriteParticipant> results = -// dao.findAllMappedByQuery(query); -// -// return results; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public List<FavoriteList> getFavoriteLists(UserAccount user) { -// try { -// FavoriteListDAO dao = -// PollenDAOHelper.getFavoriteListDAO(getTransaction()); -// -// List<FavoriteList> result = dao.findAllByUserAccount(user); -// -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void deleteFavoriteList(FavoriteList list) { -// try { -// FavoriteListDAO dao = -// PollenDAOHelper.getFavoriteListDAO(getTransaction()); -// -// FavoriteList listLoaded = dao.findByTopiaId(list.getId()); -// dao.delete(listLoaded); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public void createFavoriteList(FavoriteList list) throws FavoriteListAlreadyExistException { -// try { -// FavoriteListDAO dao = -// PollenDAOHelper.getFavoriteListDAO(getTransaction()); -// -// // check favoriteList name exist for user -// UserAccount user = list.getUserAccount(); -// -// // existing list found -// if (dao.existByNaturalId(list.getName(), user)) { -// throw new FavoriteListAlreadyExistException(); -// } -// -// dao.create(list.getName(), list.getUserAccount()); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public int getNbFavoriteParticipants(EntityFilter filter) { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// TopiaQuery query = dao.createQueryFindAllByFavoriteList(filter); -// -// int result = dao.countByQuery(query); -// return result; -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// public FavoriteParticipant getNewFavoriteParticipant(FavoriteList list) { -// FavoriteParticipant result = new FavoriteParticipantImpl(); -// result.setFavoriteList(list); -// result.setWeight(1.); -// return result; -// } -// -// public FavoriteList getNewFavoriteList(UserAccount user) { -// FavoriteList result = new FavoriteListImpl(); -// result.setUserAccount(user); -// return result; -// } -// -// public void updateFavoriteParticipant(FavoriteParticipant participant) throws ParticipantExistWithoutMailException, ParticipantAlreadyFoundInListException { -// try { -// FavoriteParticipantDAO dao = -// PollenDAOHelper.getFavoriteParticipantDAO(getTransaction()); -// -// checkFavoriteParticipant(dao, participant); -// -// FavoriteParticipant participantLoaded = -// dao.findByTopiaId(participant.getId()); -// -// // Set all fields except favoriteList that can't be changed -// participantLoaded.setName(participant.getName()); -// participantLoaded.setEmail(participant.getEmail()); -// participantLoaded.setWeight(participant.getWeight()); -// -// dao.update(participantLoaded); -// } catch (TopiaException e) { -// throw new PollenTechnicalException(e); -// } -// } -// -// /** -// * Check if the {@code participant} doesn't already exists with same email, -// * name and favoriteList. -// * -// * @param dao used to verify the existing participant -// * @param participant FavoriteParticipant to check -// * @throws TopiaException for technical errors from ToPIA -// * @throws ParticipantAlreadyFoundInListException -// * if a participant already exist in list -// * @throws ParticipantExistWithoutMailException -// * if a participan was found without email -// */ -// protected void checkFavoriteParticipant(FavoriteParticipantDAO dao, -// FavoriteParticipant participant) -// throws TopiaException, ParticipantExistWithoutMailException, ParticipantAlreadyFoundInListException { -// -// FavoriteList list = participant.getFavoriteList(); -// -// TopiaQuery query = dao.createQuery(). -// addEquals(FavoriteParticipant.PROPERTY_FAVORITE_LIST, list). -// addEquals(FavoriteParticipant.PROPERTY_NAME, participant.getName()). -// addEquals(FavoriteParticipant.PROPERTY_EMAIL, participant.getEmail()); -// -// // Check only on entities different from the one in argument -// if (StringUtils.isNotEmpty(participant.getId())) { -// query.addWhere(TopiaEntity.TOPIA_ID, TopiaQuery.Op.NEQ, participant.getId()); -// } -// -// // existing participant found -// if (dao.existByQuery(query)) { -// // The error type (message) depends on email nullity -// if (participant.getEmail() == null) { -// throw new ParticipantExistWithoutMailException(); -// } else { -// throw new ParticipantAlreadyFoundInListException(); -// } -// } -// } } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-03-14 14:40:02 UTC (rev 3181) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollCommentService.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -35,8 +35,7 @@ import org.chorem.pollen.services.PollenServiceSupport; import org.chorem.pollen.services.exceptions.PollCommentNotFound; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.web.struts2.FilterPagerUtil; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import java.util.List; @@ -170,28 +169,18 @@ return result; } - public List<Comment> getComments(FilterPagerUtil.FilterPagerBean pager, + public List<Comment> getComments(TopiaFilterPagerUtil.FilterPagerBean pager, String pollId) { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(pollId); - CommentDAO dao = getDAO(Comment.class); - try { - - TopiaQuery countQuery = dao.createQuery("e"). - addWhere("e." + Comment.PROPERTY_POLL + ".pollId", - TopiaQuery.Op.EQ, pollId); - long records = dao.countByQuery(countQuery); - pager.setRecords((int) records); - - TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, - pager); - List<Comment> result = dao.findAllByQuery(query); + CommentDAO dao = getDAO(Comment.class); + List<Comment> result = dao.getComments(pager, pollId); return result; } catch (TopiaException e) { - throw new PollenTechnicalException(e); + throw new PollenTechnicalException("Could not obtain comments", e); } } @@ -199,18 +188,12 @@ Preconditions.checkNotNull(pollId); - CommentDAO dao = getDAO(Comment.class); - try { - - TopiaQuery query = dao.createQuery("e"). - addWhere("e." + Comment.PROPERTY_POLL + ".pollId", - TopiaQuery.Op.EQ, pollId); - query.addOrderDesc(Comment.PROPERTY_POST_DATE); - List<Comment> result = dao.findAllByQuery(query); + CommentDAO dao = getDAO(Comment.class); + List<Comment> result = dao.getAllComments(pollId); return result; } catch (TopiaException e) { - throw new PollenTechnicalException(e); + throw new PollenTechnicalException("Could not obtain comments", e); } } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-14 14:40:02 UTC (rev 3181) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -24,7 +24,6 @@ package org.chorem.pollen.services.impl; import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -50,12 +49,13 @@ import org.chorem.pollen.common.ChoiceType; import org.chorem.pollen.common.PollType; import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.services.exceptions.PollAccountNotFound; import org.chorem.pollen.services.exceptions.PollChoiceNotFoundException; import org.chorem.pollen.services.exceptions.PollNotFoundException; import org.chorem.pollen.services.exceptions.PollVoteNotFoundException; +import org.chorem.pollen.services.exceptions.UnauthorizedPollAccessException; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.web.struts2.FilterPagerUtil; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import javax.imageio.ImageIO; import javax.swing.ImageIcon; @@ -281,52 +281,33 @@ return url.toString(); } - public List<Poll> getPolls(FilterPagerUtil.FilterPagerBean pager) { + public List<Poll> getPolls(TopiaFilterPagerUtil.FilterPagerBean pager) { Preconditions.checkNotNull(pager); - - PollDAO dao = getDAO(Poll.class); - try { - - long records = dao.count(); - pager.setRecords((int) records); - - TopiaQuery query = dao.createQuery("e"); - FilterPagerUtil.addPagerToQuery(query, pager); - - List<Poll> result = dao.findAllByQuery(query); + PollDAO dao = getDAO(Poll.class); + List<Poll> result = dao.getPolls(pager); return result; } catch (TopiaException e) { - throw new PollenTechnicalException(e); + throw new PollenTechnicalException("Could not obtain polls", e); } } - public List<Poll> getCreatedPolls(FilterPagerUtil.FilterPagerBean pager, + public List<Poll> getCreatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount user) { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(user); - - PollDAO dao = getDAO(Poll.class); - try { - - TopiaQuery countQuery = dao.createQuery("e"); - countQuery.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); - long records = dao.countByQuery(countQuery); - pager.setRecords((int) records); - - TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, pager); - - List<Poll> result = dao.findAllByQuery(query); + PollDAO dao = getDAO(Poll.class); + List<Poll> result = dao.getCreatedPolls(pager, user); return result; } catch (TopiaException e) { - throw new PollenTechnicalException(e); + throw new PollenTechnicalException("Could not obtain created polls", e); } } - public List<Poll> getInvitedPolls(FilterPagerUtil.FilterPagerBean pager, + public List<Poll> getInvitedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount user) { Preconditions.checkNotNull(pager); @@ -336,36 +317,15 @@ UserAccount userToUse = userService.getEntityById(UserAccount.class, user.getTopiaId()); - //FIXME Use a query to do this to avoid in memory pagination - String email = userToUse.getEmail(); - - List<Poll> polls = userService.getEntities(Poll.class); - List<Poll> allPolls = Lists.newLinkedList(); - for (Poll poll : polls) { - for (VotingList votingList : poll.getVotingList()) { - for (PersonToList personToList : votingList - .getPollAccountPersonToList()) { - if (!personToList.isHasVoted()) { - PollAccount pollAccount = personToList - .getPollAccount(); - - if (pollAccount != null - && pollAccount.getEmail() != null - && pollAccount.getEmail().equals( - email)) { - allPolls.add(poll); - } - } - } - } + Preconditions.checkNotNull(userToUse); + try { + PollDAO pollDao = getDAO(Poll.class); + List<Poll> result = pollDao.getInvitedPolls(pager, userToUse); + return result; + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not obtain invited polls", e); } - int records = allPolls.size(); - pager.setRecords(records); - - List<Poll> result = FilterPagerUtil.getPageFromList(allPolls, pager); - return result; - // try { // PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); // TopiaQuery countQuery = dao.createQuery("e"); @@ -375,7 +335,7 @@ // // PagerUtil.computeRecordIndexesAndPagesNumber(pager); // TopiaQuery query = dao.createQuery("e"); -// FilterPagerUtil.addPagerToQuery(pager, query); +// TopiaFilterPagerUtil.addPagerToQuery(pager, query); // query.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); // // List<Poll> result = dao.findAllByQuery(query); @@ -385,89 +345,43 @@ // } } - public List<Poll> getParticipatedPolls(FilterPagerUtil.FilterPagerBean pager, + public List<Poll> getParticipatedPolls(TopiaFilterPagerUtil.FilterPagerBean pager, UserAccount user) { Preconditions.checkNotNull(pager); Preconditions.checkNotNull(user); - //FIXME Use a query to do this to avoid in memory pagination - UserService userService = newService(UserService.class); UserAccount userToUse = userService.getEntityById(UserAccount.class, user.getTopiaId()); - PollDAO pollDao = getDAO(Poll.class); - VoteDAO voteDao = getDAO(Vote.class); + Preconditions.checkNotNull(userToUse); try { - - TopiaQuery countQuery = voteDao.createQuery("e"). - addDistinct(). - setSelect("e." + Vote.PROPERTY_POLL). - addWhere("e." + Vote.PROPERTY_POLL_ACCOUNT + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, userToUse); - - long records = pollDao.countByQuery(countQuery); - pager.setRecords((int) records); - - TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, pager); - - List<Poll> result = pollDao.findAllByQuery(query); + PollDAO pollDao = getDAO(Poll.class); + List<Poll> result = pollDao.getParticipatedPolls(pager, userToUse); return result; } catch (TopiaException e) { - throw new PollenTechnicalException(e); + throw new PollenTechnicalException("Could not obtain running polls", e); } } public List<Poll> getRunningPolls(boolean withEndDate) { - List<Poll> results; try { - PollDAO dao = getDAO(Poll.class); - - TopiaQuery query; - - if (withEndDate) { - - query = dao.createQuery("poll"). - addWhere("poll.endDate is not null and poll.endDate > current_timestamp()"). - addWhere("poll.beginDate is null or poll.beginDate < current_timestamp()"); - -// results = transaction -// .find("from " -// + Poll.class.getName() -// + " as poll where (poll.endDate is not null and poll.endDate > current_timestamp())" -// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); - } else { - query = dao.createQuery("poll"). - addWhere("poll.endDate is null or poll.endDate > current_timestamp()"). - addWhere("poll.beginDate is null or poll.beginDate < current_timestamp()"); - -// results = transaction -// .find("from " -// + Poll.class.getName() -// + " as poll where (poll.endDate is null or poll.endDate > current_timestamp())" -// + " and (poll.beginDate is null or poll.beginDate < current_timestamp())"); - } - - results = dao.findAllByQuery(query); - - if (log.isDebugEnabled()) { - log.debug("Entities found: " - + ((results == null) ? "null" : results.size())); - } + List<Poll> results = dao.getRunningPolls(withEndDate); return results; } catch (TopiaException e) { - throw new PollenTechnicalException("Culd not obtain polls", e); + throw new PollenTechnicalException("Could not obtain running polls", e); } } public Poll getPollByPollId(String pollId) { Preconditions.checkNotNull(pollId); - PollDAO dao = getDAO(Poll.class); try { + PollDAO dao = getDAO(Poll.class); Poll result = dao.findByPollId(pollId); return result; } catch (TopiaException e) { @@ -488,8 +402,8 @@ } public PollAccount getPollAccountByAccountId(String accountId) { - PollAccountDAO dao = getDAO(PollAccount.class); try { + PollAccountDAO dao = getDAO(PollAccount.class); PollAccount result = dao.findByAccountId(accountId); return result; } catch (TopiaException e) { @@ -497,6 +411,40 @@ } } + public void deletePoll(String pollId, + UserAccount userAccount, + String accountId) throws PollNotFoundException, PollAccountNotFound, UnauthorizedPollAccessException { + + Preconditions.checkNotNull(pollId); + Preconditions.checkNotNull(accountId); + + Poll poll = getPollByPollId(pollId); + + if (poll == null) { + throw new PollNotFoundException(); + } + + if (userAccount == null || userAccount.isAdministrator()) { + + // must check that accountId matches the poll creator id + + PollAccount account = getPollAccountByAccountId(accountId); + + if (account == null) { + throw new PollAccountNotFound(); + } + + if (!account.getAccountId().equals(poll.getCreator().getAccountId())) { + throw new UnauthorizedPollAccessException(); + } + } + + PollDAO dao = getDAO(Poll.class); + delete(dao, poll); + + commitTransaction("Could not delete poll" + poll.getTitle()); + } + public void deleteVote(String pollId, String voteId) throws PollNotFoundException, PollVoteNotFoundException { Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java 2012-03-14 14:40:02 UTC (rev 3181) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -38,11 +38,10 @@ import org.chorem.pollen.services.exceptions.UserLoginAlreadyUsedException; import org.chorem.pollen.services.exceptions.UserNotFoundException; import org.nuiton.topia.TopiaException; -import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; import org.nuiton.topia.persistence.util.TopiaEntityBinder; import org.nuiton.util.StringUtil; import org.nuiton.util.beans.Binder; -import org.nuiton.web.struts2.FilterPagerUtil; import java.util.List; @@ -177,15 +176,8 @@ // In case of email change, check if an other user has not already // the new email - // FIXME-fdesbois-20100510 : replace by using id directly - TopiaQuery query = dao.createQuery(). - addWhere(UserAccount.PROPERTY_LOGIN, - TopiaQuery.Op.NEQ, user.getLogin()); - query.addEquals(UserAccount.PROPERTY_EMAIL, - StringUtils.lowerCase(user.getEmail())); - // existing user found - if (dao.existByQuery(query)) { + if (dao.isUserExist(user)) { throw new UserEmailAlreadyUsedException(); } @@ -221,21 +213,13 @@ } } - public List<UserAccount> getUsers(FilterPagerUtil.FilterPagerBean pager) { + public List<UserAccount> getUsers(TopiaFilterPagerUtil.FilterPagerBean pager) { Preconditions.checkNotNull(pager); - UserAccountDAO dao = getDAO(UserAccount.class); - try { - - long records = dao.count(); - pager.setRecords((int) records); - - TopiaQuery query = dao.createQuery("e"); - FilterPagerUtil.addPagerToQuery(query, pager); - - List<UserAccount> result = dao.findAllByQuery(query); + UserAccountDAO dao = getDAO(UserAccount.class); + List<UserAccount> result = dao.getUsers(pager); return result; } catch (TopiaException e) { throw new PollenTechnicalException(e); Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-14 14:40:02 UTC (rev 3181) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/VoteService.java 2012-03-14 14:40:57 UTC (rev 3182) @@ -37,8 +37,8 @@ import org.chorem.pollen.services.exceptions.VoteNotFoundException; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.persistence.TopiaFilterPagerUtil; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -108,7 +108,6 @@ // feed notification - } public void updateVote(Vote vote) throws VoteNotFoundException { @@ -126,7 +125,6 @@ } commitTransaction("Could not update vote"); - } public void deleteVote(String voteId) throws VoteNotFoundException { @@ -137,7 +135,8 @@ throw new VoteNotFoundException(); } - delete(getDAO(Vote.class), entityToDelete); + VoteDAO dao = getDAO(Vote.class); + delete(dao, entityToDelete); if (log.isDebugEnabled()) { log.debug("Entity deleted: " + voteId); @@ -147,73 +146,27 @@ } public List<Vote> getVotesByPoll(Poll poll, - int startIndex, int endIndex) { - TopiaContext transaction = getTransaction(); - List<Vote> results = new ArrayList<Vote>(); + TopiaFilterPagerUtil.FilterPagerBean pager) { try { - // No need to load votes for an anonymous poll - if (poll.isAnonymous()) { - return results; - } + VoteDAO dao = getDAO(Vote.class); + List<Vote> results = dao.getVotesByPoll(poll, pager); + return results; - // FIXME-FD20100309 : change model to suppress link from poll to vote - // It's not necessary to have votes when retrieving the poll, this - // method do this job only when it's needed - - if (log.isDebugEnabled()) { - log.debug("Load votes for poll with uid = " + poll.getPollId()); - log.debug("LIMIT : startIndex = " + startIndex + " _ " + - "endIndex = " + endIndex); - } - - // FIXME : refactor this to use TopiaQuery from ToPIA 2.3 - // Order the results by creation date (the last vote done will be - // at the end of the list) - List<Vote> votes = transaction.find( - "FROM " + Vote.class.getName() + - " WHERE poll.pollId = :pollUId" + - " ORDER BY topiaCreateDate", - startIndex, endIndex, - new Object[]{"pollUId", poll.getPollId()}); - - if (log.isDebugEnabled()) { - log.debug("Nb votes found : " + votes.size()); - } - - results = Lists.newArrayList(results); - } catch (TopiaException e) { throw new PollenTechnicalException("Could not obtain votes", e); } - return results; } public boolean hasAlreadyVoted(String votingId, Poll poll) { - TopiaContext transaction = getTransaction(); - boolean result = false; try { - - // Test using a count(*) on votes - List<Long> tmp = transaction.find( - "SELECT COUNT(*)" + - " FROM " + Vote.class.getName() + - " WHERE poll.pollId = :pollUId" + - " AND pollAccount.votingId = :votingId", - "pollUId", poll.getPollId(), - "votingId", votingId); - - int count = tmp.get(0).intValue(); - - // If the count is greater than 0, there is an existing votingId - // who has already voted for the poll - result = count > 0; - + VoteDAO dao = getDAO(Vote.class); + boolean result = dao.hasAlreadyVoted(votingId, poll); + return result; } catch (Exception e) { throw new PollenTechnicalException( "Unable test vote existing for account with votingId = " + votingId + " and poll with uid = " + poll.getPollId(), e); } - return result; } public List<Vote> selectVotes(Map<String, Object> properties) {
participants (1)
-
tchemit@users.chorem.org