Commit 98cfe477 by 吴佳

兼容ZK saveData\loadData

ModuleList数据读/写适配
空格处理
parent 8b53728d
package com.brilliance.mda.runtime.mda.util; package com.brilliance.mda.runtime.mda.util;
import com.brilliance.mda.runtime.mda.*; import com.brilliance.mda.runtime.mda.*;
import com.brilliance.mda.runtime.mda.impl.ModuleList;
import com.brilliance.mda.runtime.mda.driver.I18NImpl; import com.brilliance.mda.runtime.mda.driver.I18NImpl;
import com.brilliance.mda.runtime.mda.driver.MdaDriver; import com.brilliance.mda.runtime.mda.driver.MdaDriver;
import com.brilliance.mda.runtime.mda.driver.MdaEnv; import com.brilliance.mda.runtime.mda.driver.MdaEnv;
...@@ -45,6 +46,17 @@ import static com.brilliance.mda.runtime.mda.Constants.NO_ERROR; ...@@ -45,6 +46,17 @@ import static com.brilliance.mda.runtime.mda.Constants.NO_ERROR;
public class MdaUtils { public class MdaUtils {
private static final Logger log = LoggerFactory.getLogger(MdaUtils.class); private static final Logger log = LoggerFactory.getLogger(MdaUtils.class);
public static final int MAX = 200; // 防止StackOver Overflow
private static final String FORMATDATE = "yyyyMMddHHmmssSSS";
private static final String FORMATINT = "0.00";
private static final char CR = '\r';
private static final char STRING = 'S';
private static final char INTEGER = 'I';
private static final char BIGDECIMAL = 'B';
private static final char DATE = 'D';
private static final char MODULELIST = 'M';
private static final char EQUALS = '=';
private static final char DATA = '#';
static final Format format = new DecimalFormat("#.######"); static final Format format = new DecimalFormat("#.######");
static final Format dateFormat = new SimpleDateFormat("yyyy/MM/dd"); static final Format dateFormat = new SimpleDateFormat("yyyy/MM/dd");
...@@ -961,19 +973,20 @@ public class MdaUtils { ...@@ -961,19 +973,20 @@ public class MdaUtils {
**/ **/
public static void saveMdlData(IModule module, IStream os) { public static void saveMdlData(IModule module, IStream os) {
//在第一行写入module 名字 //在第一行写入module 名字
os.insertLine(0,module.getName()); // os.insertLine(0,module.getName());
try { try {
os.getOutputStream().write(("\r"+module.getName()).getBytes(StandardCharsets.UTF_8)); os.getOutputStream().write((module.getName()+CR).getBytes(StandardCharsets.UTF_8));
}catch (IOException e){ }catch (IOException e){
log.error("流数据写入失败"); log.error("流数据写入失败");
} }
writeData("",module, os); writeData("",module, os , 0);
} }
/** /**
* 递归保存模型中的叶子节点 * 递归保存模型中的叶子节点
**/ **/
private static void writeData(String parentDir ,IModule module, IStream os) { private static int writeData(String parentDir ,IModule module, IStream os ,int deep) {
String thisDir = ""; String thisDir = "";
if(parentDir.length() !=0){ if(parentDir.length() !=0){
thisDir = parentDir +File.separator; thisDir = parentDir +File.separator;
...@@ -997,26 +1010,61 @@ public class MdaUtils { ...@@ -997,26 +1010,61 @@ public class MdaUtils {
fieldVal = method.invoke(module); fieldVal = method.invoke(module);
if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) { if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) {
//如果是IMODULE 属性 ,递归获取 //如果是IMODULE 属性 ,递归获取
if (fieldVal instanceof IModule) { if (fieldVal instanceof IModule && !(fieldVal instanceof IModuleList)) {
String subDir = thisDir + fieldName; String subDir = thisDir + fieldName;
writeData(subDir, (IModule) fieldVal, os); deep = writeData(subDir, (IModule) fieldVal, os , deep);
} else { } else{
//如果是ModuleList
if(field.getType().getName().equals("com.brilliance.mda.runtime.mda.impl.ModuleList")){
IModuleList<IModule> list = (IModuleList) fieldVal;
if (!list.isEmpty() && list.size() > 0 )
{
String subDir = thisDir + fieldName;
String line = thisDir+ fieldName + EQUALS + MODULELIST + list.size();
try {
os.getOutputStream().write((line+CR).getBytes(StandardCharsets.UTF_8));
deep++;
}catch (IOException e){
log.error("流数据写入失败");
}
deep = 0; // 设置ModuleList可将读取栈清空
for (int i = 0; i < list.size(); i++) {
String subDirIdx = subDir + "[" + i + "]";
deep = writeData(subDirIdx, (IModule) (list.get(i)), os, deep);
}
}
//叶子节点
}else if (fieldVal instanceof String || fieldVal instanceof Number || fieldVal instanceof Date){
String newFieldVal = ""; String newFieldVal = "";
//数值类型 ,值为0的 不保存
if (fieldVal instanceof Number && ((Number) fieldVal).doubleValue() == 0) {
return deep;
}
if(field.getType().getName().equals("java.util.Date")){ if(field.getType().getName().equals("java.util.Date")){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMATDATE);
newFieldVal = "D" + simpleDateFormat.format((Date)fieldVal) + "000"; newFieldVal = DATE + simpleDateFormat.format((Date)fieldVal) ;
}else if(field.getType().getName().equals("java.lang.Integer")){
newFieldVal = INTEGER+fieldVal.toString();
}else if(field.getType().getName().equals("java.math.BigDecimal")){ }else if(field.getType().getName().equals("java.math.BigDecimal")){
newFieldVal = "B"+fieldVal.toString(); newFieldVal = BIGDECIMAL+new DecimalFormat(FORMATINT).format(new BigDecimal(fieldVal.toString()));
}else{ }else if(field.getType().getName().equals("java.lang.String")){
newFieldVal = "S"+fieldVal.toString(); newFieldVal = STRING+fieldVal.toString().replaceAll("(\r\n)|\r|\n", "\r" + DATA);
} }
String line = thisDir+ fieldName + "=" + newFieldVal; String line = thisDir+ fieldName + EQUALS + newFieldVal;
// os.insertLine(0,line);
try { try {
os.getOutputStream().write(("\r"+line).getBytes(StandardCharsets.UTF_8)); os.getOutputStream().write((line+CR).getBytes(StandardCharsets.UTF_8));
deep++;
if (deep == MAX)
{
os.getOutputStream().write(CR); // 多写一个回车便于LoadModule为退出loadline的循环调用
deep = 0;
}
}catch (IOException e){ }catch (IOException e){
log.error("流数据写入失败"); log.error("流数据写入失败");
} }
}else{
return 0;
}
} }
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
...@@ -1027,6 +1075,7 @@ public class MdaUtils { ...@@ -1027,6 +1075,7 @@ public class MdaUtils {
log.error("方法{}未找到",getMethodName); log.error("方法{}未找到",getMethodName);
} }
} }
return deep;
} }
public static String getCodetableLabel(String codetable, String key) { public static String getCodetableLabel(String codetable, String key) {
...@@ -1050,7 +1099,7 @@ public class MdaUtils { ...@@ -1050,7 +1099,7 @@ public class MdaUtils {
//去除空格 //去除空格
line.trim(); line.trim();
//空行和#开头的注释行 跳过 //空行和#开头的注释行 跳过
if(line.length() == 0 || line.indexOf("#") == 0){ if(line.length() == 0 || line.indexOf(DATA) == 0){
continue; continue;
} }
//读取到本模型模型名相同数据的行,开始获取模型数据 //读取到本模型模型名相同数据的行,开始获取模型数据
...@@ -1080,7 +1129,8 @@ public class MdaUtils { ...@@ -1080,7 +1129,8 @@ public class MdaUtils {
String moduleName = moduleClass.getName(); String moduleName = moduleClass.getName();
Method getMethod = null ; Method getMethod = null ;
Method setMethod = null ; Method setMethod = null ;
if(key.indexOf(File.separator) == -1){//叶子节点赋值 //叶子节点赋值
if(key.indexOf(File.separator) == -1){
//获取setXXX方法名 //获取setXXX方法名
fieldName = key; fieldName = key;
String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
...@@ -1091,17 +1141,22 @@ public class MdaUtils { ...@@ -1091,17 +1141,22 @@ public class MdaUtils {
setMethod= moduleClass.getDeclaredMethod(setMethodName,field.getType()); setMethod= moduleClass.getDeclaredMethod(setMethodName,field.getType());
//根据类型 对数据进行处理 //根据类型 对数据进行处理
Object val = null; Object val = null;
if(field.getType().getName().equals("java.math.BigDecimal")){ if(oldval.indexOf(MODULELIST)==0){
return;
}
if(oldval.indexOf(BIGDECIMAL) == 0){
val = new BigDecimal(oldval.substring(1)); val = new BigDecimal(oldval.substring(1));
}else if(field.getType().getName().equals("java.util.Date")){ }else if(oldval.indexOf(DATE) == 0){
//String转Date //String转Date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(FORMATDATE);
try { try {
val= simpleDateFormat.parse(oldval.substring(1,14)); val= simpleDateFormat.parse(oldval.substring(1));
} catch (ParseException e) { } catch (ParseException e) {
log.error("日期{}格式不正确",oldval); log.error("日期{}格式不正确",oldval);
} }
}else{ }else if(oldval.indexOf(INTEGER) == 0){
val= new Integer(oldval.substring(1));
}else if(oldval.indexOf(STRING) == 0){
val = oldval.substring(1).toString(); val = oldval.substring(1).toString();
} }
setMethod.invoke(module,val); setMethod.invoke(module,val);
...@@ -1119,6 +1174,13 @@ public class MdaUtils { ...@@ -1119,6 +1174,13 @@ public class MdaUtils {
}else{//非叶子节点继续迭代 }else{//非叶子节点继续迭代
//获取setXXX方法名 //获取setXXX方法名
fieldName = key.substring(0,key.indexOf(File.separator)); fieldName = key.substring(0,key.indexOf(File.separator));
//moduleList 的数据包含 [?]
int index = -1;
if(fieldName.indexOf("[") >0){
String[] fieldNameArr = fieldName.split("\\[");
fieldName = fieldNameArr[0];
index = Integer.parseInt(fieldNameArr[1].replace("]",""));
}
String newKey = key.substring(key.indexOf(File.separator)+1); String newKey = key.substring(key.indexOf(File.separator)+1);
String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try { try {
...@@ -1126,7 +1188,23 @@ public class MdaUtils { ...@@ -1126,7 +1188,23 @@ public class MdaUtils {
getMethod= moduleClass.getDeclaredMethod(getMethodName); getMethod= moduleClass.getDeclaredMethod(getMethodName);
//获取下次递归的模型 //获取下次递归的模型
IModule subModule = (IModule)getMethod.invoke(module); IModule subModule = (IModule)getMethod.invoke(module);
if(subModule instanceof ModuleList){
if(subModule == null ){
subModule = new ModuleList();
}
IModuleList<IModule> list = (IModuleList)subModule;
if(list.size() -1 < index){
try {
list.add(((ModuleList<?>) subModule).getDataClass().newInstance());
}catch (InstantiationException e) {
e.printStackTrace();
}
}
readData(list.get(index), newKey, oldval);
}else {
readData(subModule, newKey, oldval); readData(subModule, newKey, oldval);
}
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
log.error("模型{}中执行方法{}时出错",moduleName,getMethodName); log.error("模型{}中执行方法{}时出错",moduleName,getMethodName);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
......
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