/**
*
* Model属性名称转换适配
* @author Administrator
*
*/
public class BsModelPropAdapter {
private BsModelPropAdapter(){}
/**
* 适配转换单个属性
*/
public static <T> T adapter(Record record,Class<T> modelClass){
/**
* record Map 转换 model 属性
*/
Record r = (Record)record;
T model = null;
try {
model = modelClass.newInstance();
Field[] fields = model.getClass().getDeclaredFields();
/* 遍历映射关系 */
for (Field field : fields) {
field.setAccessible(true);
if(field.getName().equals("serialVersionUID"))continue;
if(field.getAnnotation(Ignore.class) != null)continue;
if(field.getType() == int.class || field.getType() == Integer.class){
field.set(model, NumberUtils.toInt(StringUtil.toString(r.get(field.getName()))));
}else{
field.set(model, r.get(field.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return model;
}
/**
* 适配转换集合
*/
@SuppressWarnings("unchecked")
public static <T> List<T> adapterList(List<?> records,Class<T> modelClass){
List<T> models = Lists.newArrayList();
T model = null;
List<Record> rs = (List<Record>)records;
for (Record r : rs) {
model = adapter(r,modelClass);
models.add(model);
}
return models;
}
/**
* 适配转换分页对象
*/
@SuppressWarnings("unchecked")
public static <T> BsPageRow adapterPageRow(BsPageRow pg,Class<T> modelClass){
List<T> models = Lists.newArrayList();
List<Record> rs = (List<Record>)pg.getRows();
models = adapterList(rs, modelClass);
pg.setRows(models);
return pg;
}
/**
* 表单数据适配转换到对象属性
* @param <T>
*/
public static <T> Record adapterByModel(Record record,Class<T> modelClass){
Record r = (Record)record;
T model = null;
Record recordTo = new Record();
try {
model = modelClass.newInstance();
Field[] fields = model.getClass().getDeclaredFields();
/* 遍历映射关系 */
for (Field field : fields) {
field.setAccessible(true);
if(field.getName().equals("serialVersionUID"))continue;
if(field.getAnnotation(Ignore.class) != null)continue;
recordTo.set(field.getName(), r.get(field.getName()));
}
} catch (Exception e) {
e.printStackTrace();
}
return recordTo;
}
/**
* 表单数据适配转换到对象属性
* @param <T>
*/
public static <T> Record adapterByRecord(Class<T> modelClass){
T model = null;
Record recordTo = new Record();
try {
model = modelClass.newInstance();
Field[] fields = model.getClass().getDeclaredFields();
/* 遍历映射关系 */
for (Field field : fields) {
field.setAccessible(true);
if(field.getName().equals("serialVersionUID"))continue;
if(field.getAnnotation(Ignore.class) != null)continue;
recordTo.set(field.getName(), field.get(field.getName()));
}
} catch (Exception e) {
e.printStackTrace();
}
return recordTo;
}
/**
* 表单数据适配转换到对象属性
* @param <T>
*/
@SuppressWarnings("rawtypes")
public static <T> Record adapterModelToModel(Object object,Class<T> modelClass){
T model = null;
Record recordTo = new Record();
Class c = object.getClass();
try {
model = modelClass.newInstance();
Field[] fields = model.getClass().getDeclaredFields();
/* 遍历映射关系 */
for (Field field : fields) {
field.setAccessible(true);
if(!checkType(field.getType()) || field.getAnnotation(Ignore.class) != null
|| field.getName().equals("serialVersionUID"))
continue;
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), c);
Method getMethod = pd.getReadMethod();// 获得get方法
Object o = getMethod.invoke(object);// 执行get方法返回一个Object
if(field.getType() == int.class || field.getType() == Integer.class){
recordTo.set(field.getName(),NumberUtils.toInt(StringUtil.toString(o)));
}else{
recordTo.set(field.getName(), o==null?"":o.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return recordTo;
}
public static boolean checkType(Object type) {
if ((type == String.class) || (type == Integer.class) || (type == Integer.TYPE) || (type == Double.class)
|| (type == Double.TYPE) || (type == Float.class) || (type == Float.TYPE)) {
return true;
}
return false;
}
}