package com.jasamedika.medifirst2000.converter;

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

import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.entities.Departemen;
import com.jasamedika.medifirst2000.vo.DepartemenVO;

@Component
public class DepartemenConverter implements BaseVoConverter<DepartemenVO, Departemen> {

	@Override
	public Departemen transferVOToModel(DepartemenVO vo, Departemen model) {
		if (null == model)
			model = new Departemen();
		try {
			String[] fieldsToInclude = null;
			Map<String, Object> serialized = vo.serialize(fieldsToInclude,vo.getClass().getName());
			Gson gson = new Gson();
	        String json = gson.toJson(serialized);
	        model = gson.fromJson(json, Departemen.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return model;
	}

	@Override
	public List<DepartemenVO> transferListOfModelToListOfVO(List<Departemen> models, List<DepartemenVO> vos) {

		if (null == vos)
			vos = new ArrayList<DepartemenVO>();

		if (null == models)
			return vos;

		for (Departemen departemen : models) {
			DepartemenVO vo = new DepartemenVO();
			vo=transferModelToVO(departemen, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public DepartemenVO transferModelToVO(Departemen model, DepartemenVO vo) {
		if (null == vo)
			vo = new DepartemenVO();
		try {
			String[] fieldsToInclude = null;
			Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
			Gson gson = new Gson();
	        String json = gson.toJson(serialized);
	        vo = gson.fromJson(json, DepartemenVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	}
}
