Example #1
0
        private void btnImport_Click(object sender, EventArgs e)
        {
            ImportedDataviews = new List <string>();
            using (new AutoCursor(this)) {
                var dvNames = new List <string>();
                var dsList  = new List <DataSet>();
                foreach (ListViewItem lvi in lvDataviews.Items)
                {
                    if (lvi.Checked)
                    {
                        dvNames.Add(lvi.Text);
                        var ds = lvi.Tag as DataSet;
                        if (!dsList.Contains(ds))
                        {
                            dsList.Add(ds);
                        }
                    }
                }

                foreach (var ds in dsList)
                {
                    AdminProxy.UpconvertDataviewForImport(ds);

                    // only save the sql for the selected engines, ignore others by setting sql_statement to empty string
                    foreach (DataRow dr in ds.Tables["sys_dataview_sql"].Rows)
                    {
                        var engine = dr["database_engine_tag"].ToString().Trim().ToLower();

                        switch (engine)
                        {
                        case "sqlserver":
                            if (!chkSQLServer.Checked)
                            {
                                dr["sql_statement"] = "";
                            }
                            break;

                        case "mysql":
                            if (!chkMySQL.Checked)
                            {
                                dr["sql_statement"] = "";
                            }
                            break;

                        case "postgresql":
                            if (!chkPostgreSQL.Checked)
                            {
                                dr["sql_statement"] = "";
                            }
                            break;

                        case "oracle":
                            if (!chkOracle.Checked)
                            {
                                dr["sql_statement"] = "";
                            }
                            break;
                        }
                    }
                    ds.AcceptChanges();
                    bool      errored   = false;
                    Exception exception = null;
                    btnImport.Enabled = false;

                    var importLanguage            = chkIncludeLanguage.Checked;
                    var useTableMappings          = chkUseAsTableMappings.Checked && chkUseAsTableMappings.Enabled;
                    var importFieldsAndProperties = chkFieldsAndParameters.Checked;
                    if (!importFieldsAndProperties)
                    {
                        importLanguage   = false;
                        useTableMappings = false;
                    }

                    var done = false;
                    processInBackground(
                        (worker) => {
                        try {
                            worker.ReportProgress(0, getDisplayMember("importing{start}", "Importing..."));
                            var dsImported = AdminProxy.ImportDataViewDefinitions(ds, dvNames, false, importFieldsAndProperties, importLanguage, useTableMappings);
                            foreach (DataRow drImp in dsImported.Tables["imported"].Rows)
                            {
                                ImportedDataviews.Add(drImp["dataview_name"].ToString());
                            }
                        } catch (Exception ex) {
                            exception = ex;
                            errored   = true;
                        }
                    },
                        (BackgroundWorker worker2, ProgressChangedEventArgs e2) => {
                        this.Cursor        = Cursors.WaitCursor;
                        statProgress.Value = e2.ProgressPercentage;
                        statLabel.Text     = e2.UserState + "";
                        Refresh();
                    },
                        (worker3, e3) => {
                        try {
                            this.Cursor       = Cursors.Default;
                            btnImport.Enabled = true;
                            if (!errored)
                            {
                                DialogResult = DialogResult.OK;
                            }
                            else
                            {
                                if (exception.Message.Contains("sys_table"))
                                {
                                    var res = MessageBox.Show(this, getDisplayMember("import{error_body}", "Error during import: {0}\n\nIgnoring this error will cause the field(s) with missing mappings to be read-only and you will need to fix the mappings manually.\n\nWould you like to ignore the missing mappings and continue?", exception.Message), getDisplayMember("import{error_title}", "Error Importing Dataview(s)"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                                    if (res == DialogResult.Yes)
                                    {
                                        try {
                                            var dsImported = AdminProxy.ImportDataViewDefinitions(ds, dvNames, true, importFieldsAndProperties, importLanguage, useTableMappings);
                                            foreach (DataRow drImp in dsImported.Tables["imported"].Rows)
                                            {
                                                ImportedDataviews.Add(drImp["dataview_name"].ToString());
                                            }
                                            DialogResult = DialogResult.OK;
                                        } catch (Exception ex2ndTry) {
                                            MessageBox.Show(this, getDisplayMember("import{error2_body}", "Error during import: {0}", ex2ndTry.Message), getDisplayMember("import{error2_title}", "Error Importing Dataview(s)"));
                                        }
                                    }
                                }
                            }
                        } finally {
                            done = true;
                            this.Close();
                        }
                    }
                        );

                    while (!done)
                    {
                        Thread.Sleep(50);
                        Application.DoEvents();
                    }
                }
            }
        }