Example #1
0
        public ActionResult Edit(ImportProfileModel model, bool continueEditing, FormCollection form)
        {
            if (!Services.Permissions.Authorize(StandardPermissionProvider.ManageImports))
            {
                return(AccessDeniedView());
            }

            var profile = _importProfileService.GetImportProfileById(model.Id);

            if (profile == null)
            {
                return(RedirectToAction("List"));
            }

            var map           = new ColumnMap();
            var hasErrors     = false;
            var resetMappings = false;

            try
            {
                var propertyKeyPrefix = "ColumnMapping.Property.";
                var allPropertyKeys   = form.AllKeys.Where(x => x.HasValue() && x.StartsWith(propertyKeyPrefix));

                if (allPropertyKeys.Any())
                {
                    var entityProperties = _importProfileService.GetImportableEntityProperties(profile.EntityType);

                    foreach (var key in allPropertyKeys)
                    {
                        var index        = key.Substring(propertyKeyPrefix.Length);
                        var property     = form[key];
                        var column       = form["ColumnMapping.Column." + index];
                        var defaultValue = form["ColumnMapping.Default." + index];

                        if (column.IsEmpty())
                        {
                            // tell mapper to explicitly ignore the property
                            map.AddMapping(property, null, property, "[IGNOREPROPERTY]");
                        }
                        else if (!column.IsCaseInsensitiveEqual(property) || defaultValue.HasValue())
                        {
                            if (defaultValue.HasValue() && GetDisabledDefaultFieldNames(profile).Contains(property))
                            {
                                defaultValue = null;
                            }

                            map.AddMapping(property, null, column, defaultValue);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                hasErrors = true;
                NotifyError(exception, true, false);
            }

            if (!ModelState.IsValid || hasErrors)
            {
                PrepareProfileModel(model, profile, true, map);
                return(View(model));
            }

            profile.Name          = model.Name;
            profile.EntityType    = model.EntityType;
            profile.Enabled       = model.Enabled;
            profile.Skip          = model.Skip ?? 0;
            profile.Take          = model.Take ?? 0;
            profile.UpdateOnly    = model.UpdateOnly;
            profile.KeyFieldNames = (model.KeyFieldNames == null ? null : string.Join(",", model.KeyFieldNames));

            try
            {
                if (profile.FileType == ImportFileType.CSV && model.CsvConfiguration != null)
                {
                    var csvConverter = new CsvConfigurationConverter();

                    var oldCsvConfig = csvConverter.ConvertFrom <CsvConfiguration>(profile.FileTypeConfiguration);
                    var oldDelimiter = (oldCsvConfig != null ? oldCsvConfig.Delimiter.ToString() : null);

                    // auto reset mappings cause they are invalid. note: delimiter can be whitespaced, so no oldDelimter.HasValue() etc.
                    resetMappings = (oldDelimiter != model.CsvConfiguration.Delimiter && profile.ColumnMapping.HasValue());

                    profile.FileTypeConfiguration = csvConverter.ConvertTo(model.CsvConfiguration.Clone());
                }
                else
                {
                    profile.FileTypeConfiguration = null;
                }

                if (resetMappings)
                {
                    profile.ColumnMapping = null;
                }
                else
                {
                    var mapConverter = new ColumnMapConverter();
                    profile.ColumnMapping = mapConverter.ConvertTo(map);
                }

                if (model.ExtraData != null)
                {
                    var extraData = new ImportExtraData
                    {
                        NumberOfPictures = model.ExtraData.NumberOfPictures
                    };

                    profile.ExtraData = XmlHelper.Serialize(extraData);
                }
            }
            catch (Exception exception)
            {
                hasErrors = true;
                NotifyError(exception, true, false);
            }

            if (!hasErrors)
            {
                _importProfileService.UpdateImportProfile(profile);

                NotifySuccess(T("Admin.Common.DataSuccessfullySaved"));

                if (resetMappings)
                {
                    NotifyWarning(T("Admin.DataExchange.ColumnMapping.Validate.MappingsReset"));
                }
            }

            return(continueEditing ? RedirectToAction("Edit", new { id = profile.Id }) : RedirectToAction("List"));
        }
        public ActionResult Edit(ImportProfileModel model, bool continueEditing, FormCollection form)
        {
            if (!Services.Permissions.Authorize(StandardPermissionProvider.ManageImports))
                return AccessDeniedView();

            var profile = _importProfileService.GetImportProfileById(model.Id);
            if (profile == null)
                return RedirectToAction("List");

            var map = new ColumnMap();
            var hasErrors = false;
            var resetMappings = false;

            try
            {
                var propertyKeyPrefix = "ColumnMapping.Property.";
                var allPropertyKeys = form.AllKeys.Where(x => x.HasValue() && x.StartsWith(propertyKeyPrefix));

                if (allPropertyKeys.Any())
                {
                    var entityProperties = _importProfileService.GetImportableEntityProperties(profile.EntityType);

                    foreach (var key in allPropertyKeys)
                    {
                        var index = key.Substring(propertyKeyPrefix.Length);
                        var property = form[key];
                        var column = form["ColumnMapping.Column." + index];
                        var defaultValue = form["ColumnMapping.Default." + index];

                        if (column.IsEmpty())
                        {
                            // tell mapper to explicitly ignore the property
                            map.AddMapping(property, null, property, "[IGNOREPROPERTY]");
                        }
                        else if (!column.IsCaseInsensitiveEqual(property) || defaultValue.HasValue())
                        {
                            if (defaultValue.HasValue() && GetDisabledDefaultFieldNames(profile).Contains(property))
                                defaultValue = null;

                            map.AddMapping(property, null, column, defaultValue);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                hasErrors = true;
                NotifyError(exception, true, false);
            }

            if (!ModelState.IsValid || hasErrors)
            {
                PrepareProfileModel(model, profile, true, map);
                return View(model);
            }

            profile.Name = model.Name;
            profile.EntityType = model.EntityType;
            profile.Enabled = model.Enabled;
            profile.Skip = model.Skip ?? 0;
            profile.Take = model.Take ?? 0;
            profile.UpdateOnly = model.UpdateOnly;
            profile.KeyFieldNames = (model.KeyFieldNames == null ? null : string.Join(",", model.KeyFieldNames));

            try
            {
                if (profile.FileType == ImportFileType.CSV && model.CsvConfiguration != null)
                {
                    var csvConverter = new CsvConfigurationConverter();

                    var oldCsvConfig = csvConverter.ConvertFrom<CsvConfiguration>(profile.FileTypeConfiguration);
                    var oldDelimiter = (oldCsvConfig != null ? oldCsvConfig.Delimiter.ToString() : null);

                    // auto reset mappings cause they are invalid. note: delimiter can be whitespaced, so no oldDelimter.HasValue() etc.
                    resetMappings = (oldDelimiter != model.CsvConfiguration.Delimiter && profile.ColumnMapping.HasValue());

                    profile.FileTypeConfiguration = csvConverter.ConvertTo(model.CsvConfiguration.Clone());
                }
                else
                {
                    profile.FileTypeConfiguration = null;
                }

                if (resetMappings)
                {
                    profile.ColumnMapping = null;
                }
                else
                {
                    var mapConverter = new ColumnMapConverter();
                    profile.ColumnMapping = mapConverter.ConvertTo(map);
                }

                if (model.ExtraData != null)
                {
                    var extraData = new ImportExtraData
                    {
                        NumberOfPictures = model.ExtraData.NumberOfPictures
                    };

                    profile.ExtraData = XmlHelper.Serialize(extraData);
                }
            }
            catch (Exception exception)
            {
                hasErrors = true;
                NotifyError(exception, true, false);
            }

            if (!hasErrors)
            {
                _importProfileService.UpdateImportProfile(profile);

                NotifySuccess(T("Admin.Common.DataSuccessfullySaved"));

                if (resetMappings)
                {
                    NotifyWarning(T("Admin.DataExchange.ColumnMapping.Validate.MappingsReset"));
                }
            }

            return (continueEditing ? RedirectToAction("Edit", new { id = profile.Id }) : RedirectToAction("List"));
        }