Author: tchemit Date: 2013-06-06 18:43:31 +0200 (Thu, 06 Jun 2013) New Revision: 3817 Url: http://chorem.org/projects/pollen/repository/revisions/3817 Log: implements user + auth service + review rest api Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenDao.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenJpaDao.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenApplicationContext.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/ErrorAction.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/EmailService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/EntityNotFoundException.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserEmailAlreadyUsedException.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidEmailActiviationTokenException.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidPasswordException.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserLoginAlreadyUsedException.java Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/JpaPollenPersistenceContext.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenPersistenceContext.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserDao.java branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserJpaDao.java branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.zargo branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceContextFilter.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServices.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/AuthService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteCountingService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AbstractPollenService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AuthService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/CommentService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FavoriteListService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FixturesService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteCountingService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteService.java branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoterListService.java branches/pollen-2.0/pollen-service/src/main/resources/fixtures.yaml branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/AbstractPollenServiceTest.java branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/FakePollenServiceContext.java branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/UserServiceTest.java Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/JpaPollenPersistenceContext.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/JpaPollenPersistenceContext.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/JpaPollenPersistenceContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -23,6 +23,7 @@ * #L% */ +import org.nuiton.jpa.api.JpaEntity; import org.nuiton.jpa.api.JpaEntityIdFactory; import org.nuiton.jpa.api.hibernate.HibernateUtil; @@ -46,6 +47,11 @@ } @Override + public void detach(JpaEntity entity) { + entityManager.detach(entity); + } + + @Override public void clearDatabase() { rollback(); HibernateUtil.cleanDatabase(entityManager); Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenPersistenceContext.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenPersistenceContext.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/PollenPersistenceContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -29,11 +29,13 @@ import org.chorem.pollen.persistence.dao.FavoriteListMemberJpaDao; import org.chorem.pollen.persistence.dao.PollJpaDao; import org.chorem.pollen.persistence.dao.PollenUserJpaDao; +import org.chorem.pollen.persistence.dao.SessionTokenJpaDao; import org.chorem.pollen.persistence.dao.VoteJpaDao; import org.chorem.pollen.persistence.dao.VoteToChoiceJpaDao; import org.chorem.pollen.persistence.dao.VoterJpaDao; import org.chorem.pollen.persistence.dao.VoterListJpaDao; import org.chorem.pollen.persistence.dao.VoterListMemberJpaDao; +import org.nuiton.jpa.api.JpaEntity; /** * TODO @@ -43,6 +45,8 @@ */ public interface PollenPersistenceContext { + void detach(JpaEntity entity); + void commit(); void rollback(); @@ -61,6 +65,8 @@ PollenUserJpaDao getPollenUserDao(); + SessionTokenJpaDao getSessionTokenDao(); + VoteJpaDao getVoteDao(); VoteToChoiceJpaDao getVoteToChoiceDao(); Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserDao.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserDao.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserDao.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -34,4 +34,10 @@ public interface PollenUserDao { PollenUser findByLogin(String login); + + PollenUser findByEmail(String email); + + boolean loginExists(String login); + + boolean emailExists(String email); } Modified: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserJpaDao.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserJpaDao.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/PollenUserJpaDao.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import org.chorem.pollen.persistence.entity.PollenUser; import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; /** * TODO @@ -41,10 +42,30 @@ @Override public PollenUser findByLogin(String login) { - PollenUser result = createQuery( - "from " + getEntityClass() + " where login = :login"). - setParameter("login", login). - getSingleResult(); + TypedQuery<PollenUser> query = + createQuery(PollenUser.PROPERTY_LOGIN, login); + PollenUser result = findUniqueOrNull(query); + return result; } + + @Override + public PollenUser findByEmail(String email) { + TypedQuery<PollenUser> query = + createQuery(PollenUser.PROPERTY_EMAIL, email); + PollenUser result = findUniqueOrNull(query); + return result; + } + + @Override + public boolean loginExists(String login) { + PollenUser user = findByLogin(login); + return user != null; + } + + @Override + public boolean emailExists(String email) { + PollenUser user = findByEmail(email); + return user != null; + } } //PollenUserJpaDao Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenDao.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenDao.java (rev 0) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenDao.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,37 @@ +package org.chorem.pollen.persistence.dao; + +/* + * #%L + * Pollen :: Persistence + * $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 org.chorem.pollen.persistence.entity.SessionToken; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public interface SessionTokenDao { + + SessionToken findByToken(String token); +} Property changes on: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenDao.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenJpaDao.java =================================================================== --- branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenJpaDao.java (rev 0) +++ branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenJpaDao.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,50 @@ +package org.chorem.pollen.persistence.dao; + +/* + * #%L + * Pollen :: Persistence + * $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 org.chorem.pollen.persistence.entity.SessionToken; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class SessionTokenJpaDao extends AbstractSessionTokenJpaDao { + + public SessionTokenJpaDao(EntityManager entityManager) { + super(entityManager); + } + + @Override + public SessionToken findByToken(String token) { + TypedQuery<SessionToken> query = createQuery(SessionToken.PROPERTY_TOKEN, token); + SessionToken result = findUniqueOrNull(query); + return result; + } + +} //SessionTokenJpaDao Property changes on: branches/pollen-2.0/pollen-persistence/src/main/java/org/chorem/pollen/persistence/dao/SessionTokenJpaDao.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-persistence/src/main/xmi/pollen.zargo =================================================================== (Binary files differ) Added: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenApplicationContext.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenApplicationContext.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenApplicationContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,125 @@ +package org.chorem.pollen.rest; + +/* + * #%L + * Pollen :: Rest Api + * $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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.persistence.JpaPollenPersistenceContext; +import org.chorem.pollen.service.DefaultPollenServiceContext; +import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.config.PollenServiceConfig; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.PersistenceException; +import java.io.Closeable; +import java.util.Map; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class PollenApplicationContext implements Closeable { + + private static final Log log = LogFactory.getLog(PollenApplicationContext.class); + + protected static final String APPLICATION_CONTEXT_PARAMETER = "pollen_PollenApplicationContext"; + + protected PollenServiceConfig applicationConfig; + + protected EntityManagerFactory entityManagerFactory; + + public PollenServiceConfig getPollenServiceConfig() { + if (applicationConfig == null) { + applicationConfig = new PollenServiceConfig(); + } + return applicationConfig; + } + + public EntityManager newEntityManager() { + + if (entityManagerFactory == null) { + + PollenServiceConfig applicationConfig = getPollenServiceConfig(); + + Map<String, String> jpaParameters = applicationConfig.getJpaParameters(); + + if (log.isInfoEnabled()) { + log.info("creating entity manager factory"); + } + + try { + + entityManagerFactory = Persistence.createEntityManagerFactory("pollenPersistenceUnit", jpaParameters); + + } catch (PersistenceException e) { + + if (log.isErrorEnabled()) { + log.error("unable to create entity manager factory", e); + } + + throw e; + + } + + } + + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + return entityManager; + + } + + public void close() { + + if (entityManagerFactory != null && entityManagerFactory.isOpen()) { + + if (log.isInfoEnabled()) { + log.info("stopping magalie, will close entity manager factory"); + } + + entityManagerFactory.close(); + + } + + } + + public PollenServiceContext newServiceContext(EntityManager entityManager) { + + DefaultPollenServiceContext serviceContext = new DefaultPollenServiceContext(); + + JpaPollenPersistenceContext jpaMagaliePersistenceContext = + new JpaPollenPersistenceContext(entityManager); + + serviceContext.setPersistenceContext(jpaMagaliePersistenceContext); + + serviceContext.setPollenServiceConfig(applicationConfig); + + return serviceContext; + + } +} Property changes on: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenApplicationContext.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenJpaTransactionFilter.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -29,7 +29,6 @@ import org.nuiton.web.filter.JpaTransactionFilter; import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceException; import javax.servlet.FilterConfig; import javax.servlet.ServletException; @@ -40,7 +39,7 @@ private static final Log log = LogFactory.getLog(PollenJpaTransactionFilter.class); - protected EntityManagerFactory entityManagerFactory; + protected PollenApplicationContext applicationContext; @Override public void init(FilterConfig filterConfig) throws ServletException { @@ -51,10 +50,10 @@ log.info("Init JPA Filter"); } - entityManagerFactory = PollenServices.getEntityManagerFactory( + applicationContext = PollenServices.getApplicationContext( filterConfig.getServletContext()); - Preconditions.checkNotNull(entityManagerFactory, "Could not find entit manager factory at ServletContext#pollen_EntityManagerFactory"); + Preconditions.checkNotNull(applicationContext, "Could not find application context at ServletContext#pollen_PollenApplicationContext"); } @Override @@ -64,7 +63,7 @@ try { - entityManager = entityManagerFactory.createEntityManager(); + entityManager = applicationContext.newEntityManager(); } catch (PersistenceException e) { Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceContextFilter.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceContextFilter.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceContextFilter.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -23,8 +23,7 @@ * #L% */ -import org.chorem.pollen.service.DefaultPollenServiceContext; -import org.chorem.pollen.service.config.PollenServiceConfig; +import org.chorem.pollen.service.PollenServiceContext; import org.debux.webmotion.server.WebMotionFilter; import org.debux.webmotion.server.call.HttpContext; @@ -40,17 +39,15 @@ public void inject(HttpContext context) { - DefaultPollenServiceContext serviceContext = - new DefaultPollenServiceContext(); + PollenApplicationContext applicationContext = + PollenServices.getApplicationContext(context.getServletContext()); - PollenServiceConfig serviceConfig = - PollenServices.getServiceConfig(context.getServletContext()); - serviceContext.setPollenServiceConfig(serviceConfig); - EntityManager entityManager = PollenServices.getEntityManager(context.getRequest()); - serviceContext.setEntityManager(entityManager); + PollenServiceContext serviceContext = + applicationContext.newServiceContext(entityManager); + PollenServices.setServiceContext(context.getRequest(), serviceContext); doProcess(); Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServiceListener.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,7 +26,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.service.PollenServiceContext; -import org.chorem.pollen.service.config.PollenServiceConfig; import org.debux.webmotion.server.WebMotionServerListener; import org.debux.webmotion.server.call.Call; import org.debux.webmotion.server.call.HttpContext; @@ -34,12 +33,8 @@ import org.debux.webmotion.server.handler.ExecutorParametersInjectorHandler; import org.debux.webmotion.server.mapping.Mapping; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import javax.persistence.PersistenceException; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Type; -import java.util.Map; /** * TODO @@ -58,46 +53,26 @@ serverContext.addInjector(new PollenServiceContextInjector()); - // get configuration - PollenServiceConfig config = new PollenServiceConfig(); + // Create application context + PollenApplicationContext applicationContext = + new PollenApplicationContext(); - // push config in context - PollenServices.setServiceConfig(serverContext.getServletContext(), config); - - // get persistence properties - Map<String, String> jpaParameters = config.getJpaParameters(); - - if (log.isInfoEnabled()) { - log.info("creating entity manager factory"); - } - - // get entity manager factory - try { - - EntityManagerFactory entityManagerFactory = - Persistence.createEntityManagerFactory("pollenPersistenceUnit", jpaParameters); - - // store it in server context - PollenServices.setEntityManagerFactory(serverContext.getServletContext(), entityManagerFactory); - - } catch (PersistenceException e) { - - if (log.isErrorEnabled()) { - log.error("unable to create entity manager factory", e); - } - - throw e; - } + // push it in context + PollenServices.setApplicationContext( + serverContext.getServletContext(), applicationContext); } @Override public void onStop(ServerContext serverContext) { - EntityManagerFactory entityManagerFactory = - PollenServices.getEntityManagerFactory(serverContext.getServletContext()); + // Get application context + PollenApplicationContext applicationContext = + PollenServices.getApplicationContext( + serverContext.getServletContext()); - if (entityManagerFactory != null) { - entityManagerFactory.close(); + // close it (and all underlined resources) + if (applicationContext != null) { + applicationContext.close(); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServices.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServices.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/PollenServices.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -24,12 +24,10 @@ */ import org.chorem.pollen.service.PollenServiceContext; -import org.chorem.pollen.service.config.PollenServiceConfig; +import org.nuiton.web.filter.JpaTransactionFilter; import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.servlet.ServletContext; -import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; /** @@ -40,38 +38,24 @@ */ public class PollenServices { - static final String APPLICATION_POLLEN_SERVICE_CONFIG = "pollen_PollenServiceConfig"; + protected static final String APPLICATION_CONTEXT_PARAMETER = "pollen_PollenApplicationContext"; - static final String APPLICATION_ENTITY_MANAGER_FACTORY = "pollen_EntityManagerFactory"; + protected static final String REQUEST_POLLEN_SERVICE_CONTEXT = "pollen_PollenServiceContext"; - static final String REQUEST_POLLEN_SERVICE_CONTEXT = "pollen_PollenServiceContext"; + protected static final String REQUEST_ENTITY_MANAGER = JpaTransactionFilter.JPA_TRANSACTION_REQUEST_ATTRIBUTE; - public static PollenServiceConfig getServiceConfig(ServletContext servletContext) { - PollenServiceConfig result = (PollenServiceConfig) - servletContext.getAttribute(APPLICATION_POLLEN_SERVICE_CONFIG); + public static PollenApplicationContext getApplicationContext(ServletContext servletContext) { + PollenApplicationContext result = (PollenApplicationContext) + servletContext.getAttribute(APPLICATION_CONTEXT_PARAMETER); return result; } - public static void setServiceConfig(ServletContext servletContext, - PollenServiceConfig serviceConfig) { - servletContext.setAttribute(APPLICATION_POLLEN_SERVICE_CONFIG, serviceConfig); + public static void setApplicationContext(ServletContext servletContext, + PollenApplicationContext applicationContext) { + servletContext.setAttribute(APPLICATION_CONTEXT_PARAMETER, applicationContext); } - public static EntityManagerFactory getEntityManagerFactory(ServletContext servletContext) { - EntityManagerFactory result = (EntityManagerFactory) - servletContext.getAttribute(APPLICATION_ENTITY_MANAGER_FACTORY); - return result; - } - public static void setEntityManagerFactory(ServletContext servletContext, - EntityManagerFactory entityManagerFactory) { - servletContext.setAttribute(APPLICATION_ENTITY_MANAGER_FACTORY, entityManagerFactory); - } - - public static EntityManager getEntityManager(ServletRequest servletRequest) { - return PollenJpaTransactionFilter.getTransaction(servletRequest); - } - public static PollenServiceContext getServiceContext(HttpServletRequest request) { PollenServiceContext result = (PollenServiceContext) request.getAttribute(REQUEST_POLLEN_SERVICE_CONTEXT); @@ -83,4 +67,9 @@ request.setAttribute(REQUEST_POLLEN_SERVICE_CONTEXT, serviceContext); } -} + public static EntityManager getEntityManager(HttpServletRequest request) { + EntityManager result = (EntityManager) + request.getAttribute(REQUEST_ENTITY_MANAGER); + return result; + } +} \ No newline at end of file Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/AuthService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/AuthService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/AuthService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -24,6 +24,8 @@ */ import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; +import org.chorem.pollen.service.exception.UserInvalidPasswordException; import org.debux.webmotion.server.WebMotionController; /** @@ -34,15 +36,15 @@ */ public class AuthService extends WebMotionController { - public String login(PollenServiceContext context, String login, String password) { + public String login(PollenServiceContext context, String login, String password) throws UserInvalidPasswordException, EntityNotFoundException { return context.getAuthService().login(login, password); } - public void lostPassword(PollenServiceContext context, String token) { - context.getAuthService().lostPassword(token); + public void lostPassword(PollenServiceContext context, String login) throws EntityNotFoundException { + context.getAuthService().lostPassword(login); } - public void logout(PollenServiceContext context, String login) { - context.getAuthService().logout(login); + public void logout(PollenServiceContext context, String login, String token) throws EntityNotFoundException { + context.getAuthService().logout(login, token); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/CommentService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,7 @@ import org.chorem.pollen.persistence.entity.Comment; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; import java.util.List; @@ -37,24 +38,24 @@ */ public class CommentService extends WebMotionController { - public Comment[] getComments(PollenServiceContext context, String pollId) { + public Comment[] getComments(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<Comment> comments = context.getCommentService().getComments(pollId); return comments.toArray(new Comment[comments.size()]); } - public Comment getComment(PollenServiceContext context, String pollId, String commentId) { - return context.getCommentService().getComment(pollId, commentId); + public Comment getComment(PollenServiceContext context, String commentId) throws EntityNotFoundException { + return context.getCommentService().getComment(commentId); } - public Comment addComment(PollenServiceContext context, String pollId, Comment comment) { + public Comment addComment(PollenServiceContext context, String pollId, Comment comment) throws EntityNotFoundException { return context.getCommentService().addComment(pollId, comment); } - public Comment editComment(PollenServiceContext context, String pollId, Comment comment) { - return context.getCommentService().editComment(pollId, comment); + public Comment editComment(PollenServiceContext context,Comment comment) throws EntityNotFoundException { + return context.getCommentService().editComment(comment); } - public void deleteComment(PollenServiceContext context, String pollId, String commentId) { + public void deleteComment(PollenServiceContext context, String pollId, String commentId) throws EntityNotFoundException { context.getCommentService().deleteComment(pollId, commentId); } } Added: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/ErrorAction.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/ErrorAction.java (rev 0) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/ErrorAction.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,44 @@ +package org.chorem.pollen.rest.api; + +/* + * #%L + * Pollen :: Rest Api + * $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 org.debux.webmotion.server.WebMotionController; +import org.debux.webmotion.server.render.Render; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class ErrorAction extends WebMotionController { + + public Render on404(Exception e) { + return renderError(404, e.getMessage()); + } + + public Render on500(Exception e) { + return renderError(500, e.getMessage()); + } +} Property changes on: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/ErrorAction.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/FavoriteListService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import org.chorem.pollen.persistence.entity.FavoriteList; import org.chorem.pollen.persistence.entity.FavoriteListMember; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; import java.util.List; @@ -38,45 +39,45 @@ */ public class FavoriteListService extends WebMotionController { - public FavoriteList[] getFavoriteLists(PollenServiceContext context, String userId) { + public FavoriteList[] getFavoriteLists(PollenServiceContext context, String userId) throws EntityNotFoundException { List<FavoriteList> favoriteLists = context.getFavoriteListService().getFavoriteLists(userId); return favoriteLists.toArray(new FavoriteList[favoriteLists.size()]); } - public FavoriteList getFavoriteList(PollenServiceContext context, String userId, String favoriteListId) { - return context.getFavoriteListService().getFavoriteList(userId, favoriteListId); + public FavoriteList getFavoriteList(PollenServiceContext context, String favoriteListId) throws EntityNotFoundException { + return context.getFavoriteListService().getFavoriteList(favoriteListId); } - public FavoriteList createFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) { + public FavoriteList createFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) throws EntityNotFoundException { return context.getFavoriteListService().createFavoriteList(userId, favoriteList); } - public FavoriteList editFavoriteList(PollenServiceContext context, String userId, FavoriteList favoriteList) { - return context.getFavoriteListService().editFavoriteList(userId, favoriteList); + public FavoriteList editFavoriteList(PollenServiceContext context, FavoriteList favoriteList) throws EntityNotFoundException { + return context.getFavoriteListService().editFavoriteList(favoriteList); } - public void deleteFavoriteList(PollenServiceContext context, String userId, String favoriteListId) { + public void deleteFavoriteList(PollenServiceContext context, String userId, String favoriteListId) throws EntityNotFoundException { context.getFavoriteListService().deleteFavoriteList(userId, favoriteListId); } - public FavoriteListMember[] getMembers(PollenServiceContext context, String userId, String favoriteListId) { - List<FavoriteListMember> members = context.getFavoriteListService().getMembers(userId, favoriteListId); - return members.toArray(new FavoriteListMember[members.size()]); + public List<FavoriteListMember> getMembers(PollenServiceContext context, String favoriteListId) throws EntityNotFoundException { + List<FavoriteListMember> members = context.getFavoriteListService().getFavoriteListMembers(favoriteListId); + return members; } - public FavoriteListMember getMember(PollenServiceContext context, String userId, String favoriteListId, String memberId) { - return context.getFavoriteListService().getMember(userId, favoriteListId, memberId); + public FavoriteListMember getMember(PollenServiceContext context, String memberId) throws EntityNotFoundException { + return context.getFavoriteListService().getFavoriteListMember(memberId); } - public FavoriteListMember addMember(PollenServiceContext context, String userId, String favoriteListId, FavoriteListMember member) { - return context.getFavoriteListService().addMember(userId, favoriteListId, member); + public FavoriteListMember addMember(PollenServiceContext context, String favoriteListId, FavoriteListMember member) throws EntityNotFoundException { + return context.getFavoriteListService().addFavoriteListMember(favoriteListId, member); } - public FavoriteListMember editMember(PollenServiceContext context, String userId, String favoriteListId, FavoriteListMember member) { - return context.getFavoriteListService().editMember(userId, favoriteListId, member); + public FavoriteListMember editMember(PollenServiceContext context, FavoriteListMember member) throws EntityNotFoundException { + return context.getFavoriteListService().editFavoriteListMember(member); } - public void removeMember(PollenServiceContext context, String userId, String favoriteListId, String memberId) { - context.getFavoriteListService().removeMember(userId, favoriteListId, memberId); + public void removeMember(PollenServiceContext context, String favoriteListId, String memberId) throws EntityNotFoundException { + context.getFavoriteListService().removeFavoriteListMember( favoriteListId, memberId); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; import java.io.File; @@ -40,63 +41,63 @@ */ public class PollService extends WebMotionController { - public Poll[] getCreatedPolls(PollenServiceContext context, String userId) { + public Poll[] getCreatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getCreatedPolls(userId); return polls.toArray(new Poll[polls.size()]); } - public Poll[] getInvitedPolls(PollenServiceContext context, String userId) { + public Poll[] getInvitedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getInvitedPolls(userId); return polls.toArray(new Poll[polls.size()]); } - public Poll[] getParticipatedPolls(PollenServiceContext context, String userId) { + public Poll[] getParticipatedPolls(PollenServiceContext context, String userId) throws EntityNotFoundException { Set<Poll> polls = context.getPollService().getParticipatedPolls(userId); return polls.toArray(new Poll[polls.size()]); } - public Poll createPoll(PollenServiceContext context, String userId, Poll poll) { + public Poll createPoll(PollenServiceContext context, String userId, Poll poll) throws EntityNotFoundException { return context.getPollService().createPoll(userId, poll); } - public Poll editPoll(PollenServiceContext context, Poll poll) { + public Poll editPoll(PollenServiceContext context, Poll poll) throws EntityNotFoundException { return context.getPollService().editPoll(poll); } - public void deletePoll(PollenServiceContext context, String pollId) { + public void deletePoll(PollenServiceContext context, String pollId) throws EntityNotFoundException { context.getPollService().deletePoll(pollId); } - public Poll clonePoll(PollenServiceContext context, String pollId) { + public Poll clonePoll(PollenServiceContext context, String pollId) throws EntityNotFoundException { return context.getPollService().clonePoll(pollId); } - public File closePoll(PollenServiceContext context, String pollId) { + public File closePoll(PollenServiceContext context, String pollId) throws EntityNotFoundException { return context.getPollService().closePoll(pollId); } - public File exportPoll(PollenServiceContext context, String pollId) { + public File exportPoll(PollenServiceContext context, String pollId) throws EntityNotFoundException { return context.getPollService().exportPoll(pollId); } - public Choice[] getChoices(PollenServiceContext context, String pollId) { + public List<Choice> getChoices(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<Choice> choices = context.getPollService().getChoices(pollId); - return choices.toArray(new Choice[choices.size()]); + return choices; } - public Choice getChoice(PollenServiceContext context, String pollId, String choiceId) { - return context.getPollService().getChoice(pollId, choiceId); + public Choice getChoice(PollenServiceContext context, String choiceId) throws EntityNotFoundException { + return context.getPollService().getChoice(choiceId); } - public Choice addChoice(PollenServiceContext context, String pollId, Choice choice) { + public Choice addChoice(PollenServiceContext context, String pollId, Choice choice) throws EntityNotFoundException { return context.getPollService().addChoice(pollId, choice); } - public Choice editChoice(PollenServiceContext context, String pollId, Choice choice) { - return context.getPollService().editChoice(pollId, choice); + public Choice editChoice(PollenServiceContext context, Choice choice) throws EntityNotFoundException { + return context.getPollService().editChoice(choice); } - public void deleteChoice(PollenServiceContext context, String pollId, String choiceId) { + public void deleteChoice(PollenServiceContext context, String pollId, String choiceId) throws EntityNotFoundException { context.getPollService().deleteChoice(pollId, choiceId); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/UserService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,11 @@ import org.chorem.pollen.persistence.entity.PollenUser; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; +import org.chorem.pollen.service.exception.UserEmailAlreadyUsedException; +import org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException; +import org.chorem.pollen.service.exception.UserInvalidPasswordException; +import org.chorem.pollen.service.exception.UserLoginAlreadyUsedException; import org.debux.webmotion.server.WebMotionController; import java.util.List; @@ -42,19 +47,33 @@ return users.toArray(new PollenUser[users.size()]); } - public PollenUser getUser(PollenServiceContext context, String userId) { + public PollenUser getUser(PollenServiceContext context, + String userId) throws EntityNotFoundException { return context.getUserService().getUser(userId); } - public PollenUser createUser(PollenServiceContext context, PollenUser user) { - return context.getUserService().createUser(user); + public PollenUser createUser(PollenServiceContext context, + PollenUser user, + boolean generatePassword) throws UserEmailAlreadyUsedException, UserLoginAlreadyUsedException { + return context.getUserService().createUser(user, generatePassword); + } - public PollenUser editUser(PollenServiceContext context, PollenUser user) { + public PollenUser editUser(PollenServiceContext context, + PollenUser user) throws UserInvalidPasswordException, UserEmailAlreadyUsedException, EntityNotFoundException { return context.getUserService().editUser(user); } - public void validateUserEmail(PollenServiceContext context, String userId, String token) { + public void validateUserEmail(PollenServiceContext context, + String userId, + String token) throws EntityNotFoundException, UserInvalidEmailActiviationTokenException { context.getUserService().validateUserEmail(userId, token); } + + public void changePassword(PollenServiceContext context, + String userId, + String oldPassword, + String newPassword) throws UserInvalidPasswordException, EntityNotFoundException { + context.getUserService().changePassword(userId, oldPassword, newPassword); + } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteCountingService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteCountingService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteCountingService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,7 @@ import org.chorem.pollen.service.PollResult; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; /** @@ -35,7 +36,7 @@ */ public class VoteCountingService extends WebMotionController { - public PollResult getResult(PollenServiceContext context, String pollId) { + public PollResult getResult(PollenServiceContext context, String pollId) throws EntityNotFoundException { return context.getVoteCountingService().getResult(pollId); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoteService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,7 @@ import org.chorem.pollen.persistence.entity.Vote; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; import java.util.List; @@ -37,24 +38,24 @@ */ public class VoteService extends WebMotionController { - public Vote[] getVotes(PollenServiceContext context, String pollId) { + public Vote[] getVotes(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<Vote> votes = context.getVoteService().getVotes(pollId); return votes.toArray(new Vote[votes.size()]); } - public Vote getVote(PollenServiceContext context, String pollId, String voteId) { - return context.getVoteService().getVote(pollId, voteId); + public Vote getVote(PollenServiceContext context, String voteId) throws EntityNotFoundException { + return context.getVoteService().getVote(voteId); } - public Vote addVote(PollenServiceContext context, String pollId, Vote vote) { + public Vote addVote(PollenServiceContext context, String pollId, Vote vote) throws EntityNotFoundException { return context.getVoteService().addVote(pollId, vote); } - public Vote editVote(PollenServiceContext context, String pollId, Vote vote) { - return context.getVoteService().editVote(pollId, vote); + public Vote editVote(PollenServiceContext context, Vote vote) throws EntityNotFoundException { + return context.getVoteService().editVote(vote); } - public void deleteVote(PollenServiceContext context, String pollId, String voteId) { + public void deleteVote(PollenServiceContext context, String pollId, String voteId) throws EntityNotFoundException { context.getVoteService().deleteVote(pollId, voteId); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/VoterListService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; import org.chorem.pollen.service.PollenServiceContext; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.debux.webmotion.server.WebMotionController; import java.util.List; @@ -39,49 +40,49 @@ */ public class VoterListService extends WebMotionController { - public VoterList importFavoriteList(PollenServiceContext context, String userId, String pollId, String favoriteListId) { - return context.getVoterListService().importFavoriteList(userId, pollId, favoriteListId); + public VoterList importFavoriteList(PollenServiceContext context, String pollId, String favoriteListId) throws EntityNotFoundException { + return context.getVoterListService().importFavoriteList(pollId, favoriteListId); } - public VoterList[] getVoterLists(PollenServiceContext context, String pollId) { + public VoterList[] getVoterLists(PollenServiceContext context, String pollId) throws EntityNotFoundException { List<VoterList> voterLists = context.getVoterListService().getVoterLists(pollId); return voterLists.toArray(new VoterList[voterLists.size()]); } - public VoterList getVoterList(PollenServiceContext context, String pollId, String voterListId) { - return context.getVoterListService().getVoterList(pollId, voterListId); + public VoterList getVoterList(PollenServiceContext context, String voterListId) throws EntityNotFoundException { + return context.getVoterListService().getVoterList(voterListId); } - public VoterList addVoterList(PollenServiceContext context, String pollId, VoterList voterList) { + public VoterList addVoterList(PollenServiceContext context, String pollId, VoterList voterList) throws EntityNotFoundException { return context.getVoterListService().addVoterList(pollId, voterList); } - public VoterList editVoterList(PollenServiceContext context, String pollId, VoterList voterList) { - return context.getVoterListService().editVoterList(pollId, voterList); + public VoterList editVoterList(PollenServiceContext context, VoterList voterList) throws EntityNotFoundException { + return context.getVoterListService().editVoterList(voterList); } - public void deleteVoterList(PollenServiceContext context, String pollId, String voterListId) { + public void deleteVoterList(PollenServiceContext context, String pollId, String voterListId) throws EntityNotFoundException { context.getVoterListService().deleteVoterList(pollId, voterListId); } - public VoterListMember[] getMembers(PollenServiceContext context, String pollId, String voterListId) { - Set<VoterListMember> members = context.getVoterListService().getMembers(pollId, voterListId); - return members.toArray(new VoterListMember[members.size()]); + public Set<VoterListMember> getMembers(PollenServiceContext context, String voterListId) throws EntityNotFoundException { + Set<VoterListMember> members = context.getVoterListService().getMembers(voterListId); + return members; } - public VoterListMember getMember(PollenServiceContext context, String pollId, String voterListId, String memberId) { - return context.getVoterListService().getMember(pollId, voterListId, memberId); + public VoterListMember getMember(PollenServiceContext context, String memberId) throws EntityNotFoundException { + return context.getVoterListService().getMember(memberId); } - public VoterListMember addMember(PollenServiceContext context, String pollId, String voterListId, VoterListMember member) { - return context.getVoterListService().addMember(pollId, voterListId, member); + public VoterListMember addMember(PollenServiceContext context, String voterListId, VoterListMember member) throws EntityNotFoundException { + return context.getVoterListService().addMember(voterListId, member); } - public VoterListMember editMember(PollenServiceContext context, String pollId, String voterListId, VoterListMember member) { - return context.getVoterListService().editMember(pollId, voterListId, member); + public VoterListMember editMember(PollenServiceContext context, VoterListMember member) throws EntityNotFoundException { + return context.getVoterListService().editMember(member); } - public void deleteMember(PollenServiceContext context, String pollId, String voterListId, String memberId) { - context.getVoterListService().deleteMember(pollId, voterListId, memberId); + public void deleteMember(PollenServiceContext context, String voterListId, String memberId) throws EntityNotFoundException { + context.getVoterListService().deleteMember(voterListId, memberId); } } Modified: branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping =================================================================== --- branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-rest-api/src/main/resources/mapping 2013-06-06 16:43:31 UTC (rev 3817) @@ -8,8 +8,16 @@ * /* PollenIdOrTokenInjector.inject * /* PollenSecurityFilter.inject (prend le param auth pour retrouver le PollenUser) -- une fois un object lié à PollenUser, c'est immuable. +#- une fois un object lié à PollenUser, c'est immuable. +[errors] + +org.chorem.pollen.service.exception.EntityNotFoundException action:ErrorAction.on402 +org.chorem.pollen.service.exception.UserEmailAlreadyUsedException action:ErrorAction.on500 +org.chorem.pollen.service.exception.UserLoginAlreadyUsedException action:ErrorAction.on500 +org.chorem.pollen.service.exception.UserInvalidPasswordException action:ErrorAction.on500 +org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException action:ErrorAction.on500 + [actions] # AuthService @@ -20,74 +28,74 @@ # CommentService -GET /poll/{pollId}/comments action:CommentService.getComments -POST /poll/{pollId}/comment action:CommentService.addComment -GET /comment/{commentId} action:CommentService.getComment -PUT /comment action:CommentService.editComment -DELETE /comment/{commentId} action:CommentService.deleteComment +GET /polls/{pollId}/comments action:CommentService.getComments +POST /polls/{pollId}/comments action:CommentService.addComment +GET /comments/{commentId} action:CommentService.getComment +PUT /comments action:CommentService.editComment +DELETE /polls/{pollId}/comments/{commentId} action:CommentService.deleteComment # FavoriteListService -GET /favoriteList action:FavoriteList.getFavoriteLists -GET /favoriteList/{flId} action:FavoriteList.getFavoriteList -POST /favoriteList action:FavoriteList.createFavoriteList -PUT /favoriteList action:FavoriteList.editFavoriteList -DELETE /favoriteList/{flId} action:FavoriteList.deleteFavoriteList -GET /favoriteList/{flId}/members action:FavoriteList.getMembers -GET /favoriteListMember/{mId} action:FavoriteList.getMember -POST /favoriteList/{flId}/member action:FavoriteList.addMember -PUT /favoriteListMember action:FavoriteList.editMember -DELETE /favoriteListMember/{mId} action:FavoriteList.removeMember +GET /favoriteLists action:FavoriteList.getFavoriteLists +GET /favoriteLists/{flId} action:FavoriteList.getFavoriteList +POST /favoriteLists action:FavoriteList.createFavoriteList +PUT /favoriteLists action:FavoriteList.editFavoriteList +DELETE /favoriteLists/{flId} action:FavoriteList.deleteFavoriteList +GET /favoriteLists/{flId}/members action:FavoriteList.getMembers +GET /favoriteListMembers/{mId} action:FavoriteList.getMember +POST /favoriteLists/{flId}/members action:FavoriteList.addMember +PUT /favoriteListMembers action:FavoriteList.editMember +DELETE /favoriteLists/{flId}/members/{mId} action:FavoriteList.removeMember # PollService -GET /poll action:PollService.getPolls -GET /poll/created action:PollService.getCreatedPolls -GET /poll/invited action:PollService.getInvitedPolls -GET /poll/participated action:PollService.getParticipatedPolls -PUT /poll action:PollService.createPoll -PUT /poll action:PollService.editPoll -DELETE /poll/{pollOrTokenId} action:PollService.deletePoll -POST /poll/{pollId} action:PollService.clonePoll -GET /poll/{pollId}/export action:PollService.exportPoll -PUT /poll/{pollId}/close action:PollService.closePoll +GET /polls action:PollService.getPolls +GET /polls/created action:PollService.getCreatedPolls +GET /polls/invited action:PollService.getInvitedPolls +GET /polls/participated action:PollService.getParticipatedPolls +PUT /polls action:PollService.createPoll +PUT /polls action:PollService.editPoll +DELETE /polls/{pollOrTokenId} action:PollService.deletePoll +POST /polls/{pollId} action:PollService.clonePoll +GET /polls/{pollId}/export action:PollService.exportPoll +PUT /polls/{pollId}/close action:PollService.closePoll -GET /poll/{pollId}/choices action:PollService.getChoices -POST /poll/{pollId}/choice action:PollService.addChoice -GET /choice/{choiceId} action:PollService.getChoice -PUT /choice action:PollService.editChoice -DELETE /choice/{choiceId} action:PollService.deleteChoice - +GET /polls/{pollId}/choices action:PollService.getChoices +POST /polls/{pollId}/choices action:PollService.addChoice +GET /choices/{choiceId} action:PollService.getChoice +PUT /choices action:PollService.editChoice +DELETE /polls/{pollId}/choices/{choiceId} action:PollService.deleteChoice + # UserService -GET /user action:UserService.getUsers -GET /user/{userId} action:UserService.getUser -POST /user action:UserService.createUser -PUT /user action:UserService.editUser +GET /users action:UserService.getUsers +GET /users/{userId} action:UserService.getUser +POST /users action:UserService.createUser +PUT /users action:UserService.editUser GET /validateemail/{token} action:UserService.validateUserEmail # VoteCountingService -GET /poll/{pollId}/results action:VoteCountingService.getResult +GET /polls/{pollId}/results action:VoteCountingService.getResult # VoterListService -PUT /poll/{pollId}/favoriteLists/{flId} action:VoterListService.importFavoriteList -GET /poll/{pollId}/voterLists action:VoterListService.getVoterLists -GET /voterList/{vlId} action:VoterListService.getVoterList -PUT /voterList/{vlId} action:VoterListService.editVoterList -DELETE /voterList/{vlId} action:VoterListService.deleteVoterList +PUT /polls/{pollId}/favoriteLists/{flId} action:VoterListService.importFavoriteList +GET /polls/{pollId}/voterLists action:VoterListService.getVoterLists +GET /voterLists/{vlId} action:VoterListService.getVoterList +PUT /voterLists/{vlId} action:VoterListService.editVoterList +DELETE /polls/{pollId}/voterLists/{vlId} action:VoterListService.deleteVoterList -GET /voterList/{vlId}/members action:VoterListService.getMembers -GET /voterListMember/{mId} action:VoterListService.getMember -POST /voterList/{vlId}/member action:VoterListService.addMember -PUT /voterListMember action:VoterListService.editMember -DELETE /voterListMember/{mId} action:VoterListService.deleteMember +GET /voterLists/{vlId}/members action:VoterListService.getMembers +GET /voterListMembers/{mId} action:VoterListService.getMember +POST /voterLists/{vlId}/members action:VoterListService.addMember +PUT /voterListMembers action:VoterListService.editMember +DELETE /voterLists/{vlId}/members/{mId} action:VoterListService.deleteMember # VoteService -GET /poll/{pollId}/votes action:VoteService.getVotes -PUT /poll/{pollId}/vote action:VoteService.addVote -GET /vote/{voteId} action:VoteService.getVote -PUT /vote action:VoteService.editVote -DELETE /vote/{voteId} action:VoteService.deleteVote +GET /polls/{pollId}/votes action:VoteService.getVotes +PUT /polls/{pollId}/votes action:VoteService.addVote +GET /votes/{voteId} action:VoteService.getVote +PUT /votes action:VoteService.editVote +DELETE /polls/{pollId}/votes/{voteId} action:VoteService.deleteVote Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AbstractPollenService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AbstractPollenService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AbstractPollenService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.persistence.PollenPersistenceContext; import org.chorem.pollen.service.config.PollenServiceConfig; +import org.chorem.pollen.service.exception.EntityNotFoundException; import org.nuiton.jpa.api.JpaEntities; import org.nuiton.jpa.api.JpaEntity; @@ -101,4 +102,16 @@ protected VoteService getVoteService() { return serviceContext.getVoteService(); } + + protected EmailService getEmailService() { + return serviceContext.getEmailService(); + } + + protected <E extends JpaEntity> void checkEntityExists(Class<E> type, + E entity, + String entityId) throws EntityNotFoundException { + if (entity == null) { + throw new EntityNotFoundException(type, entityId); + } + } } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AuthService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AuthService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/AuthService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -24,6 +24,12 @@ */ import com.google.common.base.Preconditions; +import org.chorem.pollen.persistence.dao.PollenUserJpaDao; +import org.chorem.pollen.persistence.dao.SessionTokenJpaDao; +import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.persistence.entity.SessionToken; +import org.chorem.pollen.service.exception.EntityNotFoundException; +import org.chorem.pollen.service.exception.UserInvalidPasswordException; /** * TODO @@ -33,20 +39,78 @@ */ public class AuthService extends AbstractPollenService { - public String login(String login, String password) { + public String login(String login, String password) throws EntityNotFoundException, UserInvalidPasswordException { Preconditions.checkNotNull(login); Preconditions.checkNotNull(password); - //TODO - return null; + + PollenUser user = getPersistenceContext().getPollenUserDao().findByLogin(login); + if (user == null) { + throw new EntityNotFoundException(PollenUser.class, login); + } + + String encodedPassword = serviceContext.encodePassword(password); + if (!encodedPassword.equals(user.getPassword())) { + throw new UserInvalidPasswordException(); + } + + // Create a new session Token + SessionTokenJpaDao dao = getPersistenceContext().getSessionTokenDao(); + + SessionToken sessionToken = dao.newInstance(); + String token = serviceContext.generateUUID(); + String encodedToken = serviceContext.encodePassword(token); + sessionToken.setPollenUser(user); + sessionToken.setToken(encodedToken); + sessionToken.setCreationDate(serviceContext.getNow()); + + dao.persist(sessionToken); + getPersistenceContext().commit(); + + return token; } - public void lostPassword(String token) { + public void lostPassword(String login) throws EntityNotFoundException { + Preconditions.checkNotNull(login); + + PollenUserJpaDao dao = getPersistenceContext().getPollenUserDao(); + + PollenUser user = dao.findByLogin(login); + if (user == null) { + throw new EntityNotFoundException(PollenUser.class, login); + } + + // Generate a new password + String newPassword = serviceContext.generatePassword(); + String encodedPassword = serviceContext.encodePassword(newPassword); + user.setPassword(encodedPassword); + dao.merge(user); + getPersistenceContext().commit(); + + notifyPasswordChanged(user, newPassword); + } + + public void logout(String login, String token) throws EntityNotFoundException { + Preconditions.checkNotNull(login); Preconditions.checkNotNull(token); - //TODO + PollenUser user = getPersistenceContext().getPollenUserDao().findByLogin(login); + if (user == null) { + throw new EntityNotFoundException(PollenUser.class, login); + } + + String encodedToken = serviceContext.encodePassword(token); + + SessionTokenJpaDao dao = getPersistenceContext().getSessionTokenDao(); + + SessionToken sessionToken = dao.findByToken(encodedToken); + if (sessionToken == null) { + throw new EntityNotFoundException(SessionToken.class, token); + } + + dao.remove(sessionToken); + getPersistenceContext().commit(); } - public void logout(String login) { - Preconditions.checkNotNull(login); + protected void notifyPasswordChanged(PollenUser user, String newPassword) { //TODO } } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/CommentService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/CommentService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/CommentService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.persistence.entity.Comment; import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.service.exception.EntityNotFoundException; import java.util.List; @@ -37,60 +38,50 @@ */ public class CommentService extends AbstractPollenService { - public List<Comment> getComments(String pollId) { + public List<Comment> getComments(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - return poll.getComment(); } - public Comment getComment(String pollId, String commentId) { - Preconditions.checkNotNull(pollId); + public Comment getComment(String commentId) throws EntityNotFoundException { Preconditions.checkNotNull(commentId); - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - - Comment comment = poll.getCommentById(commentId); - Preconditions.checkNotNull(comment); - - return comment; + Comment result = getPersistenceContext().getCommentDao().findById(commentId); + checkEntityExists(Comment.class, result, commentId); + return result; } - public Comment addComment(String pollId, Comment comment) { + public Comment addComment(String pollId, Comment comment) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); checkHasNoId(comment); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); poll.addComment(comment); getPersistenceContext().getPollDao().merge(poll); getPersistenceContext().commit(); - return null; + + Comment result = getComment(comment.getId()); + return result; } - public Comment editComment(String pollId, Comment comment) { - Preconditions.checkNotNull(pollId); - checkHasId(comment); - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); + public Comment editComment(Comment comment) throws EntityNotFoundException { - Comment persisted = poll.getCommentById(comment.getId()); - Preconditions.checkNotNull(persisted); + // check comment exists + getComment(comment.getId()); getPersistenceContext().getCommentDao().merge(comment); - getPersistenceContext().commit(); - return null; + + Comment result = getComment(comment.getId()); + return result; } - public void deleteComment(String pollId, String commentId) { + public void deleteComment(String pollId, String commentId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(commentId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - Comment comment = poll.getCommentById(commentId); + Comment comment = getComment(commentId); Preconditions.checkNotNull(comment); poll.removeComment(comment); Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/DefaultPollenServiceContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -23,11 +23,11 @@ * #L% */ -import org.chorem.pollen.persistence.JpaPollenPersistenceContext; +import org.apache.commons.lang3.RandomStringUtils; import org.chorem.pollen.persistence.PollenPersistenceContext; import org.chorem.pollen.service.config.PollenServiceConfig; +import org.nuiton.util.StringUtil; -import javax.persistence.EntityManager; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.Date; @@ -35,20 +35,18 @@ public class DefaultPollenServiceContext implements PollenServiceContext { - protected EntityManager entityManager; - protected PollenServiceConfig pollenServiceConfig; protected PollenPersistenceContext persistenceContext; - public void setEntityManager(EntityManager entityManager) { - this.entityManager = entityManager; - } - public void setPollenServiceConfig(PollenServiceConfig pollenServiceConfig) { this.pollenServiceConfig = pollenServiceConfig; } + public void setPersistenceContext(PollenPersistenceContext persistenceContext) { + this.persistenceContext = persistenceContext; + } + @Override public PollenServiceConfig getPollenServiceConfig() { return pollenServiceConfig; @@ -65,11 +63,12 @@ return now; } + public String generatePassword() { + return RandomStringUtils.randomAlphanumeric(8); + } + @Override public PollenPersistenceContext getPersistenceContext() { - if (persistenceContext == null) { - persistenceContext = new JpaPollenPersistenceContext(entityManager); - } return persistenceContext; } @@ -113,6 +112,16 @@ return newService(VoteService.class); } + @Override + public EmailService getEmailService() { + return newService(EmailService.class); + } + + @Override + public String encodePassword(String password) { + return StringUtil.encodeMD5(password); + } + protected <E extends PollenServiceSupport> E newService(Class<E> serviceClass) { E service; Added: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/EmailService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/EmailService.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/EmailService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,39 @@ +package org.chorem.pollen.service; + +/* + * #%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 org.chorem.pollen.persistence.entity.PollenUser; + +/** + * TODO + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class EmailService extends AbstractPollenService { + + public void onUserCreated(PollenUser user) { + //TODO + } +} Property changes on: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/EmailService.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FavoriteListService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FavoriteListService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FavoriteListService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -27,6 +27,7 @@ import org.chorem.pollen.persistence.entity.FavoriteList; import org.chorem.pollen.persistence.entity.FavoriteListMember; import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.service.exception.EntityNotFoundException; import java.util.List; @@ -38,65 +39,57 @@ */ public class FavoriteListService extends AbstractPollenService { - public List<FavoriteList> getFavoriteLists(String userId) { + public List<FavoriteList> getFavoriteLists(String userId)throws EntityNotFoundException { Preconditions.checkNotNull(userId); PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); List<FavoriteList> result = user.getFavoriteList(); return result; } - public FavoriteList getFavoriteList(String userId, String favoriteListId) { - Preconditions.checkNotNull(userId); + public FavoriteList getFavoriteList(String favoriteListId) throws EntityNotFoundException{ Preconditions.checkNotNull(favoriteListId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - return favoriteList; + FavoriteList result = getPersistenceContext().getFavoriteListDao().findById(favoriteListId); + checkEntityExists(FavoriteList.class, result, favoriteListId); + return result; } - public FavoriteList createFavoriteList(String userId, FavoriteList favoriteList) { + public FavoriteList createFavoriteList(String userId, + FavoriteList favoriteList) throws EntityNotFoundException{ Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteList); checkHasNoId(favoriteList); PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); user.addFavoriteList(favoriteList); - user = getPersistenceContext().getPollenUserDao().merge(user); + getPersistenceContext().getPollenUserDao().merge(user); getPersistenceContext().commit(); - return user.getFavoriteListById(favoriteList.getId()); + FavoriteList result = getFavoriteList(favoriteList.getId()); + return result; } - public FavoriteList editFavoriteList(String userId, FavoriteList favoriteList) { - Preconditions.checkNotNull(userId); + public FavoriteList editFavoriteList(FavoriteList favoriteList) throws EntityNotFoundException{ Preconditions.checkNotNull(favoriteList); checkHasId(favoriteList); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + getFavoriteList(favoriteList.getId()); - FavoriteList persisted = user.getFavoriteListById(favoriteList.getId()); - Preconditions.checkNotNull(persisted); + getPersistenceContext().getFavoriteListDao().merge(favoriteList); - getPersistenceContext().getFavoriteListDao().persist(favoriteList); - getPersistenceContext().commit(); - return favoriteList; + FavoriteList result = getFavoriteList(favoriteList.getId()); + return result; } - public void deleteFavoriteList(String userId, String favoriteListId) { + public void deleteFavoriteList(String userId, String favoriteListId) throws EntityNotFoundException{ Preconditions.checkNotNull(userId); Preconditions.checkNotNull(favoriteListId); PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); - FavoriteList persisted = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(persisted); + FavoriteList persisted = getFavoriteList(favoriteListId); user.removeFavoriteList(persisted); getPersistenceContext().getPollenUserDao().merge(user); @@ -104,89 +97,59 @@ getPersistenceContext().commit(); } - public List<FavoriteListMember> getMembers(String userId, String favoriteListId) { - Preconditions.checkNotNull(userId); + public List<FavoriteListMember> getFavoriteListMembers(String favoriteListId)throws EntityNotFoundException { Preconditions.checkNotNull(favoriteListId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + FavoriteList favoriteList = getFavoriteList(favoriteListId); - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(favoriteList); - return favoriteList.getFavoriteListMember(); } - public FavoriteListMember getMember(String userId, String favoriteListId, String memberId) { - Preconditions.checkNotNull(userId); - Preconditions.checkNotNull(favoriteListId); + public FavoriteListMember getFavoriteListMember(String memberId)throws EntityNotFoundException { Preconditions.checkNotNull(memberId); - - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); - - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(favoriteList); - - FavoriteListMember member = favoriteList.getFavoriteListMemberById(memberId); - return member; + FavoriteListMember result = getPersistenceContext().getFavoriteListMemberDao().findById(memberId); + checkEntityExists(FavoriteListMember.class, result, memberId); + return result; } - public FavoriteListMember addMember(String userId, String favoriteListId, FavoriteListMember member) { - Preconditions.checkNotNull(userId); + public FavoriteListMember addFavoriteListMember(String favoriteListId, + FavoriteListMember member)throws EntityNotFoundException { Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(member); checkHasNoId(member); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + FavoriteList favoriteList = getFavoriteList(favoriteListId); - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(favoriteList); - favoriteList.addFavoriteListMember(member); - favoriteList = getPersistenceContext().getFavoriteListDao().merge(favoriteList); + getPersistenceContext().getFavoriteListDao().merge(favoriteList); getPersistenceContext().commit(); - FavoriteListMember result = favoriteList.getFavoriteListMemberById(member.getId()); + FavoriteListMember result = getFavoriteListMember(member.getId()); return result; } - public FavoriteListMember editMember(String userId, String favoriteListId, FavoriteListMember member) { - Preconditions.checkNotNull(userId); - Preconditions.checkNotNull(favoriteListId); + public FavoriteListMember editFavoriteListMember(FavoriteListMember member)throws EntityNotFoundException { Preconditions.checkNotNull(member); checkHasId(member); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(favoriteList); + getFavoriteListMember(member.getId()); - FavoriteListMember persisted = favoriteList.getFavoriteListMemberById(member.getId()); - Preconditions.checkNotNull(persisted); - member = getPersistenceContext().getFavoriteListMemberDao().merge(member); getPersistenceContext().commit(); - return member; + FavoriteListMember result = getFavoriteListMember(member.getId()); + return result; } - public void removeMember(String userId, String favoriteListId, String memberId) { - Preconditions.checkNotNull(userId); + public void removeFavoriteListMember(String favoriteListId, String memberId) throws EntityNotFoundException{ Preconditions.checkNotNull(favoriteListId); Preconditions.checkNotNull(memberId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + FavoriteList favoriteList = getFavoriteList(favoriteListId); - FavoriteList favoriteList = user.getFavoriteListById(favoriteListId); - Preconditions.checkNotNull(favoriteList); + FavoriteListMember member = getFavoriteListMember(memberId); - FavoriteListMember member = favoriteList.getFavoriteListMemberById(memberId); - Preconditions.checkNotNull(member); - favoriteList.removeFavoriteListMember(member); getPersistenceContext().getFavoriteListDao().merge(favoriteList); Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FixturesService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FixturesService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/FixturesService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -84,6 +84,7 @@ for (PollenUser user : users) { + user.setPassword(serviceContext.encodePassword(user.getPassword())); userDao.persist(user); } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -27,6 +27,7 @@ import org.chorem.pollen.persistence.entity.Choice; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.service.exception.EntityNotFoundException; import java.io.File; import java.util.List; @@ -40,168 +41,156 @@ */ public class PollService extends AbstractPollenService { - public Set<Poll> getCreatedPolls(String userId) { + public Set<Poll> getCreatedPolls(String userId) throws EntityNotFoundException { Preconditions.checkNotNull(userId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + getUserService().getUser(userId); return getPersistenceContext().getPollDao().findAllCreated(userId); } - public Set<Poll> getInvitedPolls(String userId) { + public Set<Poll> getInvitedPolls(String userId) throws EntityNotFoundException { Preconditions.checkNotNull(userId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + getUserService().getUser(userId); return getPersistenceContext().getPollDao().findAllInvited(userId); } - public Set<Poll> getParticipatedPolls(String userId) { + public Set<Poll> getParticipatedPolls(String userId) throws EntityNotFoundException { Preconditions.checkNotNull(userId); - PollenUser user = getUserService().getUser(userId); - Preconditions.checkNotNull(user); + getUserService().getUser(userId); return getPersistenceContext().getPollDao().findAllParticipated(userId); } - public Poll getPoll(String pollId) { + public Poll getPoll(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll result = getPersistenceContext().getPollDao().findById(pollId); + checkEntityExists(Poll.class, result, pollId); return result; } - public Poll createPoll(String userId, Poll poll) { + public Poll createPoll(String userId, Poll poll) throws EntityNotFoundException { Preconditions.checkNotNull(userId); Preconditions.checkNotNull(poll); checkHasNoId(poll); - PollenUser user = getUserService().getUser(userId); + if (userId != null) { - if (user != null) { + // get user + PollenUser user = getUserService().getUser(userId); - // link to creator - //TODO + // link it to creator + } + getPersistenceContext().getPollDao().persist(poll); getPersistenceContext().commit(); return poll; } - public Poll editPoll(Poll poll) { + public Poll editPoll(Poll poll) throws EntityNotFoundException { Preconditions.checkNotNull(poll); checkHasId(poll); - Poll persisted = getPoll(poll.getId()); - Preconditions.checkNotNull(persisted); + getPoll(poll.getId()); - Poll result = getPersistenceContext().getPollDao().merge(poll); + getPersistenceContext().getPollDao().merge(poll); getPersistenceContext().commit(); + + Poll result = getPoll(poll.getId()); return result; } - public void deletePoll(String pollId) { + public void deletePoll(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); getPersistenceContext().getPollDao().remove(poll); getPersistenceContext().commit(); } - public Poll clonePoll(String pollId) { + public Poll clonePoll(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); - + //TODO getPersistenceContext().commit(); return null; } - public File closePoll(String pollId) { + public File closePoll(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); - + //TODO getPersistenceContext().commit(); return null; } - public File exportPoll(String pollId) { + public File exportPoll(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); + Poll poll = getPoll(pollId); //TODO return null; } - public List<Choice> getChoices(String pollId) { + public List<Choice> getChoices(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); return poll.getChoice(); } - public Choice getChoice(String pollId, String choiceId) { - Preconditions.checkNotNull(pollId); + public Choice getChoice(String choiceId) throws EntityNotFoundException { Preconditions.checkNotNull(choiceId); - Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); - - Choice result = poll.getChoiceById(choiceId); + Choice result = getPersistenceContext().getChoiceDao().findById(choiceId); + checkEntityExists(Choice.class, result, choiceId); return result; } - public Choice addChoice(String pollId, Choice choice) { + public Choice addChoice(String pollId, Choice choice) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(choice); checkHasNoId(choice); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); poll.addChoice(choice); getPersistenceContext().getPollDao().merge(poll); getPersistenceContext().commit(); - return null; + Choice result = getChoice(choice.getId()); + return result; } - public Choice editChoice(String pollId, Choice choice) { - Preconditions.checkNotNull(pollId); + public Choice editChoice(Choice choice) throws EntityNotFoundException { Preconditions.checkNotNull(choice); checkHasId(choice); - Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); + getChoice(choice.getId()); - Choice persisted = poll.getChoiceById(choice.getId()); - Preconditions.checkNotNull(persisted); - - Choice result = getPersistenceContext().getChoiceDao().merge(choice); - + getPersistenceContext().getChoiceDao().merge(choice); getPersistenceContext().commit(); + + Choice result = getChoice(choice.getId()); return result; } - public void deleteChoice(String pollId, String choiceId) { + public void deleteChoice(String pollId, String choiceId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(choiceId); Poll poll = getPoll(pollId); - Preconditions.checkNotNull(poll); + Choice choice = getChoice(choiceId); - Choice choice = poll.getChoiceById(choiceId); - Preconditions.checkNotNull(choice); - poll.removeChoice(choice); getPersistenceContext().getPollDao().merge(poll); Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/PollenServiceContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -58,4 +58,9 @@ VoteService getVoteService(); + EmailService getEmailService(); + + String generatePassword(); + + String encodePassword(String password); } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/UserService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,8 +25,16 @@ import com.google.common.base.Preconditions; import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.chorem.pollen.persistence.dao.PollenUserJpaDao; import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.service.exception.EntityNotFoundException; +import org.chorem.pollen.service.exception.UserEmailAlreadyUsedException; +import org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException; +import org.chorem.pollen.service.exception.UserInvalidPasswordException; +import org.chorem.pollen.service.exception.UserLoginAlreadyUsedException; +import org.nuiton.util.beans.Binder; +import org.nuiton.util.beans.BinderFactory; import java.util.List; @@ -42,70 +50,134 @@ return getPollenUserDao().findAll(); } - public PollenUser getUser(String userId) { + public PollenUser getUser(String userId) throws EntityNotFoundException { Preconditions.checkNotNull(userId); - return getPollenUserDao().findById(userId); + PollenUser result = getPollenUserDao().findById(userId); + checkEntityExists(PollenUser.class, result, userId); + return result; } - public PollenUser createUserByAdmin(PollenUser user) { + public PollenUser createUser(PollenUser user, boolean generatePassword) throws UserLoginAlreadyUsedException, UserEmailAlreadyUsedException { Preconditions.checkNotNull(user); checkHasNoId(user); + Preconditions.checkNotNull(user.getLogin()); + Preconditions.checkNotNull(user.getEmail()); - getPollenUserDao().persist(user); - getPersistenceContext().commit(); + PollenUserJpaDao dao = getPollenUserDao(); - // send a notification to user (to validate his email changed) - notifyUserCreated(user, true); - return user; - } + // check login is available + boolean loginExists = dao.loginExists(user.getLogin()); + if (loginExists) { + throw new UserLoginAlreadyUsedException(user.getLogin()); + } - public PollenUser createUser(PollenUser user) { - Preconditions.checkNotNull(user); - checkHasNoId(user); + // check email is available + boolean emailExists = dao.emailExists(user.getEmail()); + if (emailExists) { + throw new UserEmailAlreadyUsedException(user.getEmail()); + } + PollenUser toCreate = dao.newInstance(); + // add a emailValidationToken String emailValidationToken = generateId(); - user.setEmailActivationToken(emailValidationToken); - PollenUserJpaDao dao = getPollenUserDao(); - dao.persist(user); + String password; + if (generatePassword) { + // let's generate the new password + password = serviceContext.generatePassword(); + } else { + password = user.getPassword(); + } + + // encode the password + String encodedPassword = serviceContext.encodePassword(password); + + toCreate.setLogin(user.getLogin()); + toCreate.setEmailActivationToken(emailValidationToken); + toCreate.setPassword(encodedPassword); + + copyPollenUser(user, toCreate, true); + + dao.persist(toCreate); getPersistenceContext().commit(); - // send a notification to user (to validate his email changed) - notifyUserCreated(user, false); - return user; + notifyUserCreated(toCreate); + + return toCreate; } - public PollenUser editUser(PollenUser user) { + public PollenUser editUser(PollenUser user) throws UserInvalidPasswordException, UserEmailAlreadyUsedException, EntityNotFoundException { Preconditions.checkNotNull(user); checkHasId(user); + PollenUser persisted = getUser(user.getId()); - Preconditions.checkNotNull(persisted); + PollenUserJpaDao dao = getPollenUserDao(); - boolean emailChanged = ObjectUtils.notEqual(persisted.getEmail(), user.getEmail()); + // check current password + String encodedPassword = serviceContext.encodePassword(user.getPassword()); + if (!encodedPassword.equals(persisted.getPassword())) { + throw new UserInvalidPasswordException(); + } + boolean emailChanged = ObjectUtils.notEqual(persisted.getEmail(), + user.getEmail()); + if (emailChanged) { + // check this email is not used by another user + boolean emailUsed = dao.emailExists(user.getEmail()); + if (emailUsed) { + throw new UserEmailAlreadyUsedException(user.getEmail()); + } + // add a new emailValidationtoken String emailValidationToken = generateId(); - user.setEmailActivationToken(emailValidationToken); + persisted.setEmailActivationToken(emailValidationToken); } - user = getPollenUserDao().merge(user); + copyPollenUser(user, persisted, true); + + persisted = dao.merge(persisted); getPersistenceContext().commit(); if (emailChanged) { - // send a notification to user (to validate his email changed) notifyEmailChanged(persisted); } - return user; + return persisted; } - public void validateUserEmail(String userId, String token) { + public void changePassword(String userId, + String oldPassword, + String newPassword) throws UserInvalidPasswordException, EntityNotFoundException { + Preconditions.checkNotNull(userId); + Preconditions.checkNotNull(oldPassword); + Preconditions.checkNotNull(newPassword); + PollenUser user = getUser(userId); + + // check current password + String encodedPassword = serviceContext.encodePassword(user.getPassword()); + if (!encodedPassword.equals(user.getPassword())) { + throw new UserInvalidPasswordException(); + } + + // encode new password and store it in user account + String newEncodedPassword = serviceContext.encodePassword(newPassword); + user.setPassword(newEncodedPassword); + + user = getPollenUserDao().merge(user); + getPersistenceContext().commit(); + + notifyPasswordChanged(user); + } + + public void validateUserEmail(String userId, + String token) throws EntityNotFoundException, UserInvalidEmailActiviationTokenException { + Preconditions.checkNotNull(userId); Preconditions.checkNotNull(token); @@ -116,23 +188,57 @@ boolean valid = ObjectUtils.equals( user.getEmailActivationToken(), token); - if (valid) { - user.setEmailActivationToken(null); + if (!valid) { + throw new UserInvalidEmailActiviationTokenException(); } + // reset token in database + user.setEmailActivationToken(null); + getPollenUserDao().merge(user); getPersistenceContext().commit(); } + protected void notifyUserCreated(PollenUser user) { + + //TODO + } + protected void notifyEmailChanged(PollenUser user) { + //TODO } - protected void notifyUserCreated(PollenUser user, boolean createByAdmin) { + protected void notifyPasswordChanged(PollenUser user) { + //TODO } protected PollenUserJpaDao getPollenUserDao() { return getPersistenceContext().getPollenUserDao(); } + + /** + * Copy {@code source} user account to {@code destination} one. + * The email is lower cased in the {@code destination} user account. + * + * @param source user account to copy + * @param destination which receive the copy + */ + protected void copyPollenUser(PollenUser source, + PollenUser destination, + boolean copyEmail) { + Binder<PollenUser, PollenUser> binder = + BinderFactory.newBinder(PollenUser.class); + + binder.copy(source, destination, + PollenUser.PROPERTY_ADMINISTRATOR, + PollenUser.PROPERTY_NAME, + PollenUser.PROPERTY_LANGUAGE); + + if (copyEmail) { + // Don't keep case for email + destination.setEmail(StringUtils.lowerCase(source.getEmail())); + } + } } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteCountingService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteCountingService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteCountingService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.persistence.entity.Poll; +import org.chorem.pollen.service.exception.EntityNotFoundException; /** * TODO @@ -35,10 +36,9 @@ public class VoteCountingService extends AbstractPollenService { //GET /poll/{pollId}/results - public PollResult getResult(String pollId) { + public PollResult getResult(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); //TODO return null; } Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoteService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,6 +26,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.Vote; +import org.chorem.pollen.service.exception.EntityNotFoundException; import java.util.List; @@ -37,66 +38,57 @@ */ public class VoteService extends AbstractPollenService { - public List<Vote> getVotes(String pollId) { + public List<Vote> getVotes(String pollId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - - return poll.getVote(); + List<Vote> result = poll.getVote(); + return result; } - public Vote getVote(String pollId, String voteId) { - Preconditions.checkNotNull(pollId); + public Vote getVote(String voteId) throws EntityNotFoundException{ Preconditions.checkNotNull(voteId); - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - - Vote result = poll.getVoteById(voteId); + Vote result = getPersistenceContext().getVoteDao().findById(voteId); + checkEntityExists(Vote.class, result, voteId); return result; } - public Vote addVote(String pollId, Vote vote) { + public Vote addVote(String pollId, Vote vote)throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(vote); checkHasNoId(vote); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); poll.addVote(vote); getPersistenceContext().getPollDao().merge(poll); getPersistenceContext().commit(); - return null; + Vote result = getVote(vote.getId()); + return result; } - public Vote editVote(String pollId, Vote vote) { - Preconditions.checkNotNull(pollId); + public Vote editVote(Vote vote)throws EntityNotFoundException { Preconditions.checkNotNull(vote); checkHasId(vote); - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); + getVote(vote.getId()); - Preconditions.checkState(poll.containsVoteById(vote.getId())); - - Vote result = getPersistenceContext().getVoteDao().merge(vote); - + getPersistenceContext().getVoteDao().merge(vote); getPersistenceContext().commit(); + + Vote result = getVote(vote.getId()); return result; } - public void deleteVote(String pollId, String voteId) { + public void deleteVote(String pollId, String voteId)throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(voteId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - Vote vote = getVote(pollId, voteId); - Preconditions.checkNotNull(vote); + Vote vote = getVote(voteId); poll.removeVote(vote); Modified: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoterListService.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoterListService.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/VoterListService.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -31,6 +31,7 @@ import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; +import org.chorem.pollen.service.exception.EntityNotFoundException; import java.util.List; import java.util.Set; @@ -43,16 +44,14 @@ */ public class VoterListService extends AbstractPollenService { - public VoterList importFavoriteList(String userId, String pollId, String favoriteListId) { - Preconditions.checkNotNull(userId); + public VoterList importFavoriteList(String pollId, + String favoriteListId) throws EntityNotFoundException { Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(favoriteListId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - FavoriteList favoriteList = getFavoriteListService().getFavoriteList(userId, favoriteListId); - Preconditions.checkNotNull(favoriteList); + FavoriteList favoriteList = getFavoriteListService().getFavoriteList(favoriteListId); VoterListJpaDao dao = getPersistenceContext().getVoterListDao(); VoterList result = dao.newInstance(); @@ -71,158 +70,130 @@ result.addVoterListMember(voterListMember); } - dao.persist(result); + poll.addVoterList(result); + getPersistenceContext().getPollDao().merge(poll); + getPersistenceContext().commit(); return result; } - public List<VoterList> getVoterLists(String pollId) { + public List<VoterList> getVoterLists(String pollId) throws EntityNotFoundException{ Preconditions.checkNotNull(pollId); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); List<VoterList> result = poll.getVoterList(); return result; } - public VoterList getVoterList(String pollId, String voterListId) { - Preconditions.checkNotNull(pollId); - - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - - if (voterListId == null) { - - Preconditions.checkState(!poll.isVoterListEmpty()); - - // use the first poll one - voterListId = poll.getVoterList(0).getId(); - } - + public VoterList getVoterList(String voterListId) throws EntityNotFoundException{ Preconditions.checkNotNull(voterListId); - VoterList result = poll.getVoterListById(voterListId); + VoterList result = getPersistenceContext().getVoterListDao().findById(voterListId); + checkEntityExists(VoterList.class, result, voterListId); + return result; } - public VoterList addVoterList(String pollId, VoterList voterList) { + public VoterList addVoterList(String pollId, VoterList voterList) throws EntityNotFoundException{ Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(voterList); checkHasNoId(voterList); Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); - getPersistenceContext().getVoterListDao().persist(voterList); + poll.addVoterList(voterList); + + getPersistenceContext().getPollDao().merge(poll); getPersistenceContext().commit(); - return voterList; + VoterList result = getVoterList(voterList.getId()); + return result; } - public VoterList editVoterList(String pollId, VoterList voterList) { - Preconditions.checkNotNull(pollId); + public VoterList editVoterList(VoterList voterList) throws EntityNotFoundException{ Preconditions.checkNotNull(voterList); checkHasId(voterList); - Poll poll = getPollService().getPoll(pollId); - Preconditions.checkNotNull(poll); + getVoterList(voterList.getId()); - Preconditions.checkState(poll.containsVoterListById(voterList.getId())); - - VoterList result = getPersistenceContext().getVoterListDao().merge(voterList); + getPersistenceContext().getVoterListDao().merge(voterList); getPersistenceContext().commit(); + VoterList result = getVoterList(voterList.getId()); return result; } - public void deleteVoterList(String pollId, String voterListId) { + public void deleteVoterList(String pollId, String voterListId) throws EntityNotFoundException{ Preconditions.checkNotNull(pollId); Preconditions.checkNotNull(voterListId); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); + Poll poll = getPollService().getPoll(pollId); - getPersistenceContext().getVoterListDao().remove(voterList); + VoterList voterList = getVoterList(voterListId); + poll.removeVoterList(voterList); + + getPersistenceContext().getPollDao().merge(poll); + getPersistenceContext().commit(); } - public Set<VoterListMember> getMembers(String pollId, String voterListId) { - Preconditions.checkNotNull(pollId); + public Set<VoterListMember> getMembers(String voterListId) throws EntityNotFoundException{ Preconditions.checkNotNull(voterListId); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); + VoterList voterList = getVoterList(voterListId); Set<VoterListMember> result = voterList.getVoterListMember(); return result; } - public VoterListMember getMember(String pollId, String voterListId, String memberId) { - Preconditions.checkNotNull(pollId); - Preconditions.checkNotNull(voterListId); + public VoterListMember getMember(String memberId) throws EntityNotFoundException{ Preconditions.checkNotNull(memberId); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); - VoterListMember result = getPersistenceContext().getVoterListMemberDao().findById(memberId); - + checkEntityExists(VoterListMember.class, result, memberId); return result; } - public VoterListMember addMember(String pollId, String voterListId, VoterListMember member) { - Preconditions.checkNotNull(pollId); + public VoterListMember addMember(String voterListId, VoterListMember member)throws EntityNotFoundException { Preconditions.checkNotNull(voterListId); Preconditions.checkNotNull(member); checkHasNoId(member); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); + VoterList voterList = getVoterList(voterListId); voterList.addVoterListMember(member); - getPersistenceContext().getVoterListDao().persist(voterList); + getPersistenceContext().getVoterListDao().merge(voterList); getPersistenceContext().commit(); - return member; + VoterListMember result = getMember(member.getId()); + return result; } - public VoterListMember editMember(String pollId, String voterListId, VoterListMember member) { - Preconditions.checkNotNull(pollId); - Preconditions.checkNotNull(voterListId); + public VoterListMember editMember(VoterListMember member) throws EntityNotFoundException{ Preconditions.checkNotNull(member); checkHasId(member); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); + getMember(member.getId()); - VoterListMemberJpaDao dao = getPersistenceContext().getVoterListMemberDao(); + getPersistenceContext().getVoterListMemberDao().merge(member); - VoterListMember persisted = dao.findById(member.getId()); - Preconditions.checkNotNull(persisted); - - VoterListMember result = - dao.merge(member); - getPersistenceContext().commit(); + VoterListMember result = getMember(member.getId()); return result; } - public void deleteMember(String pollId, String voterListId, String memberId) { - Preconditions.checkNotNull(pollId); + public void deleteMember(String voterListId, String memberId) throws EntityNotFoundException{ Preconditions.checkNotNull(voterListId); Preconditions.checkNotNull(memberId); - VoterList voterList = getVoterList(pollId, voterListId); - Preconditions.checkNotNull(voterList); + VoterList voterList = getVoterList(voterListId); - VoterListMemberJpaDao dao = getPersistenceContext().getVoterListMemberDao(); + VoterListMember member = getMember(memberId); + voterList.removeVoterListMember(member); - VoterListMember result = dao.findById(memberId); - Preconditions.checkNotNull(result); + getPersistenceContext().getVoterListDao().merge(voterList); - dao.remove(result); - getPersistenceContext().commit(); } Added: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/EntityNotFoundException.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/EntityNotFoundException.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/EntityNotFoundException.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,55 @@ +package org.chorem.pollen.service.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 org.nuiton.jpa.api.JpaEntity; + +/** + * When an entity is not found in database. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class EntityNotFoundException extends Exception { + + private static final long serialVersionUID = -5760536098822762990L; + + protected final Class<? extends JpaEntity> entityType; + + protected final String entityId; + + public EntityNotFoundException(Class<? extends JpaEntity> entityType, + String entityId) { + this.entityType = entityType; + this.entityId = entityId; + } + + public Class<? extends JpaEntity> getEntityType() { + return entityType; + } + + public String getEntityId() { + return entityId; + } +} Property changes on: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/EntityNotFoundException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserEmailAlreadyUsedException.java (from rev 3815, branches/pollen-2.0/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UserEmailAlreadyUsedException.java) =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserEmailAlreadyUsedException.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserEmailAlreadyUsedException.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,45 @@ +package org.chorem.pollen.service.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 UserEmailAlreadyUsedException extends Exception { + + private static final long serialVersionUID = 1L; + + protected final String email; + + public UserEmailAlreadyUsedException(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } +} Added: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidEmailActiviationTokenException.java =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidEmailActiviationTokenException.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidEmailActiviationTokenException.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,34 @@ +package org.chorem.pollen.service.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 UserInvalidEmailActiviationTokenException extends Exception { + private static final long serialVersionUID = 1L; +} Property changes on: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidEmailActiviationTokenException.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidPasswordException.java (from rev 3815, branches/pollen-2.0/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UserInvalidPasswordException.java) =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidPasswordException.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserInvalidPasswordException.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,34 @@ +package org.chorem.pollen.service.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% + */ + +/** + * @author tchemit <chemit@codelutin.com> + * @since 2.0 + */ +public class UserInvalidPasswordException extends Exception { + + private static final long serialVersionUID = 1L; + +} Copied: branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserLoginAlreadyUsedException.java (from rev 3815, branches/pollen-2.0/pollen-services/src/main/java/org/chorem/pollen/services/exceptions/UserLoginAlreadyUsedException.java) =================================================================== --- branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserLoginAlreadyUsedException.java (rev 0) +++ branches/pollen-2.0/pollen-service/src/main/java/org/chorem/pollen/service/exception/UserLoginAlreadyUsedException.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -0,0 +1,45 @@ +package org.chorem.pollen.service.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 UserLoginAlreadyUsedException extends Exception { + + private static final long serialVersionUID = 1L; + + protected final String login; + + public UserLoginAlreadyUsedException(String login) { + this.login=login; + } + + public String getLogin() { + return login; + } +} Modified: branches/pollen-2.0/pollen-service/src/main/resources/fixtures.yaml =================================================================== --- branches/pollen-2.0/pollen-service/src/main/resources/fixtures.yaml 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/main/resources/fixtures.yaml 2013-06-06 16:43:31 UTC (rev 3817) @@ -20,6 +20,7 @@ &julien !user id: pollen_user_julien login: julien + password: fake name: J email: julien@pollen.fake administrator: true Modified: branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/AbstractPollenServiceTest.java =================================================================== --- branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/AbstractPollenServiceTest.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/AbstractPollenServiceTest.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -23,6 +23,8 @@ * #L% */ +import org.chorem.pollen.persistence.JpaEntityIdFactoryIfNotEmpty; +import org.chorem.pollen.persistence.JpaPollenPersistenceContext; import org.chorem.pollen.service.config.PollenServiceConfig; import org.junit.Rule; import org.nuiton.jpa.junit.JpaEntityManagerRule; @@ -64,7 +66,10 @@ EntityManager entityManager = getJpaEntityManagerRule().getEntityManager(); - serviceContext.setEntityManager(entityManager); + JpaEntityIdFactoryIfNotEmpty idGenerator = new JpaEntityIdFactoryIfNotEmpty(); + JpaPollenPersistenceContext persistenceContext = new JpaPollenPersistenceContext(idGenerator, entityManager); + + serviceContext.setPersistenceContext(persistenceContext); } } Modified: branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/FakePollenServiceContext.java =================================================================== --- branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/FakePollenServiceContext.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/FakePollenServiceContext.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -26,9 +26,6 @@ import com.google.common.base.Preconditions; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.persistence.JpaEntityIdFactoryIfNotEmpty; -import org.chorem.pollen.persistence.JpaPollenPersistenceContext; -import org.chorem.pollen.persistence.PollenPersistenceContext; import java.util.Date; @@ -48,15 +45,6 @@ return date; } - @Override - public PollenPersistenceContext getPersistenceContext() { - if (persistenceContext == null) { - JpaEntityIdFactoryIfNotEmpty idGenerator = new JpaEntityIdFactoryIfNotEmpty(); - persistenceContext = new JpaPollenPersistenceContext(idGenerator, entityManager); - } - return persistenceContext; - } - public void setDate(Date date) { this.date = date; } Modified: branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/UserServiceTest.java =================================================================== --- branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/UserServiceTest.java 2013-06-05 16:41:46 UTC (rev 3816) +++ branches/pollen-2.0/pollen-service/src/test/java/org/chorem/pollen/service/UserServiceTest.java 2013-06-06 16:43:31 UTC (rev 3817) @@ -25,6 +25,11 @@ import org.apache.commons.collections.CollectionUtils; import org.chorem.pollen.persistence.entity.PollenUser; +import org.chorem.pollen.service.exception.EntityNotFoundException; +import org.chorem.pollen.service.exception.UserEmailAlreadyUsedException; +import org.chorem.pollen.service.exception.UserInvalidEmailActiviationTokenException; +import org.chorem.pollen.service.exception.UserInvalidPasswordException; +import org.chorem.pollen.service.exception.UserLoginAlreadyUsedException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -67,11 +72,130 @@ } @Test - public void testGetPollenUser() { + public void testGetPollenUser() throws EntityNotFoundException { + try { + service.getUser("pollen_user_tony_" + System.nanoTime()); + + } catch (EntityNotFoundException e) { + Assert.assertTrue(true); + } + PollenUser user = service.getUser("pollen_user_tony"); Assert.assertNotNull(user); Assert.assertEquals(this.user, user); } + + @Test + public void testCreatePollenUser() throws Exception { + + try { + service.createUser(user, false); + Assert.fail(); + } catch (IllegalStateException e) { + // Should having id + Assert.assertTrue(true); + } + + PollenUser newUser = new PollenUser(); + newUser.setLogin("pollen"); + newUser.setEmail("pollen@pollen.org"); + + PollenUser savedUser = service.createUser(newUser, true); + Assert.assertNotNull(savedUser); + Assert.assertNotNull(savedUser.getId()); + + PollenUser newUser2 = new PollenUser(); + newUser2.setLogin("pollen"); + newUser2.setEmail("pollen@pollen.org"); + + try { + service.createUser(newUser2, true); + Assert.fail(); + } catch (UserLoginAlreadyUsedException e) { + Assert.assertTrue(true); + } + + newUser2.setLogin("pollen2"); + try { + service.createUser(newUser2, true); + Assert.fail(); + } catch (UserEmailAlreadyUsedException e) { + Assert.assertTrue(true); + } + + newUser2.setEmail("pollen2@pollen.org"); + PollenUser savedUser2 = service.createUser(newUser2, true); + Assert.assertNotNull(savedUser2); + Assert.assertNotNull(savedUser2.getId()); + } + + @Test + public void testEditUser() throws EntityNotFoundException, UserInvalidPasswordException, UserEmailAlreadyUsedException { + + PollenUser user = service.getUser("pollen_user_tony"); + + Assert.assertNotNull(user); + Assert.assertNull(user.getEmailActivationToken()); + + serviceContext.getPersistenceContext().detach(user); + + String originalLogin = user.getLogin(); + + user.setLogin("yetanotherlogin"); + String email = "tony@pollen.org"; + user.setEmail(email); + + try { + service.editUser(user); + Assert.fail(); + } catch (UserInvalidPasswordException e) { + Assert.assertTrue(true); + } + + user.setPassword("fake"); + PollenUser savedUser = service.editUser(user); + Assert.assertNotNull(savedUser); + Assert.assertEquals(originalLogin, savedUser.getLogin()); + Assert.assertEquals(email, savedUser.getEmail()); + Assert.assertNotNull(savedUser.getEmailActivationToken()); + } + + @Test + public void testValidateEmail() throws EntityNotFoundException, UserInvalidPasswordException, UserEmailAlreadyUsedException, UserInvalidEmailActiviationTokenException { + + PollenUser user = service.getUser("pollen_user_tony"); + Assert.assertNotNull(user); + Assert.assertNull(user.getEmailActivationToken()); + + serviceContext.getPersistenceContext().detach(user); + + String email = "tony@pollen.org"; + user.setEmail(email); + user.setPassword("fake"); + + PollenUser savedUser = service.editUser(user); + Assert.assertNotNull(savedUser); + + Assert.assertEquals(email, savedUser.getEmail()); + Assert.assertNotNull(savedUser.getEmailActivationToken()); + + try { + service.validateUserEmail(user.getId(), "fakeToken"); + Assert.fail(); + } catch (UserInvalidEmailActiviationTokenException e) { + Assert.assertTrue(true); + } + + Assert.assertFalse(savedUser.isEmailActivated()); + + service.validateUserEmail(user.getId(), savedUser.getEmailActivationToken()); + + + + PollenUser reloadedUser = service.getUser("pollen_user_tony"); + Assert.assertTrue(reloadedUser.isEmailActivated()); + + } }