Blame view

src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java 5.18 KB
ef4eb263   rvangrev   Nouvelles modific...
1
2
3
4
5
6
7
  /*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package fr.plil.sio.persistence.jdbc;
  
fa02d31b   rvangrev   Commit avant le m...
8
9
  import java.util.List;
  import java.util.ArrayList;
ef4eb263   rvangrev   Nouvelles modific...
10
11
12
13
14
15
16
17
18
  import fr.plil.sio.persistence.api.Group;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import javax.sql.DataSource;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import fr.plil.sio.persistence.api.Right;
fa02d31b   rvangrev   Commit avant le m...
19
  import org.springframework.stereotype.Repository;
ef4eb263   rvangrev   Nouvelles modific...
20
  
fa02d31b   rvangrev   Commit avant le m...
21
22
23
  @Repository
  public class RightRepositoryJdbc implements RightRepository {
      
fa02d31b   rvangrev   Commit avant le m...
24
      private GroupRepository groupRepository;
73c004db   rvangrev   TP presque termin...
25
      
fa02d31b   rvangrev   Commit avant le m...
26
      private RightRepository rightRepository;
ef4eb263   rvangrev   Nouvelles modific...
27
28
29
30
31
32
33
      
       private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
  
      @Autowired
      private DataSource dataSource;
      
      @Override
fa02d31b   rvangrev   Commit avant le m...
34
      public List<Right> findByName(String name) {
ef4eb263   rvangrev   Nouvelles modific...
35
36
37
38
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
fa02d31b   rvangrev   Commit avant le m...
39
40
41
              rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE NAME_R = \'" + name + "\'");
              ArrayList<Right> list = new ArrayList<Right>();
              while (rs.next()) {
ef4eb263   rvangrev   Nouvelles modific...
42
                  logger.debug("found group " + name);
fa02d31b   rvangrev   Commit avant le m...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                  Right right = new Right();
                  right.setId(rs.getLong("RIGHT_ID"));
                  right.setName(rs.getString("NAME_R"));
                  if(right!=null){
                      list.add(right);
                  }
              }
              return list;
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
  
ef4eb263   rvangrev   Nouvelles modific...
64
              }
fa02d31b   rvangrev   Commit avant le m...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
          }
      }
      
      @Override
      public Right findOne(Long id){
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
              rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' LIMIT 1");
  
              if (rs.next()) {
                  logger.debug("found id " + id);
                  Right right = new Right();
                  right.setId(rs.getLong("RIGHT_ID"));
                  right.setName(rs.getString("NAME_R"));
                  return right;
              }
  
ef4eb263   rvangrev   Nouvelles modific...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
  
              }
          }
          return null;
      }
  
      @Override
fa02d31b   rvangrev   Commit avant le m...
103
      public void save(Right right, int parentId) {
ef4eb263   rvangrev   Nouvelles modific...
104
105
106
107
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
fa02d31b   rvangrev   Commit avant le m...
108
109
              
              stmt.executeUpdate("INSERT INTO RIGHT_T (NAME_R, PARENT_ID) VALUES (\'" + right.getName() + "\', "+ parentId +")",
ef4eb263   rvangrev   Nouvelles modific...
110
                      Statement.RETURN_GENERATED_KEYS);
fa02d31b   rvangrev   Commit avant le m...
111
    
ef4eb263   rvangrev   Nouvelles modific...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
              rs = stmt.getGeneratedKeys();
              if (rs.next()) {
                  right.setId(rs.getLong(1));
              } else {
                  throw new UnsupportedOperationException("default in key access");
              }
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
              }
          }
  
      }
  
      @Override
fa02d31b   rvangrev   Commit avant le m...
136
      public boolean delete(Long id) {
ef4eb263   rvangrev   Nouvelles modific...
137
138
139
          Statement stmt = null;
          ResultSet rs = null;
          if(id==null){
fa02d31b   rvangrev   Commit avant le m...
140
141
142
               return false;
          }
          if(findOne(id)==null){
73c004db   rvangrev   TP presque termin...
143
               throw new IllegalArgumentException("droit inexistant");
ef4eb263   rvangrev   Nouvelles modific...
144
145
146
          }
          try {
              stmt = dataSource.getConnection().createStatement();
fa02d31b   rvangrev   Commit avant le m...
147
              stmt.execute("DELETE FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' OR PARENT_ID = " + id + "");
ef4eb263   rvangrev   Nouvelles modific...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
          } catch (SQLException e) {
              throw new UnsupportedOperationException("sql exception", e);
          } finally {
              try {
                  if (rs != null) {
                      rs.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
                  throw new UnsupportedOperationException("sql exception during close", e);
              }
          }
fa02d31b   rvangrev   Commit avant le m...
162
          return true;
ef4eb263   rvangrev   Nouvelles modific...
163
164
      }
  }