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,7 +12,7 @@ public interface GroupService {
12 * @throws IllegalStateException if a group with the same name is already present 12 * @throws IllegalStateException if a group with the same name is already present
13 */ 13 */
14 Group create(String name); 14 Group create(String name);
15 - 15 +
16 /** 16 /**
17 * Delete a group in the database. Remove all users in the group. 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 package fr.plil.sio.persistence.jdbc; 1 package fr.plil.sio.persistence.jdbc;
2 2
3 import fr.plil.sio.persistence.api.Group; 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 5
5 public interface GroupRepository { 6 public interface GroupRepository {
6 7
@@ -12,5 +13,5 @@ public interface GroupRepository { @@ -12,5 +13,5 @@ public interface GroupRepository {
12 13
13 void removeRight(Long right, Long group); 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 package fr.plil.sio.persistence.jdbc; 1 package fr.plil.sio.persistence.jdbc;
2 2
3 import fr.plil.sio.persistence.api.Group; 3 import fr.plil.sio.persistence.api.Group;
  4 +import fr.plil.sio.persistence.api.Right;
4 import org.slf4j.Logger; 5 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
@@ -136,8 +137,9 @@ public class GroupRepositoryJdbc implements GroupRepository { @@ -136,8 +137,9 @@ public class GroupRepositoryJdbc implements GroupRepository {
136 } 137 }
137 } 138 }
138 } 139 }
  140 +
139 @Override 141 @Override
140 - public void addRight(Long right, Long group){ 142 + public void addRight(Right right, Group group){
141 Statement stmt = null; 143 Statement stmt = null;
142 ResultSet rs = null; 144 ResultSet rs = null;
143 if(right==null || group==null){ 145 if(right==null || group==null){
@@ -145,7 +147,7 @@ public class GroupRepositoryJdbc implements GroupRepository { @@ -145,7 +147,7 @@ public class GroupRepositoryJdbc implements GroupRepository {
145 } 147 }
146 try { 148 try {
147 stmt = dataSource.getConnection().createStatement(); 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 } catch (SQLException e) { 151 } catch (SQLException e) {
150 throw new UnsupportedOperationException("sql exception", e); 152 throw new UnsupportedOperationException("sql exception", e);
151 } finally { 153 } finally {
@@ -161,5 +163,4 @@ public class GroupRepositoryJdbc implements GroupRepository { @@ -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,6 +3,7 @@ package fr.plil.sio.persistence.jdbc;
3 import fr.plil.sio.persistence.api.Group; 3 import fr.plil.sio.persistence.api.Group;
4 import fr.plil.sio.persistence.api.GroupService; 4 import fr.plil.sio.persistence.api.GroupService;
5 import fr.plil.sio.persistence.api.Right; 5 import fr.plil.sio.persistence.api.Right;
  6 +import java.util.ArrayList;
6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service; 8 import org.springframework.stereotype.Service;
8 import java.util.List; 9 import java.util.List;
@@ -73,12 +74,13 @@ public class GroupServiceJdbc implements GroupService { @@ -73,12 +74,13 @@ public class GroupServiceJdbc implements GroupService {
73 if(userRepository.isUserHasRight(groupRepository.findByName(groupName).getId(), right.getId())){ 74 if(userRepository.isUserHasRight(groupRepository.findByName(groupName).getId(), right.getId())){
74 return false; 75 return false;
75 } 76 }
76 -  
77 Group g = groupRepository.findByName(groupName); 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 return true; 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,7 +39,9 @@ public class UserRepositoryJdbc implements UserRepository{
39 User user = new User(); 39 User user = new User();
40 user.setId(rs.getLong("USER_ID")); 40 user.setId(rs.getLong("USER_ID"));
41 user.setName(rs.getString("NAME_U")); 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 return user; 45 return user;
44 } else { 46 } else {
45 logger.debug("not found " + name); 47 logger.debug("not found " + name);
@@ -134,6 +136,10 @@ public class UserRepositoryJdbc implements UserRepository{ @@ -134,6 +136,10 @@ public class UserRepositoryJdbc implements UserRepository{
134 if (rs.next()) { 136 if (rs.next()) {
135 return true; 137 return true;
136 } else { 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 return false; 143 return false;
138 } 144 }
139 } catch (SQLException e) { 145 } catch (SQLException e) {