Blame view

src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java 2.49 KB
fd5e74ca   jcartign   First student ver...
1
2
  package fr.plil.sio.persistence.jpa;
  
fd5e74ca   jcartign   First student ver...
3
4
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.RightService;
9ae0ef87   msahmane   Amélioration des ...
5
  import java.util.ArrayList;
fd5e74ca   jcartign   First student ver...
6
7
8
  import org.springframework.stereotype.Service;
  
  import java.util.List;
9ae0ef87   msahmane   Amélioration des ...
9
  import org.springframework.beans.factory.annotation.Autowired;
fd5e74ca   jcartign   First student ver...
10
11
12
  
  @Service
  public class RightServiceJpa implements RightService {
9ae0ef87   msahmane   Amélioration des ...
13
14
15
16
      
      @Autowired
      private RightRepository rightRepository;
      
fd5e74ca   jcartign   First student ver...
17
18
      @Override
      public Right create(String name) {
9ae0ef87   msahmane   Amélioration des ...
19
20
21
22
23
24
25
26
27
28
29
30
31
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
          
          List<Right> rights = new ArrayList<>();
          
          Right right = new Right();
          right.setName(name);
          
          rights.add(right);
          rightRepository.save(rights);
          
          return right;
fd5e74ca   jcartign   First student ver...
32
33
34
35
      }
  
      @Override
      public Right create(String name, Right parent) {
9ae0ef87   msahmane   Amélioration des ...
36
37
38
          if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
          }
fb8c34cc   msahmane   RightJPA à 100%, ...
39
40
41
          if(parent == null){
              throw new IllegalArgumentException("parent cannot be null");
          }
9ae0ef87   msahmane   Amélioration des ...
42
43
44
45
46
         
          List<Right> rights = new ArrayList<>();
          
          Right right = new Right();
          right.setName(name);
fb8c34cc   msahmane   RightJPA à 100%, ...
47
48
49
50
51
52
53
          
          if(rightRepository.findByName(parent.getName()).isEmpty()){
              throw new IllegalArgumentException("parent name cannot be null");
          }
              
          
          
9ae0ef87   msahmane   Amélioration des ...
54
55
56
57
          right.setParent(parent);
          rights.add(right);
          rightRepository.save(rights);
          
fb8c34cc   msahmane   RightJPA à 100%, ...
58
59
          parent.setSiblings(rights);
          
9ae0ef87   msahmane   Amélioration des ...
60
          return right;
fd5e74ca   jcartign   First student ver...
61
62
63
64
      }
  
      @Override
      public boolean delete(Right right) {
9ae0ef87   msahmane   Amélioration des ...
65
66
67
          if(right == null)
              throw new IllegalArgumentException("Right cannot be null");
         
fb8c34cc   msahmane   RightJPA à 100%, ...
68
69
70
71
72
73
          if(right.getId() == null)
              throw new IllegalArgumentException("Id cannot be null");
          
          if(rightRepository.findByName(right.getName()).isEmpty())
              return false;
          
9ae0ef87   msahmane   Amélioration des ...
74
75
          rightRepository.delete(right);        
          return true;
fd5e74ca   jcartign   First student ver...
76
77
78
      }
  
      @Override
9ae0ef87   msahmane   Amélioration des ...
79
80
81
82
      public List<Right> findByName(String name) {        
           if(name == null)
              throw new IllegalArgumentException("Name cannot be null");
                   
fb8c34cc   msahmane   RightJPA à 100%, ...
83
         return rightRepository.findByName(name);      
9ae0ef87   msahmane   Amélioration des ...
84
          
fd5e74ca   jcartign   First student ver...
85
86
87
88
      }
  
      @Override
      public Right findOne(Long id) {
9ae0ef87   msahmane   Amélioration des ...
89
90
91
92
93
          
          if(id == null)
              throw new IllegalArgumentException("Id cannot be null");        
          
          return rightRepository.findOne(id);
fd5e74ca   jcartign   First student ver...
94
95
      }
  }