2021-01-07 11:34:56 +07:00

79 lines
1.9 KiB
Java

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.Alamat;
import com.jasamedika.medifirst2000.vo.AlamatVO;
/**
* Converter class between Alamat and AlamatVO
*
* @author Askur
*/
@Component
public class AlamatConverter implements BaseVoConverter<AlamatVO, Alamat> {
public Alamat transferVOToModel(AlamatVO vo, Alamat model) {
if (null == model)
model = new Alamat();
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, Alamat.class);
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
public List<AlamatVO> transferListOfModelToListOfVO(List<Alamat> models,
List<AlamatVO> vos) {
if (null == vos)
vos = new ArrayList<AlamatVO>();
if (null == models)
return vos;
for (Alamat alamat : models) {
AlamatVO vo = new AlamatVO();
vo = transferModelToVO(alamat, vo);
vos.add(vo);
}
return vos;
}
@Override
public AlamatVO transferModelToVO(Alamat model, AlamatVO vo) {
if (null == vo)
vo = new AlamatVO();
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, AlamatVO.class);
} catch (Exception e) {
e.printStackTrace();
}
return vo;
}
}