/*
 * 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 sikd.ikonsol.model.Periode;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author User
 */
public class PeriodeDao implements IDAO<Periode>, Serializable {

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

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

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

    public Periode findByKode(String kode) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        session = sqlSessionFactory.openSession();
        Periode obj = null;
        try {
            obj = session.selectOne("PeriodeMapper.selectKode", kode);

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

    @Override
    public Periode save(Periode obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        session = sqlSessionFactory.openSession();
        session.insert("PeriodeMapper.insertOne", obj);
        session.commit();
        session.close();
        return obj;
    }

    @Override
    public Periode update(Periode obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        session = sqlSessionFactory.openSession();
        session.insert("PeriodeMapper.updateOne", obj);
        session.commit();
        session.close();
        return obj;
    }

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

    @Override
    public List<Periode> findAll() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<Periode> list = new ArrayList<Periode>();

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

        return list;
    }

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

        session = sqlSessionFactory.openSession();
        try {
            list = session.selectList("PeriodeMapper.selectKode",kode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

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

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

    public void deleteKode(String kode) {
        session = sqlSessionFactory.openSession();
        session.delete("PeriodeMapper.deleteOne", kode);
        session.commit();
        session.close();

    }
    
     public List<Periode> findAllAktif() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<Periode> list = new ArrayList<Periode>();

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

        return list;
    }

}
