View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service.impl;
24  
25  import java.util.List;
26  
27  import javax.jws.WebService;
28  import javax.persistence.EntityExistsException;
29  
30  import org.springframework.security.userdetails.UsernameNotFoundException;
31  import org.springframework.transaction.annotation.Transactional;
32  import org.springframework.dao.DataIntegrityViolationException;
33  
34  import com.gisgraphy.dao.UserDao;
35  import com.gisgraphy.model.User;
36  import com.gisgraphy.service.UserExistsException;
37  import com.gisgraphy.service.UserManager;
38  import com.gisgraphy.service.UserService;
39  
40  /**
41   * Implementation of UserManager interface.
42   * 
43   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
44   */
45  @WebService(serviceName = "UserService", endpointInterface = "com.gisgraphy.service.UserService")
46  public class UserManagerImpl extends UniversalManagerImpl implements
47  	UserManager, UserService {
48      private UserDao dao;
49  
50      /**
51       * Set the Dao for communication with the data layer.
52       * 
53       * @param dao
54       *                the UserDao that communicates with the database
55       */
56      public void setUserDao(UserDao dao) {
57  	this.dao = dao;
58      }
59  
60      /**
61       * {@inheritDoc}
62       */
63      public User getUser(String userId) {
64  	return dao.get(new Long(userId));
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public List<User> getUsers(User user) {
71  	return dao.getUsers();
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Transactional
78      public User saveUser(User user) throws UserExistsException {
79  	// if new user, lowercase userId
80  	if (user.getVersion() == null) {
81  	    user.setUsername(user.getUsername().toLowerCase());
82  	}
83  
84  	try {
85  	    return dao.saveUser(user);
86  	} catch (DataIntegrityViolationException e) {
87  	    log.warn(e.getMessage());
88  	    throw new UserExistsException("User '" + user.getUsername()
89  		    + "' already exists!");
90  	} catch (EntityExistsException e) { // needed for JPA
91  	    log.warn(e.getMessage());
92  	    throw new UserExistsException("User '" + user.getUsername()
93  		    + "' already exists!");
94  	}
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     public void removeUser(String userId) {
101 	log.debug("removing user: " + userId);
102 	dao.remove(new Long(userId));
103     }
104 
105     /**
106      * {@inheritDoc}
107      * 
108      * @param username
109      *                the login name of the human
110      * @return User the populated user object
111      * @throws UsernameNotFoundException
112      *                 thrown when username not found
113      */
114     public User getUserByUsername(String username)
115 	    throws UsernameNotFoundException {
116 	return (User) dao.loadUserByUsername(username);
117     }
118 }