Beispiel #1
0
        private void Cancel_OnClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();

            EditorDialog.Info("CSV Import Cancelled", "No data imported.");
        }
Beispiel #2
0
        /// <summary>
        ///   Imports CSV data with the current settings into the current project.
        /// </summary>
        private void ImportData()
        {
            // Create a blueprint for each CSV row.
            var blueprintManagerViewModel = this.context.BlueprintManagerViewModel;
            var processedBlueprints       = new HashSet <string>();
            var errors = new List <Exception>();

            var newBlueprints     = 0;
            var updatedBlueprints = 0;
            var skippedBlueprints = 0;

            while (this.csvReader.CurrentRecord != null)
            {
                try
                {
                    // Get id of the blueprint to create or update.
                    var blueprintId = this.csvReader[this.BlueprintIdColumn];

                    // Skip ignored records, such as notes.
                    if (blueprintId == this.IgnoredBlueprintId)
                    {
                        this.csvReader.Read();
                        continue;
                    }

                    // Check for duplicate blueprints in the CSV file.
                    if (processedBlueprints.Contains(blueprintId))
                    {
                        throw new InvalidOperationException(string.Format("Duplicate blueprint id: {0}", blueprintId));
                    }

                    processedBlueprints.Add(blueprintId);

                    // Check whether blueprint already exists.
                    var dataBlueprint =
                        blueprintManagerViewModel.Blueprints.FirstOrDefault(
                            blueprint => blueprint.BlueprintId == blueprintId);
                    var newBlueprint = dataBlueprint == null;

                    if (newBlueprint)
                    {
                        // Create new blueprint.
                        blueprintManagerViewModel.NewBlueprintId = blueprintId;
                        dataBlueprint = blueprintManagerViewModel.CreateNewBlueprint();

                        // Reparent new blueprint.
                        blueprintManagerViewModel.ReparentBlueprint(
                            dataBlueprint.BlueprintId, this.BlueprintParent.BlueprintId, false);
                    }
                    else
                    {
                        // Check parent of existing blueprint.
                        if (dataBlueprint.Parent != this.BlueprintParent)
                        {
                            throw new InvalidOperationException(
                                      string.Format(
                                          "Blueprint {0} is child of {1} but should be child of {2}.",
                                          dataBlueprint.BlueprintId,
                                          dataBlueprint.Parent.BlueprintId,
                                          this.BlueprintParent.BlueprintId));
                        }
                    }

                    // Map attribute table keys to CSV values.
                    foreach (var valueMapping in
                             this.ValueMappings.Where(mapping => !string.IsNullOrWhiteSpace(mapping.MappingTarget)))
                    {
                        var rawValue = this.csvReader[valueMapping.MappingTarget];

                        // Skip empty entries.
                        if (string.IsNullOrWhiteSpace(rawValue))
                        {
                            if (!newBlueprint)
                            {
                                // Reset entry to parent value.
                                dataBlueprint.Blueprint.AttributeTable.RemoveValue(valueMapping.MappingSource);
                            }

                            continue;
                        }

                        object convertedValue;

                        if (!valueMapping.InspectorProperty.TryConvertStringToListOrValue(rawValue, out convertedValue) ||
                            convertedValue == null)
                        {
                            throw new CsvParserException(
                                      string.Format(
                                          "{0}: Unable to convert '{1}' to '{2}' ({3}).",
                                          blueprintId,
                                          rawValue,
                                          valueMapping.MappingSource,
                                          valueMapping.InspectorProperty.PropertyType));
                        }

                        // Check for localized values.
                        var stringProperty = valueMapping.InspectorProperty as InspectorStringAttribute;

                        if (stringProperty != null && stringProperty.Localized)
                        {
                            this.context.LocalizationContext.SetLocalizedStringForBlueprint(
                                blueprintId, valueMapping.MappingSource, (string)convertedValue);
                        }
                        else
                        {
                            dataBlueprint.Blueprint.AttributeTable[valueMapping.MappingSource] = convertedValue;
                        }
                    }

                    // Increase counter.
                    if (newBlueprint)
                    {
                        newBlueprints++;
                    }
                    else
                    {
                        updatedBlueprints++;
                    }
                }
                catch (Exception exception)
                {
                    errors.Add(exception);
                    skippedBlueprints++;
                }

                // Read next record.
                this.csvReader.Read();
            }

            // Show import results.
            if (errors.Count > 0)
            {
                EditorDialog.Warning("Some data could not be imported", errors);
            }

            var importInfoBuilder = new StringBuilder();

            importInfoBuilder.AppendLine(string.Format("{0} blueprint(s) imported.", newBlueprints));
            importInfoBuilder.AppendLine(string.Format("{0} blueprint(s) updated.", updatedBlueprints));
            importInfoBuilder.AppendLine(string.Format("{0} blueprint(s) skipped.", skippedBlueprints));
            var importInfo = importInfoBuilder.ToString();

            EditorDialog.Info("CSV Import Complete", importInfo);

            if (this.SaveSettingsForFutureImports)
            {
                this.SaveImportSettings();
            }

            blueprintManagerViewModel.RefreshBlueprintView();
        }