📄 datafactory.java
字号:
package com.base;
import java.util.Vector;
import java.util.HashMap;
import java.util.Iterator;
import java.lang.reflect.Method;
import com.base.SysException;
import com.erpbase.general.Log;
import com.erpbase.util.StringUtility;
//import com.waveful.cargoflow.bl.commonaudit.EventData;
//import com.waveful.cargoflow.bl.commonaudit.AuditEventData;
public class DataFactory
{
static HashMap hashFields;
/**
*功能:从jsp页面中得到所有的AuditEventData对象
*说明:如果jsp中没有定义审核流程,则此处会返回一个null
*/
/*
public static AuditEventData getAuditEvent(CommonData comData)
{
AuditEventData aevData = new AuditEventData();
String strHas_audit_event = comData.getValue("has_audit_event");//是否有审核流程
if(!StringUtility.isNull(strHas_audit_event) && strHas_audit_event.equals("Y"))//有审核流程
{
aevData.setMenuCode(comData.getMenuCode());
aevData.setCorporation(comData.getCor_id());
aevData.setTableName(comData.getValue("audit_event_table"));//审核事件需要记录的表
String strAuditEvent = comData.getValue("audit_event_id");
aevData.setEventId(strAuditEvent);//用户选择的审核事件号
aevData.setNotneedAudit(comData.getValue("no_audit"));//用户是否设置为无需审核
String[] strRlt_order = comData.getValues("rlt_order:"+strAuditEvent);//审核流水顺序
String[] strAuthRole = comData.getValues("auth_role:"+strAuditEvent); //得到接收方的角色编码(审核/核定)
String[] strRoleName = comData.getValues("role_name:"+strAuditEvent); //得到接收方的角色名称(审核/核定)
String[] strAuditerName = comData.getValues("s_type:"+strAuditEvent); //得到接收方名称(人/职务的名称)
String[] strAuditerCode = comData.getValues("toid:"+strAuditEvent); //得到接收方编码(人或职务的编码)
String[] strDayArray = comData.getValues("day:"+strAuditEvent); //得到转发的天数
String[] strHourArray = comData.getValues("hour:"+strAuditEvent); //得到转发的小时数
String[] strTransferCode = comData.getValues("fwtoid:"+strAuditEvent); //得到转发的接收人编码
String[] strTransferName = comData.getValues("fwtoname:"+strAuditEvent); //得到转发的接收人姓名
Vector vctEventData = new Vector();
for (int loop=0; loop<strRlt_order.length; loop++)
{
EventData eventData = new EventData();
eventData.setRlt_order(strRlt_order[loop]);
eventData.setAuthRole(strAuthRole[loop]);
eventData.setRoleName(strRoleName[loop]);
eventData.setDayArray(strDayArray[loop]);
eventData.setHourArray(strHourArray[loop]);
eventData.setTransferCode(strTransferCode[loop]);
eventData.setTransferName(strTransferName[loop]);
eventData.setAuditerName(strAuditerName[loop]);
eventData.setAuditerCode(strAuditerCode[loop]);
vctEventData.addElement(eventData);
}
aevData.setEventData(vctEventData);
}
else aevData.setNotneedAudit("N");
return aevData;
}*/
/**
*功能:从前台得到所有的输入数据,并且保存在Ojbect中,并且返回此Object
*说明:如果要从页面中一个input对象,name="object_id"的对象存放物品名称
*则Object中必须要有一个setObject_id(String str)的方法,否则将不能保存
*相关数据,在保存时,会自动查找所有的set方法,并且会在前台的jsp页面中
*定义了相关的对象才会把数据保存进来
*/
public static Object getData(CommonData comData, Object obj)throws SysException
{
try
{
HashMap mapFields = comData.getFieldMap();
HashMap mapMethods = getMapMethod(mapFields, obj);
Iterator iterKeys = (mapMethods.keySet()).iterator();
while(iterKeys.hasNext())
{
String strKey = (String)iterKeys.next();
String strFieldName = (String)mapFields.get(strKey);
String strValue = comData.getValue(strFieldName);
Method theMethod = (Method)mapMethods.get(strKey);
Object[] objValue = {strValue};
theMethod.invoke(obj, objValue);
}
}
catch(Exception ex)
{
Log.printDebug("ssssssssssssssss Exception(getData) sssssssssssssss");
Log.printException(ex);
throw new SysException("throw Exception caused by<br>执行方法getData()时发生系统错误!"+ex.getCause().getMessage());
}
return obj;
}
/**
*功能:从前台得到所有的输入数据,并且保存在Ojbect中,并且返回此Object
*说明:如果要从页面中一个input对象,name="object_id"的对象存放物品名称
*则Object中必须要有一个setObject_id(String str)的方法,否则将不能保存
*相关数据,在保存时,会自动查找所有的set方法,并且会在前台的jsp页面中
*定义了相关的对象才会把数据保存进来
@param strExceptObjects 指不包含的对象,就是不包含的对象,如jsp中有一个对象
abc_code,如果不想把此值通过obj中的方法setAbc_code()保存到对象中
*/
public static Object getData(CommonData comData, Object obj, String strExceptObjects)
throws SysException
{
try
{
if(StringUtility.isNull(strExceptObjects)) strExceptObjects = "";
HashMap mapFields = comData.getFieldMap();
HashMap mapMethods = getMapMethod(mapFields, obj);
Iterator iterKeys = (mapMethods.keySet()).iterator();
while(iterKeys.hasNext())
{
String strKey = (String)iterKeys.next();
String strFieldName = (String)mapFields.get(strKey);
if(strExceptObjects.indexOf(strFieldName)>=0) continue;
String strValue = comData.getValue(strFieldName);
Method theMethod = (Method)mapMethods.get(strKey);
Object[] objValue = {strValue};
theMethod.invoke(obj, objValue);
}
}
catch(Exception ex)
{
Log.printDebug("ssssssssssssssss Exception(getData) sssssssssssssss");
Log.printException(ex);
throw new SysException("throw Exception caused by<br>执行方法getData()时发生系统错误!"+ex.getCause().getMessage());
}
return obj;
}
/**
@function:
*说明:如前台的jsp页面中定义多个名为object_id的对象,Object中的有一个setObject_id()
*的方法
@return 返回的Vector中的每一个元素都是一个Object对象,并且已经把页面中的值存放
在此对象中
*/
public static Vector getDatas(CommonData comData, Object obj)throws SysException
{
Vector vctData = new Vector();
try
{
//得到所有需要存放值的方法
HashMap mapMethods = getMapMethod(comData.getFieldMap(), obj);
//从前台得到所有的值
HashMap mapValues = comData.getValues(hashFields);
int iValuesLength = comData.getValuesLength();
for(int loop=0; loop<iValuesLength; loop++)
{
Object oneObj = obj.getClass().newInstance();
Iterator iterKeyMap = (mapValues.keySet()).iterator();
while(iterKeyMap.hasNext())
{
String strKey = (String)iterKeyMap.next();
String[] strValues = (String[])mapValues.get(strKey);
if( strValues.length>loop)
{
Method theMethod = (Method)mapMethods.get(strKey);
String strValue = strValues[loop];
//Log.printDebug("在getDatas方法中strKey="+strKey+" || strValue="+strValue);
Object[] objValue = {strValue};
theMethod.invoke(oneObj, objValue);
}
}
vctData.addElement(oneObj);
}
}
catch(Exception ex)
{
Log.printDebug("ssssssssssssss Exception(getDatas) sssssssssssssss");
Log.printException(ex);
throw new SysException("throw Exception caused by<br>执行方法getDatas()时发生系统错误!"+ex.getCause().getMessage());
}
return vctData;
}
/**
@function:
*说明:如前台的jsp页面中定义多个名为object_id的对象,Object中的有一个setObject_id()
*的方法
@return 返回的Vector中的每一个元素都是一个Object对象,并且已经把页面中的值存放
在此对象中
*/
public static Vector getDatas(CommonData comData, Object obj, String strExceptObjects)
throws SysException
{
Vector vctData = new Vector();
if(StringUtility.isNull(strExceptObjects)) strExceptObjects = "";
try
{
//得到所有需要存放值的方法
HashMap mapMethods = getMapMethod(comData.getFieldMap(), obj);
//从前台得到所有的值
HashMap mapValues = comData.getValues(hashFields);
int iValuesLength = comData.getValuesLength();
for(int loop=0; loop<iValuesLength; loop++)
{
Object oneObj = obj.getClass().newInstance();
Iterator iterKeyMap = (mapValues.keySet()).iterator();
while(iterKeyMap.hasNext())
{
String strKey = (String)iterKeyMap.next();
if(strExceptObjects.indexOf(strKey)>=0) continue;
String[] strValues = (String[])mapValues.get(strKey);
if( strValues.length>loop)
{
Method theMethod = (Method)mapMethods.get(strKey);
String strValue = strValues[loop];
//Log.printDebug("在getDatas方法中strKey="+strKey+" || strValue="+strValue);
Object[] objValue = {strValue};
theMethod.invoke(oneObj, objValue);
}
}
vctData.addElement(oneObj);
}
}
catch(Exception ex)
{
Log.printDebug("ssssssssssssss Exception(getDatas) sssssssssssssss");
Log.printException(ex);
throw new SysException("throw Exception caused by<br>执行方法getDatas()时发生系统错误!"+ex.getCause().getMessage());
}
return vctData;
}
/**
*功能:得到所有前台jsp页面中有request field对应的所有的方法
@param mapFields中存放所有前台的input对象,包括通过url进行的参数传递
*/
private static HashMap getMapMethod(HashMap mapFields, Object obj)
{
hashFields = new HashMap();
HashMap mapMethods = new HashMap();
Class thisClass = obj.getClass();
getClassMethod( mapFields,thisClass,mapMethods);
Class superClass1 = thisClass.getSuperclass();
if( superClass1!=null && !superClass1.getName().equals("java.lang.Object"))
{
getClassMethod( mapFields,superClass1,mapMethods);
Class superClass2 = superClass1.getSuperclass();
if( superClass2!=null && !superClass2.getName().equals("java.lang.Object"))
getClassMethod( mapFields,superClass2,mapMethods);
}
return mapMethods;
}
/**
* 功能:获得类对象中的与前台jsp页面字段对应的set方法
*
*/
private static void getClassMethod( HashMap mapFields, Class cla,HashMap mapMethods)
{
Method[] classMethod = cla.getDeclaredMethods();
for(int outLoop=0; outLoop< classMethod.length; outLoop++)
{
String strMethodName = ( classMethod[outLoop]).getName();
if(strMethodName.startsWith("set"))
{
strMethodName = strMethodName.substring(3).toLowerCase();
String strMethodNameLower = strMethodName;
strMethodNameLower = strMethodNameLower.toLowerCase();
if(mapFields.containsKey(strMethodNameLower))
{
mapMethods.put(strMethodNameLower, classMethod[outLoop]);
hashFields.put(strMethodNameLower, (String)mapFields.get(strMethodNameLower));
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -