/*
 * 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.ReferensiPemda;
import sikd.ikonsol.model.ReferensiWilayah;

/**
 *
 * @author Imam Bhoim
 */
public class RefPemdaDao implements IDAO<ReferensiPemda>, Serializable {

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

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

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

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

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

        return list;
    }

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

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

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

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

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

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

    @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.
        session = sqlSessionFactory.openSession();
        session.delete("ReferensiPemdaMapper.deleteOne", pk);
        session.commit();
        session.close();
    }
    
     public List<ReferensiPemda> findAllProvinsi(String id) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<ReferensiPemda> list = new ArrayList<ReferensiPemda>();

        session = sqlSessionFactory.openSession();
        try {
            list = session.selectList("ReferensiPemdaMapper.selectAllProvinsi", id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

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

        session = sqlSessionFactory.openSession();
        try {
            list = session.selectList("ReferensiPemdaMapper.selectKodeWilayah", id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }
      
       public ReferensiPemda updateWilayah(ReferensiPemda obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        session = sqlSessionFactory.openSession();
        session.update("ReferensiPemdaMapper.updateOneWilayah", obj);
        session.commit();
        session.close();
        return obj;
    }
       
        public List<ReferensiWilayah> findAllWilayah() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<ReferensiWilayah> list = new ArrayList<ReferensiWilayah>();

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

        return list;
    }

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

        } finally {
            session.close();
        }
        return obj;
    }
   
   public ReferensiWilayah findByKodeWilayah(String kode) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        session = sqlSessionFactory.openSession();
        ReferensiWilayah obj = null;
        try {
            obj = session.selectOne("ReferensiPemdaMapper.selectNamaWilayah", kode);

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

}
