/*
 * 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.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.query.AmbilPerkiraanBelanjaBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.simpatik.Perkiraanbelanjaakun;
import sikd.sikd5.sikd5bean.ws.simpatik.PerkiraanBelanja_WS;
import sikd.sikd5.sikd5ejb.dao.PerkiraanBelanjaDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

@Stateless
@Remote({ PerkiraanBelanjaSessionBeanRemote.class })
public class PerkiraanBelanjaSessionBean implements PerkiraanBelanjaSessionBeanRemote {
	private PerkiraanBelanjaDao dao;
	private SqlSessionFactory sqlSessionFactory;
	private SqlSession session = null;

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

	@Override
	public String getInfoPengiriman(String kodeSatker, short tahun, short periode) throws Exception {
		InfoPengiriman info = new InfoPengiriman();
		try {
			session = sqlSessionFactory.openSession();
			info = dao.getInfoPengiriman(kodeSatker, tahun, periode, session);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}
		return info.getInfoString();
	}

	@Override
//	@Transactional
	public long insert(PerkiraanBelanja_WS perkiraanBelanja, String nmfile) throws Exception {
		long hasil = 0;
		try {
			if (perkiraanBelanja == null) {
				throw new SIKDServiceException("Data Perkiraan Belanja Kosong");
			}
			session = sqlSessionFactory.openSession();
			hasil = dao.insert(perkiraanBelanja, nmfile, session);
			session.commit();
		} catch (Exception e) {
			session.rollback(true);
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}
		return hasil;
	}

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

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

	@Override
	public List<Perkiraanbelanjaakun> getPerkiraanBelanjaAkun(String kodeSatker, short tahun, short bulan) throws Exception {
		List<Perkiraanbelanjaakun> infos = new ArrayList<>();
		try {
			session = sqlSessionFactory.openSession();
			infos = dao.getPerkiraanbelanjaAkun(kodeSatker, tahun, bulan, session);
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}
		return infos;
	}

   @Override
   public List<AmbilPerkiraanBelanjaBean> ambilPerkiraanBelanjaPerPemda(short tahun, short bulan, Date tgakhir, String[] inAkun)
         throws Exception {
      List<AmbilPerkiraanBelanjaBean> infos = new ArrayList<>();
      try {
         session = sqlSessionFactory.openSession();
         infos = dao.ambilPerkiraanBelanjaPerPemda(tahun, bulan, tgakhir, inAkun, session);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      } finally {
         session.close();
      }
      return infos;
   }
}
