package sikd.sikd5.sikd5ejb.session.login;

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.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.tabel.Pemda;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5ejb.dao.login.LoginDao;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

/**
 * @author : agus
 * @tanggal : Dec 10, 2018
 */
@Stateless
@Remote({LoginSessionBeanRemote.class})
public class LoginSessionBean implements LoginSessionBeanRemote {
	private SqlSessionFactory sqlSessionFactoryLogin;
	private SqlSession sessionLogin = null;
	LoginDao dao;

	@PostConstruct
	public void init() {
		try {
			sqlSessionFactoryLogin = MyBatisConnectionFactory.getSqlSessionFactoryLogin();
			dao = new LoginDao();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public TUserAccount getUserAccountbyName(String userName) throws Exception {
		TUserAccount info = new TUserAccount();
		try {
			sessionLogin = sqlSessionFactoryLogin.openSession();
			info = dao.getUserAccountbyName(userName, sessionLogin);
			return info;
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		} finally {
			sessionLogin.close();
		}

	}

	@Override
	public List<Pemda> getPemdaByTahuns(short tahun) throws Exception {
		List<Pemda> hasil = new ArrayList();
		try {
			sessionLogin = sqlSessionFactoryLogin.openSession();
			hasil = dao.getPemdaByTahuns(tahun, sessionLogin);
			return hasil;
		} catch (Exception e) {
			throw new SIKDServiceException(e.getMessage());
		} finally {
			sessionLogin.close();
		}
	}

}
