Commit cf6f5741 by 吴佳

兼容ZK saveData\loadData 调整

parent a3187b94
...@@ -34,6 +34,7 @@ import java.net.InetAddress; ...@@ -34,6 +34,7 @@ import java.net.InetAddress;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
...@@ -961,6 +962,11 @@ public class MdaUtils { ...@@ -961,6 +962,11 @@ 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 {
os.getOutputStream().write((module.getName()).getBytes(StandardCharsets.UTF_8));
}catch (IOException e){
log.error("流数据写入失败");
}
writeData("",module, os); writeData("",module, os);
} }
...@@ -970,10 +976,10 @@ public class MdaUtils { ...@@ -970,10 +976,10 @@ public class MdaUtils {
private static void writeData(String parentDir ,IModule module, IStream os) { private static void writeData(String parentDir ,IModule module, IStream os) {
String thisDir = ""; String thisDir = "";
if(parentDir.length() !=0){ if(parentDir.length() !=0){
thisDir = parentDir +"/"; thisDir = parentDir +"\\";
} }
String moduleName = module.getClass().getName(); String moduleName = module.getClass().getName();
String subDir = thisDir + module.getName();
Class moduleClass = module.getClass(); Class moduleClass = module.getClass();
Field[] fields =moduleClass.getDeclaredFields(); Field[] fields =moduleClass.getDeclaredFields();
String fieldName = ""; String fieldName = "";
...@@ -992,17 +998,25 @@ public class MdaUtils { ...@@ -992,17 +998,25 @@ public class MdaUtils {
if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) { if(!MdaUtils.isEmpty(fieldVal) && !(fieldVal instanceof IControl)) {
//如果是IMODULE 属性 ,递归获取 //如果是IMODULE 属性 ,递归获取
if (fieldVal instanceof IModule) { if (fieldVal instanceof IModule) {
String subDir = thisDir + fieldName;
writeData(subDir, (IModule) fieldVal, os); writeData(subDir, (IModule) fieldVal, os);
} else { } else {
String newFieldVal = ""; String newFieldVal = "";
if(field.getType().getName().equals("java.util.Date")){ if(field.getType().getName().equals("java.util.Date")){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
newFieldVal = simpleDateFormat.format((Date)fieldVal); newFieldVal = "D" + simpleDateFormat.format((Date)fieldVal) + "000";
}else if(field.getType().getName().equals("java.math.BigDecimal")){
newFieldVal = "B"+fieldVal.toString();
}else{ }else{
newFieldVal = fieldVal.toString(); newFieldVal = "S"+fieldVal.toString();
}
String line = thisDir+ fieldName + "=" + newFieldVal;
// os.insertLine(0,line);
try {
os.getOutputStream().write(("\r\n"+line).getBytes(StandardCharsets.UTF_8));
}catch (IOException e){
log.error("流数据写入失败");
} }
String line = subDir +"/"+ fieldName + "=" + newFieldVal;
os.insertLine(0,line);
} }
} }
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
...@@ -1023,8 +1037,13 @@ public class MdaUtils { ...@@ -1023,8 +1037,13 @@ public class MdaUtils {
public static void loadMdlData(IModule module, IStream stream) { public static void loadMdlData(IModule module, IStream stream) {
module.clear(); module.clear();
List<String> lines = stream.getRows(); String[] lines = null;
if(lines.size() > 0){ try {
lines = stream.getOutputStream().toString().split("\r\n");
}catch (Exception e){
log.error("获取流数据失败");
}
if(lines.length > 0){
for (String line : lines ) { for (String line : lines ) {
Class moduleClass = module.getClass(); Class moduleClass = module.getClass();
if(moduleClass.getName().toUpperCase().indexOf(line.toUpperCase()) != -1){ if(moduleClass.getName().toUpperCase().indexOf(line.toUpperCase()) != -1){
...@@ -1032,22 +1051,21 @@ public class MdaUtils { ...@@ -1032,22 +1051,21 @@ public class MdaUtils {
} }
String val = line.split("=")[1].trim(); String val = line.split("=")[1].trim();
String key = line.split("=")[0].trim(); String key = line.split("=")[0].trim();
String[] fields = key.split("/"); readData(module,key,val);
readData(1,module,fields,val);
} }
} }
} }
private static void readData(int index, IModule module, String[] fields, String oldval) { private static void readData(IModule module, String key, String oldval) {
String fieldName = "";
String fieldName = fields[index];
Class moduleClass = module.getClass(); Class moduleClass = module.getClass();
String moduleName = moduleClass.getName(); String moduleName = moduleClass.getName();
Method getMethod = null ; Method getMethod = null ;
Method setMethod = null ; Method setMethod = null ;
if(index == fields.length-1){//叶子节点赋值 if(key.indexOf("\\") == -1){//叶子节点赋值
//获取setXXX方法名 //获取setXXX方法名
fieldName = key;
String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try { try {
Field field = moduleClass.getDeclaredField(fieldName); Field field = moduleClass.getDeclaredField(fieldName);
...@@ -1056,20 +1074,18 @@ public class MdaUtils { ...@@ -1056,20 +1074,18 @@ 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.lang.Integer")){ if(field.getType().getName().equals("java.math.BigDecimal")){
val = Integer.parseInt(oldval); val = new BigDecimal(oldval.substring(1));
}else if(field.getType().getName().equals("java.math.BigDecimal")){
val = new BigDecimal(oldval);
}else if(field.getType().getName().equals("java.util.Date")){ }else if(field.getType().getName().equals("java.util.Date")){
//String转Date //String转Date
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
try { try {
val= simpleDateFormat.parse(oldval); val= simpleDateFormat.parse(oldval.substring(1,14));
} catch (ParseException e) { } catch (ParseException e) {
log.error("日期{}格式不正确",oldval); log.error("日期{}格式不正确",oldval);
} }
}else{ }else{
val = oldval.toString(); val = oldval.substring(1).toString();
} }
setMethod.invoke(module,val); setMethod.invoke(module,val);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
...@@ -1085,15 +1101,15 @@ public class MdaUtils { ...@@ -1085,15 +1101,15 @@ public class MdaUtils {
} }
}else{//非叶子节点继续迭代 }else{//非叶子节点继续迭代
//获取setXXX方法名 //获取setXXX方法名
fieldName = key.substring(0,key.indexOf("\\"));
String newKey = key.substring(key.indexOf("\\")+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 {
//查询getXXX方法 //查询getXXX方法
getMethod= moduleClass.getDeclaredMethod(getMethodName); getMethod= moduleClass.getDeclaredMethod(getMethodName);
//获取下次递归的模型 //获取下次递归的模型
IModule subModule = (IModule)getMethod.invoke(module); IModule subModule = (IModule)getMethod.invoke(module);
//递归 readData(subModule, newKey, oldval);
index++;
readData(index, subModule, fields, 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