Blame view

src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java 5.4 KB
ef4eb263   rvangrev   Nouvelles modific...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /*
   * 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;
  
  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 org.springframework.stereotype.Repository;
  import fr.plil.sio.persistence.api.User;
  
  @Repository
  public class UserRepositoryJdbc implements UserRepository{
      
       private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class);
  
      @Autowired
fa02d31b   rvangrev   Commit avant le m...
25
26
27
      private GroupRepository groupRepository;
       
      @Autowired
ef4eb263   rvangrev   Nouvelles modific...
28
29
30
31
32
33
34
35
      private DataSource dataSource;
      
      @Override
      public User findByName(String name) {
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
73c004db   rvangrev   TP presque termin...
36
              rs = stmt.executeQuery("SELECT USER_ID, NAME_U, U.GROUP_ID, NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_ID=G.GROUP_ID WHERE NAME_U = \'" + name + "\'");
ef4eb263   rvangrev   Nouvelles modific...
37
38
39
              if (rs.next()) {
                  logger.debug("found group " + name);
                  User user = new User();
fa02d31b   rvangrev   Commit avant le m...
40
41
                  user.setId(rs.getLong("USER_ID"));
                  user.setName(rs.getString("NAME_U"));
f540a4bc   rvangrev   Fin TP
42
43
44
                  //On ajoute le groupe, sauf si il est nul
                  if(groupRepository.findByName(rs.getString("NAME_C"))!=null)
                      user.setGroup(groupRepository.findByName(rs.getString("NAME_C")));
ef4eb263   rvangrev   Nouvelles modific...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
                  return user;
              } 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
      public void save(User user, Group group) {
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
fa02d31b   rvangrev   Commit avant le m...
73
74
75
76
77
78
              
              int groupid=0;
              if(group!=null){
                  groupid = Math.toIntExact(group.getId());
              }
              stmt.executeUpdate("INSERT INTO USER_T (NAME_U, GROUP_ID) VALUES (\'" + user.getName() + "\', " + groupid + ")",
ef4eb263   rvangrev   Nouvelles modific...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                      Statement.RETURN_GENERATED_KEYS);
              rs = stmt.getGeneratedKeys();
              if (rs.next()) {
                  user.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
      public void delete(Long id) {
          Statement stmt = null;
          ResultSet rs = null;
          if(id==null){
              throw new IllegalArgumentException();
          }
          try {
              stmt = dataSource.getConnection().createStatement();
              stmt.execute("DELETE FROM USER_T WHERE  USER_ID = " + id);
          } 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);
              }
          }
      }
73c004db   rvangrev   TP presque termin...
128
129
130
131
132
133
134
135
136
137
138
      
      @Override
      public boolean isUserHasRight(Long group, Long right){
          Statement stmt = null;
          ResultSet rs = null;
          try {
              stmt = dataSource.getConnection().createStatement();
              rs = stmt.executeQuery("SELECT * FROM LINK_T WHERE RIGHT_ID = " + right + " AND GROUP_ID = " + group );
              if (rs.next()) {
                  return true;
              } else {
f540a4bc   rvangrev   Fin TP
139
140
141
142
  //                rs = stmt.executeQuery("SELECT * FROM LINK_T LI JOIN RIGHT_T RI ON RI.RIGHT_ID = LI.RIGHT_ID WHERE RI.PARENT_ID = " + right);
  //                if(rs.next()){
  //                    return true;
  //                }
73c004db   rvangrev   TP presque termin...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
                  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);
  
              }
          }
      }
ef4eb263   rvangrev   Nouvelles modific...
161
  }