UserRepositoryJdbc.java 4.07 KB
/*
 * 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);
            }
        }
    }
}