// select id, firstname, lastname where firstname = 'John' and lastname = 'Doe'
        private void Select(string parameters)
        {
            if (string.IsNullOrEmpty(parameters))
            {
                this.printer.Print(this.CabinetService.GetRecords(), FieldsCaseDictionary.Values.ToArray());
                return;
            }

            char[]   separators    = { '=', ',', ' ' };
            string[] inputs        = parameters.Split("where", StringSplitOptions.RemoveEmptyEntries);
            string[] printedFields = inputs[0].Split(separators, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                CheckUpdateFieldsInput(printedFields);
                this.CommandHandlersExtensions.ChangeFieldCase(printedFields);
                if (inputs.Length == 1)
                {
                    this.printer.Print(this.CabinetService.GetRecords(), printedFields);
                    return;
                }

                var conditionFields = inputs[1].Split(separators, StringSplitOptions.RemoveEmptyEntries);

                (bool founded, IEnumerable <FileCabinetRecord> record) = SearchDataInCache(conditionFields);
                if (founded)
                {
                    this.printer.Print(record, printedFields);
                    return;
                }

                CheckConditionFieldsInput(conditionFields);
                string conditionSeparator = CommandHandlersExtensions.FindConditionSeparator(conditionFields);
                Dictionary <string, string> conditions =
                    this.CommandHandlersExtensions.CreateDictionaryOfFields(conditionFields, conditionSeparator);

                // finds records that satisfy the condition
                var records = this.expressionExtensions.FindSuitableRecords(
                    conditions.Values.ToArray(), conditions.Keys.ToArray(), conditionSeparator, typeof(FileCabinetRecord)).ToArray();

                this.printer.Print(records, printedFields);
                PutDataInCache(conditionFields, records);
            }
            catch (FileRecordNotFoundException ex)
            {
                this.modelWriter.LineWriter.Invoke($"{ex.Value} was not found");
            }
            catch (FormatException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
            catch (ArgumentNullException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
            catch (ArgumentException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
        }
Exemple #2
0
        private void Create(string parameters)
        {
            char[]   separators = { '(', ')', ',', ' ' };
            string[] inputs     = parameters.Split("values", StringSplitOptions.RemoveEmptyEntries);

            try
            {
                string[] fields = inputs[0].Split(separators, StringSplitOptions.RemoveEmptyEntries);
                string[] values = inputs[1].Split(separators, StringSplitOptions.RemoveEmptyEntries);

                CompareFieldsAndInputArraysLength(fields, values);
                this.CommandHandlersExtensions.ChangeFieldCase(fields);
                CommandHandlersExtensions.DeleteQuotesFromInputValues(values);

                var recordNumber = this.CabinetService.CreateRecord(this.InputValidator.PrintInputFields(fields, values));
                this.modelWriter.LineWriter.Invoke($"Record #{recordNumber} is created.");
            }
            catch (ArgumentNullException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("Record is not created ");
            }
            catch (ArgumentException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("Record is not created ");
            }
            catch (IndexOutOfRangeException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
                this.modelWriter.LineWriter.Invoke("Record is not created ");
            }
        }
Exemple #3
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");
            }
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        private void Update(string parameters)
        {
            if (string.IsNullOrEmpty(parameters))
            {
                this.modelWriter.LineWriter.Invoke("Write information");
                return;
            }

            char[]   separators      = { '=', ',', ' ' };
            string[] inputs          = parameters.Split("where", StringSplitOptions.RemoveEmptyEntries);
            string[] updatedFields   = inputs[0].Split(separators, StringSplitOptions.RemoveEmptyEntries);
            string[] conditionFields = inputs[1].Split(separators, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                CheckConditionFieldsInput(conditionFields);
                CheckUpdateFieldsInput(updatedFields);

                // finds separator (or/and)
                string conditionSeparator           = CommandHandlersExtensions.FindConditionSeparator(conditionFields);
                Dictionary <string, string> updates =
                    this.CommandHandlersExtensions.CreateDictionaryOfFields(updatedFields, "set");
                Dictionary <string, string> conditions =
                    this.CommandHandlersExtensions.CreateDictionaryOfFields(conditionFields, conditionSeparator);

                List <int> recordsId = new List <int>();

                // finds records that satisfy the condition
                var records = this.expressionExtensions.FindSuitableRecords(
                    conditions.Values.ToArray(), conditions.Keys.ToArray(), conditionSeparator, typeof(FileCabinetRecord)).ToArray();

                for (int i = 0; i < records.Length; i++)
                {
                    FileCabinetRecord record = this.InputValidator.CheckInputFields(updates.Keys.ToArray(), updates.Values.ToArray(), records[i]);
                    int id = records[i].Id;
                    if (updates.ContainsKey("Id"))
                    {
                        string newId = updates["Id"];
                        CheckUpdateId(id.ToString(Culture), newId);
                    }

                    record.Id = id;

                    this.CabinetService.EditRecord(record);
                    recordsId.Add(id);
                }

                this.modelWriter.LineWriter.Invoke(CreateOutputText(recordsId.ToArray()));
            }
            catch (FileRecordNotFoundException ex)
            {
                this.modelWriter.LineWriter.Invoke($"{ex.Value} was not found");
            }
            catch (FormatException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
            catch (ArgumentNullException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
            catch (ArgumentException ex)
            {
                this.modelWriter.LineWriter.Invoke(ex.Message);
            }
        }