/*
 * 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 sikd.ikonsol.ejb.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import sikd.ikonsol.model.User;

/**
 *
 * @author Imam Bhoim
 */
public class UserDao implements IDAO<User>, Serializable {

    private SqlSessionFactory sqlSessionFactory = null;
    private SqlSession session = null;

    public UserDao(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public User createNew() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<User> findAll() {
        List<User> list = new ArrayList<User>();

        session = sqlSessionFactory.openSession();
        try {
            list = session.selectList("UserMapper.selectAll");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    @Override
    public List<User> findByCriteria(String sql) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User findByID(int id) {
        session = sqlSessionFactory.openSession();
        User obj = null;
        try {
            obj = session.selectOne("UserMapper.selectId", id);

        } finally {
            session.close();
        }
        return obj;
    }

    @Override
    public User findByKode(String kode) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public User save(User obj) {
        session = sqlSessionFactory.openSession();
        session.insert("UserMapper.insertOne", obj);
        session.commit();
        session.close();
        return obj;
    }

    @Override
    public User update(User obj) {
        session = sqlSessionFactory.openSession();
        session.insert("UserMapper.updateOne", obj);
        session.commit();
        session.close();
        return obj;
    }

    @Override
    public void deleteByID(int id) {
        session = sqlSessionFactory.openSession();
        session.delete("UserMapper.deleteOne", id);
        session.commit();
        session.close();
    }

    @Override
    public void deleteByPK(String pk) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
