From ef4eb26320f5548f525acff420cf2635156bbc0b Mon Sep 17 00:00:00 2001 From: rvangrev Date: Thu, 22 Sep 2016 16:23:22 +0200 Subject: [PATCH] Nouvelles modifications -> Partie groupe terminée. Reste à faire les modifications pour le user et le right --- src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java | 24 +++++++++++++++++++++++- src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java | 39 ++++++++++++++++++++++++++++++++++++--- src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java | 17 +++++++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java | 24 ++++++++++++++++++++---- src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java | 17 +++++++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java | 39 ++++++++++++++++++++++++++++++++++----- 8 files changed, 388 insertions(+), 13 deletions(-) create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java create mode 100644 src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java index d121550..ea773a4 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java @@ -86,6 +86,28 @@ public class GroupRepositoryJdbc implements GroupRepository { @Override public void delete(Long id) { - throw new IllegalStateException("not implemented !"); + Statement stmt = null; + ResultSet rs = null; + if(id==null){ + throw new IllegalArgumentException(); + } + try { + stmt = dataSource.getConnection().createStatement(); + stmt.execute("DELETE FROM USER_T WHERE GROUP_U = " + id); + stmt.execute("DELETE FROM GROUP_T WHERE GROUP_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); + } + } } } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java index 971e8b1..d9affc8 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java @@ -5,6 +5,8 @@ import fr.plil.sio.persistence.api.GroupService; import fr.plil.sio.persistence.api.Right; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.LinkedList; @Service public class GroupServiceJdbc implements GroupService { @@ -29,7 +31,19 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean delete(String name) { - throw new IllegalStateException("not implemented !"); + + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + + Group group = findByName(name); + + if(group==null){ + return false; + } + + groupRepository.delete(group.getId()); + return true; } @Override @@ -42,11 +56,30 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean addRight(String groupName, Right right) { - throw new IllegalStateException("not implemented !"); + if(groupName==null || right ==null){ + throw new IllegalArgumentException("name cannot be null"); + } + Group group = groupRepository.findByName(groupName); + if (group == null) { + throw new IllegalArgumentException("group cannot be null"); + } + + group.setName(groupName); + List list = group.getRights(); + list.add(right); + group.setRights(list); + return true; } @Override public boolean removeRight(String groupName, Right right) { - throw new IllegalStateException("not implemented !"); + if(right==null){ + throw new IllegalArgumentException("name cannot be null"); + } + Group group = groupRepository.findByName(groupName); + if (group != null) { + throw new IllegalStateException("a group with the same name already exists"); + } + return true; } } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java new file mode 100644 index 0000000..e01a10d --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java @@ -0,0 +1,17 @@ +/* + * 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.Right; + +public interface RightRepository { + + Right findByName(String name); + + void delete(Long id); + + void save(Right right); +} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java new file mode 100644 index 0000000..9b8c21d --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java @@ -0,0 +1,120 @@ +/* + * 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 fr.plil.sio.persistence.api.Right; + +/** + * + * @author rvangrev + */ +public class RightRepositoryJdbc implements RightRepository{ + + private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); + + @Autowired + private DataSource dataSource; + + @Override + public Right findByName(String name) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + rs = stmt.executeQuery("SELECT * FROM GROUP_T WHERE NAME_C = \'" + name + "\'"); + if (rs.next()) { + logger.debug("found group " + name); +// Group group = new Group(); +// group.setId(rs.getLong("GROUP_ID")); +// group.setName(rs.getString("NAME_C")); +// return group; + } 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); + + } + } + return null; + } + + @Override + public void save(Right right) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + right.getName() + "\')", + Statement.RETURN_GENERATED_KEYS); + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + right.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 RIGHT_T WHERE RIGHT_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); + } + } + } +} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java index b1242a1..6f0f61b 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java @@ -8,19 +8,30 @@ import java.util.List; @Service public class RightServiceJdbc implements RightService { + @Override public Right create(String name) { - return null; + if(name==null){ + throw new IllegalArgumentException("name cannot be null"); + } + else{ + return null; + } } @Override public Right create(String name, Right parent) { - throw new IllegalStateException("not implemented !"); + return null; } @Override public boolean delete(Right right) { - throw new IllegalStateException("not implemented !"); + if(right==null){ + throw new IllegalArgumentException("name cannot be null"); + } + else{ + return true; + } } @Override @@ -30,6 +41,11 @@ public class RightServiceJdbc implements RightService { @Override public Right findOne(Long id) { - throw new IllegalStateException("not implemented !"); + if(id==null){ + throw new IllegalArgumentException("name cannot be null"); + } + else{ + return null; + } } } diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java new file mode 100644 index 0000000..31d6f24 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java @@ -0,0 +1,17 @@ +/* + * 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.User; +import fr.plil.sio.persistence.api.Group; + +public interface UserRepository { + + User findByName(String name); + + void delete(Long id); + + void save(User user, Group group); +} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java new file mode 100644 index 0000000..09295e2 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java @@ -0,0 +1,121 @@ +/* + * 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 + private DataSource dataSource; + + @Override + public User findByName(String name) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + 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 + "\' "); + if (rs.next()) { + logger.debug("found group " + name); + User user = new User(); + user.setId(rs.getLong("T.USER_ID")); + user.setName(rs.getString("T.NAME_U")); + Group group = new Group(); + GroupRepositoryJdbc g = new GroupRepositoryJdbc(); + group = g.findByName(rs.getString("G.NAME_C")); + user.setGroup(group); + 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(); + stmt.executeUpdate("INSERT INTO USER_T (NAME_U, GROUP_U) VALUES (\'" + user.getName() + "\', \'" + group.getId() + "\')", + 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); + } + } + } +} diff --git a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java index e884a52..2476c19 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java @@ -1,6 +1,7 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Right; +import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.User; import fr.plil.sio.persistence.api.UserService; import org.springframework.beans.factory.annotation.Autowired; @@ -10,25 +11,53 @@ import org.springframework.stereotype.Service; public class UserServiceJdbc implements UserService { @Autowired - private GroupRepository userRepository; + private UserRepository userRepository; + private GroupRepository groupRepository; @Override public User create(String name, String groupName) { - throw new IllegalStateException("not implemented !"); + if(name==null){ + throw new IllegalArgumentException("name cannot be null"); + } + User user = userRepository.findByName(name); + if(user!=null){ + throw new IllegalStateException("a user with the same name already exists"); + } + user = new User(); + user.setName(name); + Group group = groupRepository.findByName(groupName); + userRepository.save(user, group); + return user; } @Override public boolean delete(String name) { - throw new IllegalStateException("not implemented !"); + if(name == null){ + throw new IllegalStateException("not implemented !"); + } + User user = findByName(name); + if(user==null){ + return false; + } + userRepository.delete(user.getId()); + return true; } @Override public User findByName(String name) { - throw new IllegalStateException("not implemented !"); + if(name==null){ + throw new IllegalArgumentException("name cannot be null"); + } + return userRepository.findByName(name); } @Override public boolean isUserHasRight(String userName, Right right) { - throw new IllegalStateException("not implemented !"); + if(right==null){ + throw new IllegalArgumentException("name cannot be null"); + } + else{ + return false; + } } } -- libgit2 0.21.2