CloudDataSourceForm.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using FastReport.Cloud.FastReport.Models.DataSource;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using FastReport.Auth;
  9. using FastReport.Cloud.FastReport;
  10. using FastReport.Cloud.FastReport.ListViewCloud;
  11. using FastReport.Data;
  12. using FastReport.Data.JsonConnection;
  13. using FastReport.Utils;
  14. using XmlDocument = FastReport.Utils.XmlDocument;
  15. namespace FastReport.Forms
  16. {
  17. internal partial class CloudDataSourceForm : BaseDialogForm
  18. {
  19. internal string dataSourceId = null;
  20. internal DataConnectionBase dataConnection;
  21. private readonly MyRes localizator = new MyRes("Forms,CloudDataSourceForm");
  22. private readonly MyRes localizatorError = new MyRes("Forms,ListViewForm");
  23. private readonly string STATUS_CONNECTED;
  24. private readonly string STATUS_ERROR;
  25. private readonly string STATUS_UPDATING;
  26. private DataSourceVM dataSource;
  27. internal List<XmlItem> tableStructure;
  28. public CloudDataSourceForm()
  29. {
  30. STATUS_CONNECTED = localizator.Get("StatusConnected");
  31. STATUS_ERROR = localizator.Get("StatusError");
  32. STATUS_UPDATING = localizator.Get("StatusUpdating");
  33. InitializeComponent();
  34. Localize();
  35. UpdateDpiDependencies();
  36. }
  37. public override void UpdateDpiDependencies()
  38. {
  39. base.UpdateDpiDependencies();
  40. DSName.Width = DSConnectionType.Width = DSStatus.Width = listView1.Width / 3 - SystemInformation.VerticalScrollBarWidth;
  41. }
  42. private void FillListView()
  43. {
  44. DataSourcesVM dataSourcesCollection = DataSourceManager.GetAvailableDataSourcesCommand(
  45. new DataSourcesOptions()
  46. { SubscriptionId = ProviderManager.Subscription.Id, Skip = 0, Take = 50 });
  47. if (dataSourcesCollection.Count == 0)
  48. {
  49. this.NoConnectionsLabel.Visible = true;
  50. this.listView1.Visible = false;
  51. }
  52. else
  53. {
  54. this.NoConnectionsLabel.Visible = false;
  55. this.listView1.Visible = true;
  56. foreach (var dataSource in dataSourcesCollection.DataSources)
  57. {
  58. var item = new ListViewItem();
  59. item.Text = dataSource.Name;
  60. item.Tag = dataSource.Id;
  61. string status = "";
  62. switch (dataSource.Status)
  63. {
  64. case DataSourceStatus.Connected:
  65. status = STATUS_CONNECTED;
  66. break;
  67. case DataSourceStatus.Error:
  68. status = STATUS_ERROR;
  69. break;
  70. case DataSourceStatus.Updating:
  71. status = STATUS_UPDATING;
  72. break;
  73. }
  74. item.SubItems.AddRange(new string[]
  75. {
  76. dataSource.ConnectionType.ToString(), status
  77. });
  78. listView1.Items.Add(item);
  79. }
  80. }
  81. }
  82. internal bool SetProvider()
  83. {
  84. try
  85. {
  86. ProviderManager.SetProvider(new FRCloudProvider(AuthService.Instance), FilesModeItem.FilesMode);
  87. return true;
  88. }
  89. catch (WebException)
  90. {
  91. MessageBox.Show(localizatorError.Get("ErrorMessageBox,ErrorInApiOrServer"), localizatorError.Get("ErrorMessageBox,Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  92. this.Close();
  93. return false;
  94. }
  95. }
  96. private void FillSubscriptions()
  97. {
  98. foreach (var subscriptionVm in ProviderManager.Subscriptions)
  99. {
  100. subscriptionsCmbx.Items.Add(subscriptionVm.Name);
  101. }
  102. int i = 0;
  103. foreach (var provider in ProviderManager.Subscriptions)
  104. {
  105. if (provider == ProviderManager.Subscription)
  106. {
  107. subscriptionsCmbx.SelectedIndex = i;
  108. break;
  109. }
  110. i++;
  111. }
  112. }
  113. internal void OnLoad()
  114. {
  115. FillSubscriptions();
  116. }
  117. private void ListView1OnMouseClick(object sender, MouseEventArgs e)
  118. {
  119. if (listView1.SelectedItems.Count > 0)
  120. {
  121. var item = listView1.SelectedItems[0];
  122. dataSourceId = item.Tag.ToString();
  123. this.btnOk.Enabled = true;
  124. }
  125. }
  126. private void CloudDataSourceForm_FormClosed(object sender, FormClosedEventArgs e)
  127. {
  128. if (DialogResult == DialogResult.OK)
  129. {
  130. dataSource = DataSourceManager.GetDataSourceCommand(dataSourceId);
  131. tableStructure = GetTablesStructure();
  132. switch (dataSource.ConnectionType)
  133. {
  134. case DataSourceConnectionType.MSSQL:
  135. #if AVALONIA
  136. dataConnection = GetInstance("FastReport.Data.MsSqlDataConnection");
  137. #else
  138. dataConnection = new MsSqlDataConnection();
  139. #endif
  140. break;
  141. case DataSourceConnectionType.CSV:
  142. dataConnection = new CsvDataConnection();
  143. break;
  144. case DataSourceConnectionType.XML:
  145. dataConnection = new XmlDataConnection();
  146. break;
  147. case DataSourceConnectionType.JSON:
  148. dataConnection = new JsonDataSourceConnection();
  149. break;
  150. case DataSourceConnectionType.FirebirdDB:
  151. dataConnection = GetInstance("FastReport.Data.FirebirdDataConnection");
  152. break;
  153. case DataSourceConnectionType.MongoDB:
  154. dataConnection = GetInstance("FastReport.Data.MongoDBDataConnection");
  155. break;
  156. case DataSourceConnectionType.MySQL:
  157. dataConnection = GetInstance("FastReport.Data.MySqlDataConnection");
  158. break;
  159. case DataSourceConnectionType.OracleDB:
  160. dataConnection = GetInstance("FastReport.Data.OracleDataConnection");
  161. break;
  162. case DataSourceConnectionType.Postgres:
  163. dataConnection = GetInstance("FastReport.Data.PostgresDataConnection");
  164. break;
  165. }
  166. if (dataConnection == null)
  167. {
  168. MessageBox.Show(localizator.Get("PluginNotAdded"), localizator.Get("StatusError"));
  169. return;
  170. }
  171. dataConnection.ConnectionString = dataSource.ConnectionString;
  172. dataConnection.Name = dataSource.Name;
  173. dataConnection.CloudId = dataSourceId;
  174. }
  175. }
  176. private List<XmlItem> GetTablesStructure()
  177. {
  178. if (dataSource.DataStructure == null)
  179. return null;
  180. // TODO: xml ReadHeader
  181. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + dataSource.DataStructure));
  182. XmlDocument document = new XmlDocument();
  183. document.Load(ms);
  184. var items = document.Root.Items;
  185. return items;
  186. }
  187. private static DataConnectionBase GetInstance(string strFullyQualifiedName)
  188. {
  189. Type type = Type.GetType(strFullyQualifiedName);
  190. if (type != null)
  191. return (DataConnectionBase)Activator.CreateInstance(type);
  192. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  193. foreach (var asm in assemblies)
  194. {
  195. type = asm.GetType(strFullyQualifiedName);
  196. if (type != null)
  197. return (DataConnectionBase)Activator.CreateInstance(type);
  198. }
  199. return null;
  200. }
  201. public override void Localize()
  202. {
  203. base.Localize();
  204. this.NoConnectionsLabel.Text = localizator.Get("NotFoundSources");
  205. this.SelectSourcesLabel.Text = localizator.Get("SelectSources");
  206. this.Text = localizator.Get("Caption");
  207. this.DSConnectionType.Text = localizator.Get("TypeConnection");
  208. this.DSStatus.Text = localizator.Get("StatusConnection");
  209. this.DSName.Text = localizator.Get("NameConnection");
  210. }
  211. private void SubscriptionsCmbxOnSelectedValueChanged(object sender, EventArgs e)
  212. {
  213. var backupSubscription = ProviderManager.Subscription;
  214. var cmbx = sender as ComboBox;
  215. foreach (var subscriptionVm in ProviderManager.Subscriptions)
  216. {
  217. if (subscriptionVm.Name == cmbx.Text)
  218. {
  219. ProviderManager.Subscription = subscriptionVm;
  220. break;
  221. }
  222. }
  223. this.listView1.Items.Clear();
  224. dataSourceId = null;
  225. this.btnOk.Enabled = false;
  226. try
  227. {
  228. FillListView();
  229. }
  230. catch (WebException ex)
  231. {
  232. if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.PaymentRequired)
  233. {
  234. MessageBox.Show(Res.Get("Forms,ListViewForm,ErrorMessageBox,ErrorInSubscriptions402"), Res.Get("Designer,ListViewForm,ErrorMessageBox,CaptionSubscriptions402"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  235. ProviderManager.Subscription = backupSubscription;
  236. subscriptionsCmbx.Text = backupSubscription.Name;
  237. FillListView();
  238. }
  239. }
  240. }
  241. }
  242. }