package com.jasamedika.medifirst2000.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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.MapPegawaiToModulAplikasiConverter;
import com.jasamedika.medifirst2000.dao.MapPegawaiToModulAplikasiDao;
import com.jasamedika.medifirst2000.entities.MapPegawaiToModulAplikasi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.MapPegawaiToModulAplikasiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.MapPegawaiToModulAplikasiVO;

@Service("MapPegawaiToModulAplikasiService")
public class MapPegawaiToModulAplikasiServiceImpl extends BaseVoServiceImpl implements MapPegawaiToModulAplikasiService<MapPegawaiToModulAplikasiVO> {

	
	@Autowired
	private MapPegawaiToModulAplikasiDao mapPegawaiToModulAplikasiDao;

	@Autowired
	private MapPegawaiToModulAplikasiConverter mapPegawaiToModulAplikasiConverter;
	
	@Override
	@Transactional(readOnly = false)
	public List<MapPegawaiToModulAplikasiVO> findByPegawaiIdAndModulAplikasiId(Integer pegawaiId, Integer modulAplikasiId){
		return mapPegawaiToModulAplikasiConverter.transferListOfModelToListOfVO(mapPegawaiToModulAplikasiDao.findByPegawaiIdAndModulAplikasiId(pegawaiId, modulAplikasiId), null);
	}
	
	
	@Override
	@Transactional(readOnly = false)
	public MapPegawaiToModulAplikasiVO add(MapPegawaiToModulAplikasiVO vo) throws JpaSystemException, ServiceVOException {
		MapPegawaiToModulAplikasi mapPegawaiToModulAplikasi = mapPegawaiToModulAplikasiConverter.transferVOToModel(vo, new MapPegawaiToModulAplikasi());
		MapPegawaiToModulAplikasi resultModel = mapPegawaiToModulAplikasiDao.save(mapPegawaiToModulAplikasi);
		MapPegawaiToModulAplikasiVO resultVo = new MapPegawaiToModulAplikasiVO();
		resultVo = mapPegawaiToModulAplikasiConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

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

		MapPegawaiToModulAplikasi mapPegawaiToModulAplikasi = mapPegawaiToModulAplikasiDao.findOne(vo.getId());
		mapPegawaiToModulAplikasi = mapPegawaiToModulAplikasiConverter.transferVOToModel(vo, mapPegawaiToModulAplikasi);

		mapPegawaiToModulAplikasiDao.save(mapPegawaiToModulAplikasi);
		return vo;
	}

	@Override
	@Transactional(readOnly = false)
	public Boolean delete(Integer key) throws JpaSystemException {
		if (CommonUtil.isNotNullOrEmpty(key)) {

			MapPegawaiToModulAplikasi mapPegawaiToModulAplikasi = mapPegawaiToModulAplikasiDao.findOne(key);
			mapPegawaiToModulAplikasiDao.delete(mapPegawaiToModulAplikasi);
			MapPegawaiToModulAplikasiVO mapPegawaiToModulAplikasiVO = mapPegawaiToModulAplikasiConverter.transferModelToVO(mapPegawaiToModulAplikasi, new MapPegawaiToModulAplikasiVO());
			mapPegawaiToModulAplikasiVO.setStatusEnabled(false);
			return true;
		} else {
			return false;
		}

	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public MapPegawaiToModulAplikasiVO findById(Integer key) throws JpaSystemException {
		return mapPegawaiToModulAplikasiConverter.transferModelToVO(mapPegawaiToModulAplikasiDao.findOne(key), null);
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<MapPegawaiToModulAplikasiVO> findAll() throws JpaSystemException {
		return mapPegawaiToModulAplikasiConverter.transferListOfModelToListOfVO(mapPegawaiToModulAplikasiDao.findAll(), null);
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {
		
		List<MapPegawaiToModulAplikasiVO> mapPegawaiToModulAplikasiVOList = new ArrayList<MapPegawaiToModulAplikasiVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<MapPegawaiToModulAplikasi> resultPage = mapPegawaiToModulAplikasiDao.findAll(pageable);
		List<MapPegawaiToModulAplikasi> mapPegawaiToModulAplikasiList = resultPage.getContent();

		mapPegawaiToModulAplikasiConverter.transferListOfModelToListOfVO(mapPegawaiToModulAplikasiList, mapPegawaiToModulAplikasiVOList);

		return constructMapReturn(mapPegawaiToModulAplikasiVOList, resultPage.getTotalElements(), resultPage.getTotalPages());
	}
	
}
