package com.vci.client.omd.provider;
|
|
|
import java.io.BufferedReader;
|
import java.io.BufferedWriter;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.OutputStreamWriter;
|
import java.text.DateFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.Element;
|
import org.dom4j.io.SAXReader;
|
|
import com.vci.client.common.providers.ServiceProvider;
|
import com.vci.corba.common.VCIError;
|
import com.vci.corba.omd.stm.StatePool;
|
import com.vci.corba.omd.stm.StatePoolServicePrx;
|
|
|
public class StatepoolProvider {
|
|
|
private static StatepoolProvider instance = null;
|
public static DateFormat tsDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
public static DateFormat timeDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
public static final String OID = "oid";
|
public static final String NAME = "name";
|
public static final String LABEL = "label";
|
public static final String DESCRIPTION = "description";
|
public static final String TS = "ts";
|
public static final String CREATOR = "creator";
|
public static final String CREATETIME = "createTime";
|
public static final String MODIFIER = "modifier";
|
public static final String MODIFYTIME = "modifyTime";
|
/**
|
* 缓存已经调用new StatepoolProvider();
|
* 因此不能定义 私有的StatepoolProvider();
|
* @return
|
*/
|
public static StatepoolProvider getInstance(){
|
if(instance == null){
|
instance = new StatepoolProvider();
|
}
|
return instance;
|
}
|
|
/***
|
*
|
* 获取所有链接类型对象数组
|
*
|
*/
|
public StatePool [] getStatePools(){
|
StatePool[] statePools = null;
|
try {
|
statePools = getService().getStatePools();
|
} catch (VCIError e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return statePools;
|
}
|
|
public StatePoolServicePrx getService(){
|
try {
|
return ServiceProvider.getOMDService().getStateService();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public List<String> parseArrayToList(String[] array){
|
List<String> list = new ArrayList<String>(array.length);
|
for(String a : array){
|
list.add(a);
|
}
|
return list;
|
}
|
|
public boolean expData(String dir, StatePool[] sps){
|
BufferedWriter bw = null;
|
FileOutputStream fos = null;
|
OutputStreamWriter osw =null;
|
try{
|
new File(dir).mkdir();
|
new File(dir + "/sp").mkdir();
|
File file = new File(dir + "/sp.txt");
|
//将clob字段写到单独的文件中
|
//add by caill start 2015.12.23 导出时将txt文件设置为“utf-8”格式
|
fos = new FileOutputStream(file);
|
osw = new OutputStreamWriter(fos, "utf-8");
|
bw = new BufferedWriter(osw);
|
//add by caill end
|
/*w = new FileWriter(file);
|
bw = new BufferedWriter(w);*/
|
for(StatePool sp : sps){
|
//若str小于缓冲区大小, 将写到缓冲区;
|
//若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
|
String text = getObjectText(sp);
|
bw.write(text);
|
bw.newLine();
|
BufferedWriter clobBW = null;
|
FileOutputStream fo = null;
|
OutputStreamWriter pw =null;
|
try{
|
File clobFile = new File(new File(dir).getPath() + "/sp/" + sp.oid + ".xml");
|
//add by caill start 2015.12.23 将xml内容和xml文件格式统一
|
fo = new FileOutputStream(clobFile);
|
pw = new OutputStreamWriter(fo, "utf-8");
|
clobBW = new BufferedWriter(pw);
|
clobBW.write( "<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
|
clobBW.write(getXmlText(sp));
|
clobBW.flush();
|
//pw.close();
|
//add by caill end
|
/*clobW = new FileWriter(clobFile);
|
clobBW = new BufferedWriter(clobW);
|
clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
|
clobBW.write(getXmlText(sp));
|
clobBW.flush();*/
|
}catch(IOException e2){
|
e2.printStackTrace();
|
}finally{
|
try{
|
if(clobBW != null){
|
fo.close(); //add by caill 2015.12.23 将fo关闭
|
pw.close(); //add by caill 2015.12.23 将pw关闭
|
clobBW.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
bw.flush();
|
System.out.println("**************状态池导出成功************");
|
return true;
|
}catch(IOException e){
|
e.printStackTrace();
|
}finally{
|
try {
|
if(bw != null){
|
fos.close(); //add by caill 2015.12.23 将fos关闭
|
osw.close(); //add by caill 2015.12.23 将osw关闭
|
bw.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
System.out.println("**************状态池导出失败************");
|
return false;
|
}
|
|
/**
|
* 将对象转化成字符串
|
* {oid:qqq, name:q}
|
* @param sp
|
* @return
|
*/
|
public String getObjectText(StatePool sp) {
|
StringBuilder stb = new StringBuilder("{");
|
stb.append(OID + ":" + sp.oid + ",");
|
stb.append(NAME + ":" + sp.name + ",");
|
stb.append(LABEL + ":" + sp.tag + ",");
|
stb.append(DESCRIPTION + ":" + sp.description + ",");
|
stb.append(TS + ":" + sp.ts + ",");
|
stb.append(CREATOR + ":" + sp.creator + ",");
|
stb.append(CREATETIME + ":" + sp.createTime + ",");
|
stb.append(MODIFIER + ":" + sp.modifier + ",");
|
stb.append(MODIFYTIME + ":" + sp.modifyTime);
|
stb.append("}");
|
return stb.toString();
|
}
|
|
/**
|
* 将statePool转化成xmltext
|
* @param bt
|
* @return
|
*/
|
public String getXmlText(StatePool statePool) {
|
StringBuilder stb = new StringBuilder("<statePool>");
|
stb.append("<name>" + statePool.name + "</name>");
|
stb.append("<tag>" + statePool.tag + "</tag>");
|
stb.append("<imagePath>" + statePool.imagePath + "</imagePath>");
|
stb.append("<description>" + statePool.description + "</description>");
|
stb.append("</statePool>");
|
return stb.toString();
|
}
|
|
public List<StatePool> getSPFromFile(String dir) {
|
File file = new File(dir + "/sp.txt");
|
List<StatePool> list = new ArrayList<StatePool>();
|
BufferedReader br = null;
|
FileInputStream fo = null;
|
InputStreamReader isw =null;
|
try {
|
/*r = new FileReader(file);
|
br = new BufferedReader(r);*/
|
//add by caill start 2015.12.23 导入时将txt文件内容设置为“utf-8”格式
|
fo = new FileInputStream(file);
|
isw = new InputStreamReader(fo, "utf-8");
|
br = new BufferedReader(isw);
|
//add by caill end
|
String str = null;
|
while((str = br.readLine()) != null){
|
Map<String, String> map = getMapFormText(str);
|
String oid = map.get("oid");
|
if(oid != null && !oid.equals("")){
|
File contentFile = new File(dir + "/sp/" + oid + ".xml");
|
if(!contentFile.exists()){
|
System.out.println(dir + "/sp/" + oid + ".xml不存在");
|
break;
|
}
|
StatePool sp = new StatePool();
|
sp.oid = oid;
|
sp.name = map.get("name");
|
sp.tag = map.get("label");
|
sp.description = map.get("description");
|
sp.ts = map.get("ts");
|
sp.creator = map.get("creator");
|
sp.createTime = Long.valueOf(map.get("createTime"));
|
sp.modifier = map.get("modifier");
|
sp.modifyTime = Long.valueOf(map.get("modifyTime"));
|
SAXReader sa = new SAXReader();
|
//add by caill 2015.12.23 导入时将saxReader对象设置为“utf-8”格式
|
sa.setEncoding("utf-8");
|
Document document = sa.read(contentFile);
|
setSPValueFormDoc(sp, document.getRootElement());
|
list.add(sp);
|
}else{
|
System.out.println(dir + "sp.txt中存在oid为空的错误记录");
|
break;
|
}
|
}
|
return list;
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
} finally{
|
if(br != null){
|
try {
|
fo.close(); //add by caill 2015.12.23 将fo关闭
|
isw.close(); //add by caill 2015.12.23 将isw关闭
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 将文件中一条记录解析成key-value
|
* @param str
|
* @return
|
*/
|
public Map<String, String> getMapFormText(String str) {
|
str = str.trim();
|
if(!str.startsWith("{") || !str.endsWith("}")){
|
return null;
|
}
|
Map<String, String> map = new HashMap<String, String>();
|
str = str.substring(1, str.length() - 1);
|
String[] kvs = str.split(",");
|
for(String kv : kvs){
|
String[] kv_ = kv.split(":");
|
if(kv_.length == 1){
|
map.put(kv_[0].trim(), "");
|
}else{
|
map.put(kv_[0].trim(), kv_[1].trim());
|
}
|
}
|
return map;
|
}
|
|
/**
|
* 设置EnumItem存在
|
* @param sp
|
* @param element
|
*/
|
public void setSPValueFormDoc(StatePool sp, Element element){
|
String value = element.elementText("imagePath");
|
sp.imagePath = (value == null ? "" : value);
|
}
|
|
public boolean impData(StatePool[] sps) {
|
try {
|
for(StatePool sp : sps){
|
StatePool sp_ = getService().getStatePool(sp.name);
|
//已经存在
|
if(!sp_.oid.equals("")){
|
//覆盖
|
getService().deleteStatePool(sp_);
|
getService().addStatePool(sp);
|
System.out.println(sp.name + "在数据库中已经存在, 覆盖。");
|
}else{
|
getService().addStatePool(sp);
|
}
|
}
|
return true;
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
}
|