public virtual ActionResult IndexImport(FormCollection collection) { char CSV_SEPARATOR = ';'; string featureTrueIndicator = Worki.Resources.Views.Shared.SharedString.Yes; // by default, it's false. It's true only for the string int nbCol = 5; bool isHeaderLine = false; if (collection.Get("importCsvHeader") != null && collection.Get("importCsvHeader") == "on") isHeaderLine = true; int nbLocalisationsAdded = 0; string listLocalisationsAlreadyInDB = ""; var context = ModelFactory.GetUnitOfWork(); var mRepo = ModelFactory.GetRepository<IMemberRepository>(context); var lRepo = ModelFactory.GetRepository<ILocalisationRepository>(context); foreach (string name in Request.Files) { try { var postedFile = Request.Files[name]; if (postedFile == null || string.IsNullOrEmpty(postedFile.FileName)) continue; int fileLen; fileLen = postedFile.ContentLength; byte[] input = new byte[fileLen]; StreamReader sr = new StreamReader(postedFile.InputStream, System.Text.Encoding.Default); // Using of encoding to don't loose french accents // Because we can have a csv line in multiple line in files (because in a value, we can have line-return), we have to check it string fullCSVLine = ""; while (sr.Peek() >= 0) // Read of each line of CSV file { fullCSVLine += sr.ReadLine(); string[] infosLocalisation = fullCSVLine.Split(CSV_SEPARATOR); // We have not the full CSV line if (infosLocalisation.Length < nbCol) continue; if (!isHeaderLine) { var locName = infosLocalisation[1]; var enDesc = infosLocalisation[3]; var loc = lRepo.Get(l => string.Compare(l.Name, locName, StringComparison.InvariantCultureIgnoreCase) == 0); if (loc ==null) { listLocalisationsAlreadyInDB += "• " + locName + "<br />"; } else { loc.DescriptionEn = enDesc; nbLocalisationsAdded++; } } else isHeaderLine = false; // because we have skipped the first line fullCSVLine = ""; // Reinitialization because we have found the full CVS line } context.Commit(); } catch (Exception ex) { _Logger.Error("Edit", ex); ModelState.AddModelError("", string.Format(Worki.Resources.Views.Shared.SharedString.AddLocError, nbLocalisationsAdded, ex.Message)); context.Complete(); } } AdminImportViewModel viewModel = new AdminImportViewModel(); viewModel.resultMessage = string.Format(Worki.Resources.Views.Shared.SharedString.LocAdded, nbLocalisationsAdded); viewModel.localisationsAlreadyInDB = listLocalisationsAlreadyInDB; return View(viewModel); }
public virtual ActionResult IndexImportValidate(string result) { AdminImportViewModel viewModel = new AdminImportViewModel(); viewModel.resultMessage = result; return View(viewModel); }
public virtual ActionResult IndexImport() { AdminImportViewModel viewModel = new AdminImportViewModel(); viewModel.resultMessage = ""; return View(viewModel); }