Пример #1
0
        public EditInExcelUploadForm(Api api, EntitySchema entity, RecordSetChanges changes)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (changes == null)
            {
                throw new ArgumentNullException(nameof(changes));
            }

            _api     = api;
            _entity  = entity;
            _changes = changes;

            InitializeComponent();

            _newCheckBox.Text         = String.Format(_newCheckBox.Text, _changes.New.Count);
            _newCheckBox.Enabled      = entity.CanCreate;
            _modifiedCheckBox.Text    = String.Format(_modifiedCheckBox.Text, _changes.Modified.Count);
            _modifiedCheckBox.Enabled = entity.CanUpdate;
            _deletedCheckBox.Text     = String.Format(_deletedCheckBox.Text, _changes.Deleted.Count);
            _deletedCheckBox.Enabled  = entity.CanDelete;

            UpdateEnabled();
        }
Пример #2
0
        private void _editInExcel_Click(object sender, EventArgs e)
        {
            GetAllResults();

            using (var form = new EditInExcelInstructionsForm())
            {
                if (form.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
            }

            ApiRowErrorsCollection errors = null;

            var original = new RecordSet();
            var editing  = original;

            foreach (var resultSet in _resultSets)
            {
                original.AddResultSet(resultSet);
            }

            while (true)
            {
                string fileName;

                for (int i = 0; ; i++)
                {
                    fileName = "Wastedge Export";
                    if (i > 0)
                    {
                        fileName += $" ({i})";
                    }
                    fileName += ".xlsx";

                    fileName = Path.Combine(Path.GetTempPath(), fileName);

                    if (!File.Exists(fileName))
                    {
                        break;
                    }
                }

                try
                {
                    using (var stream = File.Create(fileName))
                    {
                        new ExcelExporter().Export(stream, _entity, editing, errors);
                    }

                    try
                    {
                        Process.Start(fileName);
                    }
                    catch
                    {
                        // Ignore exceptions.
                    }

                    using (var form = new EditInExcelWaitForm())
                    {
                        if (form.ShowDialog(this) != DialogResult.OK)
                        {
                            return;
                        }
                    }

                    RecordSetChanges changes;
                    RecordSet        modified;

                    while (true)
                    {
                        try
                        {
                            using (var stream = File.OpenRead(fileName))
                            {
                                modified = new ExcelImporter().Import(stream, _entity);
                            }

                            changes = RecordSetChanges.Create(original, modified);
                            break;
                        }
                        catch (IOException)
                        {
                            var result = TaskDialogEx.Show(
                                this,
                                "The Excel file cannot be opened. Please close the Excel file before uploading your changes",
                                Text,
                                TaskDialogCommonButtons.OK | TaskDialogCommonButtons.Cancel,
                                TaskDialogIcon.Error
                                );
                            if (result == DialogResult.Cancel)
                            {
                                return;
                            }
                        }
                    }

                    using (var form = new EditInExcelUploadForm(_api, _entity, changes))
                    {
                        form.ShowDialog(this);

                        errors = form.Errors;
                    }

                    if (errors != null)
                    {
                        using (var form = new ValidationErrorsForm(errors))
                        {
                            if (form.ShowDialog(this) != DialogResult.OK)
                            {
                                return;
                            }

                            editing = modified;
                        }
                    }
                    else
                    {
                        ReloadResults();

                        return;
                    }
                }
                finally
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch
                    {
                        // Ignore.
                    }
                }
            }
        }