ivr流程编辑器

IVRFlowSerializer.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HySoft.IVRFlowEditor.IVRControlUtility;
  6. using HySoft.IVRFlowEditor.Model;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.ComponentModel;
  12. using System.Drawing;
  13. namespace HySoft.IVRFlowEditor.Utility
  14. {
  15. /// <summary>
  16. /// IVR流程编辑器导入导出操作类
  17. /// </summary>
  18. public class IVRFlowSerializer:SerializerBase
  19. {
  20. /// <summary>
  21. /// 保存到文件(所有要序列化到文件的属性必须使用 IVRAttribute 特性加以描述)
  22. /// </summary>
  23. /// <param name="filePath">文件路径(包含文件名)</param>
  24. /// <param name="val">要保存的对象集合</param>
  25. /// <returns></returns>
  26. public override Model.MessageInfo SaveFileInfo(string filePath, List<IVRFlowSerializerInfo> val)
  27. {
  28. MessageInfo msg;
  29. try
  30. {
  31. StringBuilder xmlBuilder = new StringBuilder();
  32. xmlBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?> \r\n<ivr> \r\n");
  33. foreach (IVRFlowSerializerInfo o in val)
  34. {
  35. if (o.IVRControl != null && o.IVRControl.Count > 0)
  36. {
  37. //xmlBuilder.Append("<flow Name=\"" + o.IVR_Name + "\" Type=\"" + o.IVRFlowType.ToString() + "\" IVR_Name=\"" + o.IVR_Name + "\" IVR_Text=\"" + o.IVR_Text + "\" MatchedNum=\"\" MatchedLine=\"\" Concurrency=\"1\"> \r\n");
  38. //2017-4-28修改
  39. xmlBuilder.Append("<flow Name=\"" + o.IVR_Name + "\" Type=\"" + o.IVRFlowType.ToString() + "\" IVR_Name=\"" + o.IVR_Name + "\" IVR_Text=\"" + o.IVR_Text + "\" MatchedNum=\".\" MatchedLine=\"|-1|\" Concurrency=\"1\"> \r\n");
  40. foreach (IVRControlBase ivr in o.IVRControl)
  41. {
  42. Dictionary<int,string> disAtt=new Dictionary<int,string>();
  43. msg = CreateXmlNode("cell", ref xmlBuilder, ivr,true);
  44. if (!msg.State)
  45. return msg;
  46. }
  47. xmlBuilder.Append("</flow> \r\n");
  48. }
  49. }
  50. xmlBuilder.Append("</ivr> \r\n");
  51. File.Delete(filePath);
  52. using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite))
  53. {
  54. StreamWriter sw = new StreamWriter(fs);
  55. sw.Write(xmlBuilder.ToString());
  56. sw.Close();
  57. fs.Close();
  58. }
  59. return new MessageInfo() { Code = 0, State = true, Message = "保存成功!" };
  60. }
  61. catch (Exception ex)
  62. {
  63. return new MessageInfo() { State = false, Code = -1, Message = "保存到文件发生异常,错误信息:" + ex.Message };
  64. }
  65. }
  66. /// <summary>
  67. /// 构建XML节点
  68. /// </summary>
  69. /// <param name="nodeName">节点名称</param>
  70. /// <param name="xmlBuilder">返回的XML内容</param>
  71. /// <param name="b">要解析的对象</param>
  72. /// <param name="isWriteName">是否写节点的属性名称 针对二级节点(二级子节点没有名称)</param>
  73. /// <returns></returns>
  74. private MessageInfo CreateXmlNode(string nodeName,ref StringBuilder xmlBuilder, object b,bool isWriteName)
  75. {
  76. try
  77. {
  78. Dictionary<int, string> disAtt =new Dictionary<int,string>();
  79. List<StringBuilder> nodes = new List<StringBuilder>();
  80. xmlBuilder.Append("<" + nodeName + " "+(isWriteName?" Name=\"" + (b.GetType().GetCustomAttributes(typeof(IVRAttribute), false)[0] as IVRAttribute).Name + "\" ":"")+" ");
  81. foreach(PropertyInfo p in b.GetType().GetProperties())
  82. {
  83. if (p.GetCustomAttributes(typeof(IVRAttribute), false) == null || p.GetCustomAttributes(typeof(IVRAttribute), false).Count() == 0)
  84. continue;
  85. var ivratt = p.GetCustomAttributes(typeof(IVRAttribute), false).Last() as IVRAttribute;
  86. switch (ivratt.IVRSerializerType)
  87. {
  88. case IVRSerializerType.元素:
  89. if (p.PropertyType == typeof(IVRControlBase) && p.GetValue(b, null) != null)
  90. {
  91. disAtt.Add(ivratt.Number, ivratt.Name.Trim() + "=\"" + p.GetValue(b, null).GetType().GetProperty("ID").GetValue(p.GetValue(b, null), null) + "\"");
  92. }
  93. else if (p.PropertyType.IsEnum)
  94. {
  95. if (p.PropertyType == typeof(FinishKeyType) && p.PropertyType.GetField(p.GetValue(b, null).ToString()).GetCustomAttributes(false).Count() > 0)
  96. {
  97. var des = p.PropertyType.GetField(p.GetValue(b, null).ToString()).GetCustomAttributes(false).Last() as DescriptionAttribute;
  98. disAtt.Add(ivratt.Number,p.Name + "=\"" + des.Description + "\" ");
  99. }
  100. else
  101. disAtt.Add(ivratt.Number, ivratt.Name + "=\"" + (int)Enum.Parse(p.PropertyType, p.GetValue(b, null).ToString()) + "\" ");
  102. }
  103. else
  104. disAtt.Add(ivratt.Number, ivratt.Name.Trim() + "=\"" + p.GetValue(b, null) + "\"");
  105. break;
  106. case IVRSerializerType.节点:
  107. if (p.PropertyType.IsGenericType)
  108. {
  109. if (p.PropertyType == typeof(List<IVRDefinevarDefVar>))
  110. {
  111. var ivr = (List<IVRDefinevarDefVar>)p.GetValue(b, null);
  112. foreach (IVRDefinevarDefVar iv in ivr)
  113. {
  114. StringBuilder node = new StringBuilder();
  115. CreateXmlNode(p.Name, ref node, iv,false);
  116. nodes.Add(node);
  117. }
  118. }
  119. if (p.PropertyType == typeof(List<IVRDefaultVar>))
  120. {
  121. var ivr = (List<IVRDefaultVar>)p.GetValue(b, null);
  122. foreach (IVRDefaultVar iv in ivr)
  123. {
  124. StringBuilder node = new StringBuilder();
  125. CreateXmlNode(p.Name, ref node, iv,false);
  126. nodes.Add(node);
  127. }
  128. }
  129. if (p.PropertyType == typeof(List<IVRInputVar>))
  130. {
  131. var ivr = (List<IVRInputVar>)p.GetValue(b, null);
  132. foreach (IVRInputVar iv in ivr)
  133. {
  134. StringBuilder node = new StringBuilder();
  135. CreateXmlNode(p.Name, ref node, iv,false);
  136. nodes.Add(node);
  137. }
  138. }
  139. if (p.PropertyType == typeof(List<IVRBranchVar>))
  140. {
  141. var ivr = (List<IVRBranchVar>)p.GetValue(b, null);
  142. foreach (IVRBranchVar iv in ivr)
  143. {
  144. StringBuilder node = new StringBuilder();
  145. if (iv.Pos != "0")
  146. {
  147. CreateXmlNode(p.Name, ref node, iv,false);
  148. nodes.Add(node);
  149. }
  150. }
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. foreach (int key in disAtt.Keys.OrderBy(a=>a))
  157. {
  158. if (key == 0)
  159. continue;
  160. xmlBuilder.Append(disAtt[key]+" ");
  161. }
  162. if (nodes.Count == 0)
  163. xmlBuilder.Append("/> \r\n");
  164. else
  165. {
  166. xmlBuilder.Append("> \r\n");
  167. foreach (StringBuilder s in nodes)
  168. xmlBuilder.Append(s.ToString());
  169. xmlBuilder.Append("</" + nodeName + ">");
  170. }
  171. return new MessageInfo() { State = true };
  172. }
  173. catch (Exception ex)
  174. {
  175. return new MessageInfo() { Code = -1, State = false, Message = "生成配置文件发生异常,错误信息:" + ex.Message };
  176. }
  177. }
  178. /// <summary>
  179. /// 解析XML文件到IVR流程设计器
  180. /// </summary>
  181. /// <param name="filePath">要打开的XML文件路径</param>
  182. /// <param name="val">返回解析后的对象</param>
  183. /// <returns></returns>
  184. public override Model.MessageInfo OpenFileInfo(string filePath, ref List<IVRFlowSerializerInfo> val)
  185. {
  186. try
  187. {
  188. Dictionary<string, string> lines = new Dictionary<string, string>();
  189. if (File.Exists(filePath))
  190. {
  191. using (StreamReader reader = new StreamReader(filePath))
  192. {
  193. XmlDocument doc = new XmlDocument();
  194. doc.InnerXml = reader.ReadToEnd();
  195. XmlNodeList flowNodes = doc.GetElementsByTagName("flow");
  196. foreach (XmlNode node in flowNodes)
  197. {
  198. //val.Add(new IVRFlowSerializerInfo() { IVR_Name = node.Attributes["Name"].Value, IVR_Text = node.Attributes["Name"].Value, IVRFlowType = (IVRFlowType)Enum.Parse(typeof(IVRFlowType), node.Attributes["Type"].Value), IVRControl = new List<IVRControlBase>(), LineNodes = new List<LineInfo>(), IVRVar=new List<IVRDefinevarDefVar>() });
  199. //2017-4-27
  200. val.Add(new IVRFlowSerializerInfo() { IVR_Name = node.Attributes["IVR_Name"].Value, IVR_Text = node.Attributes["IVR_Text"].Value, IVRFlowType = (IVRFlowType)Enum.Parse(typeof(IVRFlowType), node.Attributes["Type"].Value), IVRControl = new List<IVRControlBase>(), LineNodes = new List<LineInfo>(), IVRVar = new List<IVRDefinevarDefVar>() });
  201. XmlNodeList CellNode = node.SelectNodes("cell");
  202. foreach (XmlNode cell in CellNode)
  203. {
  204. var type = GlobalController.IVRControls[cell.Attributes["Name"].Value];
  205. val.Last().IVRControl.Add((IVRControlBase)Activator.CreateInstance(type, new object[] { cell.Attributes["Name"].Value }));
  206. val.Last().IVRControl.Last().IVR_Width = Int32.Parse(cell.Attributes["IVR_Width"].Value);
  207. val.Last().IVRControl.Last().IVR_Height = Int32.Parse(cell.Attributes["IVR_Height"].Value);
  208. val.Last().IVRControl.Last().Pos = cell.Attributes["Pos"].Value;
  209. val.Last().IVRControl.Last().CenterPoint = new Point(Int32.Parse(cell.Attributes["IVR_X"].Value), Int32.Parse(cell.Attributes["IVR_Y"].Value));
  210. val.Last().IVRControl.Last().SelectedImage = new Bitmap(@"pic\Selected.png");
  211. val.Last().IVRControl.Last().Image = new Bitmap(@"pic\NoSelect.png");
  212. foreach (PropertyInfo p in type.GetProperties())
  213. {
  214. if ((p.GetCustomAttributes(typeof(IVRAttribute), false)) == null || p.GetCustomAttributes(typeof(IVRAttribute), false).Count() == 0)
  215. continue;
  216. var att = p.GetCustomAttributes(typeof(IVRAttribute), false).Last() as IVRAttribute;
  217. switch (att.IVRSerializerType)
  218. {
  219. case IVRSerializerType.元素:
  220. if (p.Name == "Name") continue;
  221. if (p.PropertyType == typeof(IVRControlBase))
  222. {
  223. if (cell.Attributes[p.Name].Value != "0" && !string.IsNullOrEmpty(cell.Attributes[p.Name].Value))
  224. val.Last().LineNodes.Add(new LineInfo() { AttributeName = p.Name , StartNodeID = val.Last().IVRControl.Last().Pos, EndNodeID = cell.Attributes[p.Name].Value });
  225. }
  226. else if (p.PropertyType.IsEnum)
  227. {
  228. if (p.PropertyType == typeof(FinishKeyType))
  229. {
  230. foreach (MemberInfo finkey in p.PropertyType.GetMembers())
  231. {
  232. if (finkey.GetCustomAttributes(typeof(DescriptionAttribute), false) != null
  233. && finkey.GetCustomAttributes(typeof(DescriptionAttribute), false).Count() != 0
  234. && (finkey.GetCustomAttributes(typeof(DescriptionAttribute), false).Last() as DescriptionAttribute).Description == cell.Attributes[p.Name].Value)
  235. {
  236. p.SetValue(val.Last().IVRControl.Last(), (int)Enum.Parse(p.PropertyType, finkey.Name, false), null);
  237. }
  238. }
  239. }
  240. else
  241. {
  242. p.SetValue(val.Last().IVRControl.Last(), Enum.Parse(p.PropertyType, cell.Attributes[p.Name].Value), null);
  243. }
  244. }
  245. else
  246. {
  247. if (p.PropertyType == typeof(Int32) || p.PropertyType == typeof(int))
  248. p.SetValue(val.Last().IVRControl.Last(), Int32.Parse(cell.Attributes[p.Name].Value), null);
  249. else if (p.PropertyType == typeof(Boolean) || p.PropertyType == typeof(bool))
  250. //2017-4-28添加
  251. {
  252. bool bvalue = false;
  253. if (cell.Attributes[p.Name].Value.ToString() == "no")
  254. { bvalue = false; }
  255. else if (cell.Attributes[p.Name].Value.ToString() == "yes")
  256. { bvalue = true ; }
  257. else
  258. { bvalue = bool.Parse(cell.Attributes[p.Name].Value); }
  259. p.SetValue(val.Last().IVRControl.Last(), bvalue, null);//p.SetValue(val.Last().IVRControl.Last(), bool.Parse(cell.Attributes[p.Name].Value), null);//2017-4-28
  260. }
  261. else
  262. p.SetValue(val.Last().IVRControl.Last(), cell.Attributes[p.Name].Value.ToString(), null);
  263. }
  264. break;
  265. case IVRSerializerType.节点:
  266. foreach (XmlNode ivrflowNode in cell.ChildNodes)
  267. {
  268. switch (p.Name+" "+ivrflowNode.Name)
  269. {
  270. case "Branch Branch":
  271. ((IVRBranch)val.Last().IVRControl.Last()).Branch.Add(new IVRBranchVar() { Pos = ivrflowNode.Attributes["Pos"].Value, Value = ivrflowNode.Attributes["Value"].Value });
  272. val.Last().LineNodes.Add(new LineInfo() { AttributeName = ivrflowNode.Attributes["Value"].Value, StartNodeID = ((IVRBranch)val.Last().IVRControl.Last()).ID, EndNodeID = ivrflowNode.Attributes["Pos"].Value });
  273. break;
  274. case "DefVar DefVar":
  275. IVRDefinevarDefVar defval = (new IVRDefinevarDefVar() { VarName = ivrflowNode.Attributes["VarName"].Value, VarVal = ivrflowNode.Attributes["VarVal"].Value, VarType = (NumberType)Enum.Parse(typeof(NumberType), ivrflowNode.Attributes["VarType"].Value) });
  276. ((IVRDefinevar)val.Last().IVRControl.Last()).DefVar.Add(defval);
  277. val.Last().IVRVar.Add(defval);
  278. break;
  279. case "IVRInputVar IVRInputVar":
  280. ((IVRSocket)val.Last().IVRControl.Last()).IVRInputVar.Add(new IVRInputVar() { VarVal = ivrflowNode.Attributes["VarVal"].Value, VarType = (NumberType)Enum.Parse(typeof(NumberType), ivrflowNode.Attributes["VarType"].Value) });
  281. break;
  282. case "OutputVar OutputVar":
  283. ((IVRSocket)val.Last().IVRControl.Last()).OutputVar.Add(new IVRDefaultVar() { Var = ivrflowNode.Attributes["Var"].Value });
  284. break;
  285. case "Map Map":
  286. ((IVRRecordSet)val.Last().IVRControl.Last()).Map.Add(new IVRDefaultVar() { Var = ivrflowNode.Attributes["Var"].Value });
  287. break;
  288. default:
  289. break;
  290. }
  291. } break;
  292. }
  293. }
  294. }
  295. }
  296. return new MessageInfo() { Code = 0, Message = "解析完成!", State = true, };
  297. }
  298. }
  299. else
  300. {
  301. return new MessageInfo()
  302. {
  303. Code = -1,
  304. Message = "没有找到IVR图形文件!",
  305. State = false,
  306. };
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. return new MessageInfo() { Code = -1, Message = "读取IVR图形文件发生异常,错误信息:" + ex.Message };
  312. }
  313. }
  314. }
  315. }