using System; using FastReport.Cloud.FastReport.Models.DataSource; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using System.Windows.Forms; using FastReport.Auth; using FastReport.Cloud.FastReport; using FastReport.Cloud.FastReport.ListViewCloud; using FastReport.Data; using FastReport.Data.JsonConnection; using FastReport.Utils; using XmlDocument = FastReport.Utils.XmlDocument; namespace FastReport.Forms { internal partial class CloudDataSourceForm : BaseDialogForm { internal string dataSourceId = null; internal DataConnectionBase dataConnection; private readonly MyRes localizator = new MyRes("Forms,CloudDataSourceForm"); private readonly MyRes localizatorError = new MyRes("Forms,ListViewForm"); private readonly string STATUS_CONNECTED; private readonly string STATUS_ERROR; private readonly string STATUS_UPDATING; private DataSourceVM dataSource; internal List tableStructure; public CloudDataSourceForm() { STATUS_CONNECTED = localizator.Get("StatusConnected"); STATUS_ERROR = localizator.Get("StatusError"); STATUS_UPDATING = localizator.Get("StatusUpdating"); InitializeComponent(); Localize(); UpdateDpiDependencies(); } public override void UpdateDpiDependencies() { base.UpdateDpiDependencies(); DSName.Width = DSConnectionType.Width = DSStatus.Width = listView1.Width / 3 - SystemInformation.VerticalScrollBarWidth; } private void FillListView() { DataSourcesVM dataSourcesCollection = DataSourceManager.GetAvailableDataSourcesCommand( new DataSourcesOptions() { SubscriptionId = ProviderManager.Subscription.Id, Skip = 0, Take = 50 }); if (dataSourcesCollection.Count == 0) { this.NoConnectionsLabel.Visible = true; this.listView1.Visible = false; } else { this.NoConnectionsLabel.Visible = false; this.listView1.Visible = true; foreach (var dataSource in dataSourcesCollection.DataSources) { var item = new ListViewItem(); item.Text = dataSource.Name; item.Tag = dataSource.Id; string status = ""; switch (dataSource.Status) { case DataSourceStatus.Connected: status = STATUS_CONNECTED; break; case DataSourceStatus.Error: status = STATUS_ERROR; break; case DataSourceStatus.Updating: status = STATUS_UPDATING; break; } item.SubItems.AddRange(new string[] { dataSource.ConnectionType.ToString(), status }); listView1.Items.Add(item); } } } internal bool SetProvider() { try { ProviderManager.SetProvider(new FRCloudProvider(AuthService.Instance), FilesModeItem.FilesMode); return true; } catch (WebException) { MessageBox.Show(localizatorError.Get("ErrorMessageBox,ErrorInApiOrServer"), localizatorError.Get("ErrorMessageBox,Caption"), MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); return false; } } private void FillSubscriptions() { foreach (var subscriptionVm in ProviderManager.Subscriptions) { subscriptionsCmbx.Items.Add(subscriptionVm.Name); } int i = 0; foreach (var provider in ProviderManager.Subscriptions) { if (provider == ProviderManager.Subscription) { subscriptionsCmbx.SelectedIndex = i; break; } i++; } } internal void OnLoad() { FillSubscriptions(); } private void ListView1OnMouseClick(object sender, MouseEventArgs e) { if (listView1.SelectedItems.Count > 0) { var item = listView1.SelectedItems[0]; dataSourceId = item.Tag.ToString(); this.btnOk.Enabled = true; } } private void CloudDataSourceForm_FormClosed(object sender, FormClosedEventArgs e) { if (DialogResult == DialogResult.OK) { dataSource = DataSourceManager.GetDataSourceCommand(dataSourceId); tableStructure = GetTablesStructure(); switch (dataSource.ConnectionType) { case DataSourceConnectionType.MSSQL: #if AVALONIA dataConnection = GetInstance("FastReport.Data.MsSqlDataConnection"); #else dataConnection = new MsSqlDataConnection(); #endif break; case DataSourceConnectionType.CSV: dataConnection = new CsvDataConnection(); break; case DataSourceConnectionType.XML: dataConnection = new XmlDataConnection(); break; case DataSourceConnectionType.JSON: dataConnection = new JsonDataSourceConnection(); break; case DataSourceConnectionType.FirebirdDB: dataConnection = GetInstance("FastReport.Data.FirebirdDataConnection"); break; case DataSourceConnectionType.MongoDB: dataConnection = GetInstance("FastReport.Data.MongoDBDataConnection"); break; case DataSourceConnectionType.MySQL: dataConnection = GetInstance("FastReport.Data.MySqlDataConnection"); break; case DataSourceConnectionType.OracleDB: dataConnection = GetInstance("FastReport.Data.OracleDataConnection"); break; case DataSourceConnectionType.Postgres: dataConnection = GetInstance("FastReport.Data.PostgresDataConnection"); break; } if (dataConnection == null) { MessageBox.Show(localizator.Get("PluginNotAdded"), localizator.Get("StatusError")); return; } dataConnection.ConnectionString = dataSource.ConnectionString; dataConnection.Name = dataSource.Name; dataConnection.CloudId = dataSourceId; } } private List GetTablesStructure() { if (dataSource.DataStructure == null) return null; // TODO: xml ReadHeader MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("\n" + dataSource.DataStructure)); XmlDocument document = new XmlDocument(); document.Load(ms); var items = document.Root.Items; return items; } private static DataConnectionBase GetInstance(string strFullyQualifiedName) { Type type = Type.GetType(strFullyQualifiedName); if (type != null) return (DataConnectionBase)Activator.CreateInstance(type); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var asm in assemblies) { type = asm.GetType(strFullyQualifiedName); if (type != null) return (DataConnectionBase)Activator.CreateInstance(type); } return null; } public override void Localize() { base.Localize(); this.NoConnectionsLabel.Text = localizator.Get("NotFoundSources"); this.SelectSourcesLabel.Text = localizator.Get("SelectSources"); this.Text = localizator.Get("Caption"); this.DSConnectionType.Text = localizator.Get("TypeConnection"); this.DSStatus.Text = localizator.Get("StatusConnection"); this.DSName.Text = localizator.Get("NameConnection"); } private void SubscriptionsCmbxOnSelectedValueChanged(object sender, EventArgs e) { var backupSubscription = ProviderManager.Subscription; var cmbx = sender as ComboBox; foreach (var subscriptionVm in ProviderManager.Subscriptions) { if (subscriptionVm.Name == cmbx.Text) { ProviderManager.Subscription = subscriptionVm; break; } } this.listView1.Items.Clear(); dataSourceId = null; this.btnOk.Enabled = false; try { FillListView(); } catch (WebException ex) { if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.PaymentRequired) { MessageBox.Show(Res.Get("Forms,ListViewForm,ErrorMessageBox,ErrorInSubscriptions402"), Res.Get("Designer,ListViewForm,ErrorMessageBox,CaptionSubscriptions402"), MessageBoxButtons.OK, MessageBoxIcon.Error); ProviderManager.Subscription = backupSubscription; subscriptionsCmbx.Text = backupSubscription.Name; FillListView(); } } } } }