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 0438208..cd18cff 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java @@ -90,7 +90,7 @@ public class GroupRepositoryJdbc implements GroupRepository { ResultSet rs = null; try { stmt = dataSource.getConnection().createStatement(); - stmt.execute("DELETE FROM GROUP_T WHERE GROUP_ID="+id, + stmt.executeUpdate("DELETE FROM GROUP_T WHERE GROUP_ID="+id, Statement.RETURN_GENERATED_KEYS); rs = stmt.getGeneratedKeys(); 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..f8000e2 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.GroupService; import fr.plil.sio.persistence.api.Right; +import java.sql.SQLException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -29,7 +30,17 @@ public class GroupServiceJdbc implements GroupService { @Override public boolean delete(String name) { - throw new IllegalStateException("not implemented !"); + //throw new IllegalStateException("not implemented !"); + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + + Group group = groupRepository.findByName(name); + if(group!=null){ + groupRepository.delete(group.getId()); + return true; + } + return false; } @Override 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..81c075d --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepository.java @@ -0,0 +1,21 @@ +/* + * 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; + +/** + * + * @author msahmane + */ +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..1ed75d0 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.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.Right; +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; + +/** + * + * @author msahmane + */ +@Repository +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 RIGHT_T WHERE NAME_R = \'" + name + "\'"); + if (rs.next()) { + logger.debug("found group " + name); + Right right = new Right(); + right.setId(rs.getLong("RIGHT_ID")); + right.setName(rs.getString("NAME_R")); + return right; + } 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(Right right) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("INSERT INTO RIGHT_T (NAME_R) 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; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("DELETE FROM RIGHT_T WHERE RIGHT_ID="+id, + Statement.RETURN_GENERATED_KEYS); + rs = stmt.getGeneratedKeys(); + + } 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/UserRepository.java b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java new file mode 100644 index 0000000..3a64ee9 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java @@ -0,0 +1,22 @@ +/* + * 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; + +/** + * + * @author msahmane + */ +public interface UserRepository { + + User findByName(String name); + + void delete(Long id); + + void save(User user); + +} 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..f38cdcd --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java @@ -0,0 +1,122 @@ +/* + * 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 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; + +/** + * + * @author msahmane + */ + +@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 * FROM USER_T WHERE NAME_U = \'" + name + "\'"); + if (rs.next()) { + logger.debug("found group " + name); + User user= new User(); + user.setId(rs.getLong("USER_ID")); + user.setName(rs.getString("NAME_U")); + 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) { + Statement stmt = null; + ResultSet rs = null; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("INSERT INTO USER_T (NAME_U) VALUES (\'" + user.getName() + "\')", + 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; + try { + stmt = dataSource.getConnection().createStatement(); + stmt.executeUpdate("DELETE FROM USER_T WHERE USER_ID="+id, + Statement.RETURN_GENERATED_KEYS); + rs = stmt.getGeneratedKeys(); + + } 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..862f6c7 100644 --- a/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java +++ b/src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java @@ -10,21 +10,43 @@ import org.springframework.stereotype.Service; public class UserServiceJdbc implements UserService { @Autowired - private GroupRepository userRepository; + private UserRepository userRepository; @Override - public User create(String name, String groupName) { - throw new IllegalStateException("not implemented !"); + public User create(String name, String userName) { + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + User user = userRepository.findByName(name); + if (user != null) { + throw new IllegalStateException("a group with the same name already exists"); + } + user = new User(); + user.setName(name); + userRepository.save(user); + return user; } @Override public boolean delete(String name) { - throw new IllegalStateException("not implemented !"); + if (name == null) { + throw new IllegalArgumentException("name cannot be null"); + } + + User user = userRepository.findByName(name); + if(user!=null){ + userRepository.delete(user.getId()); + return true; + } + return false; } @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 -- libgit2 0.21.2