View Javadoc

1   package net.ramapuram.thomas.service.impl;
2   
3   import net.ramapuram.thomas.dao.UserDao;
4   import net.ramapuram.thomas.model.User;
5   import net.ramapuram.thomas.service.UserExistsException;
6   import net.ramapuram.thomas.service.UserManager;
7   import net.ramapuram.thomas.service.UserService;
8   import org.springframework.beans.factory.annotation.Autowired;
9   import org.springframework.dao.DataIntegrityViolationException;
10  import org.springframework.orm.jpa.JpaSystemException;
11  import org.springframework.security.authentication.encoding.PasswordEncoder;
12  import org.springframework.security.core.userdetails.UsernameNotFoundException;
13  import org.springframework.stereotype.Service;
14  
15  import javax.jws.WebService;
16  import java.util.List;
17  
18  
19  /**
20   * Implementation of UserManager interface.
21   *
22   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
23   */
24  @Service("userManager")
25  @WebService(serviceName = "UserService", endpointInterface = "net.ramapuram.thomas.service.UserService")
26  public class UserManagerImpl extends GenericManagerImpl<User, Long> implements UserManager, UserService {
27      private PasswordEncoder passwordEncoder;
28      private UserDao userDao;
29  
30      @Autowired
31      public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
32          this.passwordEncoder = passwordEncoder;
33      }
34  
35      @Autowired
36      public void setUserDao(UserDao userDao) {
37          this.dao = userDao;
38          this.userDao = userDao;
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      public User getUser(String userId) {
45          return userDao.get(new Long(userId));
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public List<User> getUsers() {
52          return userDao.getAllDistinct();
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      public User saveUser(User user) throws UserExistsException {
59  
60          if (user.getVersion() == null) {
61              // if new user, lowercase userId
62              user.setUsername(user.getUsername().toLowerCase());
63          }
64  
65          // Get and prepare password management-related artifacts
66          boolean passwordChanged = false;
67          if (passwordEncoder != null) {
68              // Check whether we have to encrypt (or re-encrypt) the password
69              if (user.getVersion() == null) {
70                  // New user, always encrypt
71                  passwordChanged = true;
72              } else {
73                  // Existing user, check password in DB
74                  String currentPassword = userDao.getUserPassword(user.getUsername());
75                  if (currentPassword == null) {
76                      passwordChanged = true;
77                  } else {
78                      if (!currentPassword.equals(user.getPassword())) {
79                          passwordChanged = true;
80                      }
81                  }
82              }
83  
84              // If password was changed (or new user), encrypt it
85              if (passwordChanged) {
86                  user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
87              }
88          } else {
89              log.warn("PasswordEncoder not set, skipping password encryption...");
90          }
91  
92          try {
93              return userDao.saveUser(user);
94          } catch (DataIntegrityViolationException e) {
95              //e.printStackTrace();
96              log.warn(e.getMessage());
97              throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
98          } catch (JpaSystemException e) { // needed for JPA
99              //e.printStackTrace();
100             log.warn(e.getMessage());
101             throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
102         }
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     public void removeUser(String userId) {
109         log.debug("removing user: " + userId);
110         userDao.remove(new Long(userId));
111     }
112 
113     /**
114      * {@inheritDoc}
115      *
116      * @param username the login name of the human
117      * @return User the populated user object
118      * @throws UsernameNotFoundException thrown when username not found
119      */
120     public User getUserByUsername(String username) throws UsernameNotFoundException {
121         return (User) userDao.loadUserByUsername(username);
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     public List<User> search(String searchTerm) {
128         return super.search(searchTerm, User.class);
129     }
130 }