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.NotifikasiMessageObjekModulConverter;
import com.jasamedika.medifirst2000.dao.NotifikasiMessageObjekModulDao;
import com.jasamedika.medifirst2000.entities.NotifikasiMessageObjekModul;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.NotifikasiMessageObjekModulService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.NotifikasiMessageObjekModulVO;

@Service("NotifikasiMessageObjekModulService")
public class NotifikasiMessageObjekModulServiceImpl extends BaseVoServiceImpl 
		implements NotifikasiMessageObjekModulService<NotifikasiMessageObjekModulVO> {
	@Autowired
	NotifikasiMessageObjekModulConverter notifikasiMessageObjekModulConverter;

	@Autowired
	NotifikasiMessageObjekModulDao notifikasiMessageObjekModulDao;
	
	@Transactional(readOnly = true)
	public List<NotifikasiMessageObjekModulVO> findByNotifMessagingId(Integer notifMessagingId){
		
		List<NotifikasiMessageObjekModul> models = notifikasiMessageObjekModulDao.findByNotifMessagingId(notifMessagingId);
		
		List<NotifikasiMessageObjekModulVO> vos = notifikasiMessageObjekModulConverter.transferListOfModelToListOfVO(models, null);
		
		return vos;
	}

	@Transactional(readOnly = true)
	public List<NotifikasiMessageObjekModulVO> findByNotifMessagingIdAndRuanganId(Integer notifMessagingId, List<Integer> ruanganIds){
		List<NotifikasiMessageObjekModul> models = notifikasiMessageObjekModulDao.findByNotifMessagingIdAndRuanganId(notifMessagingId, ruanganIds);		
		List<NotifikasiMessageObjekModulVO> vos = notifikasiMessageObjekModulConverter.transferListOfModelToListOfVO(models, null);
		
		return vos;
	}

	@Override
	@Transactional(readOnly = false)
	public NotifikasiMessageObjekModulVO add(NotifikasiMessageObjekModulVO vo) throws JpaSystemException, ServiceVOException {
		NotifikasiMessageObjekModul notifikasiMessageObjekModul = notifikasiMessageObjekModulConverter.transferVOToModel(vo, new NotifikasiMessageObjekModul());
		NotifikasiMessageObjekModul resultModel = notifikasiMessageObjekModulDao.save(notifikasiMessageObjekModul);
		NotifikasiMessageObjekModulVO resultVo = new NotifikasiMessageObjekModulVO();
		resultVo = notifikasiMessageObjekModulConverter.transferModelToVO(resultModel, resultVo);
		return resultVo;
	}

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

		NotifikasiMessageObjekModul notifikasiMessageObjekModul = notifikasiMessageObjekModulDao.findOne(vo.getId());
		notifikasiMessageObjekModul = notifikasiMessageObjekModulConverter.transferVOToModel(vo, notifikasiMessageObjekModul);

		notifikasiMessageObjekModulDao.save(notifikasiMessageObjekModul);
		return vo;
	}

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

			NotifikasiMessageObjekModul notifikasiMessageObjekModul = notifikasiMessageObjekModulDao.findOne(key);
			notifikasiMessageObjekModulDao.delete(notifikasiMessageObjekModul);
			NotifikasiMessageObjekModulVO notifikasiMessageObjekModulVO = notifikasiMessageObjekModulConverter.transferModelToVO(notifikasiMessageObjekModul,
					new NotifikasiMessageObjekModulVO());
			notifikasiMessageObjekModulVO.setStatusEnabled(false);
			return true;
		} else {
			return false;
		}

	}

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

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

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

		List<NotifikasiMessageObjekModulVO> notifikasiMessageObjekModulVOList = new ArrayList<NotifikasiMessageObjekModulVO>();
		Pageable pageable = new PageRequest(page, limit, getSortBy(sort, dir));
		Page<NotifikasiMessageObjekModul> resultPage = notifikasiMessageObjekModulDao.findAll(pageable);
		List<NotifikasiMessageObjekModul> notifikasiMessageObjekModulList = resultPage.getContent();

		notifikasiMessageObjekModulConverter.transferListOfModelToListOfVO(notifikasiMessageObjekModulList, notifikasiMessageObjekModulVOList);

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