public IMarketFileExportResult[] Export(CancellationToken token) { var files = exportDataAccess.ListCustomerInfoReadyForTransmission(); if (files.Length == 0) { logger.TraceFormat("Zero Customer Billing File records found to export."); return(new IMarketFileExportResult[0]); } logger.DebugFormat("Exporting {0} Customer Billing Files for transmission.", files.Length); var context = new PrismCustomerInfoContext(); foreach (var file in files) { if (token.IsCancellationRequested) { break; } context.PushFile(file.CspDunsId, file.FileName); WriteHeader(context, file); WriteRecord(context, file); } return(context.Models); }
public void WriteHeader(PrismCustomerInfoContext context, TypeCustomerInfoFile file) { var line = string.Format("HDR|MTCRCustomerInformation|{0}|{1}", file.FileNumber, file.CrDuns); context.AppendLine(line); }
public void ParseLine(PrismCustomerInfoContext context, string line) { if (string.IsNullOrWhiteSpace(line)) { return; } var fields = line.Split('|'); var indicator = fields.AtIndex(0); switch (indicator) { case "HDR": context.ResolveToFile(); ParseHeader(context, fields); break; case "ER1": case "ER2": ParseError(context, fields); break; case "SUM": context.ResolveToFile(); break; } }
public void When_Parsing_Customer_Info_Error_With_Field_Zero_Equal_To_ER1_Then_Record_Type_Id_Is_Set_To_One() { // arrange const int expected = 1; var context = new PrismCustomerInfoContext(); var fileModel = new TypeCustomerInfoFile(); context.PushModel(fileModel); var fields = new[] { "ER1", "", "PREMNO", "" }; // act concern.ParseError(context, fields); context.ResolveToFile(); // assert var results = context.Results; var collection = results.TypeHeaders; var model = collection.First(); var errors = model.ErrorRecords; Assert.AreEqual(1, errors.Length); var error = errors[0]; Assert.AreEqual(expected, error.RecordTypeId); Assert.IsNull(error.FieldName); }
public void ParseError(PrismCustomerInfoContext context, string[] fields) { var current = context.Current; if (current == null || current.ModelType != TypeCustomerInfoTypes.File) { throw new InvalidOperationException(); } var file = current as TypeCustomerInfoFile; if (file == null) { throw new InvalidOperationException(); } var recordTypeId = 2; var recordTypeIndicator = fields[0]; if (recordTypeIndicator.Equals("ER1", StringComparison.OrdinalIgnoreCase)) { recordTypeId = 1; } var model = new TypeCustomerInfoErrorRecord { RecordTypeId = recordTypeId, RecordId = fields.AtIndexInt(4), ErrorMessage = fields.AtIndex(6), FieldName = fields.AtIndex(5), PremNo = fields.AtIndex(2), }; file.AddError(model); }
public void When_Parsing_Customer_Info_Line_With_Field_Zero_Equal_To_HDR_Then_A_Header_Is_Created() { // arrange var context = new PrismCustomerInfoContext(); var fields = new[] { "HDR", "MOCK", "", "" }; // act concern.ParseLine(context, string.Join("|", fields)); // assert Assert.IsNotNull(context.Current); var model = context.Current as TypeCustomerInfoFile; Assert.IsNotNull(model); }
public IMarketFileParseResult Parse(Stream stream) { var context = new PrismCustomerInfoContext(); using (var reader = new StreamReader(stream)) { string customerInfoFileLine; while ((customerInfoFileLine = reader.ReadLine()) != null) { ParseLine(context, customerInfoFileLine); } context.ResolveToFile(); } return(context.Results); }
public void When_Parsing_Customer_Info_Header_With_Field_One_Indicating_Response_Then_File_Type_Id_Is_Set_To_Two() { // arrange const int expected = 2; var context = new PrismCustomerInfoContext(); var fields = new[] { "", "MTCRCUSTOMERINFORMATIONERCOTRESPONSE", "", "" }; // act concern.ParseHeader(context, fields); context.ResolveToFile(); // assert var results = context.Results; var collection = results.TypeHeaders; Assert.AreEqual(1, collection.Length); var model = collection.First(); Assert.AreEqual(expected, model.FileTypeId); }
public void WriteRecord(PrismCustomerInfoContext context, TypeCustomerInfoFile file) { var records = exportDataAccess.ListRecords(file.FileId); foreach (var record in records) { var email = (emailMatch.IsMatch(record.Email)) ? record.Email : string.Empty; var line = string.Format( "DET|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|{14}|{15}|{16}|{17}|{18}|{19}", record.SequenceId, record.CrDuns, record.PremNo, record.CustNo, alphaNumericExp.Replace(record.FirstName, string.Empty), alphaNumericExp.Replace(record.LastName, string.Empty), alphaNumericExp.Replace(record.CompanyName, string.Empty), alphaNumericExp.Replace(record.ContactName, string.Empty), alphaNumericExp.Replace(record.BillingCareOfName, string.Empty), alphaNumericExp.Replace(record.BillingAddress1, string.Empty), alphaNumericExp.Replace(record.BillingAddress2, string.Empty), alphaNumericExp.Replace(record.BillingCity, string.Empty), record.BillingState, alphaNumericExp.Replace(record.BillingPostalCode, string.Empty), record.BillingCountryCode.Trim().Substring(0, 2), numericExp.Replace(record.PrimaryTelephone, string.Empty), alphaNumericExp.Replace(record.PrimaryTelephoneExt, string.Empty), alphaNumericExp.Replace(record.SecondaryTelephone, string.Empty), numericExp.Replace(record.SecondaryTelephoneExt, string.Empty), email); context.AppendLine(line); } context.AppendLine(string.Format("SUM|{0}|0|0", records.Length)); }
public void ParseHeader(PrismCustomerInfoContext context, string[] fields) { var typeId = 3; var typeIndicator = fields.AtIndex(1); if (typeIndicator.Equals("MTCRCUSTOMERINFORMATIONERCOTRESPONSE", StringComparison.OrdinalIgnoreCase)) { typeId = 2; } var model = new TypeCustomerInfoFile { FileTypeId = typeId, ReferenceNumber = fields.AtIndex(2), CrDuns = fields.AtIndex(3), Status = CustomerInfoFileStatusOptions.Imported, }; var cspDunsId = clientDataAccess.IdentifyCspDunsId(model.CrDuns); model.CspDunsId = cspDunsId; context.PushModel(model); }