package common.bean;
import java.util.*;
import java.sql.*;
import java.lang.reflect.*;
import org.apache.commons.beanutils.*;
import com.jspsmart.upload.*;
import javax.servlet.http.*;
/**
* classname Bean
* action 此类主要负责对Bean的一些操作,example:自动冲bean
* @author 传说
* @version 2007/7/25
* */
public class Bean {
/**
* methodName popConvert
* action 把rs冲到bean中,以list返回
* scope public static
* @param rs ResultSet
* @param c Bean的反射对象
* @return List
* */
public static List popConvert(ResultSet rs, Class c) throws Exception {
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
List ret = new ArrayList();
Field[] f = c.getDeclaredFields();
Object n = null;
while (rs.next()) {
n = c.newInstance();
for (int i = 1; i <= count; i++) {
Object v = rs.getObject(i);
for (int j = 0; j < f.length; j++) {
Field f1 = f[j];
if (f1.getName().equalsIgnoreCase(metaData.getColumnName(i))) {
BeanUtils.copyProperty(n, f1.getName(), v);
break;
}
}
}
ret.add(n);
}
return ret;
}
/**
* methodName popConvert
* action 把rs冲到bean中,以list返回
* scope public static
* @param req Request
* @param c Bean的反射对象
* @return Object
* */
public static Object popConvert(Request req, Class c) throws Exception {
Enumeration enumObj = req.getParameterNames();
List list = Collections.list(enumObj);
int count = list.size();
Field[] f = c.getDeclaredFields();
Object n = null;
n = c.newInstance();
for (int i = 0; i < count; i++) {
Object v = list.get(i);
for (int j = 0; j < f.length; j++) {
Field f1 = f[j];
if (f1.getName().equalsIgnoreCase(v.toString())) {
BeanUtils.copyProperty(n, f1.getName(), req.getParameter(v.toString()));
break;
}
}
}
return n;
}
/**
* methodName popConvert
* action 把rs冲到bean中,以list返回
* scope public static
* @param req HttpServletRequest
* @param c Bean的反射对象
* @return Object
* */
public static Object popConvert(HttpServletRequest req, Class c) throws
Exception {
Enumeration enumObj = req.getParameterNames();
List list = Collections.list(enumObj);
int count = list.size();
Field[] f = c.getDeclaredFields();
Object n = null;
n = c.newInstance();
for (int i = 0; i < count; i++) {
Object v = list.get(i);
for (int j = 0; j < f.length; j++) {
Field f1 = f[j];
if (f1.getName().equalsIgnoreCase(v.toString())) {
BeanUtils.copyProperty(n, f1.getName(),common.webclass.RequestInformation.getValueOfParamWithString(req,v.toString()));
break;
}
}
}
return n;
}
/**
* methodName popConvert
* action 把rs冲到bean中,以list返回
* scope public static
* @param rs ResultSet
* @param c Bean的反射对象
* @param replaceValue 因为在middlegen生成bean的时候把_都删除掉了,所以这里
* 增加了替换字符 这里的replaceValue是正则表达式
* @return List
* */
public static List popConvert(ResultSet rs, Class c, String replaceValue) throws
Exception {
ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount();
List ret = new ArrayList();
Field[] f = c.getDeclaredFields();
Object n = null;
while (rs.next()) {
n = c.newInstance();
for (int i = 1; i <= count; i++) {
Object v = rs.getObject(i);
for (int j = 0; j < f.length; j++) {
Field f1 = f[j];
if (f1.getName().equalsIgnoreCase(metaData.getColumnName(i).
replaceAll(replaceValue, ""))) {
BeanUtils.copyProperty(n, f1.getName(), v);
break;
}
}
}
ret.add(n);
}
return ret;
}
}