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.google.gson.GsonBuilder;
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
import com.jasamedika.medifirst2000.entities.DesaKelurahan;
import com.jasamedika.medifirst2000.vo.DesaKelurahanVO;

/**
 * Converter class between DesaKelurahan and DesaKelurahanVO
 * 
 * @author Askur
 */
@Component
public class DesaKelurahanConverter implements BaseVoConverter<DesaKelurahanVO, DesaKelurahan> {
	

	public DesaKelurahan transferVOToModel(DesaKelurahanVO vo, DesaKelurahan model) {
		if (null == model)
			model = new DesaKelurahan();
		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, DesaKelurahan.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;

	}

	public List<DesaKelurahanVO> transferListOfModelToListOfVO(List<DesaKelurahan> models,
			List<DesaKelurahanVO> vos) {
		if (null == vos)
			vos = new ArrayList<DesaKelurahanVO>();

		if (null == models)
			return vos;

		for (DesaKelurahan DesaKelurahan : models) {
			DesaKelurahanVO vo = new DesaKelurahanVO();
			vo = transferModelToVO(DesaKelurahan, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public DesaKelurahanVO transferModelToVO(DesaKelurahan model, DesaKelurahanVO vo) {

		if (null == vo)
			vo = new DesaKelurahanVO();
		try {
			String[] fieldsToInclude = null;
			Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
			Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
	        String json = gson.toJson(serialized);
	        Gson gson2 =new Gson(); 
	        vo = gson2.fromJson(json, DesaKelurahanVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	
	}

	
}
