DataReg.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Data;
  5. using System.Collections;
  6. using Tofly.Core.Context;
  7. using Tofly.Core.Context.Support;
  8. using Tofly.Core.Util;
  9. using Tofly.Core.Stereotype;
  10. using Tofly.CoreUI.Message;
  11. using Tofly.CoreUI.Control;
  12. using Tofly.CoreUI.Plot.Config;
  13. using Tofly.Data.General;
  14. using Tofly.Data.Util;
  15. using Tofly.Data.MetadataManager;
  16. using Tofly.DataUI.Catalog.Logic;
  17. using Tofly.GIS.SpatialDatabase;
  18. using Tofly.CoreUI.Plot;
  19. using Tofly.DataUI.DataConnection;
  20. namespace Tofly.DataUI
  21. {
  22. /// <summary>
  23. /// 定义数据注册对外插件接口
  24. /// </summary>
  25. [Component(IsSingleton = "false", Name = "IDataReg")]
  26. [ConfigAttribute(GroupName = "数据服务管理", ControlCaption = "数据服务注册", Index = "1,1")]
  27. public class DataReg : IDataReg, IConfigure
  28. {
  29. private IDataRegForm pDataRegForm;
  30. private IAddDataRegForm2 addDataRegForm2;
  31. private IApplicationContext context;
  32. public DataReg()
  33. : this("IDataRegForm")
  34. {
  35. }
  36. #region 构造函数
  37. public DataReg(string name)
  38. {
  39. context = ContextRegistry.GetContext();
  40. pDataRegForm = context.GetObject(name) as IDataRegForm;
  41. //pDataRegForm.FieldGrid.MultiSelecte = false;
  42. addDataRegForm2 = context.GetObject("IAddDataRegForm2") as IAddDataRegForm2;
  43. if (pDataRegForm != null)
  44. {
  45. if (pDataRegForm is IUserControl)
  46. (pDataRegForm as IUserControl).LoadEvent += FormLoad;
  47. if (pDataRegForm is IForm)
  48. (pDataRegForm as IForm).LoadEvent += FormLoad;
  49. if (pDataRegForm.MetadataTableTree != null)
  50. {
  51. pDataRegForm.MetadataTableTree.CodeFieldName = MetadataTableRow.FIELD_TABLEID;
  52. pDataRegForm.MetadataTableTree.DisplayFieldName = MetadataTableRow.FIELD_TABLEALIASNAME;
  53. pDataRegForm.MetadataTableTree.SelectedIndexChangedEvent += MetadataTableTreeSelectedIndexChangedEvent;
  54. }
  55. if (pDataRegForm.DataRegTree != null)
  56. {
  57. pDataRegForm.DataRegTree.CodeFieldName = ConnectionRow.FIELD_CONNECTIONID;
  58. pDataRegForm.DataRegTree.DisplayFieldName = ConnectionRow.FIELD_CONNECTIONNAME;
  59. pDataRegForm.DataRegTree.SelectedIndexChangedEvent += DataRegTableTreeSelectedIndexChangedEvent;
  60. //pDataRegForm.CreateStructAndReg.ClickEvent += new CoreUI.Control.EventHandler(bbiCreateStructAndReg_ClickEvent);
  61. pDataRegForm.DataRegTree.DoubleClickEvent += new CoreUI.Control.EventHandler(DataRegTree_DoubleClickEvent);
  62. }
  63. if (pDataRegForm.RelationGrid != null)
  64. pDataRegForm.RelationGrid.LoadEvent += RelationGridLoad;
  65. if (pDataRegForm.AddTableButton != null)
  66. pDataRegForm.AddTableButton.ClickEvent += AddTableEvent;
  67. if (pDataRegForm.EditTableButton != null)
  68. pDataRegForm.EditTableButton.ClickEvent += EditTableEvent;
  69. if (pDataRegForm.DelTableButton != null)
  70. pDataRegForm.DelTableButton.ClickEvent += DelTableEvent;
  71. if (pDataRegForm.AddFieldButton != null)
  72. pDataRegForm.AddFieldButton.ClickEvent += AddFieldButtonClickEvent;
  73. if (pDataRegForm.EditFieldButton != null)
  74. pDataRegForm.EditFieldButton.ClickEvent += EditFieldButtonClickEvent;
  75. if (pDataRegForm.FieldGrid != null)
  76. pDataRegForm.FieldGrid.DoubleClickEvent += new CoreUI.Control.EventHandler(FieldGrid_DoubleClickEvent);
  77. if (pDataRegForm.DelFieldButton != null)
  78. pDataRegForm.DelFieldButton.ClickEvent += DelFieldButtonClickEvent;
  79. if( pDataRegForm.SynDomainButton!=null)
  80. pDataRegForm.SynDomainButton.ClickEvent += new CoreUI.Control.EventHandler(SynDomainButton_ClickEvent);
  81. if (pDataRegForm.AddRelationButton != null)
  82. pDataRegForm.AddRelationButton.ClickEvent += AddRelationButtonClickEvent;
  83. if (pDataRegForm.EditRelationButton != null)
  84. pDataRegForm.EditRelationButton.ClickEvent += EditRelationButtonClickEvent;
  85. if (pDataRegForm.DelRelationButton != null)
  86. pDataRegForm.DelRelationButton.ClickEvent += DelRelationButtonClickEvent;
  87. if (pDataRegForm.AddRegButton != null)
  88. pDataRegForm.AddRegButton.ClickEvent += AddRegButton_ClickEvent;
  89. if (pDataRegForm.DelRegButton != null)
  90. pDataRegForm.DelRegButton.ClickEvent += DelRegButton_ClickEvent;
  91. if (pDataRegForm.ImportButton != null)
  92. pDataRegForm.ImportButton.ClickEvent += ImportButton_ClickEvent;
  93. }
  94. }
  95. #endregion
  96. #region 加载一个连接的所有表
  97. /// <summary>
  98. /// 双击连接加载下一级节点(待注册的数据表)
  99. /// </summary>
  100. /// <param name="sender"></param>
  101. void DataRegTree_DoubleClickEvent(object sender)
  102. {
  103. ConnectionRow CurrentNodeentity = pDataRegForm.DataRegTree.SelectedValue as ConnectionRow;
  104. if (CurrentNodeentity != null)
  105. {
  106. if (pDataRegForm.DataRegTree.GetFocusedNodeFirstChildData() != null)
  107. return;
  108. bool bHasChildNode = false;
  109. ConnectionInfo conInfo = new ConnectionInfo(CurrentNodeentity.ConnectionStr);
  110. conInfo.WorkspaceType = CurrentNodeentity.DataBaseType;
  111. try
  112. {
  113. IWorkspace workspace = Data.General.WorkspaceFactoryProvider.GetWorkspaceFactory().OpenWorkspace(conInfo);
  114. IFeatureWorkspace featureWorkSpace = workspace as IFeatureWorkspace;
  115. List<string> DataSetTables = new List<string>();
  116. if (featureWorkSpace != null)
  117. {
  118. string[] strDataSet = featureWorkSpace.GetFeatureDatasets();
  119. if (strDataSet != null)
  120. {
  121. foreach (string item in strDataSet)
  122. {
  123. try
  124. {
  125. IFeatureDataset pDataset = featureWorkSpace.OpenFeatureDataset(item);
  126. string[] tableNames = featureWorkSpace.GetClassNameFromDataset(pDataset);
  127. if (tableNames != null && tableNames.Length > 0)
  128. {
  129. foreach (string tablename in tableNames)
  130. {
  131. if (!DataSetTables.Contains(tablename))
  132. {
  133. DataSetTables.Add(tablename);
  134. }
  135. }
  136. }
  137. }
  138. catch { }
  139. }
  140. }
  141. }
  142. //属性表集合
  143. string[] tablesName = null;
  144. if (workspace is IMixedWorkspace)
  145. {
  146. IMixedWorkspace mixWorkspace = workspace as IMixedWorkspace;
  147. PluginGISType plugingistype = (PluginGISType)Core.ServiceLocator.ServiceLocatorFactory.
  148. ServiceLocator.GetInstance<int>(Core.ServiceLocator.ServiceLocatorKeys.CurGISType);
  149. if(mixWorkspace.WorkspaceType==WorkspaceType.Oracle && plugingistype==PluginGISType.SuperMap)
  150. if(mixWorkspace.ADOWorkspace!=null)
  151. tablesName = mixWorkspace.ADOWorkspace.GetObjects(TableType.Table);
  152. }
  153. if(tablesName==null)
  154. tablesName = workspace.GetObjects(TableType.Table);
  155. object currentNode = pDataRegForm.DataRegTree.GetFocusedNode();
  156. if (tablesName != null && tablesName.Length > 0)
  157. {
  158. object objAttribute = pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, "属性表" }, currentNode, "属性表");
  159. foreach (string item in tablesName)
  160. {
  161. string regtablename = GetRegName(item, CurrentNodeentity.ConnectionID);
  162. if (regtablename == null)
  163. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item }, objAttribute, conInfo);
  164. else
  165. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item + "(注册结构:" + regtablename + ")" }, objAttribute, conInfo);
  166. }
  167. bHasChildNode = true;
  168. }
  169. List<string> lstFeatureTable = new List<string>();
  170. //点要素集合
  171. string[] tablePoint = workspace.GetObjects(TableType.PointFeatureClass);
  172. //线要素集合
  173. string[] tableLine = workspace.GetObjects(TableType.PolylineFeatureClass);
  174. //面要素集合(下面将点、线、面合并)
  175. string[] tablePolygon = workspace.GetObjects(TableType.PolygoneFeatureClass);
  176. if (tablePoint != null)
  177. {
  178. foreach (string item in tablePoint)
  179. {
  180. lstFeatureTable.Add(item);
  181. }
  182. }
  183. if (tablePoint != null)
  184. {
  185. foreach (string item in tableLine)
  186. {
  187. lstFeatureTable.Add(item);
  188. }
  189. }
  190. if (tablePoint != null)
  191. {
  192. foreach (string item in tablePolygon)
  193. {
  194. lstFeatureTable.Add(item);
  195. }
  196. }
  197. if (DataSetTables != null && DataSetTables.Count > 0)
  198. {
  199. for (int i = 0; i < DataSetTables.Count; i++)
  200. {
  201. if (!lstFeatureTable.Contains(DataSetTables[i]))
  202. {
  203. lstFeatureTable.Add(DataSetTables[i]);
  204. }
  205. }
  206. }
  207. object objFeature = pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, "图形要素" }, currentNode, "图形要素");
  208. if (lstFeatureTable.Count > 0)
  209. {
  210. foreach (string item in lstFeatureTable)
  211. {
  212. string regtablename = GetRegName(item, CurrentNodeentity.ConnectionID);
  213. if (regtablename == null)
  214. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item }, objFeature, conInfo);
  215. else
  216. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item + "(注册结构:" + regtablename + ")" }, objFeature, conInfo);
  217. }
  218. bHasChildNode = true;
  219. }
  220. //视图
  221. string[] viewInfo = workspace.GetObjects(TableType.View);
  222. object objView = pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, "视图" }, currentNode, "视图");
  223. if (viewInfo != null)
  224. {
  225. foreach (string item in viewInfo)
  226. {
  227. string regtablename = GetRegName(item, CurrentNodeentity.ConnectionID);
  228. if (regtablename == null)
  229. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item}, objView, conInfo);
  230. else
  231. pDataRegForm.DataRegTree.AddNode(new object[] { CurrentNodeentity.ConnectionID, item + "(注册结构:" + regtablename + ")" }, objView, conInfo);
  232. }
  233. bHasChildNode = true;
  234. }
  235. pDataRegForm.DataRegTree.Refresh();
  236. pDataRegForm.DataRegTree.ExpendNode(currentNode);
  237. }
  238. catch (Exception ex)
  239. {
  240. if(!bHasChildNode)
  241. MessageManager.Show(MessageType.Warning, "数据不存在!", "系统提示");
  242. }
  243. }
  244. }
  245. private string GetRegName(string tableName, string connectionID)
  246. {
  247. DataRegRow row = new DataRegRow();
  248. row.TableName = tableName;
  249. row.ConnectionID = connectionID;
  250. row = DataRegTable.dataRegTable.GetEntityByRow<DataRegRow>(row);
  251. if (row == null)
  252. return null;
  253. else
  254. return row.TableAliasName;
  255. }
  256. #endregion
  257. /// <summary>
  258. /// 数据注册参数
  259. /// </summary>
  260. public DataRegParameter DataRegParameter { get; set; }
  261. /// <summary>
  262. /// 获取数据注册UI
  263. /// </summary>
  264. public IDataRegForm DataRegForm
  265. {
  266. get { return pDataRegForm; }
  267. }
  268. #region 数据加载
  269. protected virtual void RelationGridLoad(object sender)
  270. {
  271. IComboBoxColumn pColumn = pDataRegForm.RelationGrid.GetColumn(TableRelationRow.FIELD_CTABLEID) as IComboBoxColumn;
  272. if (pColumn != null && pColumn.DataSource == null)
  273. {
  274. pColumn.CodeFieldName = MetadataTableRow.FIELD_TABLEID;
  275. pColumn.DisplayFieldName = MetadataTableRow.FIELD_TABLEALIASNAME;
  276. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  277. pColumn.DataSource = MetadataDataSourceUtils.GetTables(true);
  278. else
  279. pColumn.DataSource = MetadataDataSourceUtils.GetTables(false);
  280. }
  281. }
  282. /// <summary>
  283. /// 初始化数据
  284. /// </summary>
  285. protected virtual void FormLoad(object sender)
  286. {
  287. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  288. {
  289. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(true);
  290. pDataRegForm.DataRegTree.DataSource = InitDataRegTreeData();
  291. }
  292. else
  293. {
  294. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(false);
  295. pDataRegForm.DataRegTree.DataSource = InitDataRegTreeData();
  296. }
  297. }
  298. private List<TreeDataNode> InitDataRegTreeData()
  299. {
  300. IQueryFilter queryFilter = new QueryFilter();
  301. queryFilter.SelectFields = "*";
  302. List<ConnectionRow> conndataTables = ConnectionTable.connectionTable.GetEntityList<ConnectionRow>(queryFilter);
  303. TreeDataNode Root = new TreeDataNode();
  304. Root.ChildNodes = new List<TreeDataNode>();
  305. if (conndataTables != null)
  306. {
  307. foreach (var item in conndataTables)
  308. {
  309. TreeDataNode NewNode = new TreeDataNode();
  310. NewNode.NodeName = item.ConnectionName;
  311. NewNode.NodeValue = item.ConnectionID;
  312. NewNode.NodeData = item;
  313. Root.ChildNodes.Add(NewNode);
  314. }
  315. return Root.ChildNodes;
  316. }
  317. return Root.ChildNodes;
  318. }
  319. bool bRegTree = false ;
  320. protected virtual void MetadataTableTreeSelectedIndexChangedEvent(object sender, object data)
  321. {
  322. if (!bRegTree )
  323. {
  324. if (pDataRegForm.DataRegTree.GetAllNodes() != null && pDataRegForm.DataRegTree.GetAllNodes().Count > 0)
  325. pDataRegForm.DataRegTree.SetFocusedNode(pDataRegForm.DataRegTree.GetAllNodes()[0]);
  326. else
  327. pDataRegForm.DataRegTree.SetFocusedNode(null);
  328. bRegTree = !bRegTree ;
  329. }
  330. if (!(data is IRow))
  331. {
  332. SetControlEnable(false);
  333. }
  334. else
  335. {
  336. IRow tableRow = data as IRow;
  337. if (tableRow.GetValue<bool>("ISSYSTABLE"))
  338. {
  339. SetControlEnable(false);
  340. }
  341. else
  342. {
  343. SetControlEnable(true);
  344. }
  345. }
  346. }
  347. protected virtual void DataRegTableTreeSelectedIndexChangedEvent(object sender, object data)
  348. {
  349. if (bRegTree)
  350. {
  351. if (pDataRegForm.MetadataTableTree.GetAllNodes() != null && pDataRegForm.MetadataTableTree.GetAllNodes().Count > 0)
  352. pDataRegForm.MetadataTableTree.SetFocusedNode(pDataRegForm.MetadataTableTree.GetAllNodes()[0]);
  353. else
  354. pDataRegForm.MetadataTableTree.SetFocusedNode(null);
  355. bRegTree = !bRegTree;
  356. }
  357. SetControlEnable(false);
  358. pDataRegForm.FieldGrid.DataSource = null;
  359. ConnectionInfo connectionInfo = pDataRegForm.DataRegTree.SelectedValue as ConnectionInfo;
  360. if (connectionInfo == null)
  361. return;
  362. object obj = pDataRegForm.DataRegTree.GetFocusedNode();
  363. if (obj == null)
  364. return;
  365. string tableName = pDataRegForm.DataRegTree.GetValue(obj, 1).ToString();
  366. if (tableName.Contains("("))
  367. tableName = tableName.Remove(tableName.IndexOf("("));
  368. IWorkspace pWorkspace = Data.General.WorkspaceFactoryProvider.GetWorkspaceFactory().OpenWorkspace(connectionInfo);
  369. if (pWorkspace == null)
  370. return;
  371. ITable pTable = null;
  372. try
  373. {
  374. pTable = pWorkspace.OpenTable(tableName);
  375. if (pTable == null)
  376. return;
  377. }
  378. catch
  379. {
  380. return;
  381. }
  382. List<TableMetadataRow> rows = new List<TableMetadataRow>();
  383. NetDataTypeConvert dataTypeConvert = new NetDataTypeConvert();
  384. foreach (Field field in pTable.Fields)
  385. {
  386. TableMetadataRow row = new TableMetadataRow();
  387. row.AllowNull = field.AllowNull;
  388. row.CanShow = field.CanShow;
  389. row.DataType = dataTypeConvert.Convert(field.DataType);
  390. row.DataType = row.DataType.Replace("SYSTEM.", "");
  391. row.Decimaldigits = field.Decimaldigits;
  392. row.DefaultValue = field.DefaultValue;
  393. row.FieldAliasName = field.FieldAliasName;
  394. row.FieldID = field.FieldID;
  395. row.FieldName = field.FieldName;
  396. row.IsAuto = field.IsAuto;
  397. row.IsKey = field.IsKey;
  398. row.MaxLen = field.MaxLen;
  399. row.ReadOnly = field.ReadOnly;
  400. row.UniqueGroup = field.UniqueGroup;
  401. rows.Add(row);
  402. }
  403. if (rows == null || rows.Count <= 0)
  404. return;
  405. pDataRegForm.FieldGrid.DataSource = rows;
  406. }
  407. private void SetControlEnable(bool enable)
  408. {
  409. if (pDataRegForm.EditTableButton != null)
  410. pDataRegForm.EditTableButton.Enabled = enable;
  411. if (pDataRegForm.DelTableButton != null)
  412. pDataRegForm.DelTableButton.Enabled = enable;
  413. if (pDataRegForm.AddFieldButton != null)
  414. pDataRegForm.AddFieldButton.Enabled = enable;
  415. if (pDataRegForm.EditFieldButton != null)
  416. pDataRegForm.EditFieldButton.Enabled = enable;
  417. if (pDataRegForm.DelFieldButton != null)
  418. pDataRegForm.DelFieldButton.Enabled = enable;
  419. if (pDataRegForm.ImportButton != null)
  420. pDataRegForm.ImportButton.Enabled = enable;
  421. if (pDataRegForm.AddRelationButton != null)
  422. pDataRegForm.AddRelationButton.Enabled = enable;
  423. if (pDataRegForm.EditRelationButton != null)
  424. pDataRegForm.EditRelationButton.Enabled = enable;
  425. if (pDataRegForm.DelRelationButton != null)
  426. pDataRegForm.DelRelationButton.Enabled = enable;
  427. if (pDataRegForm.SynDomainButton != null)
  428. pDataRegForm.SynDomainButton.Enabled = enable;
  429. }
  430. #endregion
  431. #region 元数据表操作
  432. /// <summary>
  433. /// 添加结构表信息
  434. /// </summary>
  435. /// <param name="sender"></param>
  436. protected virtual void AddTableEvent(object sender)
  437. {
  438. if (pDataRegForm.MetadataTableTree == null)
  439. return;
  440. IAddTableForm addTableForm = context.GetObject("IAddTableForm") as IAddTableForm;
  441. if (addTableForm != null)
  442. {
  443. if (addTableForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  444. {
  445. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  446. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(true);
  447. else
  448. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(false);
  449. }
  450. }
  451. }
  452. /// <summary>
  453. /// 编辑结构表信息
  454. /// </summary>
  455. /// <param name="sender"></param>
  456. protected virtual void EditTableEvent(object sender)
  457. {
  458. if (pDataRegForm.MetadataTableTree == null)
  459. return;
  460. if (pDataRegForm.MetadataTableTree.SelectedValue == null)
  461. return;
  462. IAddTableForm addTableForm = context.GetObject("IAddTableForm") as IAddTableForm;
  463. if (addTableForm != null)
  464. {
  465. addTableForm.InitControlData();
  466. addTableForm.DataSource = pDataRegForm.MetadataTableTree.SelectedValue;
  467. if (addTableForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  468. {
  469. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  470. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(true);
  471. else
  472. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(false);
  473. }
  474. }
  475. }
  476. /// <summary>
  477. /// 删除结构表信息
  478. /// </summary>
  479. /// <param name="sender"></param>
  480. protected virtual void DelTableEvent(object sender)
  481. {
  482. if (pDataRegForm.MetadataTableTree == null)
  483. return;
  484. if (pDataRegForm.MetadataTableTree.SelectedValue == null)
  485. return;
  486. if (MessageManager.Show(MessageType.Information, "您确认删除数据吗?",
  487. "系统提示", MessageButton.YesNo) == MessageResult.Yes)
  488. {
  489. pDataRegForm.MetadataTableTree.DeleteFocusedNode(true);
  490. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  491. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(true);
  492. else
  493. pDataRegForm.MetadataTableTree.DataSource = MetadataDataSourceUtils.GetTables(false);
  494. pDataRegForm.DataRegTree.DataSource = InitDataRegTreeData();
  495. }
  496. }
  497. #endregion
  498. #region 字段操作
  499. protected virtual void DelFieldButtonClickEvent(object sender)
  500. {
  501. if (pDataRegForm.FieldGrid == null || pDataRegForm.FieldGrid.SelectedValue == null)
  502. return;
  503. if (MessageManager.Show(MessageType.Information, "您确认删除数据吗?",
  504. "系统提示", MessageButton.YesNo) == MessageResult.Yes)
  505. pDataRegForm.FieldGrid.DeleteFocusedRow(true);
  506. }
  507. void FieldGrid_DoubleClickEvent(object sender)
  508. {
  509. if(pDataRegForm.EditFieldButton.Enabled)
  510. EditFieldButtonClickEvent(sender);
  511. }
  512. protected virtual void EditFieldButtonClickEvent(object sender)
  513. {
  514. if (pDataRegForm.FieldGrid == null || pDataRegForm.FieldGrid.SelectedValue == null)
  515. return;
  516. IAddFieldForm addFieldForm = context.GetObject("IAddFieldForm") as IAddFieldForm;
  517. if (addFieldForm != null)
  518. {
  519. InitFieldFormCodeTableComboBox(addFieldForm);
  520. addFieldForm.InitControlData();
  521. addFieldForm.DataSource = pDataRegForm.FieldGrid.SelectedValue;
  522. if (addFieldForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  523. {
  524. pDataRegForm.FieldGrid.EditFocusedRow(addFieldForm.DataSource);
  525. }
  526. }
  527. }
  528. protected virtual void AddFieldButtonClickEvent(object sender)
  529. {
  530. int ID = GetMaxID()+1;
  531. IAddFieldForm addFieldForm = context.GetObject("IAddFieldForm",new object[]{ID}) as IAddFieldForm;
  532. if (addFieldForm != null)
  533. {
  534. InitFieldFormCodeTableComboBox(addFieldForm);
  535. addFieldForm.BeforeSave += FieldBeforeSave;
  536. if (addFieldForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  537. {
  538. if (pDataRegForm.FieldGrid != null)
  539. pDataRegForm.FieldGrid.AddGridRow(addFieldForm.DataSource);
  540. }
  541. }
  542. }
  543. private int GetMaxID()
  544. {
  545. List<IRow> metadataTable = pDataRegForm.FieldGrid.DataSource as List<IRow>;
  546. if (metadataTable == null || metadataTable.Count == 0)
  547. return 0;
  548. int i=0;
  549. int id = 0;
  550. foreach (RowBase row in metadataTable)
  551. {
  552. i = int.Parse(row[TableMetadataRow.FIELD_FIELDID].ToString());
  553. id = id < i ? i : id;
  554. }
  555. return id;
  556. }
  557. /// <summary>
  558. /// 导入字段数据
  559. /// </summary>
  560. /// <param name="sender"></param>
  561. private void ImportButton_ClickEvent(object sender)
  562. {
  563. List<ITable> tables = SelectDataUtils.OpenTableAndGraphs(false);
  564. if (tables == null || tables.Count == 0)
  565. return;
  566. ITable pTable = tables[0];
  567. List<TableMetadataRow> rows = new List<TableMetadataRow>();
  568. MetadataTableRow tableRow = pDataRegForm.MetadataTableTree.SelectedValue as MetadataTableRow;
  569. if (tableRow == null)
  570. {
  571. MessageManager.Show(MessageType.Warning, "请选择或者创建元数据结构!", "提示");
  572. return;
  573. }
  574. List<IRow> metadataTable = pDataRegForm.FieldGrid.DataSource as List<IRow>;
  575. metadataTable.Sort((one, two) => one.GetValue<Int32>(TableMetadataRow.FIELD_FIELDID).CompareTo(two.GetValue<Int32>(TableMetadataRow.FIELD_FIELDID)));
  576. int addIndex = metadataTable[metadataTable.Count - 1].GetValue<Int32>(TableMetadataRow.FIELD_FIELDID);
  577. NetDataTypeConvert dataTypeConvert = new NetDataTypeConvert();
  578. foreach (Field field in pTable.Fields)
  579. {
  580. if (metadataTable.Exists(temp => temp.GetValue<string>(TableMetadataRow.FIELD_FIELDNAME).ToUpper().Equals(field.FieldName.ToUpper())))
  581. continue;
  582. addIndex++;
  583. TableMetadataRow row = new TableMetadataRow();
  584. row.SetValue(TableMetadataRow.FIELD_TABLEID, tableRow.TableID);
  585. row.AllowNull = field.AllowNull;
  586. row.CanShow = field.CanShow;
  587. row.DataType = dataTypeConvert.Convert(field.DataType);
  588. row.DataType = row.DataType.Replace("SYSTEM.", "");
  589. row.Decimaldigits = field.Decimaldigits;
  590. row.DefaultValue = field.DefaultValue;
  591. row.FieldAliasName = field.FieldAliasName;
  592. //row.FieldID = field.FieldID;
  593. row.FieldID = addIndex;
  594. row.FieldName = field.FieldName;
  595. row.IsAuto = field.IsAuto;
  596. row.IsKey = field.IsKey;
  597. row.MaxLen = field.MaxLen;
  598. row.ReadOnly = field.ReadOnly;
  599. row.UniqueGroup = field.UniqueGroup;
  600. rows.Add(row);
  601. }
  602. if (rows == null || rows.Count <= 0)
  603. return;
  604. // 保存数据
  605. try
  606. {
  607. MetadataDataSourceUtils.SaveTableMetadataRow(rows);
  608. pDataRegForm.FieldGrid.AddGridRows(rows.ToArray());
  609. }
  610. catch (Exception ex)
  611. {
  612. MessageManager.Show(MessageType.Error, ex.Message, "提示");
  613. }
  614. //MetadataDataSourceUtils.ClearCache();
  615. }
  616. protected virtual bool FieldBeforeSave(object data)
  617. {
  618. RowBase row = data as RowBase;
  619. if (row != null)
  620. {
  621. MetadataTableRow tableRow = pDataRegForm.MetadataTableTree.SelectedValue as MetadataTableRow;
  622. if (tableRow != null)
  623. row.SetValue(TableMetadataRow.FIELD_TABLEID, tableRow.TableID);
  624. }
  625. return true;
  626. }
  627. protected virtual void InitFieldFormCodeTableComboBox(IAddFieldForm addFieldForm)
  628. {
  629. addFieldForm.CodeTableComboBox.CodeFieldName = MetadataTableRow.FIELD_TABLEID;
  630. addFieldForm.CodeTableComboBox.DisplayFieldName = MetadataTableRow.FIELD_TABLEALIASNAME;
  631. addFieldForm.CodeTableComboBox.DataSource = MetadataDataSourceUtils.GetCodeTables();
  632. }
  633. protected virtual void SynDomainButton_ClickEvent(object sender)
  634. {
  635. try
  636. {
  637. ConnectionInfo connectionInfo = pDataRegForm.DataRegTree.SelectedValue as ConnectionInfo;
  638. if (connectionInfo == null)
  639. return;
  640. object obj = pDataRegForm.DataRegTree.GetFocusedNode();
  641. if (obj == null)
  642. return;
  643. string tableName = pDataRegForm.DataRegTree.GetValue(obj, 1).ToString();
  644. if (tableName.Contains("("))
  645. tableName = tableName.Remove(tableName.IndexOf("("));
  646. IWorkspace pWorkspace = Data.General.WorkspaceFactoryProvider.GetWorkspaceFactory().OpenWorkspace(connectionInfo);
  647. if (pWorkspace == null)
  648. return;
  649. ITable pTable = pWorkspace.OpenTable(tableName);
  650. if (pTable == null)
  651. return;
  652. IUpdateDomain pUpdateDomain = context.GetObject("IUpdateDomain") as IUpdateDomain;
  653. if (pUpdateDomain.SynchorDomainFormStructure(pTable))
  654. {
  655. MessageManager.Show("同步成功!");
  656. }
  657. }
  658. catch
  659. {
  660. MessageManager.Show("同步失败!");
  661. }
  662. }
  663. #endregion
  664. #region 子表操作
  665. /// <summary>
  666. /// 删除关系表
  667. /// </summary>
  668. /// <param name="sender"></param>
  669. protected virtual void DelRelationButtonClickEvent(object sender)
  670. {
  671. if (pDataRegForm.RelationGrid == null || pDataRegForm.RelationGrid.SelectedValue == null)
  672. return;
  673. //if (pDataRegForm.MessageBox.Show(MessageType.Information, "您确认删除数据吗?",
  674. // "系统提示", MessageButton.YesNo) == MessageResult.Yes)
  675. // pDataRegForm.RelationGrid.DeleteFocusedRow(true);
  676. if (MessageManager.Show(MessageType.Information, "您确认删除数据吗?",
  677. "系统提示", MessageButton.YesNo) == MessageResult.Yes)
  678. pDataRegForm.RelationGrid.DeleteFocusedRow(true);
  679. }
  680. /// <summary>
  681. /// 编辑关系表
  682. /// </summary>
  683. /// <param name="sender"></param>
  684. protected virtual void EditRelationButtonClickEvent(object sender)
  685. {
  686. if (pDataRegForm.RelationGrid == null || pDataRegForm.RelationGrid.SelectedValue == null)
  687. return;
  688. IAddRelationForm addFieldForm = context.GetObject("IAddRelationForm") as IAddRelationForm;
  689. if (addFieldForm != null)
  690. {
  691. InitCTableComboBox(addFieldForm);
  692. addFieldForm.InitControlData();
  693. addFieldForm.DataSource = pDataRegForm.RelationGrid.SelectedValue;
  694. if (addFieldForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  695. {
  696. pDataRegForm.RelationGrid.EditFocusedRow(addFieldForm.DataSource);
  697. }
  698. }
  699. }
  700. /// <summary>
  701. /// 添加关系表信息
  702. /// </summary>
  703. /// <param name="sender"></param>
  704. protected virtual void AddRelationButtonClickEvent(object sender)
  705. {
  706. if (pDataRegForm.RelationGrid == null)
  707. return;
  708. IAddRelationForm addFieldForm = context.GetObject("IAddRelationForm") as IAddRelationForm;
  709. if (addFieldForm != null)
  710. {
  711. InitCTableComboBox(addFieldForm);
  712. addFieldForm.BeforeSave += RelationBeforeSave;
  713. if (addFieldForm.ShowDialog(null) == Tofly.CoreUI.Control.DialogResult.OK)
  714. pDataRegForm.RelationGrid.AddGridRow(addFieldForm.DataSource);
  715. }
  716. }
  717. protected virtual bool RelationBeforeSave(object data)
  718. {
  719. RowBase row = data as RowBase;
  720. if (row != null)
  721. {
  722. MetadataTableRow tableRow = pDataRegForm.MetadataTableTree.SelectedValue as MetadataTableRow;
  723. if (tableRow != null)
  724. row.SetValue(TableRelationRow.FIELD_PTABLEID, tableRow.TableID);
  725. if (row.GetValue<string>(TableRelationRow.FIELD_PTABLEID).Equals(
  726. row.GetValue<string>(TableRelationRow.FIELD_CTABLEID)))
  727. {
  728. throw new Exception("主子表不能是同一个表!");
  729. }
  730. }
  731. return true;
  732. }
  733. protected virtual void InitCTableComboBox(IAddRelationForm addFieldForm)
  734. {
  735. addFieldForm.CTableComboBox.CodeFieldName = MetadataTableRow.FIELD_TABLEID;
  736. addFieldForm.CTableComboBox.DisplayFieldName = MetadataTableRow.FIELD_TABLEALIASNAME;
  737. if (DataRegParameter != null && DataRegParameter.ShowSystemTables)
  738. addFieldForm.CTableComboBox.DataSource = MetadataDataSourceUtils.GetTables(true);
  739. else
  740. addFieldForm.CTableComboBox.DataSource = MetadataDataSourceUtils.GetTables(false);
  741. }
  742. #endregion
  743. #region 注册操作
  744. /// <summary>
  745. /// 添加注册
  746. /// </summary>
  747. /// <param name="sender"></param>
  748. private void AddRegButton_ClickEvent(object sender)
  749. {
  750. addDataRegForm2 = ContextRegistry.GetContext().GetObject("IAddDataRegForm2") as IAddDataRegForm2;
  751. if (addDataRegForm2 != null)
  752. {
  753. if (addDataRegForm2.ShowDialog() == Tofly.CoreUI.Control.DialogResult.OK)
  754. {
  755. pDataRegForm.DataRegTree.Clear();
  756. saveConnectionInfo(addDataRegForm2.ConnectionName, addDataRegForm2.ConnectionInfo);
  757. pDataRegForm.DataRegTree.DataSource = InitDataRegTreeData();
  758. }
  759. }
  760. }
  761. private void saveConnectionInfo(string connectionname,ConnectionInfo info)
  762. {
  763. string errorinfo = "";
  764. bool successs = Tofly.Data.Util.DefaultRegTableMetadata.SaveConnectionInfo(connectionname, info, ref errorinfo);
  765. if (!successs && !String.IsNullOrEmpty(errorinfo))
  766. MessageManager.Show(MessageType.Error, errorinfo, "提示");
  767. }
  768. /// <summary>
  769. /// 删除连接
  770. /// </summary>
  771. /// <param name="sender"></param>
  772. private void DelRegButton_ClickEvent(object sender)
  773. {
  774. if (pDataRegForm.RegGrid == null || pDataRegForm.RegGrid.SelectedValue == null)
  775. return;
  776. MessageResult result = MessageManager.Show("您确认删除数据吗?", "提示", MessageButton.YesNo, MessageType.Information);
  777. if (result == MessageResult.Yes)
  778. pDataRegForm.RegGrid.DeleteFocusedRow(true);
  779. }
  780. #endregion
  781. #region IConfigure 成员
  782. public IUserControl GetFigureControl
  783. {
  784. get { return pDataRegForm as IUserControl; }
  785. }
  786. #endregion
  787. }
  788. }