public void CsvReportTest() { string expected = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "movie.csv"); if (File.Exists(expected)) { File.Delete(expected); } Movie movie1 = new Movie(); movie1.Name = "Oblivion"; movie1.PaymentForTomCruise = 2000000M; Movie movie2 = new Movie(); movie2.Name = "Edge of Tomorrow"; movie2.PaymentForTomCruise = 3000000M; CompareLogic compareLogic = new CompareLogic(); compareLogic.Config.MaxDifferences = Int32.MaxValue; ComparisonResult result = compareLogic.Compare(movie1, movie2); CsvReport csvReport = new CsvReport(); csvReport.OutputFile(result.Differences, expected); Assert.IsTrue(File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, expected))); csvReport.LaunchApplication(expected); }
public IActionResult Download() { var buildings = _buildingRepository.GetBuildings(); var header = new string[] { "Id", "Name", "Address", "City", "Zip Code" }; var data = new List <string[]> { header }; data.AddRange(buildings.Select(b => new string[] { b.BuildingId.ToString(), b.BuildingName, b.AddressLine1, b.City, b.ZipCode }).ToList()); var csvReport = new CsvReport(); var fileContents = csvReport.GenerateByteArray(data); if (fileContents == null || fileContents.Length == 0) { return(NotFound()); } return(File( fileContents: fileContents, contentType: "text/csv", fileDownloadName: "test.csv" )); }
//[Test] public void CsvReportTest() { if (File.Exists("movie.csv")) { File.Delete("movie.csv"); } Movie movie1 = new Movie(); movie1.Name = "Oblivion"; movie1.PaymentForTomCruise = 2000000M; Movie movie2 = new Movie(); movie2.Name = "Edge of Tomorrow"; movie2.PaymentForTomCruise = 3000000M; CompareLogic compareLogic = new CompareLogic(); compareLogic.Config.MaxDifferences = Int32.MaxValue; ComparisonResult result = compareLogic.Compare(movie1, movie2); CsvReport csvReport = new CsvReport(); csvReport.OutputFile(result.Differences, "movie.csv"); Assert.IsTrue(File.Exists("movie.csv")); csvReport.LaunchApplication("movie.csv"); }
private InstallerResult AddOrUpdateTheDefaultLanguageStrings(InstallerResult installerResult) { // Read in CSV and import like it does normally in the admin section using (var unitOfWork = _UnitOfWorkManager.NewUnitOfWork()) { var report = new CsvReport(); try { // Get the base language file var file = System.Web.HttpContext.Current.Server.MapPath(@"~/Installer/en-GB.csv"); // Verify that the user selected a file if (file != null) { // Unpack the data var allLines = new List <string>(); using (var streamReader = new StreamReader(file, Encoding.UTF8, true)) { while (streamReader.Peek() >= 0) { allLines.Add(streamReader.ReadLine()); } } // Read the CSV file and generate a language report = _localizationService.FromCsv("en-GB", allLines); } unitOfWork.Commit(); } catch (Exception ex) { unitOfWork.Rollback(); //Loop through report errors and spit them out in the installer result message? var sb = new StringBuilder(); foreach (var error in report.Errors) { if (error.ErrorWarningType == CsvErrorWarningType.BadDataFormat || error.ErrorWarningType == CsvErrorWarningType.GeneralError) { sb.AppendFormat("{0}<br />", error.Message); } } installerResult.Exception = ex.InnerException; installerResult.Message = "Error creating the initial data >> Language Strings"; if (!string.IsNullOrEmpty(sb.ToString())) { installerResult.Message += string.Concat("<br />", sb.ToString()); } installerResult.Successful = false; } return(installerResult); } }
/// <summary> /// Convert an import report into JSON data /// </summary> /// <param name="report"></param> /// <returns></returns> private static object ToJson(CsvReport report) { var json = new { HasErrors = report.Errors.Any(), HasWarnings = report.Warnings.Any(), Warnings = JsonConvert.SerializeObject(report.Warnings.ExtractMessages()), Errors = JsonConvert.SerializeObject(report.Errors.ExtractMessages()) }; return(json); }
/// <summary> /// Import a language from CSV /// </summary> /// <param name="langKey"> </param> /// <param name="allLines"></param> /// <returns>A report on the import</returns> public CsvReport FromCsv(string langKey, List <string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return(report); } // Look up the language and culture Language language; try { var cultureInfo = LanguageUtils.GetCulture(langKey); if (cultureInfo == null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.DoesNotExist, Message = string.Format("The language culture '{0}' does not exist.", langKey) }); return(report); } // See if this language exists already, and if not then create it language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo); } catch (LanguageOrCultureAlreadyExistsException ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message }); return(report); } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message }); return(report); } return(FromCsv(language, allLines)); }
/// <summary> /// Convert an import report into JSON data /// </summary> /// <param name="report"></param> /// <returns></returns> private static object ToJSON(CsvReport report) { var oSerializer = new JavaScriptSerializer(); var json = new { HasErrors = report.Errors.Any(), HasWarnings = report.Warnings.Any(), Warnings = oSerializer.Serialize(report.Warnings.ExtractMessages()), Errors = oSerializer.Serialize(report.Errors.ExtractMessages()), }; return(json); }
public WrappedJsonResult ImportUsers(HttpPostedFileBase file) { using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { var report = new CsvReport(); //http://www.dustinhorne.com/post/2011/11/16/AJAX-File-Uploads-with-jQuery-and-MVC-3.aspx try { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // Unpack the data var allLines = new List <string>(); using (var streamReader = new StreamReader(file.InputStream, System.Text.Encoding.UTF8, true)) { while (streamReader.Peek() >= 0) { allLines.Add(streamReader.ReadLine()); } } // Read the CSV file and generate a language report = MembershipService.FromCsv(allLines); unitOfWork.Commit(); } else { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "File does not contain any users." }); } } catch (Exception ex) { unitOfWork.Rollback(); report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = string.Format("Unable to import users: {0}", ex.Message) }); } return(new WrappedJsonResult { Data = ToJSON(report) }); } }
public int isGovActivity(string organizations, string strDate, string endDate, int repType, string membNo, string lang, string fileName) { // get all learner activity for CCOHS within selected organizations SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["v8server"].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "dbo.spXisGovActivity"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@organizations", organizations)); cmd.Parameters.Add(new SqlParameter("@strDate", strDate)); cmd.Parameters.Add(new SqlParameter("@endDate", endDate)); cmd.Parameters.Add(new SqlParameter("@repType", repType)); Int32 rowCount = 0; using (SqlDataReader drd = cmd.ExecuteReader()) // count all records { while (drd.Read()) { rowCount = drd.GetInt32(0); } // get all records if (drd.NextResult()) { while (drd.Read()) { // generate an excel or csv file if (rowCount <= 5000) { // pass the drd to the excel writer and return count in JSON ExcelReport report = null; report = new ExcelReport(membNo, lang, fileName); rowCount = report.isGovActivityReport(drd); } else { CsvReport csvReport = new CsvReport(); rowCount = csvReport.generate(membNo, lang, fileName, drd); } } } } con.Close(); return rowCount; }
public WrappedJsonResult ImportLanguage(string languageCulture, HttpPostedFileBase file) { var report = new CsvReport(); //http://www.dustinhorne.com/post/2011/11/16/AJAX-File-Uploads-with-jQuery-and-MVC-3.aspx try { // Verify that the user selected a file if (file != null && file.ContentLength > 0) { // Unpack the data var allLines = new List <string>(); using (var streamReader = new StreamReader(file.InputStream, Encoding.UTF8, true)) { while (streamReader.Peek() >= 0) { allLines.Add(streamReader.ReadLine()); } } // Read the CSV file and generate a language report = _localizationService.FromCsv(languageCulture, allLines); Context.SaveChanges(); } else { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "File does not contain a language." }); } } catch (Exception ex) { Context.RollBack(); report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = $"Unable to import language: {ex.Message}" }); } return(new WrappedJsonResult { Data = ToJson(report) }); }
private void GenerateReport() { var pos = _result.Results.Count(x => x.Success); var all = _result.Results.Count; Cursor.Show(); using (new CenteredMessageBox(this, new Font(Font.FontFamily, 12, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont))) { MessageBox.Show( $@"{_user.LastName} {_user.FirstName} {_user.SecondName}{Environment.NewLine}Правильно: {pos}{Environment.NewLine}Всего: {all}", @"Тестирование завершено", MessageBoxButtons.OK, MessageBoxIcon.Information); } CsvReport.WriteCsv(new[] { _result }); }
public void CsvReportWithCommaTest() { // set up data Person person1 = new Person(); person1.Name = "Greg"; person1.LastName = "Miller"; person1.Age = 42; Person person2 = new Person(); person2.Name = "Greg"; person2.LastName = "Miller"; person2.Age = 17; // compare var left = new List <Person> { person1 }; var right = new List <Person> { person2 }; CompareLogic compareLogic = new CompareLogic(); compareLogic.Config.IgnoreCollectionOrder = true; compareLogic.Config.CollectionMatchingSpec.Add( typeof(Person), new string[] { "Name", "LastName" }); // specify two indexes ComparisonResult result = compareLogic.Compare(left, right); // write to csv var csv = new CsvReport(); string output = csv.OutputString(result.Differences); Console.WriteLine(output); Assert.IsTrue(output.Contains("\"[Name:Greg,LastName:Miller].Age\"")); }
public int programActivityDetailsReport(string custId, string strDate, string endDate, string membId, string membNo, string lang, string fileName) { // get all learner activity for custId between dates SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["v8client"].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@custId", custId)); cmd.Parameters.Add(new SqlParameter("@strDate", strDate)); cmd.Parameters.Add(new SqlParameter("@endDate", endDate)); cmd.Parameters.Add(new SqlParameter("@membId", membId)); // count all ecommerce records for this channel (cust) between these dates cmd.CommandText = "dbo.spXprogramActivityDetailsCount"; Int32 rowCount = (Int32)cmd.ExecuteScalar(); // get all ecommerce records for this channel (cust) between these dates cmd.CommandText = "dbo.spXprogramActivityDetailsReport"; SqlDataReader drd = cmd.ExecuteReader(); // generate an excel or csv file if (rowCount <= 5000) { // pass the drd to the excel writer and return count in JSON ExcelReport report = null; report = new ExcelReport(membNo, lang, fileName); rowCount = report.programActivityDetailsReport(drd); } else { CsvReport csvReport = new CsvReport(); rowCount = csvReport.generate(membNo, lang, fileName, drd); } con.Close(); drd.Close(); return rowCount; }
public int ecommerceReport(string cust, string strDate, string endDate, string membNo, string lang, string fileName) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["v8client"].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@cust", cust)); cmd.Parameters.Add(new SqlParameter("@strDate", strDate)); cmd.Parameters.Add(new SqlParameter("@endDate", endDate)); // count all records cmd.CommandText = "dbo.spXecommerceCount"; Int32 rowCount = (Int32)cmd.ExecuteScalar(); // get all records cmd.CommandText = "dbo.spXecommerceReport"; SqlDataReader drd = cmd.ExecuteReader(); // generate an excel or csv file if (rowCount <= 5000) { // pass the drd to the excel writer (in hiveCat.appBuilder) and return count in JSON ExcelReport report = null; report = new ExcelReport(membNo, lang, fileName); rowCount = report.ecommerceReport(drd); } else { CsvReport csvReport = new CsvReport(); rowCount = csvReport.generate(membNo, lang, fileName, drd); } con.Close(); drd.Close(); return rowCount; }
public CsvReport FromCsv(Language language, List<string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return report; } try { //var allResourceKeys = GetAllResourceKeys(); var lineCounter = 0; foreach (var line in allLines) { lineCounter++; //var keyValuePair = line.Split(commaSeparator); var keyValuePair = line.Split(commaSeparator, 2, StringSplitOptions.None); if (keyValuePair.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: a key and a value are required." }); continue; } var key = keyValuePair[0]; if (string.IsNullOrEmpty(key)) { // Ignore empty keys continue; } key = key.Trim(); var value = keyValuePair[1]; var resourceKey = GetResourceKey(key); if (language == null) { throw new ApplicationException("Unable to create language"); } // If key does not exist, it is a new one to be created if (resourceKey == null) { resourceKey = new LocaleResourceKey { Name = key, DateAdded = DateTime.UtcNow, }; Add(resourceKey); report.Warnings.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.NewKeyCreated, Message = $"A new key named '{key}' has been created, and will require a value in all languages." }); } // In the new language (only) set the value for the resource var stringResource = language.LocaleStringResources.FirstOrDefault(res => res.LocaleResourceKey.Name == resourceKey.Name); if (stringResource != null) { if (!stringResource.ResourceValue.Equals(value)) { stringResource.ResourceValue = value; } } else { // No string resources have been created, so most probably // this is the installer creating the keys so we need to create the // string resource to go with it and add it stringResource = new LocaleStringResource { Language = language, LocaleResourceKey = resourceKey, ResourceValue = value }; Add(stringResource); } } } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } _cacheService.ClearStartsWith(AppConstants.LocalisationCacheName); return report; }
protected IActionResult CsvReport(CsvReport csvReport) => File(csvReport.Data, "text/csv", csvReport.FileName);
/// <summary> /// Extract users from CSV format and import them /// </summary> /// <returns></returns> public CsvReport FromCsv(List <string> allLines) { var usersProcessed = new List <string>(); var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No users found." }); return(report); } var settings = _settingsService.GetSettings(true); var lineCounter = 0; foreach (var line in allLines) { try { lineCounter++; // Each line is made up of n items in a predefined order var values = line.Split(commaSeparator); if (values.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: insufficient values supplied." }); continue; } var userName = values[0]; if (userName.IsNullEmpty()) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: no username supplied." }); continue; } var email = values[1]; if (email.IsNullEmpty()) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: no email supplied." }); continue; } // get the user var userToImport = GetUser(userName); if (userToImport != null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = $"Line {lineCounter}: user already exists in forum." }); continue; } if (usersProcessed.Contains(userName)) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = $"Line {lineCounter}: user already exists in import file." }); continue; } usersProcessed.Add(userName); userToImport = CreateEmptyUser(); userToImport.UserName = userName; userToImport.Slug = ServiceHelpers.GenerateSlug(userToImport.UserName, GetUserBySlugLike(ServiceHelpers.CreateUrl(userToImport.UserName)), userToImport.Slug); userToImport.Email = email; userToImport.IsApproved = true; userToImport.PasswordSalt = StringUtils.CreateSalt(AppConstants.SaltSize); string createDateStr = null; if (values.Length >= 3) { createDateStr = values[2]; } userToImport.CreateDate = createDateStr.IsNullEmpty() ? DateTime.UtcNow : DateTime.Parse(createDateStr); if (values.Length >= 4) { userToImport.Age = int.Parse(values[3]); } if (values.Length >= 5) { userToImport.Location = values[4]; } if (values.Length >= 6) { userToImport.Website = values[5]; } if (values.Length >= 7) { userToImport.Facebook = values[6]; } if (values.Length >= 8) { userToImport.Signature = values[7]; } userToImport.Roles = new List <MembershipRole> { settings.NewMemberStartingRole }; Add(userToImport); } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } } return(report); }
public CsvReport FromCsv(Language language, List <string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return(report); } try { //var allResourceKeys = GetAllResourceKeys(); var lineCounter = 0; foreach (var line in allLines) { lineCounter++; //var keyValuePair = line.Split(commaSeparator); var keyValuePair = line.Split(commaSeparator, 2, StringSplitOptions.None); if (keyValuePair.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = $"Line {lineCounter}: a key and a value are required." }); continue; } var key = keyValuePair[0]; if (string.IsNullOrEmpty(key)) { // Ignore empty keys continue; } key = key.Trim(); var value = keyValuePair[1]; var resourceKey = GetResourceKey(key); if (language == null) { throw new ApplicationException("Unable to create language"); } // If key does not exist, it is a new one to be created if (resourceKey == null) { resourceKey = new LocaleResourceKey { Name = key, DateAdded = DateTime.UtcNow, }; Add(resourceKey); report.Warnings.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.NewKeyCreated, Message = $"A new key named '{key}' has been created, and will require a value in all languages." }); } // In the new language (only) set the value for the resource var stringResource = GetResource(language.Id, resourceKey.Name); if (stringResource != null) { if (!stringResource.ResourceValue.Equals(value)) { stringResource.ResourceValue = value; } Update(stringResource); } else { // No string resources have been created, so most probably // this is the installer creating the keys so we need to create the // string resource to go with it and add it stringResource = new LocaleStringResource { Language_Id = language.Id, LocaleResourceKey_Id = resourceKey.Id, ResourceValue = value }; Add(stringResource); } } } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } return(report); }
public void Process() { var data = new List<List<string>>(); var headers = new List<string>(); using (var fs = new FileStream(_sourceDataPath, FileMode.Open, FileAccess.Read)) { var parser = new MimeParser(fs, MimeFormat.Mbox); int count = 0; var stats = new Stats(); while (!parser.IsEndOfStream) { MimeMessage message = parser.ParseMessage(); var userLabels = new List<string>(); string labels = message.Headers.Where(x => x.Field == "X-Gmail-Labels") .FirstOrDefault()?.Value ?? "nouserlabels"; foreach (string l in labels.Split(',')) { if (!Config.GmailSystemLabels.Contains(l.ToUpper())) { // Add a stat for that label stats.Add(new StatItem() { Date = message.Date, Label = l, MessageCount = 1 }); } } // Print status if (count % 10000 == 0) Console.WriteLine(count / 1000 + "k complete"); count++; } // Agregate stats var statsDict = new Dictionary<DateTimeOffset, List<int>>(); DateTimeOffset startDate = stats.Items.Min(x => x.Date); startDate = startDate.AddMilliseconds(startDate.Millisecond * -1); DateTimeOffset endDate = stats.Items.Max(x => x.Date); endDate = endDate.AddMilliseconds(endDate.Millisecond * -1); List<string> uniqueLabels = stats.Items.Select(x => x.Label) .Distinct() .ToList(); _headers.Add("DateTime"); _headers.AddRange(uniqueLabels); for (var date = startDate; date <= endDate; date = date.AddHours(1)) { statsDict.Add(date, new int[uniqueLabels.Count].ToList()); } for(int i = 0; i < stats.Items.Count; i++) { statsDict[stats.Items[i].Date][uniqueLabels.IndexOf(stats.Items[i].Label)]++; } for (var date = startDate; date <= endDate; date = date.AddHours(1)) { var row = new List<string> { date.ToString() }; foreach (var c in statsDict[date]) { row.Add(c.ToString()); } data.Add(row); } } var csvReport = new CsvReport(_headers, data); File.WriteAllLines(_destinationCsvPath, csvReport.GetCsvText()); }
/// <summary> /// Import a language from CSV /// </summary> /// <param name="langKey"> </param> /// <param name="allLines"></param> /// <returns>A report on the import</returns> public CsvReport FromCsv(string langKey, List<string> allLines) { var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return report; } // Look up the language and culture Language language; try { var cultureInfo = LanguageUtils.GetCulture(langKey); if (cultureInfo == null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.DoesNotExist, Message = $"The language culture '{langKey}' does not exist." }); return report; } // See if this language exists already, and if not then create it language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo); } catch (LanguageOrCultureAlreadyExistsException ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message }); return report; } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message }); return report; } return FromCsv(language, allLines); }
public void Setup(){ target = new StringWriter(); report = new CsvReport<ReportLine>(target); }
public void Init() { _fileWriter = Substitute.For <IFileWriter>(); _target = new CsvReport(_fileWriter); }
internal void SaveCSV(string file) { CsvReport.Save(__cTradeService.Opens, __cTradeService.Closes, file); }
/// <summary> /// Import a language from CSV /// </summary> /// <param name="langKey"> </param> /// <param name="allLines"></param> /// <returns>A report on the import</returns> public CsvReport FromCsv(string langKey, List <string> allLines) { var commaSeparator = new[] { ',' }; var report = new CsvReport(); if (allLines == null || allLines.Count == 0) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.BadDataFormat, Message = "No language keys or values found." }); return(report); } // Look up the language and culture Language language; try { var cultureInfo = LanguageUtils.GetCulture(langKey); if (cultureInfo == null) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.DoesNotExist, Message = string.Format("The language culture '{0}' does not exist.", langKey) }); return(report); } // See if this language exists already, and if not then create it language = GetLanguageByLanguageCulture(langKey) ?? Add(cultureInfo); } catch (LanguageOrCultureAlreadyExistsException ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.AlreadyExists, Message = ex.Message }); return(report); } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.ItemBad, Message = ex.Message }); return(report); } try { var lineCounter = 0; foreach (var line in allLines) { lineCounter++; var keyValuePair = line.Split(commaSeparator); if (keyValuePair.Length < 2) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.MissingKeyOrValue, Message = string.Format("Line {0}: a key and a value are required.", lineCounter) }); continue; } var key = keyValuePair[0]; if (string.IsNullOrEmpty(key)) { // Ignore empty keys continue; } key = key.Trim(); var value = keyValuePair[1]; var resourceKey = _localizationRepository.GetResourceKey(key); if (language == null) { throw new ApplicationException(string.Format("Unable to create language")); } // If key does not exist, it is a new one to be created if (resourceKey == null) { resourceKey = new LocaleResourceKey { Name = key, DateAdded = DateTime.UtcNow, }; Add(resourceKey); report.Warnings.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.NewKeyCreated, Message = string.Format("A new key named '{0}' has been created, and will require a value in all languages.", key) }); } // In the new language (only) set the value for the resource var stringResources = language.LocaleStringResources.Where(res => res.LocaleResourceKey.Name == resourceKey.Name).ToList(); if (stringResources.Any()) { foreach (var res in stringResources) { res.ResourceValue = value; break; } } else { // No string resources have been created, so most probably // this is the installer creating the keys so we need to create the // string resource to go with it and add it var stringResource = new LocaleStringResource { Language = language, LocaleResourceKey = resourceKey, ResourceValue = value }; _localizationRepository.Add(stringResource); } } } catch (Exception ex) { report.Errors.Add(new CsvErrorWarning { ErrorWarningType = CsvErrorWarningType.GeneralError, Message = ex.Message }); } return(report); }