Blame view

src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java 4.99 KB
a800fde0   jcartign   First version, in...
1
2
  package fr.plil.sio.persistence.jdbc;
  
7940aa22   jcartign   Finishing API jav...
3
  import fr.plil.sio.persistence.api.Group;
60e63100   msahmane   Test des Right à ...
4
  import fr.plil.sio.persistence.api.Right;
a800fde0   jcartign   First version, in...
5
6
7
8
9
10
11
12
13
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import javax.sql.DataSource;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
4e3e24c9   msahmane   Amélioration des ...
14
  import java.util.List;
a800fde0   jcartign   First version, in...
15
16
  
  @Repository
7940aa22   jcartign   Finishing API jav...
17
  public class GroupRepositoryJdbc implements GroupRepository {
a800fde0   jcartign   First version, in...
18
  
7940aa22   jcartign   Finishing API jav...
19
      private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
a800fde0   jcartign   First version, in...
20
21
22
23
24
  
      @Autowired
      private DataSource dataSource;
  
      @Override
7940aa22   jcartign   Finishing API jav...
25
      public Group findByName(String name) {
a800fde0   jcartign   First version, in...
26
27
28
29
30
31
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
              rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'");
              if (rs.next()) {
7940aa22   jcartign   Finishing API jav...
32
33
34
35
36
                  logger.debug("found group " + name);
                  Group group = new Group();
                  group.setId(rs.getLong("GROUP_ID"));
                  group.setName(rs.getString("NAME_C"));
                  return group;
a800fde0   jcartign   First version, in...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
              } else {
                  logger.debug("not found " + name);
                  return null;
              }
          } 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
7940aa22   jcartign   Finishing API jav...
59
      public void save(Group group) {
a800fde0   jcartign   First version, in...
60
61
62
63
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
7940aa22   jcartign   Finishing API jav...
64
              stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')",
a800fde0   jcartign   First version, in...
65
66
                      Statement.RETURN_GENERATED_KEYS);
              rs = stmt.getGeneratedKeys();
4e3e24c9   msahmane   Amélioration des ...
67
              if (rs.next()) {                
7940aa22   jcartign   Finishing API jav...
68
                  group.setId(rs.getLong(1));
a800fde0   jcartign   First version, in...
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
              } 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);
              }
          }
  
      }
7940aa22   jcartign   Finishing API jav...
88
89
90
  
      @Override
      public void delete(Long id) {
74b95d8e   msahmane   --no commit message
91
92
93
94
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
31bdbe1b   msahmane   Version où les te...
95
              stmt.executeUpdate("DELETE FROM GROUP_T WHERE GROUP_ID="+id,
74b95d8e   msahmane   --no commit message
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                      Statement.RETURN_GENERATED_KEYS);
              rs = stmt.getGeneratedKeys();
         
          } 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);
              }
          }
7940aa22   jcartign   Finishing API jav...
113
      }
60e63100   msahmane   Test des Right à ...
114
115
116
      
      @Override
      public boolean addRight(String groupName, Right right){
4e3e24c9   msahmane   Amélioration des ...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
          Statement stmt = null;
          ResultSet rs = null;
          try{
              stmt = dataSource.getConnection().createStatement();
              if(this.findByName(groupName) != null && right.getId() != null)
                  stmt.executeUpdate("INSERT INTO GROUP_RIGHTS VALUES("+this.findByName(groupName).getId()+","+right.getId()+")",
                          Statement.RETURN_GENERATED_KEYS);
              rs = stmt.getGeneratedKeys();
              if(rs.next()){
                  List<Right> rights = this.findByName(groupName).getRights();
                  rights.add(right);
                  this.findByName(groupName).setRights(rights);
                  return true;                
              }else{
                  return false;
              }
          }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);
              }
          }
60e63100   msahmane   Test des Right à ...
147
148
149
150
151
152
      }
      
      public boolean removeRight(String groupName, Right right){
          
          return false;
      }
a800fde0   jcartign   First version, in...
153
  }