Coverage Report - org.directdemocracyportal.democracy.service.UserServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UserServiceImpl
27%
20/73
11%
2/18
2.2
 
 1  
 /*
 2  
 Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
 3  
         $Id
 4  
 */
 5  
 package org.directdemocracyportal.democracy.service;
 6  
 
 7  
 import java.util.Date;
 8  
 import java.util.List;
 9  
 
 10  
 import org.directdemocracyportal.democracy.model.application.Action;
 11  
 import org.directdemocracyportal.democracy.model.application.Authority;
 12  
 import org.directdemocracyportal.democracy.model.application.Event;
 13  
 import org.directdemocracyportal.democracy.model.application.Portal;
 14  
 import org.directdemocracyportal.democracy.model.application.SecurityRoleType;
 15  
 import org.directdemocracyportal.democracy.model.application.User;
 16  
 import org.directdemocracyportal.democracy.model.application.UserSession;
 17  
 import org.directdemocracyportal.democracy.model.world.Person;
 18  
 import org.directdemocracyportal.democracy.model.world.Region;
 19  
 import org.directdemocracyportal.democracy.model.world.RegionType;
 20  
 import org.directdemocracyportal.democracy.service.command.RegisterCommand;
 21  
 import org.directdemocracyportal.democracy.service.dao.PersonDAO;
 22  
 import org.directdemocracyportal.democracy.service.dao.PortalDAO;
 23  
 import org.directdemocracyportal.democracy.service.dao.UserDAO;
 24  
 import org.directdemocracyportal.democracy.service.dao.UserSessionDAO;
 25  
 import org.springframework.mail.MailException;
 26  
 import org.springframework.mail.SimpleMailMessage;
 27  
 import org.springframework.mail.javamail.JavaMailSenderImpl;
 28  
 import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
 29  
 import org.springframework.security.authentication.encoding.PasswordEncoder;
 30  
 import org.springframework.transaction.annotation.Propagation;
 31  
 import org.springframework.transaction.annotation.Transactional;
 32  
 
 33  
 /**
 34  
  * The Class UserServiceImpl.
 35  
  */
 36  
 @Transactional(propagation = Propagation.REQUIRED)
 37  
 public class UserServiceImpl implements UserService
 38  
 {
 39  
 
 40  
     /** The java mail sender impl. */
 41  
     final private JavaMailSenderImpl javaMailSenderImpl;
 42  
 
 43  
     /** The simple mail message. */
 44  
     final private SimpleMailMessage simpleMailMessage;
 45  
 
 46  
     /** The user dao. */
 47  
     final private UserDAO userDAO;
 48  
 
 49  
     /** The portal dao. */
 50  
     final private PortalDAO portalDAO;
 51  
 
 52  
     /** The person dao. */
 53  
     final private PersonDAO personDAO;
 54  
 
 55  
     /** The user session dao. */
 56  
     final private UserSessionDAO userSessionDAO;
 57  
 
 58  
     /** The password encoder. */
 59  
     private final PasswordEncoder passwordEncoder;
 60  
 
 61  
     /**
 62  
      * Instantiates a new user service impl.
 63  
      *
 64  
      * @param userDAO the user dao
 65  
      * @param portalDAO the portal dao
 66  
      * @param personDAO the person dao
 67  
      * @param userSessionDAO the user session dao
 68  
      * @param javaMailSenderImpl the java mail sender impl
 69  
      * @param simpleMailMessage the simple mail message
 70  
      */
 71  
     public UserServiceImpl(UserDAO userDAO, PortalDAO portalDAO,
 72  
             PersonDAO personDAO, UserSessionDAO userSessionDAO,
 73  
             JavaMailSenderImpl javaMailSenderImpl,
 74  4
             SimpleMailMessage simpleMailMessage) {
 75  4
         this.userDAO = userDAO;
 76  4
         this.portalDAO = portalDAO;
 77  4
         this.personDAO = personDAO;
 78  4
         this.userSessionDAO = userSessionDAO;
 79  4
         this.javaMailSenderImpl = javaMailSenderImpl;
 80  4
         this.simpleMailMessage = simpleMailMessage;
 81  4
         this.passwordEncoder = new Md5PasswordEncoder();
 82  4
     }
 83  
 
 84  
     /**
 85  
      * Adds the member.
 86  
      *
 87  
      * @param portal the portal
 88  
      * @param person the person
 89  
      */
 90  
     private void addMember(Portal portal, Person person) {
 91  0
         portal.addMember(person);
 92  0
         personDAO.save(person);
 93  0
         portalDAO.save(portal);
 94  0
     }
 95  
 
 96  
     /*
 97  
      * (non-Javadoc)
 98  
      *
 99  
      * @see org.directdemocracyportal.democracy.service.UserService#register(org.directdemocracyportal.democracy.service.command.RegisterCommand,
 100  
      *      java.lang.String)
 101  
      */
 102  
     public User register(RegisterCommand command, String sessionId)
 103  
             throws UserAlreadyExistException {
 104  1
         if (this.userDAO.findByUsername(command.getUsername()) == null) {
 105  
 
 106  0
             Person person = new Person(command.getName());
 107  0
             this.personDAO.save(person);
 108  
 
 109  0
             Portal localPortal = this.portalDAO.load(command.getPortalId());
 110  0
             if (localPortal != null) {
 111  0
                 addMember(localPortal, person);
 112  
             }
 113  
 
 114  0
             Region region = localPortal.getRegion().getParent();
 115  0
             if (region.getRegionType().equals(RegionType.Regional)) {
 116  0
                 Portal regionalPortal = portalDAO.findRegionalByRegion(region);
 117  0
                 if (regionalPortal != null) {
 118  0
                     addMember(regionalPortal, person);
 119  
                 }
 120  
             }
 121  
 
 122  0
             Portal nationalPortal = portalDAO
 123  
                     .findNationalByCountry(localPortal.getCountry());
 124  0
             if (nationalPortal != null) {
 125  0
                 addMember(nationalPortal, person);
 126  
             }
 127  
 
 128  0
             Portal globalPortal = portalDAO
 129  
                     .findByName("Direct Democracy Portal");
 130  0
             if (globalPortal != null) {
 131  0
                 addMember(globalPortal, person);
 132  
             }
 133  
 
 134  0
             String encodedPassword = passwordEncoder.encodePassword(command
 135  
                     .getPassword(), null);
 136  
 
 137  0
             User newUser = new User(new Date(), command.getUsername(),
 138  
                     encodedPassword,command.getEmail(), person);
 139  0
             newUser.setAccountNonExpired(true);
 140  0
             newUser.setCredentialsNonExpired(true);
 141  0
             newUser.setEnabled(true);
 142  0
             newUser.setAccountNonLocked(true);
 143  0
             newUser.getEvents().add(
 144  
                     new Event(newUser, Action.Register, new Date()));
 145  
 
 146  0
             Authority authority = new Authority(SecurityRoleType.ROLE_USER);
 147  0
             newUser.getGrantedAuthorities().add(authority);
 148  
 
 149  0
             UserSession userSession = new UserSession(newUser, sessionId,
 150  
                     new Date());
 151  0
             userSession.setActive(true);
 152  0
             this.userSessionDAO.save(userSession);
 153  
 
 154  0
             return newUser;
 155  
         } else {
 156  1
             throw new UserAlreadyExistException();
 157  
         }
 158  
     }
 159  
 
 160  
     /**
 161  
      * Send email confirmation request.
 162  
      *
 163  
      * @param command the command
 164  
      */
 165  
     public void sendEmailConfirmationRequest(RegisterCommand command) {
 166  0
         SimpleMailMessage msg = new SimpleMailMessage(this.simpleMailMessage);
 167  0
         msg.setTo(command.getEmail());
 168  0
         msg.setText("Hello " + command.getName()
 169  
                 + ", thank you for registering. confirm with link below ");
 170  
         try {
 171  0
             this.javaMailSenderImpl.send(msg);
 172  0
         } catch (MailException ex) {
 173  
             // simply log it and go on...
 174  0
             System.err.println(ex.getMessage());
 175  0
         }
 176  0
     }
 177  
 
 178  
     /*
 179  
      * (non-Javadoc)
 180  
      *
 181  
      * @see org.directdemocracyportal.democracy.service.UserService#getUsers()
 182  
      */
 183  
     public List<User> getUsers() {
 184  1
         return this.userDAO.getAll();
 185  
     }
 186  
 
 187  
     /*
 188  
      * (non-Javadoc)
 189  
      *
 190  
      * @see org.directdemocracyportal.democracy.service.UserService#login(java.lang.String,
 191  
      *      java.lang.String, java.lang.String)
 192  
      */
 193  
     public User login(String username, String password, String sessionId) {
 194  0
         String encodedPassword = passwordEncoder.encodePassword(password, null);
 195  0
         User user = userDAO.findByUsername(username);
 196  
 
 197  0
         if (user.getPassword().equals(encodedPassword)) {
 198  0
             if (this.userSessionDAO.findByUserId(user.getId()) == null) {
 199  0
                 user.getEvents().add(new Event(user, Action.Login, new Date()));
 200  0
                 this.userSessionDAO.save(new UserSession(user, sessionId,
 201  
                         new Date()));
 202  0
                 userDAO.save(user);
 203  
             }
 204  
         }
 205  0
         return user;
 206  
     }
 207  
 
 208  
     /*
 209  
      * (non-Javadoc)
 210  
      *
 211  
      * @see org.directdemocracyportal.democracy.service.UserService#logout(org.directdemocracyportal.democracy.model.application.User)
 212  
      */
 213  
     public void logout(User user) {
 214  1
         UserSession userSession = this.userSessionDAO
 215  
                 .findByUserId(user.getId());
 216  1
         if (userSession != null) {
 217  1
             userSession.getUser().getEvents()
 218  
                     .add(
 219  
                             new Event(userSession.getUser(), Action.Logout,
 220  
                                     new Date()));
 221  1
             userSession.setEndedAt(new Date());
 222  1
             userSession.setActive(false);
 223  1
             this.userSessionDAO.save(userSession);
 224  1
             this.userDAO.save(userSession.getUser());
 225  
         }
 226  1
     }
 227  
 
 228  
     /*
 229  
      * (non-Javadoc)
 230  
      *
 231  
      * @see org.directdemocracyportal.democracy.service.UserService#getPortals()
 232  
      */
 233  
     public List<Portal> getPortals() {
 234  0
         return this.portalDAO.getAll();
 235  
     }
 236  
 
 237  
     /*
 238  
      * (non-Javadoc)
 239  
      *
 240  
      * @see org.directdemocracyportal.democracy.service.UserService#endActiveUserSessions()
 241  
      */
 242  
     public void endActiveUserSessions() {
 243  0
         this.userSessionDAO.endActiveSessions();
 244  0
     }
 245  
 
 246  
     /*
 247  
      * (non-Javadoc)
 248  
      *
 249  
      * @see org.directdemocracyportal.democracy.service.UserService#getUser(java.lang.Long)
 250  
      */
 251  
     public User getUser(Long userId) {
 252  0
         return this.userDAO.load(userId);
 253  
     }
 254  
 }