package com.jasamedika.medifirst2000.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jasamedika.medifirst2000.converter.KelompokUserConverter;
import com.jasamedika.medifirst2000.dao.KelompokUserDao;
import com.jasamedika.medifirst2000.entities.KelompokUser;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.KelompokUserService;
import com.jasamedika.medifirst2000.vo.KelompokUserVO;

/**
 * Implement class for KelompokUserService
 * 
 * @author Roberto
 */
@Service("KelompokUserService")
public class KelompokUserServiceImpl extends BaseVoServiceImpl implements
		KelompokUserService {
	@Autowired
	private KelompokUserDao kelompokUserDao;

	@Autowired
	private KelompokUserConverter kelompokUserConverter;

	@Override
	@Transactional(readOnly = false)
	public KelompokUserVO add(KelompokUserVO vo) throws JpaSystemException,
			ServiceVOException {
		// convert to model and call repository
		KelompokUser kelUser = new KelompokUser();
		kelompokUserConverter.transferVOToModel(vo, kelUser);

		KelompokUser resultModel = kelompokUserDao.save(kelUser);

		// return VO
		KelompokUserVO resultVo = new KelompokUserVO();
		kelompokUserConverter.transferModelToVO(resultModel, resultVo);

		return resultVo;
	}

	@Override
	@Transactional(readOnly = false)
	public KelompokUserVO update(KelompokUserVO vo) throws JpaSystemException,
			ServiceVOException {

		KelompokUser kelUser = kelompokUserDao.findOne(vo.getId());

		kelompokUserDao.save(kelUser);

		kelompokUserConverter.transferVOToModel(vo, kelUser);

		return vo;
	}

	@Override
	@Transactional(readOnly = false)
	public Boolean delete(Integer key) throws JpaSystemException {
		KelompokUser kelUser = kelompokUserDao.findOne(key);
		if (kelUser != null) {
			kelompokUserDao.delete(kelUser);
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public KelompokUserVO findById(Integer key) throws JpaSystemException {
		KelompokUser kelUser = kelompokUserDao.findOne(key);
		if (kelUser == null) {
			return null;
		}
		KelompokUserVO kelUserVO = new KelompokUserVO();
		kelompokUserConverter.transferModelToVO(kelUser, kelUserVO);
		return kelUserVO;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<KelompokUserVO> findAll() throws JpaSystemException {

		List<KelompokUser> kelUserList = IteratorUtils.toList(kelompokUserDao
				.findAll().iterator());

		List<KelompokUserVO> kelUserVOList = new ArrayList<KelompokUserVO>();
		kelompokUserConverter.transferListOfModelToListOfVO(kelUserList,
				kelUserVOList);

		return kelUserVOList;
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(
			Integer page, Integer limit, String sort, String dir) {
		return null;
	}

}
