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.HashSet;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.Element;
|
import org.dom4j.io.SAXReader;
|
|
import com.vci.corba.omd.lcm.Bound;
|
import com.vci.corba.omd.lcm.LifeCycle;
|
import com.vci.corba.omd.lcm.LifeCycleServicePrx;
|
import com.vci.corba.omd.lcm.TransitionVO;
|
import com.vci.corba.omd.lcm.TransitionVOEvent;
|
import com.vci.corba.omd.stm.StatePool;
|
import com.vci.client.common.providers.ServiceProvider;
|
import com.vci.corba.common.VCIError;
|
|
|
public class LifeCycleProvider {
|
|
private static LifeCycleProvider 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 LifeCycleProvider();
|
* 因此不能定义 私有的LifeCycleProvider();
|
* @return
|
*/
|
public static LifeCycleProvider getInstance(){
|
if(instance == null){
|
instance = new LifeCycleProvider();
|
}
|
return instance;
|
}
|
/***
|
*
|
* 获取所有链接类型对象数组
|
*
|
*/
|
public LifeCycle [] getLifeCyles(){
|
LifeCycle[] lifeCyles = null;
|
try {
|
lifeCyles = getService().getLifeCycles();
|
} catch (VCIError e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return lifeCyles;
|
}
|
|
public LifeCycle getLifeCycleByName(String name){
|
LifeCycle[] lcs = null;
|
try {
|
lcs = getService().getLifeCycles();
|
|
for (LifeCycle lc : lcs){
|
if (lc.name.equals(name))
|
return lc;
|
}
|
} catch (VCIError e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public LifeCycleServicePrx getService(){
|
try {
|
return ServiceProvider.getOMDService().getLifeCycleService();
|
} 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, LifeCycle[] lcs){
|
Set<String> spNames = new HashSet<String>();
|
BufferedWriter bw = null;
|
FileOutputStream fos = null;
|
OutputStreamWriter osw =null;
|
try{
|
File file = new File(dir + "/lc.txt");
|
//将clob字段写到单独的文件中
|
new File(dir + "/lc").mkdir();
|
//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(LifeCycle lc : lcs){
|
for(Bound b : lc.bounds){
|
spNames.add(b.name);
|
}
|
//若str小于缓冲区大小, 将写到缓冲区;
|
//若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
|
String text = getObjectText(lc);
|
bw.write(text);
|
bw.newLine();
|
BufferedWriter clobBW = null;
|
FileOutputStream fo = null;
|
OutputStreamWriter pw =null;
|
try{
|
File clobFile = new File(dir + "/lc/" + lc.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(lc));
|
clobBW.flush();
|
//add by caill end
|
/*clobW = new FileWriter(clobFile);
|
clobBW = new BufferedWriter(clobW);
|
clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
|
clobBW.write(getXmlText(lc));
|
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();
|
List<StatePool> sps = new ArrayList<StatePool>();
|
for(String name : spNames){
|
StatePool sp = StatepoolProvider.getInstance().getService().getStatePool(name);
|
sps.add(sp);
|
}
|
boolean spFlag = StatepoolProvider.getInstance().expData(dir, sps.toArray(new StatePool[0]));
|
if(true & spFlag){
|
System.out.println("**************生命周期导出成功************");
|
}else{
|
System.out.println("**************生命周期导出失败************");
|
}
|
|
return true & spFlag;
|
}catch(IOException e){
|
e.printStackTrace();
|
} catch (VCIError 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();
|
}
|
}
|
return false;
|
}
|
|
/**
|
* 将对象转化成字符串
|
* {oid:qqq, name:q}
|
* @param lc
|
* @return
|
*/
|
public String getObjectText(LifeCycle lc) {
|
StringBuilder stb = new StringBuilder("{");
|
stb.append(OID + ":" + lc.oid + ",");
|
stb.append(NAME + ":" + lc.name + ",");
|
stb.append(LABEL + ":" + lc.tag + ",");
|
stb.append(DESCRIPTION + ":" + lc.description + ",");
|
stb.append(TS + ":" + lc.ts + ",");
|
stb.append(CREATOR + ":" + lc.creator + ",");
|
stb.append(CREATETIME + ":" + lc.createTime + ",");
|
stb.append(MODIFIER + ":" + lc.modifier + ",");
|
stb.append(MODIFYTIME + ":" + lc.modifyTime);
|
stb.append("}");
|
return stb.toString();
|
}
|
|
/**
|
* 将LifeCyle转化成xmltext
|
* @param bt
|
* @return
|
*/
|
public String getXmlText(LifeCycle lc){
|
StringBuilder stb = new StringBuilder("<LifeCyle>");
|
stb.append("<name>" + lc.name + "</name>");
|
stb.append("<tag>" + lc.tag + "</tag>");
|
stb.append("<description>" + lc.description + "</description>");
|
stb.append("<startState>" + lc.startState + "</startState>");
|
for(Bound b : lc.bounds){
|
stb.append("<bound>");
|
stb.append("<name>" + b.name + "</name>");
|
stb.append("<cellx>" + b.cellx + "</cellx>");
|
stb.append("<celly>" + b.celly + "</celly>");
|
stb.append("<cellw>" + b.cellw + "</cellw>");
|
stb.append("<cellh>" + b.cellh + "</cellh>");
|
stb.append("<cellicon>" + b.cellicon + "</cellicon>");
|
stb.append("</bound>");
|
}
|
|
for(TransitionVO t : lc.routes){
|
stb.append("<transitionVO>");
|
stb.append("<source>" + t.source + "</source>");
|
stb.append("<destination>" + t.destination + "</destination>");
|
stb.append("<connect>" + t.connect + "</connect>");
|
for(TransitionVOEvent te : t.transitionVOEvents){
|
stb.append("<transitionVOEvent>");
|
stb.append("<id>" + te.id + "</id>");
|
stb.append("<name>" + te.name + "</name>");
|
stb.append("</transitionVOEvent>");
|
}
|
stb.append("</transitionVO>");
|
}
|
stb.append("</LifeCyle>");
|
return stb.toString();
|
}
|
|
public List<LifeCycle> getLCFromFile(String dir) {
|
File file = new File(dir + "/lc.txt");
|
List<LifeCycle> list = new ArrayList<LifeCycle>();
|
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 + "/lc/" + oid + ".xml");
|
if(!contentFile.exists()){
|
System.out.println(dir + "/lc/" + oid + ".xml不存在");
|
break;
|
}
|
LifeCycle lc = new LifeCycle();
|
lc.oid = oid;
|
lc.name = map.get("name");
|
lc.tag = map.get("label");
|
lc.description = map.get("description");
|
lc.ts = map.get("ts");
|
lc.creator = map.get("creator");
|
lc.createTime = Long.valueOf(map.get("createTime"));
|
lc.modifier = map.get("modifier");
|
lc.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);
|
setLCValueFormDoc(lc, document.getRootElement());
|
list.add(lc);
|
}else{
|
System.out.println(dir + "lc.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.16 将fo关闭
|
isw.close(); //add by caill 2015.12.16 将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 lc
|
* @param element
|
*/
|
public void setLCValueFormDoc(LifeCycle lc, Element element){
|
String value = element.elementText("startState");
|
lc.startState = (value == null ? "" : value);
|
List<Element> bElements = element.elements("bound");
|
List<Bound> bs = new ArrayList<Bound>();
|
for(Element bElement : bElements){
|
Bound b = new Bound();
|
value = bElement.elementText("name");
|
b.name = (value == null ? "" : value);
|
value = bElement.elementText("cellx");
|
b.cellx = (value == null ? "" : value);
|
value = bElement.elementText("celly");
|
b.celly = (value == null ? "" : value);
|
value = bElement.elementText("cellw");
|
b.cellw = (value == null ? "" : value);
|
value = bElement.elementText("cellh");
|
b.cellh = (value == null ? "" : value);
|
value = bElement.elementText("cellicon");
|
b.cellicon = (value == null ? "" : value);
|
bs.add(b);
|
}
|
lc.bounds = bs.toArray(new Bound[0]);
|
|
List<Element> tElements = element.elements("transitionVO");
|
List<TransitionVO> ts = new ArrayList<TransitionVO>();
|
for(Element tElement : tElements){
|
TransitionVO t = new TransitionVO();
|
value = tElement.elementText("source");
|
t.source = (value == null ? "" : value);
|
value = tElement.elementText("destination");
|
t.destination = (value == null ? "" : value);
|
value = tElement.elementText("connect");
|
t.connect = (value == null ? "" : value);
|
List<Element> teElements = tElement.elements("transitionVOEvent");
|
List<TransitionVOEvent> tes = new ArrayList<TransitionVOEvent>();
|
for(Element teElement : teElements){
|
TransitionVOEvent te = new TransitionVOEvent();
|
value = teElement.elementText("id");
|
te.id = value;
|
value = teElement.elementText("name");
|
te.name = value;
|
tes.add(te);
|
}
|
t.transitionVOEvents = tes.toArray(new TransitionVOEvent[0]);
|
ts.add(t);
|
}
|
lc.routes = ts.toArray(new TransitionVO[0]);
|
}
|
|
public boolean impData(String dir, LifeCycle[] lcs) {
|
try {
|
List<StatePool> sps = StatepoolProvider.getInstance().getSPFromFile(dir);
|
boolean spFlag = StatepoolProvider.getInstance().impData(sps.toArray(new StatePool[0]));
|
if(!spFlag){
|
System.out.println("生命周期使用的状态池导入失败。导入中止。");
|
return false;
|
}
|
for(LifeCycle lc : lcs){
|
LifeCycle lc_ = getService().getLifeCycle(lc.name);
|
//已经存在
|
if(!lc_.oid.equals("")){
|
//覆盖
|
getService().deleteLifeCycle(lc_);
|
getService().addLifeCycle(lc);
|
System.out.println(lc.name + "在数据库中已经存在, 覆盖。");
|
}else{
|
getService().addLifeCycle(lc);
|
}
|
}
|
return true;
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
}
|