/*
 * 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 org.mybatis.cdi.Transactional;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.query.AmbilPosisiKasBean;
import sikd.sikd5.sikd5bean.query.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.simpatik.Posisikasakun;
import sikd.sikd5.sikd5bean.ws.simpatik.PosisiKas_WS;
import sikd.sikd5.sikd5ejb.dao.PosisiKasDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

@Stateless
@Remote({ PosisiKasSessionBeanRemote.class })
public class PosisiKasSessionBean implements PosisiKasSessionBeanRemote {
	private SqlSessionFactory sqlSessionFactory;
	private SqlSession session = null;
	PosisiKasDao dao;

	@PostConstruct
	public void init() {
		try {
			sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
			dao = new PosisiKasDao();
		} 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) {
			e.printStackTrace();
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}
		return info.getInfoString();
	}

	@Override
//	@Transactional
	public long insert(PosisiKas_WS posisiKas, String nmfile) throws Exception {
		long hasil = 0;
//		System.out.println("Insert Posisi Kas........................................");
		try {
			if (posisiKas == null) {
				throw new SIKDServiceException("Data Posisi Kas Kosong");
			}
//			System.out.println("sqlSessionFactory.openSession(false)........................................");
			session = sqlSessionFactory.openSession();
			hasil = dao.insert(posisiKas, nmfile, session);
			
			session.commit();
			return hasil;
		} catch (Exception e) {
			
			session.rollback(true);
//			System.out.println("session.rollback()........................................");
//			e.printStackTrace();
			throw new SIKDServiceException(e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}

	}
	@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) {
			e.printStackTrace();
			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) {
			e.printStackTrace();
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}
		return infos;
	}
	   @Override
	    public List<Posisikasakun> getPosisiKasAkun(String kodeSatker, short tahun, short bulan) throws Exception {
	        List<Posisikasakun> infos = new ArrayList<Posisikasakun>();
	        try {
	            session = sqlSessionFactory.openSession();
	            infos = dao.getPosisiKasAkun(kodeSatker, tahun, bulan, session);
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new SIKDServiceException(e.getMessage());
	        } finally {
	            session.close();
	        }
	        return infos;
	    }

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