package sikd.sikd5.sikd5ejb.session;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.laporan.Lampiran1Apbd;
import sikd.sikd5.sikd5ejb.dao.LaporanDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

/**
 * @author : Soraturaes
 */
@Stateless
@Remote({LaporanSessionBeanRemote.class})
public class LaporanSessionBean implements LaporanSessionBeanRemote {

    private SqlSessionFactory sqlSessionFactory;
    private SqlSession session = null;

    private LaporanDao dao;

    @PostConstruct
    public void init() {
        try {
            sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
            dao = new LaporanDao();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public List<Lampiran1Apbd> getLaporanApbdNasional(short tahun)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanApbdNasional(tahun, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanApbdPemda(short tahun, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanApbdPemda(tahun, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedLraNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedLraNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedLraPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedLraPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanLraNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanLraNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanLraPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanLraPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedLoNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedLoNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedLoPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedLoPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanLoNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanLoNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanLoPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanLoPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedNeracaNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedNeracaNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanPostingedNeracaPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanPostingedNeracaPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanNeracaNasional(String tanggal1, String tanggal2)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanNeracaNasional(tanggal1, tanggal2, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanNeracaPemda(String tanggal1, String tanggal2, String kodesatker)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanNeracaPemda(tanggal1, tanggal2, kodesatker, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

    @Override
    public List<Lampiran1Apbd> getLaporanJumlahPemdaPengirim(short tahun)
			throws Exception {
        List<Lampiran1Apbd> hasil = new ArrayList<>();
        try {
            session = sqlSessionFactory.openSession();
            hasil = dao.getLaporanJumlahPemdaPengirim(tahun, session);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SIKDServiceException(e.getMessage());
        } finally {
            session.close();
        }
        return hasil;

    }

}
