Blame view

src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java 2.42 KB
a800fde0   jcartign   First version, in...
1
2
3
4
5
  package fr.plil.sio.persistence.jdbc;
  
  import fr.plil.sio.persistence.api.Group;
  import fr.plil.sio.persistence.api.GroupService;
  import fr.plil.sio.persistence.api.Right;
7940aa22   jcartign   Finishing API jav...
6
  import org.springframework.beans.factory.annotation.Autowired;
a800fde0   jcartign   First version, in...
7
  import org.springframework.stereotype.Service;
ef4eb263   rvangrev   Nouvelles modific...
8
9
  import java.util.List;
  import java.util.LinkedList;
a800fde0   jcartign   First version, in...
10
11
12
  
  @Service
  public class GroupServiceJdbc implements GroupService {
7940aa22   jcartign   Finishing API jav...
13
14
15
16
  
      @Autowired
      private GroupRepository groupRepository;
  
a800fde0   jcartign   First version, in...
17
18
      @Override
      public Group create(String name) {
7940aa22   jcartign   Finishing API jav...
19
20
21
22
23
24
25
26
27
28
29
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
          Group group = groupRepository.findByName(name);
          if (group != null) {
              throw new IllegalStateException("a group with the same name already exists");
          }
          group = new Group();
          group.setName(name);
          groupRepository.save(group);
          return group;
a800fde0   jcartign   First version, in...
30
31
32
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
33
      public boolean delete(String name) {
ef4eb263   rvangrev   Nouvelles modific...
34
35
36
37
38
39
40
41
42
43
44
45
46
           
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
          
          Group group = findByName(name);
          
          if(group==null){
              return false;
          }
          
          groupRepository.delete(group.getId());
          return true;
a800fde0   jcartign   First version, in...
47
48
49
50
      }
  
      @Override
      public Group findByName(String name) {
7940aa22   jcartign   Finishing API jav...
51
52
53
54
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
          return groupRepository.findByName(name);
a800fde0   jcartign   First version, in...
55
56
57
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
58
      public boolean addRight(String groupName, Right right) {
ef4eb263   rvangrev   Nouvelles modific...
59
60
61
62
63
64
65
66
67
68
69
70
71
          if(groupName==null || right ==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          Group group = groupRepository.findByName(groupName);
          if (group == null) {
              throw new IllegalArgumentException("group cannot be null");
          }
          
          group.setName(groupName);
          List<Right> list = group.getRights();
          list.add(right);
          group.setRights(list);
          return true;
a800fde0   jcartign   First version, in...
72
73
74
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
75
      public boolean removeRight(String groupName, Right right) {
ef4eb263   rvangrev   Nouvelles modific...
76
77
78
79
80
81
82
83
          if(right==null){
              throw new IllegalArgumentException("name cannot be null");
          }
          Group group = groupRepository.findByName(groupName);
          if (group != null) {
              throw new IllegalStateException("a group with the same name already exists");
          }
          return true;
a800fde0   jcartign   First version, in...
84
85
      }
  }