Blame view

src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java 2.7 KB
a800fde0   jcartign   First version, in...
1
2
3
  package fr.plil.sio.persistence.jdbc;
  
  import fr.plil.sio.persistence.api.Right;
ef4eb263   rvangrev   Nouvelles modific...
4
  import fr.plil.sio.persistence.api.Group;
a800fde0   jcartign   First version, in...
5
6
  import fr.plil.sio.persistence.api.User;
  import fr.plil.sio.persistence.api.UserService;
73c004db   rvangrev   TP presque termin...
7
8
9
10
11
12
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import javax.sql.DataSource;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
a800fde0   jcartign   First version, in...
13
14
15
16
17
18
19
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  @Service
  public class UserServiceJdbc implements UserService {
  
      @Autowired
ef4eb263   rvangrev   Nouvelles modific...
20
      private UserRepository userRepository;
fa02d31b   rvangrev   Commit avant le m...
21
22
      
      @Autowired
ef4eb263   rvangrev   Nouvelles modific...
23
      private GroupRepository groupRepository;
73c004db   rvangrev   TP presque termin...
24
25
26
      
      @Autowired
      private RightRepository rightRepository;
a800fde0   jcartign   First version, in...
27
  
73c004db   rvangrev   TP presque termin...
28
29
30
31
32
      @Autowired
      private DataSource dataSource;
      
      private static final Logger logger = LoggerFactory.getLogger(UserRepository.class);
      
a800fde0   jcartign   First version, in...
33
      @Override
5418cb05   jcartign   Javadoc of API is...
34
      public User create(String name, String groupName) {
e1ed8749   RĂ©mi Vangrevelynghe   save before merge
35
          if(name==null || groupName==null){
ef4eb263   rvangrev   Nouvelles modific...
36
37
              throw new IllegalArgumentException("name cannot be null");
          }
73c004db   rvangrev   TP presque termin...
38
39
          Group group = new Group();
          group = groupRepository.findByName(groupName);
ef4eb263   rvangrev   Nouvelles modific...
40
          User user = userRepository.findByName(name);
73c004db   rvangrev   TP presque termin...
41
42
43
          if(group==null){
              throw new IllegalArgumentException("group does not exist");
          }
ef4eb263   rvangrev   Nouvelles modific...
44
          if(user!=null){
73c004db   rvangrev   TP presque termin...
45
              throw new IllegalStateException("a user with the same name already exists");
ef4eb263   rvangrev   Nouvelles modific...
46
47
48
          }
          user = new User();
          user.setName(name);
73c004db   rvangrev   TP presque termin...
49
          
fa02d31b   rvangrev   Commit avant le m...
50
51
          if(group!=null)
              user.setGroup(group);
73c004db   rvangrev   TP presque termin...
52
       
ef4eb263   rvangrev   Nouvelles modific...
53
54
          userRepository.save(user, group);
          return user;
a800fde0   jcartign   First version, in...
55
56
57
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
58
      public boolean delete(String name) {
ef4eb263   rvangrev   Nouvelles modific...
59
          if(name == null){
fa02d31b   rvangrev   Commit avant le m...
60
              throw new IllegalArgumentException("name cannot be null");
ef4eb263   rvangrev   Nouvelles modific...
61
62
63
64
65
66
67
          }
          User user = findByName(name);
          if(user==null){
              return false;
          }
          userRepository.delete(user.getId());
          return true;
a800fde0   jcartign   First version, in...
68
69
70
71
      }
  
      @Override
      public User findByName(String name) {
ef4eb263   rvangrev   Nouvelles modific...
72
73
74
75
          if(name==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          return userRepository.findByName(name);
a800fde0   jcartign   First version, in...
76
77
      }
  
5418cb05   jcartign   Javadoc of API is...
78
79
      @Override
      public boolean isUserHasRight(String userName, Right right) {
fa02d31b   rvangrev   Commit avant le m...
80
          if(userName==null){
ef4eb263   rvangrev   Nouvelles modific...
81
82
              throw new IllegalArgumentException("name cannot be null");
          }
fa02d31b   rvangrev   Commit avant le m...
83
84
85
          if(right==null){
              throw new IllegalArgumentException("right cannot be null");
          }
73c004db   rvangrev   TP presque termin...
86
87
          if(rightRepository.findByName(right.getName()).size()==0){
              throw new IllegalArgumentException("right not in database");
ef4eb263   rvangrev   Nouvelles modific...
88
          }
73c004db   rvangrev   TP presque termin...
89
          return userRepository.isUserHasRight(userRepository.findByName(userName).getId(), right.getId());
a800fde0   jcartign   First version, in...
90
91
      }
  }