Commit 31bdbe1b5555740f61c3b4c7a7a0204fefc811c7
1 parent
a54db7eb
Version où les tests ne passent pas pour des raisons incompréhensibles.
Showing
7 changed files
with
326 additions
and
7 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
... | ... | @@ -90,7 +90,7 @@ public class GroupRepositoryJdbc implements GroupRepository { |
90 | 90 | ResultSet rs = null; |
91 | 91 | try { |
92 | 92 | stmt = dataSource.getConnection().createStatement(); |
93 | - stmt.execute("DELETE FROM GROUP_T WHERE GROUP_ID="+id, | |
93 | + stmt.executeUpdate("DELETE FROM GROUP_T WHERE GROUP_ID="+id, | |
94 | 94 | Statement.RETURN_GENERATED_KEYS); |
95 | 95 | rs = stmt.getGeneratedKeys(); |
96 | 96 | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... | ... | @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.jdbc; |
3 | 3 | import fr.plil.sio.persistence.api.Group; |
4 | 4 | import fr.plil.sio.persistence.api.GroupService; |
5 | 5 | import fr.plil.sio.persistence.api.Right; |
6 | +import java.sql.SQLException; | |
6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 8 | import org.springframework.stereotype.Service; |
8 | 9 | |
... | ... | @@ -29,7 +30,17 @@ public class GroupServiceJdbc implements GroupService { |
29 | 30 | |
30 | 31 | @Override |
31 | 32 | public boolean delete(String name) { |
32 | - throw new IllegalStateException("not implemented !"); | |
33 | + //throw new IllegalStateException("not implemented !"); | |
34 | + if (name == null) { | |
35 | + throw new IllegalArgumentException("name cannot be null"); | |
36 | + } | |
37 | + | |
38 | + Group group = groupRepository.findByName(name); | |
39 | + if(group!=null){ | |
40 | + groupRepository.delete(group.getId()); | |
41 | + return true; | |
42 | + } | |
43 | + return false; | |
33 | 44 | } |
34 | 45 | |
35 | 46 | @Override | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java
0 → 100644
... | ... | @@ -0,0 +1,21 @@ |
1 | +/* | |
2 | + * To change this license header, choose License Headers in Project Properties. | |
3 | + * To change this template file, choose Tools | Templates | |
4 | + * and open the template in the editor. | |
5 | + */ | |
6 | +package fr.plil.sio.persistence.jdbc; | |
7 | + | |
8 | +import fr.plil.sio.persistence.api.Right; | |
9 | + | |
10 | +/** | |
11 | + * | |
12 | + * @author msahmane | |
13 | + */ | |
14 | +public interface RightRepository { | |
15 | + | |
16 | + Right findByName(String name); | |
17 | + | |
18 | + void delete(Long id); | |
19 | + | |
20 | + void save(Right right); | |
21 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
0 → 100644
... | ... | @@ -0,0 +1,121 @@ |
1 | +/* | |
2 | + * To change this license header, choose License Headers in Project Properties. | |
3 | + * To change this template file, choose Tools | Templates | |
4 | + * and open the template in the editor. | |
5 | + */ | |
6 | +package fr.plil.sio.persistence.jdbc; | |
7 | + | |
8 | +import fr.plil.sio.persistence.api.Right; | |
9 | +import java.sql.ResultSet; | |
10 | +import java.sql.SQLException; | |
11 | +import java.sql.Statement; | |
12 | +import javax.sql.DataSource; | |
13 | +import org.slf4j.Logger; | |
14 | +import org.slf4j.LoggerFactory; | |
15 | +import org.springframework.beans.factory.annotation.Autowired; | |
16 | +import org.springframework.stereotype.Repository; | |
17 | + | |
18 | +/** | |
19 | + * | |
20 | + * @author msahmane | |
21 | + */ | |
22 | +@Repository | |
23 | +public class RightRepositoryJdbc implements RightRepository { | |
24 | + | |
25 | + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); | |
26 | + | |
27 | + @Autowired | |
28 | + private DataSource dataSource; | |
29 | + | |
30 | + @Override | |
31 | + public Right findByName(String name) { | |
32 | + Statement stmt = null; | |
33 | + ResultSet rs = null; | |
34 | + try { | |
35 | + stmt = dataSource.getConnection().createStatement(); | |
36 | + rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE NAME_R = \'" + name + "\'"); | |
37 | + if (rs.next()) { | |
38 | + logger.debug("found group " + name); | |
39 | + Right right = new Right(); | |
40 | + right.setId(rs.getLong("RIGHT_ID")); | |
41 | + right.setName(rs.getString("NAME_R")); | |
42 | + return right; | |
43 | + } else { | |
44 | + logger.debug("not found " + name); | |
45 | + return null; | |
46 | + } | |
47 | + } catch (SQLException e) { | |
48 | + throw new UnsupportedOperationException("sql exception", e); | |
49 | + } finally { | |
50 | + try { | |
51 | + if (rs != null) { | |
52 | + rs.close(); | |
53 | + } | |
54 | + if (stmt != null) { | |
55 | + stmt.close(); | |
56 | + } | |
57 | + } catch (SQLException e) { | |
58 | + throw new UnsupportedOperationException("sql exception during close", e); | |
59 | + | |
60 | + } | |
61 | + } | |
62 | + } | |
63 | + | |
64 | + @Override | |
65 | + public void save(Right right) { | |
66 | + Statement stmt = null; | |
67 | + ResultSet rs = null; | |
68 | + try { | |
69 | + stmt = dataSource.getConnection().createStatement(); | |
70 | + stmt.executeUpdate("INSERT INTO RIGHT_T (NAME_R) VALUES (\'" + right.getName() + "\')", | |
71 | + Statement.RETURN_GENERATED_KEYS); | |
72 | + rs = stmt.getGeneratedKeys(); | |
73 | + if (rs.next()) { | |
74 | + right.setId(rs.getLong(1)); | |
75 | + } else { | |
76 | + throw new UnsupportedOperationException("default in key access"); | |
77 | + } | |
78 | + } catch (SQLException e) { | |
79 | + throw new UnsupportedOperationException("sql exception", e); | |
80 | + } finally { | |
81 | + try { | |
82 | + if (rs != null) { | |
83 | + rs.close(); | |
84 | + } | |
85 | + if (stmt != null) { | |
86 | + stmt.close(); | |
87 | + } | |
88 | + } catch (SQLException e) { | |
89 | + throw new UnsupportedOperationException("sql exception during close", e); | |
90 | + } | |
91 | + } | |
92 | + | |
93 | + } | |
94 | + | |
95 | + @Override | |
96 | + public void delete(Long id) { | |
97 | + Statement stmt = null; | |
98 | + ResultSet rs = null; | |
99 | + try { | |
100 | + stmt = dataSource.getConnection().createStatement(); | |
101 | + stmt.executeUpdate("DELETE FROM RIGHT_T WHERE RIGHT_ID="+id, | |
102 | + Statement.RETURN_GENERATED_KEYS); | |
103 | + rs = stmt.getGeneratedKeys(); | |
104 | + | |
105 | + } catch (SQLException e) { | |
106 | + throw new UnsupportedOperationException("sql exception", e); | |
107 | + } finally { | |
108 | + try { | |
109 | + if (rs != null) { | |
110 | + rs.close(); | |
111 | + } | |
112 | + if (stmt != null) { | |
113 | + stmt.close(); | |
114 | + } | |
115 | + } catch (SQLException e) { | |
116 | + throw new UnsupportedOperationException("sql exception during close", e); | |
117 | + } | |
118 | + } | |
119 | + } | |
120 | + | |
121 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java
0 → 100644
... | ... | @@ -0,0 +1,22 @@ |
1 | +/* | |
2 | + * To change this license header, choose License Headers in Project Properties. | |
3 | + * To change this template file, choose Tools | Templates | |
4 | + * and open the template in the editor. | |
5 | + */ | |
6 | +package fr.plil.sio.persistence.jdbc; | |
7 | + | |
8 | +import fr.plil.sio.persistence.api.User; | |
9 | + | |
10 | +/** | |
11 | + * | |
12 | + * @author msahmane | |
13 | + */ | |
14 | +public interface UserRepository { | |
15 | + | |
16 | + User findByName(String name); | |
17 | + | |
18 | + void delete(Long id); | |
19 | + | |
20 | + void save(User user); | |
21 | + | |
22 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
0 → 100644
... | ... | @@ -0,0 +1,122 @@ |
1 | +/* | |
2 | + * To change this license header, choose License Headers in Project Properties. | |
3 | + * To change this template file, choose Tools | Templates | |
4 | + * and open the template in the editor. | |
5 | + */ | |
6 | +package fr.plil.sio.persistence.jdbc; | |
7 | + | |
8 | +import fr.plil.sio.persistence.api.User; | |
9 | +import java.sql.ResultSet; | |
10 | +import java.sql.SQLException; | |
11 | +import java.sql.Statement; | |
12 | +import javax.sql.DataSource; | |
13 | +import org.slf4j.Logger; | |
14 | +import org.slf4j.LoggerFactory; | |
15 | +import org.springframework.beans.factory.annotation.Autowired; | |
16 | +import org.springframework.stereotype.Repository; | |
17 | + | |
18 | +/** | |
19 | + * | |
20 | + * @author msahmane | |
21 | + */ | |
22 | + | |
23 | +@Repository | |
24 | +public class UserRepositoryJdbc implements UserRepository { | |
25 | + | |
26 | + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); | |
27 | + | |
28 | + @Autowired | |
29 | + private DataSource dataSource; | |
30 | + | |
31 | + @Override | |
32 | + public User findByName(String name) { | |
33 | + Statement stmt = null; | |
34 | + ResultSet rs = null; | |
35 | + try { | |
36 | + stmt = dataSource.getConnection().createStatement(); | |
37 | + rs = stmt.executeQuery("SELECT * FROM USER_T WHERE NAME_U = \'" + name + "\'"); | |
38 | + if (rs.next()) { | |
39 | + logger.debug("found group " + name); | |
40 | + User user= new User(); | |
41 | + user.setId(rs.getLong("USER_ID")); | |
42 | + user.setName(rs.getString("NAME_U")); | |
43 | + return user; | |
44 | + } else { | |
45 | + logger.debug("not found " + name); | |
46 | + return null; | |
47 | + } | |
48 | + } catch (SQLException e) { | |
49 | + throw new UnsupportedOperationException("sql exception", e); | |
50 | + } finally { | |
51 | + try { | |
52 | + if (rs != null) { | |
53 | + rs.close(); | |
54 | + } | |
55 | + if (stmt != null) { | |
56 | + stmt.close(); | |
57 | + } | |
58 | + } catch (SQLException e) { | |
59 | + throw new UnsupportedOperationException("sql exception during close", e); | |
60 | + | |
61 | + } | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public void save(User user) { | |
67 | + Statement stmt = null; | |
68 | + ResultSet rs = null; | |
69 | + try { | |
70 | + stmt = dataSource.getConnection().createStatement(); | |
71 | + stmt.executeUpdate("INSERT INTO USER_T (NAME_U) VALUES (\'" + user.getName() + "\')", | |
72 | + Statement.RETURN_GENERATED_KEYS); | |
73 | + rs = stmt.getGeneratedKeys(); | |
74 | + if (rs.next()) { | |
75 | + user.setId(rs.getLong(1)); | |
76 | + } else { | |
77 | + throw new UnsupportedOperationException("default in key access"); | |
78 | + } | |
79 | + } catch (SQLException e) { | |
80 | + throw new UnsupportedOperationException("sql exception", e); | |
81 | + } finally { | |
82 | + try { | |
83 | + if (rs != null) { | |
84 | + rs.close(); | |
85 | + } | |
86 | + if (stmt != null) { | |
87 | + stmt.close(); | |
88 | + } | |
89 | + } catch (SQLException e) { | |
90 | + throw new UnsupportedOperationException("sql exception during close", e); | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + } | |
95 | + | |
96 | + @Override | |
97 | + public void delete(Long id) { | |
98 | + Statement stmt = null; | |
99 | + ResultSet rs = null; | |
100 | + try { | |
101 | + stmt = dataSource.getConnection().createStatement(); | |
102 | + stmt.executeUpdate("DELETE FROM USER_T WHERE USER_ID="+id, | |
103 | + Statement.RETURN_GENERATED_KEYS); | |
104 | + rs = stmt.getGeneratedKeys(); | |
105 | + | |
106 | + } catch (SQLException e) { | |
107 | + throw new UnsupportedOperationException("sql exception", e); | |
108 | + } finally { | |
109 | + try { | |
110 | + if (rs != null) { | |
111 | + rs.close(); | |
112 | + } | |
113 | + if (stmt != null) { | |
114 | + stmt.close(); | |
115 | + } | |
116 | + } catch (SQLException e) { | |
117 | + throw new UnsupportedOperationException("sql exception during close", e); | |
118 | + } | |
119 | + } | |
120 | + } | |
121 | + | |
122 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
... | ... | @@ -10,21 +10,43 @@ import org.springframework.stereotype.Service; |
10 | 10 | public class UserServiceJdbc implements UserService { |
11 | 11 | |
12 | 12 | @Autowired |
13 | - private GroupRepository userRepository; | |
13 | + private UserRepository userRepository; | |
14 | 14 | |
15 | 15 | @Override |
16 | - public User create(String name, String groupName) { | |
17 | - throw new IllegalStateException("not implemented !"); | |
16 | + public User create(String name, String userName) { | |
17 | + if (name == null) { | |
18 | + throw new IllegalArgumentException("name cannot be null"); | |
19 | + } | |
20 | + User user = userRepository.findByName(name); | |
21 | + if (user != null) { | |
22 | + throw new IllegalStateException("a group with the same name already exists"); | |
23 | + } | |
24 | + user = new User(); | |
25 | + user.setName(name); | |
26 | + userRepository.save(user); | |
27 | + return user; | |
18 | 28 | } |
19 | 29 | |
20 | 30 | @Override |
21 | 31 | public boolean delete(String name) { |
22 | - throw new IllegalStateException("not implemented !"); | |
32 | + if (name == null) { | |
33 | + throw new IllegalArgumentException("name cannot be null"); | |
34 | + } | |
35 | + | |
36 | + User user = userRepository.findByName(name); | |
37 | + if(user!=null){ | |
38 | + userRepository.delete(user.getId()); | |
39 | + return true; | |
40 | + } | |
41 | + return false; | |
23 | 42 | } |
24 | 43 | |
25 | 44 | @Override |
26 | 45 | public User findByName(String name) { |
27 | - throw new IllegalStateException("not implemented !"); | |
46 | + if (name == null) { | |
47 | + throw new IllegalArgumentException("name cannot be null"); | |
48 | + } | |
49 | + return userRepository.findByName(name); | |
28 | 50 | } |
29 | 51 | |
30 | 52 | @Override | ... | ... |