Blame view

src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java 2.06 KB
a800fde0   jcartign   First version, in...
1
2
3
4
  package fr.plil.sio.persistence.jdbc;
  
  import fr.plil.sio.persistence.api.Right;
  import fr.plil.sio.persistence.api.RightService;
a800fde0   jcartign   First version, in...
5
6
  import org.springframework.stereotype.Service;
  
5418cb05   jcartign   Javadoc of API is...
7
  import java.util.List;
a54db7eb   msahmane   Ajout des fichier...
8
  import org.springframework.beans.factory.annotation.Autowired;
5418cb05   jcartign   Javadoc of API is...
9
  
a800fde0   jcartign   First version, in...
10
  @Service
292aec6f   jcartign   First version bef...
11
  public class RightServiceJdbc implements RightService {
a54db7eb   msahmane   Ajout des fichier...
12
13
14
15
16
      
      @Autowired
      private RightRepository rightRepository;
      
      
a800fde0   jcartign   First version, in...
17
18
      @Override
      public Right create(String name) {
a54db7eb   msahmane   Ajout des fichier...
19
20
        if (name == null) {
              throw new IllegalArgumentException("name cannot be null");
60e63100   msahmane   Test des Right à ...
21
22
          }                
          Right right = new Right();
a54db7eb   msahmane   Ajout des fichier...
23
24
25
          right.setName(name);
          rightRepository.save(right);
          return right;
a800fde0   jcartign   First version, in...
26
27
28
29
      }
  
      @Override
      public Right create(String name, Right parent) {
60e63100   msahmane   Test des Right à ...
30
          if(name == null)
a54db7eb   msahmane   Ajout des fichier...
31
              throw new IllegalArgumentException("Name cannot be null");
60e63100   msahmane   Test des Right à ...
32
33
34
35
36
37
          if(parent == null)
              throw new IllegalArgumentException("Parent cannot be null");
          if(rightRepository.findOne(parent.getId()) == null){
              throw new IllegalArgumentException("Parent doesn't exist in table");
          }
          Right right = new Right();
a54db7eb   msahmane   Ajout des fichier...
38
          right.setName(name);
60e63100   msahmane   Test des Right à ...
39
          right.setParent(parent);
a54db7eb   msahmane   Ajout des fichier...
40
41
42
          rightRepository.save(right);
          
          return right;
a800fde0   jcartign   First version, in...
43
44
45
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
46
      public boolean delete(Right right) {
60e63100   msahmane   Test des Right à ...
47
48
49
50
51
52
53
54
55
          if(right == null)
              throw new IllegalArgumentException("Right cannot be null");
          
          List<Right> r = rightRepository.findByName(right.getName());
          if(!r.isEmpty()){
              rightRepository.delete(right.getId());
              return true;
          }
          return false;
a800fde0   jcartign   First version, in...
56
57
58
      }
  
      @Override
5418cb05   jcartign   Javadoc of API is...
59
      public List<Right> findByName(String name) {
60e63100   msahmane   Test des Right à ...
60
61
62
63
64
          if(name == null)
              throw new IllegalArgumentException("Name cannot be null");
          
          return rightRepository.findByName(name);
          
5418cb05   jcartign   Javadoc of API is...
65
66
67
68
      }
  
      @Override
      public Right findOne(Long id) {
60e63100   msahmane   Test des Right à ...
69
70
71
72
          if(id == null)
              throw new IllegalArgumentException("Id cannot be null");
          
         return rightRepository.findOne(id);
a800fde0   jcartign   First version, in...
73
74
      }
  }