Commit cf6f5741 by 吴佳

兼容ZK saveData\loadData 调整

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