Author: tchemit Date: 2012-02-22 13:07:31 +0100 (Wed, 22 Feb 2012) New Revision: 3135 Url: http://chorem.org/repositories/revision/pollen/3135 Log: use nuiton-struts2 filter pager bean + improve some code Removed: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenPagerBean.java Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 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/PollService.java branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java Deleted: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenPagerBean.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenPagerBean.java 2012-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenPagerBean.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -1,94 +0,0 @@ -/* - * #%L - * Pollen :: Services - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin, Tony Chemit - * %% - * 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; - -import org.apache.commons.lang3.StringUtils; -import org.nuiton.topia.framework.TopiaQuery; -import org.nuiton.util.PagerUtil; - -import java.util.ArrayList; -import java.util.List; - -/** - * Override nuiton pager while adding sort informations. - * <p/> - * Maybe this should be push back to nuiton ? - * - * @author tchemit <chemit@codelutin.com> - * @since 1.2.6 - */ -public class PollenPagerBean extends PagerUtil.PagerBean { - - private static final long serialVersionUID = 1L; - - // sorting order - asc or desc - protected boolean sortAscendant; - - // get index row - i.e. user click to sort. - protected String sortColumn; - - public boolean isSortAscendant() { - return sortAscendant; - } - - public void setSortAscendant(boolean sortAscendant) { - this.sortAscendant = sortAscendant; - } - - public String getSortColumn() { - return sortColumn; - } - - public void setSortColumn(String sortColumn) { - this.sortColumn = sortColumn; - } - - public static TopiaQuery addPagerToQuery(TopiaQuery query, - PollenPagerBean pager) { - PagerUtil.computeRecordIndexesAndPagesNumber(pager); - query.setLimit(pager.getRecordStartIndex(), - pager.getRecordEndIndex() - 1); - - if (StringUtils.isNotEmpty(pager.getSortColumn())) { - if (pager.isSortAscendant()) { - query.addOrder(pager.getSortColumn()); - } else { - query.addOrderDesc(pager.getSortColumn()); - } - } - return query; - } - - public static <E> List<E> getPageFromList(List<E> elements, - PollenPagerBean pager) { - - PagerUtil.computeRecordIndexesAndPagesNumber(pager); - List<E> subList = elements.subList(pager.getRecordStartIndex(), - pager.getRecordEndIndex()); - List<E> result = new ArrayList<E>(subList); - - return result; - } - -} Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceSupport.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -83,6 +83,24 @@ } } + public <T extends TopiaEntity, D extends TopiaDAO<? super T>> D getDAO(Class<T> entityType) { + Preconditions.checkNotNull(entityType); + try { + D dao = PollenDAOHelper.getDAO(getTransaction(), entityType); + return dao; + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not obtain dao of type " + entityType.getName(), e); + } + } + + protected <T extends TopiaEntity, D extends TopiaDAO<? super T>> T newInstance(D dao) { + try { + return (T) dao.newInstance(); + } catch (TopiaException e) { + throw new PollenTechnicalException("Could not obtain new instance from dao " + dao, e); + } + } + protected TopiaContext getTransaction() { return serviceContext.getTransaction(); } Modified: branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java =================================================================== --- branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/EmailService.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -23,6 +23,7 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.mail.EmailException; @@ -39,6 +40,11 @@ public void sendEmail(String to, String subject, String content) { + + Preconditions.checkNotNull(to); + Preconditions.checkNotNull(subject); + Preconditions.checkNotNull(content); + try { // Create the SimpleEmail to send SimpleEmail email = createSimpleEmail(to, subject, content); @@ -65,6 +71,11 @@ String to, String subject, String content) throws EmailException { + + Preconditions.checkNotNull(to); + Preconditions.checkNotNull(subject); + Preconditions.checkNotNull(content); + PollenConfiguration configuration = getConfiguration(); SimpleEmail result = new SimpleEmail(); 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-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/FavoriteService.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -23,15 +23,368 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Preconditions; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.PollenTechnicalException; +import org.chorem.pollen.business.persistence.PersonList; +import org.chorem.pollen.business.persistence.PersonListDAO; +import org.chorem.pollen.business.persistence.PollAccount; +import org.chorem.pollen.business.persistence.PollAccountDAO; +import org.chorem.pollen.business.persistence.UserAccount; +import org.chorem.pollen.business.persistence.UserAccountDAO; import org.chorem.pollen.services.PollenServiceSupport; +import org.chorem.pollen.services.exceptions.FavoriteListAlreadyExistException; +import org.chorem.pollen.services.exceptions.FavoriteListNotFoundException; +import org.chorem.pollen.services.exceptions.FavoriteListNotOwnedByUserException; +import org.chorem.pollen.services.exceptions.ParticipantAlreadyFoundInListException; +import org.chorem.pollen.services.exceptions.ParticipantNotFoundException; +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 java.util.List; + public class FavoriteService extends PollenServiceSupport { /** Logger. */ private static final Log log = LogFactory.getLog(FavoriteService.class); + public List<PersonList> getFavoriteLists(UserAccount user, + FilterPagerUtil.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); + return result; + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public List<PersonList> getFavoriteLists(UserAccount user) { + + 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); + return result; + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public PersonList getFavoriteList(UserAccount user, + String favoriteListId) throws FavoriteListNotFoundException, FavoriteListNotOwnedByUserException { + + Preconditions.checkNotNull(user); + Preconditions.checkNotNull(favoriteListId); + + PersonListDAO dao = getDAO(PersonList.class); + + try { + + PersonList favoriteList = dao.findByTopiaId(favoriteListId); + + if (favoriteList == null) { + throw new FavoriteListNotFoundException(); + } + + // check this favorite belongs to the given user + UserAccount favoriteListOwner = favoriteList.getOwner(); + if (!favoriteListOwner.equals(user)) { + throw new FavoriteListNotOwnedByUserException(); + } + + return favoriteList; + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public List<PollAccount> getFavoriteListUsers(PersonList favoriteList, + FilterPagerUtil.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); + return result; + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public void createFavoriteList(UserAccount user, + String name) throws FavoriteListAlreadyExistException, UserNotFoundException { + + Preconditions.checkNotNull(user); + Preconditions.checkNotNull(name); + + // can't accept favorite list without name + Preconditions.checkArgument(StringUtils.isNotEmpty(name)); + + UserAccountDAO userDAO = getDAO(UserAccount.class); + PersonListDAO dao = getDAO(PersonList.class); + + try { + + UserAccount userToUse = + userDAO.findByTopiaId(user.getTopiaId()); + + if (userToUse == null) { + throw new UserNotFoundException(); + } + + // check list does not already exists + + TopiaQuery query = dao.createQuery("e"); + + 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(); + } + + PersonList favoriteListCreated = dao.create( + PersonList.PROPERTY_OWNER, userToUse + ); + favoriteListCreated.setName(name); + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public PersonList deleteFavoriteList(UserAccount user, + PersonList favoriteList) throws FavoriteListNotFoundException, FavoriteListNotOwnedByUserException, UserNotFoundException { + + Preconditions.checkNotNull(user); + Preconditions.checkNotNull(favoriteList); + + UserAccountDAO userDAO = getDAO(UserAccount.class); + PersonListDAO dao = getDAO(PersonList.class); + + try { + + UserAccount userToUse = + userDAO.findByTopiaId(user.getTopiaId()); + + if (userToUse == null) { + throw new UserNotFoundException(); + } + + PersonList favoriteListToDelete = + dao.findByTopiaId(favoriteList.getTopiaId()); + + if (favoriteListToDelete == null) { + throw new FavoriteListNotFoundException(); + } + + // check this favorite belongs to the given user + UserAccount favoriteListOwner = favoriteListToDelete.getOwner(); + if (!favoriteListOwner.equals(user)) { + throw new FavoriteListNotOwnedByUserException(); + } + + dao.delete(favoriteListToDelete); + return favoriteListToDelete; + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public void addPollAccountToFavoriteList(PersonList favoriteList, + PollAccount pollAccount) throws ParticipantAlreadyFoundInListException, FavoriteListNotFoundException { + + Preconditions.checkNotNull(favoriteList); + Preconditions.checkNotNull(pollAccount); + + // can't accept poll account without name + Preconditions.checkArgument( + StringUtils.isNotEmpty(pollAccount.getVotingId())); + + // can't accept poll account without email + Preconditions.checkArgument( + StringUtils.isNotEmpty(pollAccount.getEmail())); + + PollAccountDAO dao = getDAO(PollAccount.class); + PersonListDAO personListDAO = getDAO(PersonList.class); + + try { + + PersonList personListToUpdate = + personListDAO.findByTopiaId(favoriteList.getTopiaId()); + + if (personListToUpdate == null) { + throw new FavoriteListNotFoundException(); + } + + // 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); + if (pollAccountExists) { + + throw new ParticipantAlreadyFoundInListException(); + } + + PollAccount pollAccountCreated = + dao.create(PollAccount.PROPERTY_PERSON_LIST, + personListToUpdate); + + pollAccountCreated.setVotingId(pollAccount.getVotingId()); + pollAccountCreated.setEmail(pollAccount.getEmail()); + personListToUpdate.addPollAccount(pollAccountCreated); + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public void editPollAccountToFavoriteList(PersonList favoriteList, + PollAccount pollAccount) throws ParticipantAlreadyFoundInListException, FavoriteListNotFoundException, ParticipantNotFoundException { + + Preconditions.checkNotNull(favoriteList); + Preconditions.checkNotNull(pollAccount); + + // can't accept poll account without name + Preconditions.checkArgument( + StringUtils.isNotEmpty(pollAccount.getVotingId())); + + // can't accept poll account without email + Preconditions.checkArgument( + StringUtils.isNotEmpty(pollAccount.getEmail())); + + PollAccountDAO dao = getDAO(PollAccount.class); + PersonListDAO personListDAO = getDAO(PersonList.class); + + try { + + PersonList personListToUpdate = + personListDAO.findByTopiaId(favoriteList.getTopiaId()); + + if (personListToUpdate == null) { + throw new FavoriteListNotFoundException(); + } + + PollAccount pollAccountToUpdate = + dao.findByTopiaId(pollAccount.getTopiaId()); + + if (pollAccountToUpdate == null) { + throw new ParticipantNotFoundException(); + } + + // 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); + if (pollAccountExists) { + + throw new ParticipantAlreadyFoundInListException(); + } + + pollAccountToUpdate.setVotingId(pollAccount.getVotingId()); + pollAccountToUpdate.setEmail(pollAccount.getEmail()); + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public void removePollAccountToFavoriteList(PersonList favoriteList, + PollAccount pollAccount) throws ParticipantNotFoundException, FavoriteListNotFoundException, ParticipantNotFoundInListException { + + Preconditions.checkNotNull(favoriteList); + Preconditions.checkNotNull(pollAccount); + + PollAccountDAO dao = getDAO(PollAccount.class); + PersonListDAO personListDAO = getDAO(PersonList.class); + + try { + + PersonList personListToUpdate = + personListDAO.findByTopiaId(favoriteList.getTopiaId()); + + if (personListToUpdate == null) { + throw new FavoriteListNotFoundException(); + } + + PollAccount pollAccountToRemove = + dao.findByTopiaId(pollAccount.getTopiaId()); + + if (pollAccountToRemove == null) { + + throw new ParticipantNotFoundException(); + } + + PollAccount pollAccountInList = + personListToUpdate.getPollAccountByTopiaId(pollAccount.getTopiaId()); + + if (pollAccountInList == null) { + + throw new ParticipantNotFoundInListException(); + } + + personListToUpdate.removePollAccount(pollAccountToRemove); + + } catch (TopiaException e) { + throw new PollenTechnicalException(e); + } + } + + public PersonList newFavoriteList() { + PersonListDAO dao = getDAO(PersonList.class); + PersonList personList = newInstance(dao); + return personList; + } + + public PollAccount newPollAccountForFavoriteList() { + PollAccountDAO dao = getDAO(PollAccount.class); + PollAccount pollAccount = newInstance(dao); + return pollAccount; + } + // public void createFavoriteParticipant(FavoriteParticipant participant) throws ParticipantExistWithoutMailException, ParticipantAlreadyFoundInListException { // try { // FavoriteParticipantDAO dao = 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-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollService.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -23,6 +23,7 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -31,18 +32,16 @@ import org.chorem.pollen.business.persistence.PersonToList; import org.chorem.pollen.business.persistence.Poll; import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.PollAccountImpl; +import org.chorem.pollen.business.persistence.PollAccountDAO; import org.chorem.pollen.business.persistence.PollDAO; -import org.chorem.pollen.business.persistence.PollImpl; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.Vote; import org.chorem.pollen.business.persistence.VoteDAO; import org.chorem.pollen.business.persistence.VotingList; -import org.chorem.pollen.entities.PollenDAOHelper; -import org.chorem.pollen.services.PollenPagerBean; import org.chorem.pollen.services.PollenServiceSupport; import org.nuiton.topia.TopiaException; import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.web.struts2.FilterPagerUtil; import java.util.List; @@ -58,8 +57,10 @@ * @return Build a new Poll instance with given {@code user} as administrator */ public Poll getNewPoll(UserAccount user) { - Poll result = new PollImpl(); + PollDAO pollDAO = getDAO(Poll.class); + Poll result = newInstance(pollDAO); + PollenConfiguration configuration = getConfiguration(); // default values from configuration @@ -68,7 +69,8 @@ result.setPollType(configuration.getDefaultPollType()); // Initialize creator as an admin for the poll - PollAccount creator = new PollAccountImpl(); + PollAccountDAO pollAccountDAO = getDAO(PollAccount.class); + PollAccount creator = newInstance(pollAccountDAO); // creator.setAdmin(true); if (user != null) { // Link the creator with the user @@ -83,16 +85,19 @@ return result; } - public List<Poll> getPolls(PollenPagerBean pager) { + public List<Poll> getPolls(FilterPagerUtil.FilterPagerBean pager) { + Preconditions.checkNotNull(pager); + + PollDAO dao = getDAO(Poll.class); + try { - PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); long records = dao.count(); pager.setRecords((int) records); TopiaQuery query = dao.createQuery("e"); - PollenPagerBean.addPagerToQuery(query, pager); + FilterPagerUtil.addPagerToQuery(query, pager); List<Poll> result = dao.findAllByQuery(query); return result; @@ -101,17 +106,21 @@ } } - public List<Poll> getCreatedPolls(PollenPagerBean pager, UserAccount user) { + public List<Poll> getCreatedPolls(FilterPagerUtil.FilterPagerBean pager, UserAccount user) { + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(user); + + PollDAO dao = getDAO(Poll.class); + try { - PollDAO dao = PollenDAOHelper.getPollDAO(getTransaction()); 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 = PollenPagerBean.addPagerToQuery(countQuery, pager); + TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, pager); List<Poll> result = dao.findAllByQuery(query); return result; @@ -120,8 +129,11 @@ } } - public List<Poll> getInvitedPolls(PollenPagerBean pager, UserAccount user) { + public List<Poll> getInvitedPolls(FilterPagerUtil.FilterPagerBean pager, UserAccount user) { + Preconditions.checkNotNull(pager); + Preconditions.checkNotNull(user); + UserService userService = newService(UserService.class); UserAccount userToUse = userService.getEntityById(UserAccount.class, user.getTopiaId()); @@ -152,7 +164,7 @@ int records = allPolls.size(); pager.setRecords(records); - List<Poll> result = PollenPagerBean.getPageFromList(allPolls, pager); + List<Poll> result = FilterPagerUtil.getPageFromList(allPolls, pager); return result; // try { @@ -164,7 +176,7 @@ // // PagerUtil.computeRecordIndexesAndPagesNumber(pager); // TopiaQuery query = dao.createQuery("e"); -// PollenPagerBean.addPagerToQuery(pager, query); +// FilterPagerUtil.addPagerToQuery(pager, query); // query.addWhere("e." + Poll.PROPERTY_CREATOR + "." + PollAccount.PROPERTY_USER_ACCOUNT, TopiaQuery.Op.EQ, user); // // List<Poll> result = dao.findAllByQuery(query); @@ -174,9 +186,12 @@ // } } - public List<Poll> getParticipatedPolls(PollenPagerBean pager, + public List<Poll> getParticipatedPolls(FilterPagerUtil.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); @@ -195,13 +210,14 @@ // int records = allPolls.size(); // pager.setRecords(records); // PagerUtil.computeRecordIndexesAndPagesNumber(pager); -// List<Poll> result = PollenPagerBean.getPageFromList(allPolls, pager); +// List<Poll> result = FilterPagerUtil.getPageFromList(allPolls, pager); // return result; + PollDAO pollDao = getDAO(Poll.class); + VoteDAO voteDao = getDAO(Vote.class); + try { - PollDAO pollDao = PollenDAOHelper.getPollDAO(getTransaction()); - VoteDAO voteDao = PollenDAOHelper.getVoteDAO(getTransaction()); TopiaQuery countQuery = voteDao.createQuery("e"). addDistinct(). @@ -211,7 +227,7 @@ long records = pollDao.countByQuery(countQuery); pager.setRecords((int) records); - TopiaQuery query = PollenPagerBean.addPagerToQuery(countQuery, pager); + TopiaQuery query = FilterPagerUtil.addPagerToQuery(countQuery, pager); // query.addDistinct(); // query.setSelect("e." + Vote.PROPERTY_POLL); 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-02-22 12:06:41 UTC (rev 3134) +++ branches/pollen-1.2.6-struts2/pollen-services/src/main/java/org/chorem/pollen/services/impl/UserService.java 2012-02-22 12:07:31 UTC (rev 3135) @@ -29,25 +29,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.PollenTechnicalException; -import org.chorem.pollen.business.persistence.PersonList; -import org.chorem.pollen.business.persistence.PersonListDAO; -import org.chorem.pollen.business.persistence.PersonListImpl; -import org.chorem.pollen.business.persistence.PollAccount; -import org.chorem.pollen.business.persistence.PollAccountDAO; -import org.chorem.pollen.business.persistence.PollAccountImpl; import org.chorem.pollen.business.persistence.UserAccount; import org.chorem.pollen.business.persistence.UserAccountDAO; -import org.chorem.pollen.business.persistence.UserAccountImpl; import org.chorem.pollen.entities.PollenBinderHelper; -import org.chorem.pollen.entities.PollenDAOHelper; -import org.chorem.pollen.services.PollenPagerBean; import org.chorem.pollen.services.PollenServiceSupport; -import org.chorem.pollen.services.exceptions.FavoriteListAlreadyExistException; -import org.chorem.pollen.services.exceptions.FavoriteListNotFoundException; -import org.chorem.pollen.services.exceptions.FavoriteListNotOwnedByUserException; -import org.chorem.pollen.services.exceptions.ParticipantAlreadyFoundInListException; -import org.chorem.pollen.services.exceptions.ParticipantNotFoundException; -import org.chorem.pollen.services.exceptions.ParticipantNotFoundInListException; import org.chorem.pollen.services.exceptions.UserEmailAlreadyUsedException; import org.chorem.pollen.services.exceptions.UserInvalidPasswordException; import org.chorem.pollen.services.exceptions.UserLoginAlreadyUsedException; @@ -57,6 +42,7 @@ 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.net.URL; import java.util.List; @@ -71,9 +57,13 @@ private static final Log log = LogFactory.getLog(UserService.class); public UserAccount connect(String login, String password) throws UserNotFoundException, UserInvalidPasswordException { + + Preconditions.checkNotNull(login); + Preconditions.checkNotNull(password); + + UserAccountDAO dao = getDAO(UserAccount.class); + try { - UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(getTransaction()); UserAccount user = dao.findByLogin(login); @@ -96,12 +86,14 @@ boolean byAdmin, URL url) throws UserLoginAlreadyUsedException, UserEmailAlreadyUsedException { + Preconditions.checkNotNull(user); + String password = user.getPassword(); // le mot de passe en clair + UserAccountDAO dao = getDAO(UserAccount.class); + UserAccount userAccount; try { - UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(getTransaction()); UserAccount userByLogin = dao.findByLogin(user.getLogin()); if (userByLogin != null) { @@ -153,11 +145,12 @@ String newPassword, boolean byAdmin) throws UserEmailAlreadyUsedException, UserInvalidPasswordException { - try { + Preconditions.checkNotNull(user); - UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(getTransaction()); + UserAccountDAO dao = getDAO(UserAccount.class); + try { + UserAccount userToUpdate = dao.findByTopiaId(user.getTopiaId()); // Do not manage password for an admin update @@ -200,14 +193,18 @@ } public UserAccount getNewUser() { - UserAccount result = new UserAccountImpl(); + UserAccountDAO dao = getDAO(UserAccount.class); + UserAccount result = newInstance(dao); return result; } public void deleteUser(String id) throws UserNotFoundException { + + Preconditions.checkNotNull(id); + + UserAccountDAO dao = getDAO(UserAccount.class); + try { - UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(getTransaction()); UserAccount user = dao.findByTopiaId(id); if (user == null) { @@ -219,17 +216,19 @@ } } - public List<UserAccount> getUsers(PollenPagerBean pager) { + public List<UserAccount> getUsers(FilterPagerUtil.FilterPagerBean pager) { + Preconditions.checkNotNull(pager); + + UserAccountDAO dao = getDAO(UserAccount.class); + try { - UserAccountDAO dao = - PollenDAOHelper.getUserAccountDAO(getTransaction()); long records = dao.count(); pager.setRecords((int) records); TopiaQuery query = dao.createQuery("e"); - PollenPagerBean.addPagerToQuery(query, pager); + FilterPagerUtil.addPagerToQuery(query, pager); List<UserAccount> result = dao.findAllByQuery(query); return result; @@ -238,336 +237,11 @@ } } - public List<PersonList> getFavoriteLists(UserAccount user, - PollenPagerBean pager) { + public int getNbUsers() { - try { + UserAccountDAO dao = getDAO(UserAccount.class); - PersonListDAO dao = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - 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 = PollenPagerBean.addPagerToQuery(countQuery, pager); - - List<PersonList> result = dao.findAllByQuery(query); - return result; - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public List<PersonList> getFavoriteLists(UserAccount user) { try { - - PersonListDAO dao = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - TopiaQuery query = dao.createQuery("e"); - query.addWhere("e." + PersonList.PROPERTY_OWNER, TopiaQuery.Op.EQ, user); - List<PersonList> result = dao.findAllByQuery(query); - return result; - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public PersonList getFavoriteList(UserAccount user, - String favoriteListId) throws FavoriteListNotFoundException, FavoriteListNotOwnedByUserException { - try { - - PersonListDAO dao = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - PersonList favoriteList = dao.findByTopiaId(favoriteListId); - - if (favoriteList == null) { - throw new FavoriteListNotFoundException(); - } - - // check this favorite belongs to the given user - UserAccount favoriteListOwner = favoriteList.getOwner(); - if (!favoriteListOwner.equals(user)) { - throw new FavoriteListNotOwnedByUserException(); - } - - return favoriteList; - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public List<PollAccount> getFavoriteListUsers(PersonList favoriteList, - PollenPagerBean pager) { - - Preconditions.checkNotNull(favoriteList); - Preconditions.checkNotNull(pager); - try { - - int records = favoriteList.sizePollAccount(); - pager.setRecords(records); - - PollAccountDAO dao = - PollenDAOHelper.getPollAccountDAO(getTransaction()); - - TopiaQuery query = dao.createQuery("e"); - PollenPagerBean.addPagerToQuery(query, pager); - query.addWhere("e." + PollAccount.PROPERTY_PERSON_LIST, TopiaQuery.Op.EQ, favoriteList); - - List<PollAccount> result = dao.findAllByQuery(query); - return result; - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public void createFavoriteList(UserAccount user, - String name) throws FavoriteListAlreadyExistException, UserNotFoundException { - - Preconditions.checkNotNull(user); - Preconditions.checkNotNull(name); - - // can't accept favorite list without name - Preconditions.checkArgument(StringUtils.isNotEmpty(name)); - try { - - UserAccountDAO userDAO = - PollenDAOHelper.getUserAccountDAO(getTransaction()); - - UserAccount userToUse = - userDAO.findByTopiaId(user.getTopiaId()); - - if (userToUse == null) { - throw new UserNotFoundException(); - } - - PersonListDAO dao = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - // check list does not already exists - - TopiaQuery query = dao.createQuery("e"); - - 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(); - } - - PersonList favoriteListCreated = dao.create( - PersonList.PROPERTY_OWNER, userToUse - ); - favoriteListCreated.setName(name); - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public PersonList deleteFavoriteList(UserAccount user, - PersonList favoriteList) throws FavoriteListNotFoundException, FavoriteListNotOwnedByUserException, UserNotFoundException { - try { - - Preconditions.checkNotNull(user); - Preconditions.checkNotNull(favoriteList); - - UserAccountDAO userDAO = - PollenDAOHelper.getUserAccountDAO(getTransaction()); - - UserAccount userToUse = - userDAO.findByTopiaId(user.getTopiaId()); - - if (userToUse == null) { - throw new UserNotFoundException(); - } - - PersonListDAO dao = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - PersonList favoriteListToDelete = - dao.findByTopiaId(favoriteList.getTopiaId()); - - if (favoriteListToDelete == null) { - throw new FavoriteListNotFoundException(); - } - - // check this favorite belongs to the given user - UserAccount favoriteListOwner = favoriteListToDelete.getOwner(); - if (!favoriteListOwner.equals(user)) { - throw new FavoriteListNotOwnedByUserException(); - } - - dao.delete(favoriteListToDelete); - return favoriteListToDelete; - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public void addPollAccountToFavoriteList(PersonList favoriteList, - PollAccount pollAccount) throws ParticipantAlreadyFoundInListException, FavoriteListNotFoundException { - - Preconditions.checkNotNull(favoriteList); - Preconditions.checkNotNull(pollAccount); - - // can't accept poll account without name - Preconditions.checkArgument( - StringUtils.isNotEmpty(pollAccount.getVotingId())); - - // can't accept poll account without email - Preconditions.checkArgument( - StringUtils.isNotEmpty(pollAccount.getEmail())); - - try { - - PersonListDAO personListDAO = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - PersonList personListToUpdate = - personListDAO.findByTopiaId(favoriteList.getTopiaId()); - - if (personListToUpdate == null) { - throw new FavoriteListNotFoundException(); - } - - PollAccountDAO dao = PollenDAOHelper.getPollAccountDAO(getTransaction()); - - // 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); - if (pollAccountExists) { - - throw new ParticipantAlreadyFoundInListException(); - } - - PollAccount pollAccountCreated = - dao.create(PollAccount.PROPERTY_PERSON_LIST, - personListToUpdate); - - pollAccountCreated.setVotingId(pollAccount.getVotingId()); - pollAccountCreated.setEmail(pollAccount.getEmail()); - personListToUpdate.addPollAccount(pollAccountCreated); - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public void editPollAccountToFavoriteList(PersonList favoriteList, - PollAccount pollAccount) throws ParticipantAlreadyFoundInListException, FavoriteListNotFoundException, ParticipantNotFoundException { - - Preconditions.checkNotNull(favoriteList); - Preconditions.checkNotNull(pollAccount); - - // can't accept poll account without name - Preconditions.checkArgument( - StringUtils.isNotEmpty(pollAccount.getVotingId())); - - // can't accept poll account without email - Preconditions.checkArgument( - StringUtils.isNotEmpty(pollAccount.getEmail())); - - try { - - PersonListDAO personListDAO = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - PersonList personListToUpdate = - personListDAO.findByTopiaId(favoriteList.getTopiaId()); - - if (personListToUpdate == null) { - throw new FavoriteListNotFoundException(); - } - - PollAccountDAO dao = - PollenDAOHelper.getPollAccountDAO(getTransaction()); - - - PollAccount pollAccountToUpdate = - dao.findByTopiaId(pollAccount.getTopiaId()); - - if (pollAccountToUpdate == null) { - throw new ParticipantNotFoundException(); - } - - // 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); - if (pollAccountExists) { - - throw new ParticipantAlreadyFoundInListException(); - } - - pollAccountToUpdate.setVotingId(pollAccount.getVotingId()); - pollAccountToUpdate.setEmail(pollAccount.getEmail()); - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public void removePollAccountToFavoriteList(PersonList favoriteList, - PollAccount pollAccount) throws ParticipantNotFoundException, FavoriteListNotFoundException, ParticipantNotFoundInListException { - - Preconditions.checkNotNull(favoriteList); - Preconditions.checkNotNull(pollAccount); - - try { - - PersonListDAO personListDAO = - PollenDAOHelper.getPersonListDAO(getTransaction()); - - PersonList personListToUpdate = - personListDAO.findByTopiaId(favoriteList.getTopiaId()); - - if (personListToUpdate == null) { - throw new FavoriteListNotFoundException(); - } - - PollAccountDAO dao = - PollenDAOHelper.getPollAccountDAO(getTransaction()); - - PollAccount pollAccountToRemove = - dao.findByTopiaId(pollAccount.getTopiaId()); - - if (pollAccountToRemove == null) { - - throw new ParticipantNotFoundException(); - } - - PollAccount pollAccountInList = - personListToUpdate.getPollAccountByTopiaId(pollAccount.getTopiaId()); - - if (pollAccountInList == null) { - - throw new ParticipantNotFoundInListException(); - } - - personListToUpdate.removePollAccount(pollAccountToRemove); - - } catch (TopiaException e) { - throw new PollenTechnicalException(e); - } - } - - public int getNbUsers() { - try { - UserAccountDAO dao = PollenDAOHelper.getUserAccountDAO(getTransaction()); Long result = dao.count(); return result.intValue(); } catch (TopiaException e) { @@ -576,7 +250,9 @@ } public void createDefaultUsers() { + UserAccount user = getNewUser(); + user.setAdministrator(true); String login = getConfiguration().getAdminLogin(); user.setLogin(login); @@ -621,12 +297,4 @@ destination.setEmail(StringUtils.lowerCase(source.getEmail())); } - public PersonList newFavoriteList() { - PersonListImpl personList = new PersonListImpl(); - return personList; - } - - public PollAccount newPollAccountForFavoriteList() { - return new PollAccountImpl(); - } }