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.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;

import com.jasamedika.medifirst2000.converter.BaseConverterImpl;
import com.jasamedika.medifirst2000.dao.GradeRemunDao;
import com.jasamedika.medifirst2000.entities.GradeRemunerasi;
import com.jasamedika.medifirst2000.exception.ServiceVOException;
import com.jasamedika.medifirst2000.service.GradeRemunService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.vo.GradeRemunerasiVO;

@Service("gradeRemunService")
public class GradeRemunServiceImpl extends BaseVoServiceImpl implements GradeRemunService {

	@Autowired
	private BaseConverterImpl<GradeRemunerasiVO, GradeRemunerasi> gradeRemunConverter;

	@Autowired
	private GradeRemunDao gradeRemunDao;

	@Override
	public GradeRemunerasiVO add(GradeRemunerasiVO vo) throws JpaSystemException, ServiceVOException {
		GradeRemunerasi grade = gradeRemunConverter.transferVOToModel(vo, new GradeRemunerasi());

		GradeRemunerasi resultModel = gradeRemunDao.save(grade);

		GradeRemunerasiVO resultVO = new GradeRemunerasiVO();
		resultVO = gradeRemunConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

	@Override
	public GradeRemunerasiVO update(GradeRemunerasiVO vo) throws JpaSystemException, ServiceVOException {
		GradeRemunerasi gradeLama = gradeRemunDao.findOne(vo.getId());

		GradeRemunerasi gradeBaru = gradeRemunConverter.transferVOToModel(vo, gradeLama);

		GradeRemunerasi resultModel = gradeRemunDao.save(gradeBaru);

		GradeRemunerasiVO resultVO = new GradeRemunerasiVO();
		resultVO = gradeRemunConverter.transferModelToVO(resultModel, resultVO);

		return resultVO;
	}

	@Override
	public Boolean delete(Integer key) throws JpaSystemException {
		gradeRemunDao.delete(key);

		return true;
	}

	@Override
	public GradeRemunerasiVO findById(Integer key) throws JpaSystemException {
		GradeRemunerasiVO result = new GradeRemunerasiVO();

		GradeRemunerasi grade = gradeRemunDao.findOne(key);
		if (CommonUtil.isNotNullOrEmpty(grade)) {
			result = gradeRemunConverter.transferModelToVO(grade, result);
		}

		return result;
	}

	@Override
	public List<GradeRemunerasiVO> findAll() throws JpaSystemException {
		List<GradeRemunerasiVO> result = new ArrayList<GradeRemunerasiVO>();

		Iterable<GradeRemunerasi> models = gradeRemunDao.findAll();
		if (CommonUtil.isNotNullOrEmpty(models)) {
			for (GradeRemunerasi model : models) {
				GradeRemunerasiVO vo = new GradeRemunerasiVO();
				vo = gradeRemunConverter.transferModelToVO(model, vo);
				result.add(vo);
			}
		}

		return result;
	}

	@Override
	public Map<String, Object> findAllWithPageAndLimitAndSortByAndDirectionParameter(Integer page, Integer limit,
			String sort, String dir) {

		return null;
	}

}
