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.DraftSurat;
import com.jasamedika.medifirst2000.vo.DraftSuratVO;

/**
 * Converter class between DraftSurat and DraftSuratVO
 * 
 * @author Askur
 */
@Component
public class DraftSuratConverter implements BaseVoConverter<DraftSuratVO, DraftSurat> {
	

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

	}

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

		if (null == models)
			return vos;

		for (DraftSurat draftSurat : models) {
			DraftSuratVO vo = new DraftSuratVO();
			vo = transferModelToVO(draftSurat, vo);
			vos.add(vo);
		}

		return vos;
	}

	@Override
	public DraftSuratVO transferModelToVO(DraftSurat model, DraftSuratVO vo) {

		if (null == vo)
			vo = new DraftSuratVO();
		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, DraftSuratVO.class);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return vo;
	
	}

	
}
