package fr.plil.sio.persistence.jdbc; import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.Right; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import javax.sql.DataSource; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @Repository public class GroupRepositoryJdbc implements GroupRepository { private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); @Autowired private DataSource dataSource; @Override public Group 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); } } } @Override public void save(Group group) { Statement stmt = null; ResultSet rs = null; try { stmt = dataSource.getConnection().createStatement(); stmt.executeUpdate("INSERT INTO GROUP_T (NAME_C) VALUES (\'" + group.getName() + "\')", Statement.RETURN_GENERATED_KEYS); rs = stmt.getGeneratedKeys(); if (rs.next()) { group.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 GROUP_ID = " + 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); } } } @Override public void removeRight(Long right, Long group){ Statement stmt = null; ResultSet rs = null; if(right==null || group==null){ throw new IllegalArgumentException(); } try { stmt = dataSource.getConnection().createStatement(); stmt.execute("DELETE FROM LINK_T WHERE GROUP_ID = " + group + " AND RIGHT_ID = " + right); } 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 addRight(Right right, Group group){ Statement stmt = null; ResultSet rs = null; if(right==null || group==null){ throw new IllegalArgumentException(); } try { stmt = dataSource.getConnection().createStatement(); stmt.execute("INSERT INTO LINK_T(RIGHT_ID, GROUP_ID) VALUES (" + right.getId() + "," + group.getId() + ")"); } 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); } } } }