/* * 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 java.util.List; import java.util.ArrayList; 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; import org.springframework.stereotype.Repository; @Repository public class RightRepositoryJdbc implements RightRepository { private GroupRepository groupRepository; private RightRepository rightRepository; private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); @Autowired private DataSource dataSource; @Override public List 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 + "\'"); ArrayList list = new ArrayList(); while (rs.next()) { logger.debug("found group " + name); Right right = new Right(); right.setId(rs.getLong("RIGHT_ID")); right.setName(rs.getString("NAME_R")); if(right!=null){ list.add(right); } } return list; } 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 Right findOne(Long id){ Statement stmt = null; ResultSet rs = null; try { stmt = dataSource.getConnection().createStatement(); rs = stmt.executeQuery("SELECT * FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' LIMIT 1"); if (rs.next()) { logger.debug("found id " + id); Right right = new Right(); right.setId(rs.getLong("RIGHT_ID")); right.setName(rs.getString("NAME_R")); return 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); } } return null; } @Override public void save(Right right, int parentId) { Statement stmt = null; ResultSet rs = null; try { stmt = dataSource.getConnection().createStatement(); stmt.executeUpdate("INSERT INTO RIGHT_T (NAME_R, PARENT_ID) VALUES (\'" + right.getName() + "\', "+ parentId +")", 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 boolean delete(Long id) { Statement stmt = null; ResultSet rs = null; if(id==null){ return false; } if(findOne(id)==null){ throw new IllegalArgumentException("droit inexistant"); } try { stmt = dataSource.getConnection().createStatement(); stmt.execute("DELETE FROM RIGHT_T WHERE RIGHT_ID = \'" + id + "\' OR PARENT_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); } } return true; } }