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.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.omd.constants.OmdConstants;
|
import com.vci.client.common.providers.ServiceProvider;
|
import com.vci.corba.common.VCIError;
|
import com.vci.corba.omd.etm.EnumChild;
|
import com.vci.corba.omd.etm.EnumItem;
|
import com.vci.corba.omd.etm.EnumServicePrx;
|
|
public class EnumProvider {
|
|
|
private EnumProvider(){
|
|
}
|
private static class InstanceHolder{
|
private static EnumProvider instance = new EnumProvider();
|
}
|
public static EnumProvider getInstance(){
|
return InstanceHolder.instance;
|
}
|
public EnumServicePrx getService(){
|
try {
|
return ServiceProvider.getOMDService().getEnumService();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取所有枚举
|
* @return
|
*/
|
public EnumItem[] getAllEnums(){
|
try {
|
return getService().getEmItems("", 1, 1);
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取指定的枚举
|
* @param name
|
* @return
|
*/
|
public EnumItem getEnumByName(String name){
|
try {
|
return getService().getEmItemByName(name);
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 根据枚举名和 枚举项名获取枚举项值
|
* @param enumName
|
* @param enumChildName
|
* @return
|
*/
|
public String getEnumItemValue(String enumName, String enumChildName) {
|
EnumItem enumItem = getInstance().getEnumByName(enumName);
|
for(EnumChild ec : enumItem.children){
|
if(ec.name.equalsIgnoreCase(enumChildName)){
|
return ec.value;
|
}
|
}
|
return null;
|
}
|
|
public boolean expData(String dir, EnumItem[] objs) {
|
//TODO调用ENUM的导出
|
BufferedWriter bw = null;
|
FileOutputStream fos = null;
|
OutputStreamWriter osw =null;
|
try{
|
File file = new File(dir + "/enum.txt");
|
//将clob字段写到单独的文件中
|
new File(dir + "/enum").mkdir();
|
//add by caill start 2015.12.16 导出时将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(EnumItem o : objs){
|
//若str小于缓冲区大小, 将写到缓冲区;
|
//若str大于缓冲区大小, 将刷新缓冲区(将缓冲区内容写到底层流), 然后str直接写到底层流.
|
String text = getObjectText(o);
|
bw.write(text);
|
bw.newLine();
|
BufferedWriter clobBW = null;
|
FileOutputStream fo = null;
|
OutputStreamWriter pw =null;
|
try{
|
File clobFile = new File(dir + "/enum/" + o.oid + ".xml");
|
//add by caill start 2015.12.15 将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(o));
|
clobBW.flush();
|
//pw.close();
|
//add by caill end
|
/*File clobFile = new File(dir + "/enum/" + o.oid + ".xml");
|
clobW = new FileWriter(clobFile);
|
clobBW = new BufferedWriter(clobW);
|
clobBW.write( "<?xml version=\"1.0\" encoding=\"gb2312\" ?>");//gb2312
|
clobBW.write(getXmlText(o));
|
clobBW.flush();*/
|
}catch(IOException e2){
|
e2.printStackTrace();
|
}finally{
|
|
try{
|
if(clobBW != null){
|
//clobW.close();
|
fo.close(); //add by caill 2015.12.16 将fo关闭
|
pw.close(); //add by caill 2015.12.16 将pw关闭
|
clobBW.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
bw.flush();
|
System.out.println("**************枚举导出成功************");
|
return true;
|
}catch(IOException e){
|
e.printStackTrace();
|
System.out.println("**************枚举导出失败************");
|
return false;
|
}finally{
|
try {
|
if(bw != null){
|
//w.close();
|
fos.close(); //add by caill 2015.12.16 将fos关闭
|
osw.close(); //add by caill 2015.12.16 将osw关闭
|
bw.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
/**
|
* 将对象转化成字符串
|
* {oid:qqq, name:q}
|
* @param o
|
* @return
|
*/
|
public String getObjectText(EnumItem o) {
|
StringBuilder stb = new StringBuilder("{");
|
stb.append(OmdConstants.OID + ":" + o.oid + ",");
|
stb.append(OmdConstants.NAME + ":" + o.name + ",");
|
stb.append(OmdConstants.LABEL + ":" + o.label + ",");
|
stb.append(OmdConstants.TS + ":" + o.ts + ",");
|
stb.append(OmdConstants.CREATOR + ":" + o.creator + ",");
|
stb.append(OmdConstants.CREATETIME + ":" + o.createTime + ",");
|
stb.append(OmdConstants.MODIFIER + ":" + o.modifier + ",");
|
stb.append(OmdConstants.MODIFYTIME + ":" + o.modifyTime);
|
stb.append("}");
|
return stb.toString();
|
}
|
|
/**
|
* 将emItem转化成xmltext
|
* @param emItem
|
* @return
|
*/
|
public String getXmlText(EnumItem emItem) {
|
StringBuilder stb = new StringBuilder("<enum>");
|
stb.append("<name>" + emItem.name + "</name>");
|
stb.append("<label>" + emItem.label + "</label>");
|
stb.append("<type>" + emItem.type + "</type>");
|
stb.append("<length>" + emItem.length + "</length>");
|
for(EnumChild ec : emItem.children){
|
stb.append("<child>");
|
stb.append("<name>" + ec.name + "</name>");
|
stb.append("<value>" + ec.value + "</value>");
|
stb.append("<description>" + ec.description + "</description>");
|
stb.append("</child>");
|
}
|
stb.append("</enum>");
|
return stb.toString();
|
}
|
|
/**
|
* 从导入文件中解析出EnumItem
|
* @param dir
|
* @return
|
*/
|
public List<EnumItem> getEnumsFromFile(String dir){
|
File file = new File(dir + "/enum.txt");
|
List<EnumItem> list = new ArrayList<EnumItem>();
|
BufferedReader br = null;
|
FileInputStream fo = null;
|
InputStreamReader isw =null;
|
try {
|
/*r = new FileReader(file);
|
br = new BufferedReader(r);*/
|
//add by caill start 2015.12.16 导入时将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 + "/enum/" + oid + ".xml");
|
if(!contentFile.exists()){
|
System.out.println(dir + "/enum/" + oid + ".xml不存在");
|
break;
|
}
|
EnumItem em = new EnumItem();
|
em.oid = oid;
|
em.name = map.get("name");
|
em.label = map.get("label");
|
//em.label = map.get("ts");
|
em.ts = map.get("ts");
|
em.creator = map.get("creator");
|
em.createTime = Long.valueOf(map.get("createTime"));
|
em.modifier = map.get("modifier");
|
em.modifyTime = Long.valueOf(map.get("modifyTime"));
|
SAXReader sa = new SAXReader();
|
//add by caill 2015.12.16 导入时将saxReader对象设置为“utf-8”格式
|
sa.setEncoding("utf-8");
|
|
Document document = sa.read(contentFile);
|
setEnumValueFormDoc(em, document.getRootElement());
|
list.add(em);
|
}else{
|
System.out.println(dir + "enum.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 {
|
//r.close();
|
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 em
|
* @param element
|
*/
|
public void setEnumValueFormDoc(EnumItem em, Element element){
|
String value = element.elementText("type");
|
em.type = (value == null ? "" : value);
|
value = element.elementText("length");
|
em.length = (value == null ? 0 : Integer.valueOf(value));
|
List<Element> children = element.elements("child");
|
List<EnumChild> ecList = new ArrayList<EnumChild>();
|
for(Element child : children){
|
EnumChild ec = new EnumChild();
|
value = child.elementText("name");
|
ec.name = (value == null ? "" : value);
|
value = child.elementText("value");
|
ec.value = (value == null ? "" : value);
|
value = child.elementText("description");
|
ec.description = (value == null ? "" : value);
|
ecList.add(ec);
|
}
|
em.children = ecList.toArray(new EnumChild[0]);
|
}
|
|
/**
|
* 导入数据
|
* @param array
|
* @return
|
*/
|
public boolean impData(EnumItem[] ems) {
|
try {
|
for(EnumItem em : ems){
|
EnumItem em_ = getService().getEmItemByName(em.name);
|
//已经存在
|
if(!em_.oid.equals("")){
|
//类型一致则覆盖
|
if(em.type.equals(em_.type)){
|
getService().deleteEmItem(em_);
|
getService().addEmItem(em);
|
System.out.println(em.name + "在数据库中已经存在,且类型与文件中的一致。 覆盖。");
|
//类型不一致则中止导入
|
}else{
|
System.out.println(em.name + "在数据库中已经存在,且类型与文件中的不一致。 中止导入。");
|
return false;
|
}
|
}else{
|
getService().addEmItem(em);
|
}
|
}
|
return true;
|
} catch (VCIError e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
}
|