Blame view

src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java 3.14 KB
fd5e74ca   jcartign   First student ver...
1
2
3
4
5
  package fr.plil.sio.persistence.jpa;
  
  import fr.plil.sio.persistence.api.Group;
  import fr.plil.sio.persistence.api.GroupService;
  import fr.plil.sio.persistence.api.Right;
2b83e939   rvangrev   TP terminé
6
7
  import java.util.ArrayList;
  import java.util.List;
fd5e74ca   jcartign   First student ver...
8
9
10
11
12
13
14
15
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  @Service
  public class GroupServiceJpa implements GroupService {
  
      @Autowired
      private GroupRepository groupRepository;
2b83e939   rvangrev   TP terminé
16
17
18
19
20
21
22
      
      @Autowired
      private RightRepository rightRepository;
      
      @Autowired
      private UserRepository userRepository;
              
fd5e74ca   jcartign   First student ver...
23
24
25
26
27
28
29
30
31
32
33
      @Override
      public Group create(String name) {
          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);
2b83e939   rvangrev   TP terminé
34
          
fd5e74ca   jcartign   First student ver...
35
          groupRepository.save(group);
2b83e939   rvangrev   TP terminé
36
          
fd5e74ca   jcartign   First student ver...
37
38
39
40
41
          return group;
      }
  
      @Override
      public boolean delete(String name) {
2b83e939   rvangrev   TP terminé
42
43
44
45
46
47
48
49
50
51
52
53
54
          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;
fd5e74ca   jcartign   First student ver...
55
56
57
58
59
60
61
62
63
64
65
66
      }
  
      @Override
      public Group findByName(String name) {
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
          return groupRepository.findByName(name);
      }
  
      @Override
      public boolean addRight(String groupName, Right right) {
2b83e939   rvangrev   TP terminé
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
          if(groupName==null){
              throw new IllegalArgumentException("groupname cannot be null");
          }
          if(right == null){
             throw new IllegalArgumentException("right cannot be null");
          }
                  
          Group group = groupRepository.findByName(groupName);
          
          if (group == null) {
              throw new IllegalArgumentException("group cannot be null");
          }
          
          List<Right> list = group.getRights();
          if(list.contains(right)){
              return false;
          }
          assert list != null;
          list.add(right);
          group.setRights(list);
          
          return true;
fd5e74ca   jcartign   First student ver...
89
90
91
92
      }
  
      @Override
      public boolean removeRight(String groupName, Right right) {
2b83e939   rvangrev   TP terminé
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
          if(groupName==null){
              throw new IllegalArgumentException("Groupname cannot be null");
          }
          if(right==null){
              throw new IllegalArgumentException("right cannot be null");
          }
          if(rightRepository.findByName(right.getName()).size()==0){
              throw new IllegalArgumentException("right not in database");
          }
          Group group = groupRepository.findByName(groupName);
          
          if (group == null) {
              throw new IllegalArgumentException("group cannot be null");
          }
          
          List<Right> list = group.getRights();
          list.remove(right);
          group.setRights(list);
          
          return true;
fd5e74ca   jcartign   First student ver...
113
114
      }
  }