package sikd.sikd5.sikd5ejb.session;

import java.util.ArrayList;
import java.util.Date;
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.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikDataKirimPemdaChart;
import sikd.sikd5.sikd5bean.query.MonitorSimpatikPemdaKoneksi;
import sikd.sikd5.sikd5ejb.dao.LraDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

/**
 * @author : agus
 * @tanggal : Jan 10, 2019
 */
@Stateless
@Remote({LraSessionBeanRemote.class})
public class LraSessionBean implements LraSessionBeanRemote {

    private SqlSessionFactory sqlSessionFactory;
    private SqlSession session = null;

    private LraDao dao;

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

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

    }

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

    }

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

    }

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

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

}
