Author: tchemit Date: 2013-06-17 17:36:31 +0200 (Mon, 17 Jun 2013) New Revision: 3832 Url: http://chorem.org/projects/pollen/repository/revisions/3832 Log: review favorite list service Added: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListFormException.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListMemberFormException.java Removed: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberEmailAlreadyUsedException.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberNameAlreadyUsedException.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListNameAlreadyUsedException.java Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListService.java trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListService.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListService.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListService.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/FavoriteListService.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -28,11 +28,10 @@ import org.chorem.pollen.services.PollenServiceContext; import org.chorem.pollen.services.exception.EntityNotFoundException; import org.chorem.pollen.services.exception.FavoriteListImportException; -import org.chorem.pollen.services.exception.FavoriteListMemberEmailAlreadyUsedException; -import org.chorem.pollen.services.exception.FavoriteListMemberNameAlreadyUsedException; import org.chorem.pollen.services.exception.FavoriteListMemberNotOwnedByFavoriteListException; -import org.chorem.pollen.services.exception.FavoriteListNameAlreadyUsedException; import org.chorem.pollen.services.exception.FavoriteListNotOwnedByUserException; +import org.chorem.pollen.services.exception.InvalidFavoriteListFormException; +import org.chorem.pollen.services.exception.InvalidFavoriteListMemberFormException; import org.debux.webmotion.server.WebMotionController; import java.io.File; @@ -52,15 +51,15 @@ return favoriteLists; } - public FavoriteList getFavoriteList(PollenServiceContext context, String favoriteListId) throws EntityNotFoundException { - return context.getFavoriteListService().getFavoriteList(favoriteListId); + public FavoriteList getFavoriteList(PollenServiceContext context, String userId, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + return context.getFavoriteListService().getFavoriteList(userId, favoriteListId); } - public FavoriteList createFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) throws EntityNotFoundException, FavoriteListNameAlreadyUsedException { + public FavoriteList createFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) throws EntityNotFoundException, InvalidFavoriteListFormException { return context.getFavoriteListService().createFavoriteList(userId, favoriteList); } - public FavoriteList editFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) throws EntityNotFoundException, FavoriteListNameAlreadyUsedException, FavoriteListNotOwnedByUserException { + public FavoriteList editFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) throws EntityNotFoundException, FavoriteListNotOwnedByUserException, InvalidFavoriteListFormException { return context.getFavoriteListService().editFavoriteList(userId, favoriteList); } @@ -76,24 +75,24 @@ return context.getFavoriteListService().importFavoriteListMembersFromLdap(userId, favoriteListId, ldap); } - public List<FavoriteListMember> getMembers(PollenServiceContext context, String favoriteListId) throws EntityNotFoundException { - List<FavoriteListMember> members = context.getFavoriteListService().getFavoriteListMembers(favoriteListId); + public List<FavoriteListMember> getMembers(PollenServiceContext context, String userId, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + List<FavoriteListMember> members = context.getFavoriteListService().getFavoriteListMembers(userId, favoriteListId); return members; } - public FavoriteListMember getMember(PollenServiceContext context, String memberId) throws EntityNotFoundException { - return context.getFavoriteListService().getFavoriteListMember(memberId); + public FavoriteListMember getMember(PollenServiceContext context, String userId, String favoriteListId, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, FavoriteListNotOwnedByUserException { + return context.getFavoriteListService().getFavoriteListMember(userId, favoriteListId, memberId); } - public FavoriteListMember addMember(PollenServiceContext context, String favoriteListId, FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberEmailAlreadyUsedException, FavoriteListMemberNameAlreadyUsedException { - return context.getFavoriteListService().addFavoriteListMember(favoriteListId, member); + public FavoriteListMember addMember(PollenServiceContext context, String userId, String favoriteListId, FavoriteListMember member) throws EntityNotFoundException, InvalidFavoriteListMemberFormException, FavoriteListNotOwnedByUserException { + return context.getFavoriteListService().addFavoriteListMember(userId, favoriteListId, member); } - public FavoriteListMember editMember(PollenServiceContext context, String favoriteListId, FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberEmailAlreadyUsedException, FavoriteListMemberNameAlreadyUsedException, FavoriteListMemberNotOwnedByFavoriteListException { - return context.getFavoriteListService().editFavoriteListMember(favoriteListId, member); + public FavoriteListMember editMember(PollenServiceContext context, String userId, String favoriteListId, FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, InvalidFavoriteListMemberFormException, FavoriteListNotOwnedByUserException { + return context.getFavoriteListService().editFavoriteListMember(userId, favoriteListId, member); } - public void removeMember(PollenServiceContext context, String favoriteListId, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException { - context.getFavoriteListService().removeFavoriteListMember(favoriteListId, memberId); + public void removeMember(PollenServiceContext context, String userId, String favoriteListId, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, FavoriteListNotOwnedByUserException { + context.getFavoriteListService().removeFavoriteListMember(userId, favoriteListId, memberId); } } Modified: trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListService.java =================================================================== --- trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListService.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoterListService.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -27,6 +27,7 @@ import org.chorem.pollen.persistence.entity.VoterListMember; import org.chorem.pollen.services.PollenServiceContext; import org.chorem.pollen.services.exception.EntityNotFoundException; +import org.chorem.pollen.services.exception.FavoriteListNotOwnedByUserException; import org.chorem.pollen.services.exception.InvalidVoterListFormException; import org.debux.webmotion.server.WebMotionController; @@ -41,8 +42,8 @@ */ public class VoterListService extends WebMotionController { - public VoterList importFavoriteList(PollenServiceContext context, String pollId, String favoriteListId) throws EntityNotFoundException { - return context.getVoterListService().importFavoriteList(pollId, favoriteListId); + public VoterList importFavoriteList(PollenServiceContext context, String userId, String pollId, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + return context.getVoterListService().importFavoriteList(userId, pollId, favoriteListId); } public List<VoterList> getVoterLists(PollenServiceContext context, String pollId) throws EntityNotFoundException { Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberEmailAlreadyUsedException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberEmailAlreadyUsedException.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberEmailAlreadyUsedException.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -1,45 +0,0 @@ -package org.chorem.pollen.services.exception; - -/* - * #%L - * Pollen :: Service - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2013 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% - */ - -/** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ -public class FavoriteListMemberEmailAlreadyUsedException extends Exception { - - private static final long serialVersionUID = 1L; - - protected final String email; - - public FavoriteListMemberEmailAlreadyUsedException(String email) { - this.email = email; - } - - public String getEmail() { - return email; - } -} \ No newline at end of file Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberNameAlreadyUsedException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberNameAlreadyUsedException.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListMemberNameAlreadyUsedException.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -1,45 +0,0 @@ -package org.chorem.pollen.services.exception; - -/* - * #%L - * Pollen :: Service - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2013 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% - */ - -/** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ -public class FavoriteListMemberNameAlreadyUsedException extends Exception { - - private static final long serialVersionUID = 1L; - - protected final String name; - - public FavoriteListMemberNameAlreadyUsedException(String name) { - this.name = name; - } - - public String getName() { - return name; - } -} \ No newline at end of file Deleted: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListNameAlreadyUsedException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListNameAlreadyUsedException.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/FavoriteListNameAlreadyUsedException.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -1,45 +0,0 @@ -package org.chorem.pollen.services.exception; - -/* - * #%L - * Pollen :: Service - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2013 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% - */ - -/** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0 - */ -public class FavoriteListNameAlreadyUsedException extends Exception { - - private static final long serialVersionUID = 1L; - - protected final String name; - - public FavoriteListNameAlreadyUsedException(String name) { - this.name = name; - } - - public String getName() { - return name; - } -} \ No newline at end of file Added: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListFormException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListFormException.java (rev 0) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListFormException.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -0,0 +1,41 @@ +package org.chorem.pollen.services.exception; + +/* + * #%L + * Pollen :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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% + */ + +import com.google.common.collect.Multimap; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class InvalidFavoriteListFormException extends AbstractInvalidFormException { + + private static final long serialVersionUID = 1L; + + public InvalidFavoriteListFormException(Multimap<String, String> errors) { + super(errors); + } +} Property changes on: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListFormException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListMemberFormException.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListMemberFormException.java (rev 0) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListMemberFormException.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -0,0 +1,41 @@ +package org.chorem.pollen.services.exception; + +/* + * #%L + * Pollen :: Service + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2013 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% + */ + +import com.google.common.collect.Multimap; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class InvalidFavoriteListMemberFormException extends AbstractInvalidFormException { + + private static final long serialVersionUID = 1L; + + public InvalidFavoriteListMemberFormException(Multimap<String, String> errors) { + super(errors); + } +} Property changes on: trunk/pollen-services/src/main/java/org/chorem/pollen/services/exception/InvalidFavoriteListMemberFormException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/FavoriteListService.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -24,9 +24,10 @@ */ import com.google.common.base.Preconditions; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.chorem.pollen.persistence.entity.FavoriteList; import org.chorem.pollen.persistence.entity.FavoriteListMember; @@ -34,11 +35,11 @@ import org.chorem.pollen.services.PollenTechnicalException; import org.chorem.pollen.services.exception.EntityNotFoundException; import org.chorem.pollen.services.exception.FavoriteListImportException; -import org.chorem.pollen.services.exception.FavoriteListMemberEmailAlreadyUsedException; -import org.chorem.pollen.services.exception.FavoriteListMemberNameAlreadyUsedException; import org.chorem.pollen.services.exception.FavoriteListMemberNotOwnedByFavoriteListException; -import org.chorem.pollen.services.exception.FavoriteListNameAlreadyUsedException; import org.chorem.pollen.services.exception.FavoriteListNotOwnedByUserException; +import org.chorem.pollen.services.exception.InvalidFavoriteListFormException; +import org.chorem.pollen.services.exception.InvalidFavoriteListMemberFormException; +import org.nuiton.jpa.api.JpaEntities; import org.nuiton.util.StringUtil; import javax.naming.NamingEnumeration; @@ -76,41 +77,26 @@ return result; } - public FavoriteList getFavoriteList(String favoriteListId) throws EntityNotFoundException { + public FavoriteList getFavoriteList(String userId, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); - FavoriteList result = getPersistenceContext().getFavoriteListDao().findById(favoriteListId); - checkEntityExists(FavoriteList.class, result, favoriteListId); + + PollenUser user = getUserService().getUser(userId); + + FavoriteList result = getFavoriteList(user, favoriteListId); return result; } public FavoriteList createFavoriteList(String userId, - FavoriteList favoriteList) throws EntityNotFoundException, FavoriteListNameAlreadyUsedException { + FavoriteList favoriteList) throws EntityNotFoundException, InvalidFavoriteListFormException { Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteList); checkHasNoId(favoriteList); - String favoriteListName = favoriteList.getName(); - - // can't accept favorite list without name - Preconditions.checkArgument(StringUtils.isNotEmpty(favoriteListName)); - PollenUser user = getUserService().getUser(userId); - // check there is not already a favorite list with same name - boolean nameFound = false; - if (!user.isFavoriteListEmpty()) { - for (FavoriteList list : user.getFavoriteList()) { - if (favoriteListName.equals(list.getName())) { - nameFound = true; - break; - } - } - } + checkFavoriteListForm(user, favoriteList); - if (nameFound) { - throw new FavoriteListNameAlreadyUsedException(favoriteListName); - } - FavoriteList toSave = getPersistenceContext().getFavoriteListDao().newInstance(); copyFavoriteList(favoriteList, toSave); @@ -122,51 +108,22 @@ } public FavoriteList editFavoriteList(String userId, - FavoriteList favoriteList) throws EntityNotFoundException, FavoriteListNameAlreadyUsedException, FavoriteListNotOwnedByUserException { + FavoriteList favoriteList) throws EntityNotFoundException, InvalidFavoriteListFormException, FavoriteListNotOwnedByUserException { Preconditions.checkNotNull(favoriteList); checkHasId(favoriteList); - String favoriteListName = favoriteList.getName(); - - // can't accept favorite list without name - Preconditions.checkArgument(StringUtils.isNotEmpty(favoriteListName)); - // get user PollenUser user = getUserService().getUser(userId); - // get list - FavoriteList toSave = getFavoriteList(favoriteList.getId()); + // get list to save + FavoriteList toSave = getFavoriteList(user, favoriteList.getId()); - // check user own this list - boolean ownedByUser = user.containsFavoriteListById(favoriteList.getId()); + checkFavoriteListForm(user, favoriteList); - if (!ownedByUser) { - throw new FavoriteListNotOwnedByUserException(); - } - if (ObjectUtils.notEqual(favoriteListName, toSave.getName())) { + copyFavoriteList(favoriteList, toSave); - // check that this name is not existing in another list - boolean nameFound = false; - if (!user.isFavoriteListEmpty()) { - for (FavoriteList list : user.getFavoriteList()) { - if (toSave != list && - favoriteListName.equals(list.getName())) { - - // found another list with same name - nameFound = true; - break; - } - } - } - if (nameFound) { - throw new FavoriteListNameAlreadyUsedException(favoriteListName); - } - } - - copyFavoriteList(favoriteList, toSave); getPersistenceContext().getFavoriteListDao().merge(toSave); - getPersistenceContext().commit(); return toSave; } @@ -177,7 +134,7 @@ PollenUser user = getUserService().getUser(userId); - FavoriteList persisted = getFavoriteList(favoriteListId); + FavoriteList persisted = getFavoriteList(user, favoriteListId); // check user own this list boolean ownedByUser = user.containsFavoriteListById(favoriteListId); @@ -186,62 +143,49 @@ } user.removeFavoriteList(persisted); + getPersistenceContext().getPollenUserDao().merge(user); - getPersistenceContext().commit(); } - public List<FavoriteListMember> getFavoriteListMembers(String favoriteListId) throws EntityNotFoundException { + public List<FavoriteListMember> getFavoriteListMembers(String userId, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); - FavoriteList favoriteList = getFavoriteList(favoriteListId); + PollenUser user = getUserService().getUser(userId); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); + return favoriteList.getFavoriteListMember(); } - public FavoriteListMember getFavoriteListMember(String memberId) throws EntityNotFoundException { + public FavoriteListMember getFavoriteListMember(String userId, String favoriteListId, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); + Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(memberId); - FavoriteListMember result = getPersistenceContext().getFavoriteListMemberDao().findById(memberId); - checkEntityExists(FavoriteListMember.class, result, memberId); + + PollenUser user = getUserService().getUser(userId); + + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); + + FavoriteListMember result = getFavoriteListMember(favoriteList, memberId); return result; } - public FavoriteListMember addFavoriteListMember(String favoriteListId, - FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberNameAlreadyUsedException, FavoriteListMemberEmailAlreadyUsedException { + public FavoriteListMember addFavoriteListMember(String userId, + String favoriteListId, + FavoriteListMember member) throws EntityNotFoundException, InvalidFavoriteListMemberFormException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(member); checkHasNoId(member); - // can't accept member without name - String memberName = member.getName(); - Preconditions.checkArgument(StringUtils.isNotEmpty(memberName)); + PollenUser user = getUserService().getUser(userId); - // can't accept member without email - String memberEmail = member.getEmail(); - Preconditions.checkArgument(StringUtils.isNotEmpty(memberEmail)); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); - // lowerCase email - memberEmail = StringUtils.lowerCase(memberEmail); + checkFavoriteListMemberForm(favoriteList, member); - //TODO Validate email form - - FavoriteList favoriteList = getFavoriteList(favoriteListId); - - if (!favoriteList.isFavoriteListMemberEmpty()) { - - for (FavoriteListMember listMember : favoriteList.getFavoriteListMember()) { - - // check there is no other member with same name - if (memberName.equals(listMember.getName())) { - throw new FavoriteListMemberNameAlreadyUsedException(memberName); - } - // check there is no other member with same email - if (memberEmail.equals(listMember.getEmail())) { - throw new FavoriteListMemberEmailAlreadyUsedException(memberEmail); - } - } - } - FavoriteListMember newMember = getPersistenceContext().getFavoriteListMemberDao().newInstance(); copyFavoriteListMember(member, newMember); @@ -253,60 +197,25 @@ return newMember; } - public FavoriteListMember editFavoriteListMember(String favoriteListId, FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, FavoriteListMemberNameAlreadyUsedException, FavoriteListMemberEmailAlreadyUsedException { + public FavoriteListMember editFavoriteListMember(String userId, + String favoriteListId, + FavoriteListMember member) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, InvalidFavoriteListMemberFormException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); + Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(member); checkHasId(member); - // can't accept member without name - String memberName = member.getName(); - Preconditions.checkArgument(StringUtils.isNotEmpty(memberName)); - // can't accept member without email - String memberEmail = member.getEmail(); - Preconditions.checkArgument(StringUtils.isNotEmpty(memberEmail)); + PollenUser user = getUserService().getUser(userId); - // lowerCase email - memberEmail = StringUtils.lowerCase(memberEmail); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); - //TODO Validate email form - - FavoriteList favoriteList = getFavoriteList(favoriteListId); - String memberId = member.getId(); - FavoriteListMember toSave = getFavoriteListMember(memberId); + FavoriteListMember toSave = getFavoriteListMember(favoriteList, memberId); - // check favorite list own this member - boolean ownedByUser = favoriteList.containsFavoriteListMemberById(memberId); - if (!ownedByUser) { - throw new FavoriteListMemberNotOwnedByFavoriteListException(); - } + checkFavoriteListMemberForm(favoriteList, member); - - if (!memberName.equals(toSave.getName())) { - - // name has changed must check name is available in the list - for (FavoriteListMember listMember : favoriteList.getFavoriteListMember()) { - - // check there is no other member with same name - if (!memberId.equals(listMember.getId()) && - memberName.equals(listMember.getName())) { - throw new FavoriteListMemberNameAlreadyUsedException(memberName); - } - } - } - - if (!memberEmail.equals(toSave.getEmail())) { - - // email has changed must check name is available in the list - for (FavoriteListMember listMember : favoriteList.getFavoriteListMember()) { - - if (!memberId.equals(listMember.getId()) && - memberEmail.equals(listMember.getEmail())) { - throw new FavoriteListMemberEmailAlreadyUsedException(memberEmail); - } - } - } copyFavoriteListMember(member, toSave); getPersistenceContext().getFavoriteListMemberDao().merge(toSave); @@ -315,19 +224,18 @@ return toSave; } - public void removeFavoriteListMember(String favoriteListId, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException { + public void removeFavoriteListMember(String userId, + String favoriteListId, + String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(memberId); - FavoriteList favoriteList = getFavoriteList(favoriteListId); + PollenUser user = getUserService().getUser(userId); - FavoriteListMember member = getFavoriteListMember(memberId); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); - // check favorite list own this member - boolean ownedByUser = favoriteList.containsFavoriteListMemberById(memberId); - if (!ownedByUser) { - throw new FavoriteListMemberNotOwnedByFavoriteListException(); - } + FavoriteListMember member = getFavoriteListMember(favoriteList, memberId); favoriteList.removeFavoriteListMember(member); @@ -335,7 +243,8 @@ getPersistenceContext().commit(); } - public int importFavoriteListMembersFromCsv(String userId, String favoriteListId, + public int importFavoriteListMembersFromCsv(String userId, + String favoriteListId, File file) throws EntityNotFoundException, FavoriteListImportException, FavoriteListNotOwnedByUserException { Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); @@ -343,14 +252,8 @@ PollenUser user = getUserService().getUser(userId); - FavoriteList favoriteList = getFavoriteList(favoriteListId); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); - // check user own this list - boolean ownedByUser = user.containsFavoriteListById(favoriteListId); - if (!ownedByUser) { - throw new FavoriteListNotOwnedByUserException(); - } - Locale locale = serviceContext.getLocale(); Set<String> usedName = Sets.newHashSet(); @@ -447,14 +350,8 @@ PollenUser user = getUserService().getUser(userId); - FavoriteList favoriteList = getFavoriteList(favoriteListId); + FavoriteList favoriteList = getFavoriteList(user, favoriteListId); - // check user own this list - boolean ownedByUser = user.containsFavoriteListById(favoriteListId); - if (!ownedByUser) { - throw new FavoriteListNotOwnedByUserException(); - } - Locale locale = serviceContext.getLocale(); Set<String> usedName = Sets.newHashSet(); @@ -532,6 +429,34 @@ return result; } + protected FavoriteList getFavoriteList(PollenUser user, String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { + Preconditions.checkNotNull(favoriteListId); + + FavoriteList result = user.getFavoriteListById(favoriteListId); + checkEntityExists(FavoriteList.class, result, favoriteListId); + + // check user own this list + boolean ownedByUser = user.containsFavoriteListById(favoriteListId); + + if (!ownedByUser) { + throw new FavoriteListNotOwnedByUserException(); + } + return result; + } + + protected FavoriteListMember getFavoriteListMember(FavoriteList favoriteList, String memberId) throws EntityNotFoundException, FavoriteListMemberNotOwnedByFavoriteListException { + + FavoriteListMember result = favoriteList.getFavoriteListMemberById(memberId); + checkEntityExists(FavoriteListMember.class, result, memberId); + + // check favorite list own this member + boolean ownedByUser = favoriteList.containsFavoriteListMemberById(memberId); + if (!ownedByUser) { + throw new FavoriteListMemberNotOwnedByFavoriteListException(); + } + return result; + } + protected void copyFavoriteList(FavoriteList source, FavoriteList destination) { @@ -544,4 +469,119 @@ destination.setName(source.getName()); destination.setEmail(StringUtils.lowerCase(source.getEmail())); } + + protected void checkFavoriteListForm(PollenUser user, FavoriteList favoriteList) throws InvalidFavoriteListFormException { + + //TODO use nuiton validator ? + Multimap<String, String> errors = ArrayListMultimap.create(); + + String favoriteListName = favoriteList.getName(); + + // can't accept favorite list without name + checkNotBlank(errors, "name", favoriteListName, "name can not be empty"); + + + // check there is not already a favorite list with same name + boolean nameFound = false; + if (!user.isFavoriteListEmpty()) { + for (FavoriteList list : user.getFavoriteList()) { + if (favoriteListName.equals(list.getName())) { + nameFound = true; + break; + } + } + } + + check(errors, "name", !nameFound, "name already used by another favorite list"); + + if (!favoriteList.isFavoriteListMemberEmpty()) { + + int memberIndex = 0; + + Set<String> memberNames = Sets.newHashSet(); + Set<String> memberEmails = Sets.newHashSet(); + + for (FavoriteListMember member : favoriteList.getFavoriteListMember()) { + + String choiceField = "favoriteListMember[" + (memberIndex++) + "]."; + + boolean notBlankName = checkNotBlank(errors, choiceField + "name", member.getName(), "member name can not be empty"); + if (notBlankName) { + boolean added = memberNames.add(member.getName()); + + check(errors, choiceField + "name", added, "member name already used"); + } + + String memberEmail = member.getEmail(); + + boolean emailNotNull = checkNotBlank(errors, choiceField + "email", memberEmail, "member email can not be empty"); + + if (emailNotNull) { + + memberEmail = StringUtils.lowerCase(memberEmail); + + checkValidEmail(errors, choiceField + "email", memberEmail, "member email is not valid"); + boolean emailAdded = memberEmails.add(memberEmail); + check(errors, choiceField + "email", emailAdded, "member email already used in this list"); + } + } + } + + if (!errors.isEmpty()) { + + throw new InvalidFavoriteListFormException(errors); + } + } + + + protected void checkFavoriteListMemberForm(FavoriteList favoriteList, FavoriteListMember favoriteListMember) throws InvalidFavoriteListMemberFormException { + + //TODO use nuiton validator ? + Multimap<String, String> errors = ArrayListMultimap.create(); + + boolean voterListMemberExists = JpaEntities.isEntityHasId(favoriteListMember); + + Set<String> memberNames = Sets.newHashSet(); + Set<String> memberEmails = Sets.newHashSet(); + + if (!favoriteList.isFavoriteListMemberEmpty()) { + + // get all used names + + for (FavoriteListMember member : favoriteList.getFavoriteListMember()) { + + if (voterListMemberExists && member.getId().equals(member.getId())) { + continue; + } + + memberNames.add(member.getName()); + memberEmails.add(member.getEmail()); + } + } + + boolean notBlankName = checkNotBlank(errors, "name", favoriteListMember.getName(), "member name can not be empty"); + if (notBlankName) { + boolean added = memberNames.add(favoriteListMember.getName()); + + check(errors, "name", added, "member name already used"); + } + + String memberEmail = favoriteListMember.getEmail(); + + boolean emailNotNull = checkNotBlank(errors, "email", memberEmail, "member email can not be empty"); + + if (emailNotNull) { + + memberEmail = StringUtils.lowerCase(memberEmail); + + checkValidEmail(errors, "email", memberEmail, "member email is not valid"); + boolean emailAdded = memberEmails.add(memberEmail); + check(errors, "email", emailAdded, "member email already used in this list"); + } + + if (!errors.isEmpty()) { + + throw new InvalidFavoriteListMemberFormException(errors); + } + } } Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/PollService.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -274,8 +274,10 @@ if (poll.isFreePoll()) { checkEmpty(errors, "voterList", poll.getVoterList(), "can't have voterList with free poll"); - } else { - checkNotEmpty(errors, "voterList", poll.getVoterList(), "must have voterList with not free poll"); + } else if (poll.isRestrictedPoll()) { + check(errors, "voterList", 1 == poll.sizeVoterList(), "must have a unique voterList with a resitricted poll"); + } else if (poll.isGroupPoll()) { + checkNotEmpty(errors, "voterList", poll.getVoterList(), "must have at least one voterList with a group poll"); } if (!poll.isChoiceEmpty()) { Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java 2013-06-15 14:29:32 UTC (rev 3831) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/service/VoterListService.java 2013-06-17 15:36:31 UTC (rev 3832) @@ -37,6 +37,7 @@ import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; import org.chorem.pollen.services.exception.EntityNotFoundException; +import org.chorem.pollen.services.exception.FavoriteListNotOwnedByUserException; import org.chorem.pollen.services.exception.InvalidVoterListFormException; import org.chorem.pollen.services.exception.InvalidVoterListMemberFormException; import org.nuiton.jpa.api.JpaEntities; @@ -52,14 +53,14 @@ */ public class VoterListService extends AbstractPollenService { - public VoterList importFavoriteList(String pollId, - String favoriteListId) throws EntityNotFoundException { + public VoterList importFavoriteList(String userId, String pollId, + String favoriteListId) throws EntityNotFoundException, FavoriteListNotOwnedByUserException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(favoriteListId); Poll poll = getPollService().getPoll(pollId); - FavoriteList favoriteList = getFavoriteListService().getFavoriteList(favoriteListId); + FavoriteList favoriteList = getFavoriteListService().getFavoriteList(userId, favoriteListId); VoterListJpaDao dao = getPersistenceContext().getVoterListDao(); VoterList result = dao.newInstance();