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.InfoPengiriman;
import sikd.sikd5.sikd5bean.tabel.UploadFile;
import sikd.sikd5.sikd5ejb.dao.PosisiKasDao;
import sikd.sikd5.sikd5ejb.dao.UploadFileDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

@Stateless
@Remote({ UploadFileSessionBeanRemote.class })
public class UploadFileSessionBean implements UploadFileSessionBeanRemote {
	private SqlSessionFactory sqlSessionFactory;
	private SqlSession session = null;
	UploadFileDao dao;

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

	@Override
	public void insert(UploadFile uploadFile) throws Exception {
		try {
			session = sqlSessionFactory.openSession();
			dao.insert(uploadFile, session);
			session.commit();
		} catch (Exception e) {
			session.rollback();
			throw new SIKDServiceException(e.getMessage());
		} finally {
			session.close();
		}

	}

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

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

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

	public List<UploadFile> getUploadFilesPerPemda(short tahun, String kdikdSingkat, Date tgakhir) throws Exception {
      List<UploadFile> hasil = new ArrayList<>(); 
      try {
         session = sqlSessionFactory.openSession();
         hasil = dao.getUploadFilesPerPemda(tahun, kdikdSingkat, tgakhir, session);
      } catch (Exception e) {
         throw new SIKDServiceException(e.getMessage());
      } finally {
         session.close();
      }
      return hasil;
   }
	
}
