Beispiel #1
0
        private void Import(string parameters)
        {
            if (!CommandHandlersExtensions.ImportExportParametersSpliter(parameters, out var fileFormat, out var path, "import"))
            {
                return;
            }

            try
            {
                using (StreamReader stream = new StreamReader(path))
                {
                    if (fileFormat == "csv")
                    {
                        this.snapshot = this.CabinetService.MakeSnapshot();
                        this.snapshot.LoadFromCsv(stream, RecordValidator, Converter, this.modelWriter);
                        int count = this.CabinetService.Restore(this.snapshot);
                        this.modelWriter.LineWriter.Invoke($"{count} records were imported from {path}");
                    }
                    else if (fileFormat == "xml")
                    {
                        this.snapshot = this.CabinetService.MakeSnapshot();
                        this.xmlValidator.ValidateXml(this.xsdValidatorFile, path);
                        this.snapshot.LoadFromXml(stream, RecordValidator, this.modelWriter);
                        int count = this.CabinetService.Restore(this.snapshot);
                        this.modelWriter.LineWriter.Invoke($"{count} records were imported from {path}");
                    }
                    else
                    {
                        this.modelWriter.LineWriter.Invoke($"{fileFormat} writer is not found");
                    }
                }
            }
            catch (IOException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("File wasn't imported");
            }
            catch (UnauthorizedAccessException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("File wasn't imported");
            }
            catch (ArgumentException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("File wasn't imported");
            }
            catch (XmlException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("File wasn't imported");
            }
        }
Beispiel #2
0
        private void Export(string parameters)
        {
            if (!CommandHandlersExtensions.ImportExportParametersSpliter(parameters, out var fileFormat, out var path, "export"))
            {
                return;
            }

            try
            {
                using (StreamWriter stream = new StreamWriter(path))
                {
                    if (fileFormat == "csv")
                    {
                        this.snapshot = this.CabinetService.MakeSnapshot();
                        this.snapshot.SaveToCsv(stream);
                    }
                    else if (fileFormat == "xml")
                    {
                        this.snapshot = this.CabinetService.MakeSnapshot();
                        this.snapshot.SaveToXml(stream);
                    }
                    else
                    {
                        this.modelWriter.LineWriter.Invoke($"{fileFormat} writer is not found");
                        return;
                    }
                }

                this.modelWriter.LineWriter.Invoke($"File {path} was successfully exported");
            }
            catch (IOException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
            catch (UnauthorizedAccessException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
        }