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 = _importService.GetImportProfileById(model.Id); if (profile == null) { return(RedirectToAction("List")); } var multipleMapped = new List <string>(); 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 = _importService.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]; var result = true; // ignored properties: column is empty means swap column and property (otherwise mapping impossible) if (column.IsEmpty()) { result = map.AddMapping(property, null, null); } else { result = map.AddMapping(column, null, property, defaultValue); } if (!result) { // add warning for ignored, multiple mapped properties multipleMapped.Add("{0} ({1})".FormatInvariant(entityProperties.ContainsKey(property) ? entityProperties[property] : "".NaIfEmpty(), property)); } } } } 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; profile.Take = model.Take; 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); } } catch (Exception exception) { hasErrors = true; NotifyError(exception, true, false); } if (!hasErrors) { _importService.UpdateImportProfile(profile); NotifySuccess(T("Admin.Common.DataSuccessfullySaved")); if (resetMappings) { NotifyWarning(T("Admin.DataExchange.ColumnMapping.Validate.MappingsReset")); } else if (multipleMapped.Any()) { NotifyWarning(T("Admin.DataExchange.ColumnMapping.Validate.MultipleMappedIgnored", "<p>" + string.Join("<br />", multipleMapped) + "</p>")); } } return(continueEditing ? RedirectToAction("Edit", new { id = profile.Id }) : RedirectToAction("List")); }