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.DashboardChartValue;
import sikd.sikd5.sikd5bean.DashboardValue;
import sikd.sikd5.sikd5bean.StatusData;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5ejb.dao.HomeDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

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

    private SqlSessionFactory sqlSessionFactory;
    private SqlSession session = null;

    private HomeDao dao;

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

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

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

    }

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

    }

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

    }

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

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

    }

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

    }

}
