Commit f540a4bc29173d44db5fbdeddd97c13d805bcddf

Authored by rvangrev
1 parent 73c004db

Fin TP

src/main/java/fr/plil/sio/persistence/api/GroupService.java
... ... @@ -12,7 +12,7 @@ public interface GroupService {
12 12 * @throws IllegalStateException if a group with the same name is already present
13 13 */
14 14 Group create(String name);
15   -
  15 +
16 16 /**
17 17 * Delete a group in the database. Remove all users in the group.
18 18 *
... ...
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
3 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 5  
5 6 public interface GroupRepository {
6 7  
... ... @@ -12,5 +13,5 @@ public interface GroupRepository {
12 13  
13 14 void removeRight(Long right, Long group);
14 15  
15   - void addRight(Long right, Long group);
  16 + void addRight(Right right, Group group);
16 17 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
1 1 package fr.plil.sio.persistence.jdbc;
2 2  
3 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 5 import org.slf4j.Logger;
5 6 import org.slf4j.LoggerFactory;
6 7 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -136,8 +137,9 @@ public class GroupRepositoryJdbc implements GroupRepository {
136 137 }
137 138 }
138 139 }
  140 +
139 141 @Override
140   - public void addRight(Long right, Long group){
  142 + public void addRight(Right right, Group group){
141 143 Statement stmt = null;
142 144 ResultSet rs = null;
143 145 if(right==null || group==null){
... ... @@ -145,7 +147,7 @@ public class GroupRepositoryJdbc implements GroupRepository {
145 147 }
146 148 try {
147 149 stmt = dataSource.getConnection().createStatement();
148   - stmt.execute("INSERT INTO LINK_T(RIGHT_ID, GROUP_ID) VALUES (" + group + "," + right + ")");
  150 + stmt.execute("INSERT INTO LINK_T(RIGHT_ID, GROUP_ID) VALUES (" + right.getId() + "," + group.getId() + ")");
149 151 } catch (SQLException e) {
150 152 throw new UnsupportedOperationException("sql exception", e);
151 153 } finally {
... ... @@ -161,5 +163,4 @@ public class GroupRepositoryJdbc implements GroupRepository {
161 163 }
162 164 }
163 165 }
164   -
165 166 }
... ...
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.util.ArrayList;
6 7 import org.springframework.beans.factory.annotation.Autowired;
7 8 import org.springframework.stereotype.Service;
8 9 import java.util.List;
... ... @@ -73,12 +74,13 @@ public class GroupServiceJdbc implements GroupService {
73 74 if(userRepository.isUserHasRight(groupRepository.findByName(groupName).getId(), right.getId())){
74 75 return false;
75 76 }
76   -
77 77 Group g = groupRepository.findByName(groupName);
78   -
79   - g.getRights().add(right);
80 78  
81   - groupRepository.addRight(g.getId(), right.getId());
  79 + groupRepository.addRight(right, g);
  80 +
  81 + if(!(g.getRights()).contains(right)){
  82 + g.addRight(right);
  83 + }
82 84  
83 85 return true;
84 86 }
... ...
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
... ... @@ -39,7 +39,9 @@ public class UserRepositoryJdbc implements UserRepository{
39 39 User user = new User();
40 40 user.setId(rs.getLong("USER_ID"));
41 41 user.setName(rs.getString("NAME_U"));
42   - user.setGroup(groupRepository.findByName("NAME_C"));
  42 + //On ajoute le groupe, sauf si il est nul
  43 + if(groupRepository.findByName(rs.getString("NAME_C"))!=null)
  44 + user.setGroup(groupRepository.findByName(rs.getString("NAME_C")));
43 45 return user;
44 46 } else {
45 47 logger.debug("not found " + name);
... ... @@ -134,6 +136,10 @@ public class UserRepositoryJdbc implements UserRepository{
134 136 if (rs.next()) {
135 137 return true;
136 138 } else {
  139 +// rs = stmt.executeQuery("SELECT * FROM LINK_T LI JOIN RIGHT_T RI ON RI.RIGHT_ID = LI.RIGHT_ID WHERE RI.PARENT_ID = " + right);
  140 +// if(rs.next()){
  141 +// return true;
  142 +// }
137 143 return false;
138 144 }
139 145 } catch (SQLException e) {
... ...