Commit ec73e535 by 吴佳

增加saveMdlData 和 loadMdlData 供模板保存时使用

parent 016979a6
......@@ -26,16 +26,16 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.text.*;
import java.util.*;
import java.util.concurrent.Semaphore;
......@@ -955,6 +955,65 @@ public class MdaUtils {
log.error("模型序列化异常", e);
}
}
/**
* 模板数据保存
**/
public static void saveMdlData(IModule module, IStream os) {
//在第一行写入module 名字
os.insertLine(0,module.getName());
writeData("",module, os);
}
/**
* 递归保存模型中的叶子节点
**/
private static void writeData(String parentDir ,IModule module, IStream os) {
String thisDir = "";
if(parentDir.length() !=0){
thisDir = parentDir +"/";
}
String moduleName = module.getClass().getName();
String subDir = thisDir + module.getName();
Class moduleClass = module.getClass();
Field[] fields =moduleClass.getDeclaredFields();
String fieldName = "";
String getMethodName = "";
Object fieldVal = null;
Method method = null;
for(Field field: fields){
fieldName = field.getName();
if(fieldName.equalsIgnoreCase("log")){
continue;
}
getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
method= moduleClass.getDeclaredMethod(getMethodName);
fieldVal = method.invoke(module);
if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) {
//如果是IMODULE 属性 ,递归获取
if (fieldVal instanceof IModule) {
writeData(subDir, (IModule) fieldVal, os);
} else {
String newFieldVal = "";
if(field.getType().getName().equals("java.util.Date")){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
newFieldVal = simpleDateFormat.format((Date)fieldVal);
}else{
newFieldVal = fieldVal.toString();
}
String line = subDir +"/"+ fieldName + "=" + newFieldVal;
os.insertLine(0,line);
}
}
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
} catch (NoSuchMethodException e) {
log.error("方法{}未找到",getMethodName);
}
}
}
public static String getCodetableLabel(String codetable, String key) {
IContext ctx = MdaEnv.getContext();
......@@ -962,6 +1021,90 @@ public class MdaUtils {
return Codetables.getCodetableLabel(key, codetable, locale.getLanguage());
}
public static void loadMdlData(IModule module, IStream stream) {
module.clear();
List<String> lines = stream.getRows();
if(lines.size() > 0){
for (String line : lines ) {
Class moduleClass = module.getClass();
if(moduleClass.getName().toUpperCase().indexOf(line.toUpperCase()) != -1){
continue;
}
String val = line.split("=")[1].trim();
String key = line.split("=")[0].trim();
String[] fields = key.split("/");
readData(1,module,fields,val);
}
}
}
private static void readData(int index, IModule module, String[] fields, String oldval) {
String fieldName = fields[index];
Class moduleClass = module.getClass();
String moduleName = moduleClass.getName();
Method getMethod = null ;
Method setMethod = null ;
if(index == fields.length-1){//叶子节点赋值
//获取setXXX方法名
String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
Field field = moduleClass.getDeclaredField(fieldName);
Class fieldTyp = field.getType();
//获取setXXX方法
setMethod= moduleClass.getDeclaredMethod(setMethodName,field.getType());
//根据类型 对数据进行处理
Object val = null;
if (field.getType().getName().equals("java.lang.Integer")){
val = Integer.parseInt(oldval);
}else if(field.getType().getName().equals("java.math.BigDecimal")){
val = new BigDecimal(oldval);
}else if(field.getType().getName().equals("java.util.Date")){
//String转Date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
val= simpleDateFormat.parse(oldval);
} catch (ParseException e) {
log.error("日期{}格式不正确",oldval);
}
}else{
val = oldval.toString();
}
setMethod.invoke(module,val);
} catch (NoSuchMethodException e) {
log.error("方法{}未找到",setMethodName);
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,setMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,setMethodName);
} catch (NoSuchFieldException e) {
log.error("模型{}中属性{}未找到",moduleName,fieldName);
}
}else{//非叶子节点继续迭代
//获取setXXX方法名
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
//查询getXXX方法
getMethod= moduleClass.getDeclaredMethod(getMethodName);
//获取下次递归的模型
IModule subModule = (IModule)getMethod.invoke(module);
//递归
index++;
readData(index, subModule, fields, oldval);
} catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
} catch (InvocationTargetException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
} catch (NoSuchMethodException e) {
log.error("模型{}方法{}未找到",moduleName,getMethodName);
}
}
}
public static String getCodetableLabel(List<CodetableItem> codeTable, String key) {
String label = "";
if (!CollectionUtils.isEmpty(codeTable)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment