View Javadoc

1   package com.hack23.cia.service.user.impl;
2   
3   import javax.persistence.EntityManager;
4   import javax.persistence.PersistenceContext;
5   
6   import org.springframework.stereotype.Service;
7   import org.springframework.transaction.annotation.Propagation;
8   import org.springframework.transaction.annotation.Transactional;
9   
10  import com.hack23.cia.model.user.api.User;
11  import com.hack23.cia.service.user.api.NoSuchUser;
12  import com.hack23.cia.service.user.api.NoSuchUserException;
13  import com.hack23.cia.service.user.api.UserService;
14  
15  @Transactional
16  @Service(value = "UserService")
17  public class UserServiceImpl implements UserService {
18  
19  	/*** The entity manager. */
20  	@PersistenceContext(name = "com.hack23.cia.model.user.api")
21  	private EntityManager entityManager;
22  
23  	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
24  	public void deleteUserById(final Integer userId) throws NoSuchUserException {
25  		final User user = this.entityManager.find(User.class, userId);
26  		this.entityManager.remove(user);
27  	}
28  
29  	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
30  	public User getUserById(final Integer userId) throws NoSuchUserException {
31  
32  		final User user = this.entityManager.find(User.class, userId);
33  
34  		if (user == null) {
35  			NoSuchUser noSuchUser = new NoSuchUser();
36  			noSuchUser.setUserId(userId);
37  			throw new NoSuchUserException(
38  					"Did not find any matching user for id [" + userId + "].",
39  					noSuchUser);
40  
41  		} else {
42  			return user;
43  		}
44  	}
45  
46  	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
47  	public Integer updateUser(User user) {
48  		final User mergedUser = this.entityManager.merge(user);
49  		return mergedUser.getModelObjectId();
50  	}
51  
52  }