115 lines
3.0 KiB
Java
115 lines
3.0 KiB
Java
package com.jasamedika.medifirst2000.converter;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.persistence.Column;
|
|
import javax.persistence.JoinColumn;
|
|
import javax.persistence.OneToMany;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.jasamedika.medifirst2000.converter.base.BaseVoConverter;
|
|
import com.jasamedika.medifirst2000.entities.Peran;
|
|
import com.jasamedika.medifirst2000.vo.PeranVO;
|
|
|
|
@Component
|
|
public class PeranConverter implements BaseVoConverter<PeranVO, Peran>{
|
|
|
|
@Override
|
|
public Peran transferVOToModel(PeranVO vo, Peran model) {
|
|
if (null == model)
|
|
model = new Peran();
|
|
|
|
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, Peran.class);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return model;
|
|
}
|
|
|
|
@Override
|
|
public List<PeranVO> transferListOfModelToListOfVO(List<Peran> models, List<PeranVO> vos) {
|
|
|
|
if (null == vos)
|
|
vos = new ArrayList<PeranVO>();
|
|
|
|
if (null == models)
|
|
return vos;
|
|
|
|
for (Peran pegawai : models) {
|
|
PeranVO vo = new PeranVO();
|
|
vo=transferModelToVO(pegawai, vo);
|
|
vos.add(vo);
|
|
}
|
|
|
|
return vos;
|
|
}
|
|
|
|
@Override
|
|
public PeranVO transferModelToVO(Peran model, PeranVO vo) {
|
|
if (null == vo)
|
|
vo = new PeranVO();
|
|
try {
|
|
|
|
List<String> fieldsToInclude = new ArrayList<String>();
|
|
for (Field field : model.GetFields(model.getClass())) {
|
|
String str = field.getName();
|
|
String name = field.getName();
|
|
if(name.equals("serialVersionUID"))continue;
|
|
Boolean valid = false;
|
|
for (java.lang.annotation.Annotation annotation : field.getDeclaredAnnotations()) {
|
|
if (annotation instanceof JoinColumn) {
|
|
valid=true;
|
|
} else if (annotation instanceof Column) {
|
|
Column column = (Column)annotation;
|
|
if(column.name().endsWith("Fk"))
|
|
if(field.getName().endsWith("Id")==false)
|
|
valid=true;
|
|
}else if (annotation instanceof OneToMany) {
|
|
|
|
valid=true;
|
|
}
|
|
|
|
}
|
|
if(valid ==false)
|
|
fieldsToInclude.add(str);
|
|
|
|
}
|
|
Map<String, Object> serialized = model.serialize(fieldsToInclude.toArray(new String[0]),model.getClass().getSimpleName());
|
|
Gson gson = new Gson();
|
|
String json = gson.toJson(serialized);
|
|
vo = gson.fromJson(json, PeranVO.class);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return vo;
|
|
}
|
|
|
|
public PeranVO transferModelToVOCustom(Peran model, PeranVO vo,String[] fieldsToInclude) {
|
|
if (null == vo)
|
|
|
|
vo = new PeranVO();
|
|
|
|
try {
|
|
Map<String, Object> serialized = model.serialize(fieldsToInclude,model.getClass().getSimpleName());
|
|
Gson gson = new Gson();
|
|
String json = gson.toJson(serialized);
|
|
vo = gson.fromJson(json, PeranVO.class);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return vo;
|
|
}
|
|
}
|