package com.zteits.oa.util.pagepaper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;
import com.zteits.oa.api.base.bean.PageBean;
/**
* 分页插件结果转换工具类.
*
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: PageBeanUtil.java
* @Description:
* @version: v1.0.0
* @author: wangfs
* @date: 2017年4月19日 下午4:03:33
* Modification History:
* Date Author Version Description
*---------------------------------------------------------*
* 2017年4月19日 wangfs v1.0.0 创建
*/
public class PageBeanUtil {
/**
* 将PageInfo 转换成PageBean.
* @param form PageInfo 对象.
* @param to PageBean 对象.
* @param clazz
*/
public static void copyProperties(PageInfo form,PageBean to,Class clazz){
if(form != null && to != null && clazz != null){
List list = new ArrayList();
if(!org.springframework.util.CollectionUtils.isEmpty(form.getList())){
for(R source:form.getList()){
if(source == null){
continue;
}
try {
L target = clazz.newInstance();
BeanUtils.copyProperties(source, target);
list.add(target);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
to.setDataList(list);
to.setPageNum(form.getPageNum());
to.setPageSize(form.getPageSize());
to.setPages(form.getPages());
to.setPageTotals(Integer.valueOf(String.valueOf(form.getTotal())));
}else{
to.setDataList(list);
to.setPageNum(form.getPageNum());
to.setPageSize(form.getPageSize());
to.setPages(form.getPages());
to.setPageTotals(Integer.valueOf(String.valueOf(form.getTotal())));
}
}else{
try {
throw new Exception("源对象,目标对象,clazz 不能为空!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}