Commit ef4eb26320f5548f525acff420cf2635156bbc0b
1 parent
8882a9c3
Nouvelles modifications -> Partie groupe terminée.
Reste à faire les modifications pour le user et le right
Showing
8 changed files
with
388 additions
and
13 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
... | ... | @@ -86,6 +86,28 @@ public class GroupRepositoryJdbc implements GroupRepository { |
86 | 86 | |
87 | 87 | @Override |
88 | 88 | public void delete(Long id) { |
89 | - throw new IllegalStateException("not implemented !"); | |
89 | + Statement stmt = null; | |
90 | + ResultSet rs = null; | |
91 | + if(id==null){ | |
92 | + throw new IllegalArgumentException(); | |
93 | + } | |
94 | + try { | |
95 | + stmt = dataSource.getConnection().createStatement(); | |
96 | + stmt.execute("DELETE FROM USER_T WHERE GROUP_U = " + id); | |
97 | + stmt.execute("DELETE FROM GROUP_T WHERE GROUP_ID = " + id); | |
98 | + } catch (SQLException e) { | |
99 | + throw new UnsupportedOperationException("sql exception", e); | |
100 | + } finally { | |
101 | + try { | |
102 | + if (rs != null) { | |
103 | + rs.close(); | |
104 | + } | |
105 | + if (stmt != null) { | |
106 | + stmt.close(); | |
107 | + } | |
108 | + } catch (SQLException e) { | |
109 | + throw new UnsupportedOperationException("sql exception during close", e); | |
110 | + } | |
111 | + } | |
90 | 112 | } |
91 | 113 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... | ... | @@ -5,6 +5,8 @@ import fr.plil.sio.persistence.api.GroupService; |
5 | 5 | import fr.plil.sio.persistence.api.Right; |
6 | 6 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 7 | import org.springframework.stereotype.Service; |
8 | +import java.util.List; | |
9 | +import java.util.LinkedList; | |
8 | 10 | |
9 | 11 | @Service |
10 | 12 | public class GroupServiceJdbc implements GroupService { |
... | ... | @@ -29,7 +31,19 @@ public class GroupServiceJdbc implements GroupService { |
29 | 31 | |
30 | 32 | @Override |
31 | 33 | public boolean delete(String name) { |
32 | - throw new IllegalStateException("not implemented !"); | |
34 | + | |
35 | + if (name == null) { | |
36 | + throw new IllegalArgumentException("name cannot be null"); | |
37 | + } | |
38 | + | |
39 | + Group group = findByName(name); | |
40 | + | |
41 | + if(group==null){ | |
42 | + return false; | |
43 | + } | |
44 | + | |
45 | + groupRepository.delete(group.getId()); | |
46 | + return true; | |
33 | 47 | } |
34 | 48 | |
35 | 49 | @Override |
... | ... | @@ -42,11 +56,30 @@ public class GroupServiceJdbc implements GroupService { |
42 | 56 | |
43 | 57 | @Override |
44 | 58 | public boolean addRight(String groupName, Right right) { |
45 | - throw new IllegalStateException("not implemented !"); | |
59 | + if(groupName==null || right ==null){ | |
60 | + throw new IllegalArgumentException("name cannot be null"); | |
61 | + } | |
62 | + Group group = groupRepository.findByName(groupName); | |
63 | + if (group == null) { | |
64 | + throw new IllegalArgumentException("group cannot be null"); | |
65 | + } | |
66 | + | |
67 | + group.setName(groupName); | |
68 | + List<Right> list = group.getRights(); | |
69 | + list.add(right); | |
70 | + group.setRights(list); | |
71 | + return true; | |
46 | 72 | } |
47 | 73 | |
48 | 74 | @Override |
49 | 75 | public boolean removeRight(String groupName, Right right) { |
50 | - throw new IllegalStateException("not implemented !"); | |
76 | + if(right==null){ | |
77 | + throw new IllegalArgumentException("name cannot be null"); | |
78 | + } | |
79 | + Group group = groupRepository.findByName(groupName); | |
80 | + if (group != null) { | |
81 | + throw new IllegalStateException("a group with the same name already exists"); | |
82 | + } | |
83 | + return true; | |
51 | 84 | } |
52 | 85 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java
0 → 100644
... | ... | @@ -0,0 +1,17 @@ |
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 | +public interface RightRepository { | |
11 | + | |
12 | + Right findByName(String name); | |
13 | + | |
14 | + void delete(Long id); | |
15 | + | |
16 | + void save(Right right); | |
17 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
0 → 100644
... | ... | @@ -0,0 +1,120 @@ |
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.Group; | |
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 fr.plil.sio.persistence.api.Right; | |
17 | + | |
18 | +/** | |
19 | + * | |
20 | + * @author rvangrev | |
21 | + */ | |
22 | +public class RightRepositoryJdbc implements RightRepository{ | |
23 | + | |
24 | + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); | |
25 | + | |
26 | + @Autowired | |
27 | + private DataSource dataSource; | |
28 | + | |
29 | + @Override | |
30 | + public Right findByName(String name) { | |
31 | + Statement stmt = null; | |
32 | + ResultSet rs = null; | |
33 | + try { | |
34 | + stmt = dataSource.getConnection().createStatement(); | |
35 | + rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'"); | |
36 | + if (rs.next()) { | |
37 | + logger.debug("found group " + name); | |
38 | +// Group group = new Group(); | |
39 | +// group.setId(rs.getLong("GROUP_ID")); | |
40 | +// group.setName(rs.getString("NAME_C")); | |
41 | +// return group; | |
42 | + } else { | |
43 | + logger.debug("not found " + name); | |
44 | + return null; | |
45 | + } | |
46 | + } catch (SQLException e) { | |
47 | + throw new UnsupportedOperationException("sql exception", e); | |
48 | + } finally { | |
49 | + try { | |
50 | + if (rs != null) { | |
51 | + rs.close(); | |
52 | + } | |
53 | + if (stmt != null) { | |
54 | + stmt.close(); | |
55 | + } | |
56 | + } catch (SQLException e) { | |
57 | + throw new UnsupportedOperationException("sql exception during close", e); | |
58 | + | |
59 | + } | |
60 | + } | |
61 | + return null; | |
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 GROUP_T (NAME_C) 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 | + if(id==null){ | |
100 | + throw new IllegalArgumentException(); | |
101 | + } | |
102 | + try { | |
103 | + stmt = dataSource.getConnection().createStatement(); | |
104 | + stmt.execute("DELETE FROM RIGHT_T WHERE RIGHT_ID = " + id); | |
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 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... | ... | @@ -8,19 +8,30 @@ import java.util.List; |
8 | 8 | |
9 | 9 | @Service |
10 | 10 | public class RightServiceJdbc implements RightService { |
11 | + | |
11 | 12 | @Override |
12 | 13 | public Right create(String name) { |
13 | - return null; | |
14 | + if(name==null){ | |
15 | + throw new IllegalArgumentException("name cannot be null"); | |
16 | + } | |
17 | + else{ | |
18 | + return null; | |
19 | + } | |
14 | 20 | } |
15 | 21 | |
16 | 22 | @Override |
17 | 23 | public Right create(String name, Right parent) { |
18 | - throw new IllegalStateException("not implemented !"); | |
24 | + return null; | |
19 | 25 | } |
20 | 26 | |
21 | 27 | @Override |
22 | 28 | public boolean delete(Right right) { |
23 | - throw new IllegalStateException("not implemented !"); | |
29 | + if(right==null){ | |
30 | + throw new IllegalArgumentException("name cannot be null"); | |
31 | + } | |
32 | + else{ | |
33 | + return true; | |
34 | + } | |
24 | 35 | } |
25 | 36 | |
26 | 37 | @Override |
... | ... | @@ -30,6 +41,11 @@ public class RightServiceJdbc implements RightService { |
30 | 41 | |
31 | 42 | @Override |
32 | 43 | public Right findOne(Long id) { |
33 | - throw new IllegalStateException("not implemented !"); | |
44 | + if(id==null){ | |
45 | + throw new IllegalArgumentException("name cannot be null"); | |
46 | + } | |
47 | + else{ | |
48 | + return null; | |
49 | + } | |
34 | 50 | } |
35 | 51 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java
0 → 100644
... | ... | @@ -0,0 +1,17 @@ |
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 | +import fr.plil.sio.persistence.api.User; | |
8 | +import fr.plil.sio.persistence.api.Group; | |
9 | + | |
10 | +public interface UserRepository { | |
11 | + | |
12 | + User findByName(String name); | |
13 | + | |
14 | + void delete(Long id); | |
15 | + | |
16 | + void save(User user, Group group); | |
17 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.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.Group; | |
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 | +import fr.plil.sio.persistence.api.User; | |
18 | + | |
19 | +@Repository | |
20 | +public class UserRepositoryJdbc implements UserRepository{ | |
21 | + | |
22 | + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); | |
23 | + | |
24 | + @Autowired | |
25 | + private DataSource dataSource; | |
26 | + | |
27 | + @Override | |
28 | + public User findByName(String name) { | |
29 | + Statement stmt = null; | |
30 | + ResultSet rs = null; | |
31 | + try { | |
32 | + stmt = dataSource.getConnection().createStatement(); | |
33 | + rs = stmt.executeQuery("SELECT U.USER_ID, U.NAME_U, U.GROUP_U, G.NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_U=G.GROUP_ID WHERE NAME_U = \'" + name + "\' "); | |
34 | + if (rs.next()) { | |
35 | + logger.debug("found group " + name); | |
36 | + User user = new User(); | |
37 | + user.setId(rs.getLong("T.USER_ID")); | |
38 | + user.setName(rs.getString("T.NAME_U")); | |
39 | + Group group = new Group(); | |
40 | + GroupRepositoryJdbc g = new GroupRepositoryJdbc(); | |
41 | + group = g.findByName(rs.getString("G.NAME_C")); | |
42 | + user.setGroup(group); | |
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, Group group) { | |
67 | + Statement stmt = null; | |
68 | + ResultSet rs = null; | |
69 | + try { | |
70 | + stmt = dataSource.getConnection().createStatement(); | |
71 | + stmt.executeUpdate("INSERT INTO USER_T (NAME_U, GROUP_U) VALUES (\'" + user.getName() + "\', \'" + group.getId() + "\')", | |
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 | + if(id==null){ | |
101 | + throw new IllegalArgumentException(); | |
102 | + } | |
103 | + try { | |
104 | + stmt = dataSource.getConnection().createStatement(); | |
105 | + stmt.execute("DELETE FROM USER_T WHERE USER_ID = " + id); | |
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 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
1 | 1 | package fr.plil.sio.persistence.jdbc; |
2 | 2 | |
3 | 3 | import fr.plil.sio.persistence.api.Right; |
4 | +import fr.plil.sio.persistence.api.Group; | |
4 | 5 | import fr.plil.sio.persistence.api.User; |
5 | 6 | import fr.plil.sio.persistence.api.UserService; |
6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -10,25 +11,53 @@ import org.springframework.stereotype.Service; |
10 | 11 | public class UserServiceJdbc implements UserService { |
11 | 12 | |
12 | 13 | @Autowired |
13 | - private GroupRepository userRepository; | |
14 | + private UserRepository userRepository; | |
15 | + private GroupRepository groupRepository; | |
14 | 16 | |
15 | 17 | @Override |
16 | 18 | public User create(String name, String groupName) { |
17 | - throw new IllegalStateException("not implemented !"); | |
19 | + if(name==null){ | |
20 | + throw new IllegalArgumentException("name cannot be null"); | |
21 | + } | |
22 | + User user = userRepository.findByName(name); | |
23 | + if(user!=null){ | |
24 | + throw new IllegalStateException("a user with the same name already exists"); | |
25 | + } | |
26 | + user = new User(); | |
27 | + user.setName(name); | |
28 | + Group group = groupRepository.findByName(groupName); | |
29 | + userRepository.save(user, group); | |
30 | + return user; | |
18 | 31 | } |
19 | 32 | |
20 | 33 | @Override |
21 | 34 | public boolean delete(String name) { |
22 | - throw new IllegalStateException("not implemented !"); | |
35 | + if(name == null){ | |
36 | + throw new IllegalStateException("not implemented !"); | |
37 | + } | |
38 | + User user = findByName(name); | |
39 | + if(user==null){ | |
40 | + return false; | |
41 | + } | |
42 | + userRepository.delete(user.getId()); | |
43 | + return true; | |
23 | 44 | } |
24 | 45 | |
25 | 46 | @Override |
26 | 47 | public User findByName(String name) { |
27 | - throw new IllegalStateException("not implemented !"); | |
48 | + if(name==null){ | |
49 | + throw new IllegalArgumentException("name cannot be null"); | |
50 | + } | |
51 | + return userRepository.findByName(name); | |
28 | 52 | } |
29 | 53 | |
30 | 54 | @Override |
31 | 55 | public boolean isUserHasRight(String userName, Right right) { |
32 | - throw new IllegalStateException("not implemented !"); | |
56 | + if(right==null){ | |
57 | + throw new IllegalArgumentException("name cannot be null"); | |
58 | + } | |
59 | + else{ | |
60 | + return false; | |
61 | + } | |
33 | 62 | } |
34 | 63 | } | ... | ... |