private void CheckAndAdd(ref ImportExportDictionaryItems entries, ImportExportDictionaryItem entry) { if (entries[entry.Key] == null) { entries.Items.Add(entry); } }
public JsonResult ImportDictionaries(HttpPostedFileBase file, bool overrideExisting = false) { var entries = new ImportExportDictionaryItems(); List <ImportResult> results = new List <ImportResult>(); if (file?.ContentLength > 0) { const int RowStart = 2; // ignore headers ImportDigest excelDigest = null; var import = new List <ExcelCell>(); bool importSuccessful = false; excelDigest = new ImportDigest(file.InputStream); import = excelDigest.Import(); importSuccessful = excelDigest.ErrorList.Errors.Count == 0; //var languages = localizationService.GetAllLanguages(); if (importSuccessful) { for (int i = RowStart; i <= excelDigest.RowsImported; i++) { var current = new ImportExportDictionaryItem { ParentKey = import.Find(x => x.CellName == $"A{i}").CellValue?.ToString(), Key = import.Find(x => x.CellName == $"B{i}").CellValue?.ToString(), Value = import.Find(x => x.CellName == $"C{i}").CellValue != null?import.Find(x => x.CellName == $"C{i}").CellValue .ToString() .Trim() .Replace("\n", string.Empty) .Replace("\t", string.Empty) : string.Empty, LanguageCode = import.Find(x => x.CellName == $"D{i}").CellValue?.ToString() }; var previous = entries.Items.Count > 0 ? entries[current.Key] : null; if (previous != null) { if (current.ParentKey.Equals(previous.Key) || current.ParentKey.Equals(previous.ParentKey)) { if (current.Key.Equals(previous.Key)) { if (current.LanguageCode.Equals(previous.LanguageCode)) { if (!string.IsNullOrEmpty(current.Value) && !string.IsNullOrEmpty(previous.Value)) { if (!current.Value.Equals(previous.Value)) { previous.Value = current.Value; } } else if (!string.IsNullOrEmpty(current.Value) && string.IsNullOrEmpty(previous.Value)) { previous.Value = current.Value; } else if (string.IsNullOrEmpty(current.Value) && string.IsNullOrEmpty(previous.Value)) { previous.Value = string.Empty; } entries[current.Key] = current; } else { previous.Translations = previous.Translations ?? new Dictionary <string, string>(); var translation = previous.Translations.SingleOrDefault(x => x.Key.Equals(current.LanguageCode)); if (translation.Equals(default(KeyValuePair <string, string>))) { previous.Translations.Add(current.LanguageCode, current.Value); } else if (string.IsNullOrEmpty(translation.Value)) { previous.Translations[current.LanguageCode] = current.Value ?? string.Empty; } entries[current.Key] = previous; } } else { CheckAndAdd(ref entries, current); } } else { CheckAndAdd(ref entries, current); } } else { CheckAndAdd(ref entries, current); } } } results = languageDictionaryService.ImportDictionaryItems(entries.Items, overrideExisting); } return(base.Json(new { Success = results.Count > 0, count = results.Count, results }, JsonRequestBehavior.DenyGet)); }