private string WriteCsvHospitalPartnerEvent(string fileName, IEnumerable <HospitalPartnerEventViewModel> modelData) { var csvFilePath = _tempMediaLocation.PhysicalPath + fileName; // using (var streamWriter = new StreamWriter(csvFilePath, false)) //{ var csvStringBuilder = new StringBuilder(); var members = (typeof(HospitalPartnerEventViewModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <NotesViewModel>)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } header.Add("Notes"); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <NotesViewModel>)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } if (model.Notes != null && model.Notes.Count() > 0) { var notesString = string.Empty; foreach (var notesViewModel in model.Notes) { notesString += "Updated On: " + (notesViewModel.EnteredOn.HasValue ? notesViewModel.EnteredOn.Value.ToShortDateString() : string.Empty) + "\n"; notesString += "Updated By: " + notesViewModel.CreatedByUser + "\n"; notesString += "Notes: " + notesViewModel.Note + "\n\n"; } values.Add(sanitizer.EscapeString(notesString)); } else { values.Add(string.Empty); } csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } // streamWriter.Close(); //} var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = GetResponse(request, RequestSubcriberChannelNames.GenerateHospitalPartnerEventReportQueue, RequestSubcriberChannelNames.GenerateHospitalPartnerEventReportChannel); if (!isGenerated) { return("CSV File Export failed!"); } DownloadZipFile(_tempMediaLocation, fileName); return("CSV File Export was succesful!"); }
private void WriteCsv(IEnumerable <UniversalProviderViewModel> collection, string fileName) { var fs = new FileStream(fileName, FileMode.Append); var streamWriter = new StreamWriter(fs); try { var members = (typeof(UniversalProviderViewModel)).GetMembers(); foreach (var model in collection) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(obj.ToString());//EscapeString(obj.ToString()) } } streamWriter.Write(string.Join(Delimiter, values.ToArray()) + Environment.NewLine); } } catch (Exception ex) { _logger.Error("While creating CSV File : " + ex.Message + "\n\t" + ex.StackTrace + "\n\n"); } finally { streamWriter.Close(); streamWriter.Dispose(); fs.Close(); fs.Dispose(); } }
//private string WriteCsv<T>(string fileName, CSVExporter<T> exporter, IEnumerable<T> modelData) //{ // var csvFilePath = _tempMediaLocation.PhysicalPath + fileName; // using (var streamWriter = new StreamWriter(csvFilePath, false)) // { // streamWriter.Write(exporter.Header + Environment.NewLine); // foreach (string line in exporter.ExportObjects(modelData)) // { // streamWriter.Write(line + Environment.NewLine); // } // streamWriter.Close(); // } // DownloadZipFile(_tempMediaLocation, fileName); // return "CSV File Export was succesful!"; //} private string WriteCsvHospitalPartnerCustomer(string fileName, IEnumerable <HospitalPartnerCustomerViewModel> modelData) { var csvFilePath = _tempMediaLocation.PhysicalPath + fileName; //using (var streamWriter = new StreamWriter(csvFilePath, false)) //{ var csvStringBuilder = new StringBuilder(); var members = (typeof(HospitalPartnerCustomerViewModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <long, string> >) || propInfo.PropertyType == typeof(IEnumerable <HospitalPartnerCustomerActivityViewModel>)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } var tests = _testRepository.GetRecordableTests(); header.AddRange(tests.Select(test => test.Name)); header.Add("Activity"); header.Add(HealthAssessmentQuestionLabel.PrimaryCare.GetDescription()); header.Add(HealthAssessmentQuestionLabel.MammogramProstateScreening.GetDescription()); header.Add(HealthAssessmentQuestionLabel.Colonoscopy.GetDescription()); header.Add(HealthAssessmentQuestionLabel.Cancer.GetDescription()); header.Add(HealthAssessmentQuestionLabel.WeightBariatric.GetDescription()); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <long, string> >) || propInfo.PropertyType == typeof(IEnumerable <HospitalPartnerCustomerActivityViewModel>)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } foreach (var test in tests) { if (model.TestSummary != null && model.TestSummary.Count() > 0) { var summaryFinding = model.TestSummary.Where(ts => ts.FirstValue == test.Id).Select(ts => ts.SecondValue). FirstOrDefault(); values.Add(summaryFinding); } else { values.Add(string.Empty); } } if (model.Activities != null && model.Activities.Count() > 0) { var activitySummary = string.Empty; foreach (var activity in model.Activities) { activitySummary += "Status: " + activity.Status + "\n"; activitySummary += "Outcome: " + activity.Outcome + "\n"; activitySummary += "Updated On: " + activity.UpdateOn.ToShortDateString() + "\n"; activitySummary += "Updated By: " + activity.UpdatedBy + "\n"; activitySummary += "Notes: " + activity.Notes + "\n\n"; } values.Add(sanitizer.EscapeString(activitySummary)); } else { values.Add(string.Empty); } values.Add(model.PrimaryCare); values.Add(model.MammogramProstateScreening); values.Add(model.Colonoscopy); values.Add(model.Cancer); values.Add(model.WeightBariatric); csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } // streamWriter.Close(); //} var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = GetResponse(request, RequestSubcriberChannelNames.GenerateHospitalPartnerCustomerReportQueue, RequestSubcriberChannelNames.GenerateHospitalPartnerCustomerReportChannel); if (!isGenerated) { return("CSV File Export failed!"); } DownloadZipFile(_tempMediaLocation, fileName); return("CSV File Export was succesful!"); }
private string WriteCsvAppointmentBooked(string fileName, IEnumerable <AppointmentsBookedModel> modelData, long userId) { var csvFilePath = ExportableMediaLocation.PhysicalPath + fileName; var csvStringBuilder = new StringBuilder(); var members = (typeof(AppointmentsBookedModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } header.Add("Additional Fields"); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } if (propInfo.PropertyType == typeof(IEnumerable <string>)) { if (model.ShippingOptions != null && model.ShippingOptions.Count() > 0) { var shippingOptions = string.Join(", \n", model.ShippingOptions.ToArray()); values.Add(sanitizer.EscapeString(shippingOptions)); } else { values.Add(string.Empty); } continue; } if (propInfo.PropertyType == typeof(IEnumerable <RescheduleApplointmentModel>) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { if (model.RescheduleInfo != null && model.RescheduleInfo.Any()) { var rescheduleInfoString = string.Empty; foreach (var rescheduleInfo in model.RescheduleInfo) { rescheduleInfoString += "Rescheduled By: " + rescheduleInfo.RescheduledBy + "\n"; rescheduleInfoString += "Reason: " + rescheduleInfo.Reason + "\n"; if (!string.IsNullOrEmpty(rescheduleInfo.SubReason)) { rescheduleInfoString += "SubReason: " + rescheduleInfo.SubReason + "\n"; } if (!string.IsNullOrEmpty(rescheduleInfo.Notes)) { rescheduleInfoString += "Notes: " + rescheduleInfo.Notes + "\n"; } rescheduleInfoString += "---------------------------\n"; } values.Add(sanitizer.EscapeString(rescheduleInfoString)); } else { values.Add("N/A"); } continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (memberInfo.Name == "Zip") { values.Add("=" + sanitizer.EscapeString(obj.ToString())); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } if (model.AdditionalFields != null && model.AdditionalFields.Any()) { string additionFiledString = model.AdditionalFields.Aggregate(string.Empty, (current, item) => current + item.FirstValue + ": " + item.SecondValue + "\n"); values.Add(sanitizer.EscapeString(additionFiledString)); } else { values.Add(string.Empty); } csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = _baseReportService.GetResponse(request); if (!isGenerated) { return("CSV File Export failed!"); } return(_baseReportService.DownloadZipFile(ExportableMediaLocation, fileName, userId, Logger)); }
private string WriteCsvForCustomerExport(string fileName, IEnumerable <CustomerExportModel> modelData, long userId) { var csvFilePath = ExportableMediaLocation.PhysicalPath + fileName; var csvStringBuilder = new StringBuilder(); var members = (typeof(CustomerExportModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } if (propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { string additionalFieldsString; if (model.AdditionalFields == null || !model.AdditionalFields.Any()) { additionalFieldsString = "N/A"; } else { additionalFieldsString = model.AdditionalFields.Aggregate(string.Empty, (current, item) => current + item.FirstValue + ": " + item.SecondValue + "\n"); } values.Add(sanitizer.EscapeString(additionalFieldsString)); continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = _baseReportService.GetResponse(request); if (!isGenerated) { return("CSV File Export failed!"); } return(_baseReportService.DownloadZipFile(ExportableMediaLocation, fileName, userId, Logger)); }
public void WriteCsvforHourlyAppointmentBook(IEnumerable <HourlyAppointmentBookedModel> modelData, string fileName, ILogger logger) { logger.Info("Writing CSV file " + fileName); if (!DirectoryOperationsHelper.DeleteFileIfExist(fileName)) { logger.Info("file Name contain some illegal Character"); return; } var fileWriter = new StreamWriter(fileName); try { var members = (typeof(HourlyAppointmentBookedModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { continue; } } else { continue; } var propertyName = memberInfo.Name; var isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } header.Add("Additional Fields"); fileWriter.WriteLine(string.Join(",", header.ToArray())); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { continue; } if (propInfo.PropertyType == typeof(IEnumerable <string>)) { if (model.ShippingOptions != null && model.ShippingOptions.Any()) { var shippingOptions = string.Join(", \n", model.ShippingOptions.ToArray()); values.Add(sanitizer.EscapeString(shippingOptions)); } else { values.Add(string.Empty); } continue; } if (propInfo.PropertyType == typeof(IEnumerable <RescheduleApplointmentModel>)) { if (model.RescheduleInfo != null && model.RescheduleInfo.Any()) { var rescheduleInfoString = string.Empty; foreach (var rescheduleInfo in model.RescheduleInfo) { rescheduleInfoString += "Rescheduled By: " + rescheduleInfo.RescheduledBy + "\n"; rescheduleInfoString += "Reason: " + rescheduleInfo.Reason + "\n"; if (!string.IsNullOrEmpty(rescheduleInfo.SubReason)) { rescheduleInfoString += "SubReason: " + rescheduleInfo.SubReason + "\n"; } if (!string.IsNullOrEmpty(rescheduleInfo.Notes)) { rescheduleInfoString += "Notes: " + rescheduleInfo.Notes + "\n"; } rescheduleInfoString += "---------------------------\n"; } values.Add(sanitizer.EscapeString(rescheduleInfoString)); } else { values.Add("N/A"); } continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } if (model.AdditionalFields != null && model.AdditionalFields.Any()) { string additionFiledString = model.AdditionalFields.Aggregate(string.Empty, (current, item) => current + item.FirstValue + ": " + item.SecondValue + "\n"); values.Add(sanitizer.EscapeString(additionFiledString)); } else { values.Add(string.Empty); } fileWriter.WriteLine(string.Join(",", values.ToArray())); } logger.Info("CSV File Export was succesful!"); } catch (Exception ex) { logger.Error((string.Format("File Write: \n Error {0} \n Trace: {1} \n\n\n", ex.Message, ex.StackTrace))); } finally { fileWriter.Close(); fileWriter.Dispose(); } }
private void WriteCsv(IEnumerable <HealthPlanGiftCertificateReportViewModel> modelData, string fileName) { _logger.Info("Writing CSV file " + fileName); if (File.Exists(fileName)) { File.Delete(fileName); } var fileWriter = new StreamWriter(fileName); try { var members = (typeof(HealthPlanGiftCertificateReportViewModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } var propertyName = memberInfo.Name; var isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } fileWriter.WriteLine(string.Join(",", header.ToArray())); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } fileWriter.WriteLine(string.Join(",", values.ToArray())); } _logger.Info("CSV File Export was succesful!"); } catch (Exception ex) { _logger.Error((string.Format("File Write: \n Error {0} \n Trace: {1} \n\n\n", ex.Message, ex.StackTrace))); } finally { fileWriter.Close(); fileWriter.Dispose(); } }
private void UpdateFailedRecords(string filePath, IEnumerable <BloodTestResultParserLog> failedCustomers) { var sb = new StringBuilder(); var members = (typeof(BloodTestResultParserLog)).GetMembers(); var sanitizer = new CSVSanitizer(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo == null) { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } sb.Append(string.Join(",", header.ToArray()) + Environment.NewLine); foreach (var customer in failedCustomers) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo == null) { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(customer, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } sb.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } File.AppendAllText(filePath, sb.ToString()); }
private void WriteCsv(HouseCallHafResultExportModel model, string fileName) { var fs = new FileStream(fileName, FileMode.Append); var streamWriter = new StreamWriter(fs); try { var members = (typeof(HouseCallHafResultExportModel)).GetMembers(); var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <long, string> >)) { foreach (var question in HouseCallHafResultExportHelper.Questions) { if (model.HealthAssesmentAnswer != null && model.HealthAssesmentAnswer.Any()) { var answer = model.HealthAssesmentAnswer.Where(a => a.FirstValue == question.FirstValue).FirstOrDefault(); values.Add(EscapeString(answer.SecondValue)); } else { values.Add(string.Empty); } } continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(EscapeString(formatter.ToString(obj))); } else { values.Add(EscapeString(obj.ToString())); } } streamWriter.Write(string.Join(HouseCallHafResultExportHelper.Delimiter, values.ToArray()) + Environment.NewLine); } catch (Exception ex) { _logger.Error("While creating CSV File : " + ex.Message + "\n\t" + ex.StackTrace + "\n\n"); } finally { streamWriter.Close(); streamWriter.Dispose(); fs.Close(); fs.Dispose(); } }
private void AppendCustomerFileData(string filePath, IEnumerable <WellCareResultPdfLog> customerLog) { var sb = new StringBuilder(); var members = (typeof(WellCareResultPdfLog)).GetMembers(); var sanitizer = new CSVSanitizer(); foreach (var customer in customerLog) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(customer, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else if (obj.GetType() == typeof(List <string>)) { values.Add(sanitizer.EscapeString(string.Join(",", ((List <string>)obj).ToArray()))); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } sb.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } File.AppendAllText(filePath, sb.ToString()); }
public void Write <T>(IEnumerable <T> modelData, string folderLocation, string filePath) { var csvStringBuilder = new StringBuilder(); var members = (typeof(T)).GetMembers(); if (!File.Exists(folderLocation + "\\" + filePath)) { var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } csvStringBuilder.Append(string.Join("|", header.ToArray()) + Environment.NewLine); } foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo == null) { continue; } if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(obj.ToString()); } } csvStringBuilder.Append(string.Join("|", values.ToArray()) + Environment.NewLine); } SaveToFile(csvStringBuilder.ToString(), filePath, folderLocation); }
private void CreateCrossWalkFile(string csvFilePath, IEnumerable <AnthemPdfCrossWalkVeiwModel> modelData) { try { if (modelData.IsNullOrEmpty()) { _logger.Info("No Data found to generate crosswalk file "); return; } try { DirectoryOperationsHelper.DeleteFiles(csvFilePath, "CrosswalkFile_*.csv"); } catch (Exception exception) { _logger.Error(" While deleting old files"); _logger.Error("message: " + exception); _logger.Error("stack Trace: " + exception.StackTrace); } DirectoryOperationsHelper.CreateDirectoryIfNotExist(_crosswalkFilePath); DirectoryOperationsHelper.CreateDirectoryIfNotExist(csvFilePath); var filePath = Path.Combine(_crosswalkFilePath, "CrosswalkFile_" + _crosswalkFileYear + ".csv"); bool createHeaderRow = !DirectoryOperationsHelper.IsFileExist(filePath); _logger.Info("File Path: " + filePath); if (createHeaderRow) { _logger.Info("Header info being created"); } var csvFileName = Path.Combine(csvFilePath, "CrosswalkFile_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".csv"); using (var streamWriter = new StreamWriter(filePath, append: !createHeaderRow)) { var members = (typeof(AnthemPdfCrossWalkVeiwModel)).GetMembers(); if (createHeaderRow) { var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } streamWriter.Write(string.Join(",", header.ToArray()) + Environment.NewLine); } var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } streamWriter.Write(string.Join(",", values.ToArray()) + Environment.NewLine); } streamWriter.Close(); _logger.Info("crosswalk .csv has been created at following location: "); _logger.Info(filePath); if (DirectoryOperationsHelper.IsFileExist(filePath)) { DirectoryOperationsHelper.DeleteFileIfExist(csvFileName); DirectoryOperationsHelper.Copy(filePath, csvFileName); } } } catch (Exception ex) { _logger.Error("While Generating crosswalk file: message " + ex.Message + " stack trace: " + ex.StackTrace); } }
public void UpdateFailedRecords(string filePath, IEnumerable <CorporateCustomerEditModel> failedCustomers) { var sb = new StringBuilder(); var members = (typeof(CorporateCustomerEditModel)).GetMembers(); var sanitizer = new CSVSanitizer(); foreach (var customer in failedCustomers) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(customer, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else if (obj.GetType() == typeof(List <string>)) { values.Add(sanitizer.EscapeString(string.Join(",", ((List <string>)obj).ToArray()))); } else if (obj.GetType() == typeof(IEnumerable <string>)) { values.Add(sanitizer.EscapeString(string.Join(",", ((IEnumerable <string>)obj).ToArray()))); } else if (obj.GetType() == typeof(String[]) || obj.GetType() == typeof(string[])) { values.Add(sanitizer.EscapeString(string.Join(",", ((string[])obj).ToArray()))); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } sb.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } System.IO.File.AppendAllText(filePath, sb.ToString()); }
private string WriteCsvGapClosureCustomer(string fileName, IEnumerable <GapsClosureModel> modelData, long userId) { var csvFilePath = ExportableMediaLocation.PhysicalPath + fileName; //using (var streamWriter = new StreamWriter(csvFilePath, false)) //{ var csvStringBuilder = new StringBuilder(); var members = (typeof(GapsClosureModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } //header.Add("Pre-approved Tests"); //header.Add("Test Performed"); //header.Add("Test Not Performed"); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <string>) || propInfo.PropertyType == typeof(IEnumerable <OrderedPair <string, string> >)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } //if (model.PreApprovedTests != null && model.PreApprovedTests.Any()) //{ // var notesString = string.Join(", ", model.PreApprovedTests); // values.Add(sanitizer.EscapeString(notesString)); //} //else // values.Add(string.Empty); //if (model.ResultStatus != null && model.ResultStatus.Any()) //{ // var resultString = model.ResultStatus.Aggregate(string.Empty, (current, status) => current + status.FirstValue + " : " + status.SecondValue + " \n"); // values.Add(sanitizer.EscapeString(resultString)); //} //else // values.Add(string.Empty); //if (model.TestNotPerformed != null && model.TestNotPerformed.Any()) //{ // var resultString = model.TestNotPerformed.Aggregate(string.Empty, (current, status) => current + status.FirstValue + " : " + status.SecondValue + " \n"); // values.Add(sanitizer.EscapeString(resultString)); //} //else // values.Add(string.Empty); csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = _baseReportService.GetResponse(request); if (!isGenerated) { return("CSV File Export failed!"); } return(_baseReportService.DownloadZipFile(ExportableMediaLocation, fileName, userId, Logger)); }
//Todo : Move this function in a generic class. private void WriteCsv(IEnumerable <CustomAppointmentsBookedModel> modelData, string fileName) { if (File.Exists(fileName)) { File.Delete(fileName); } var fileWriter = new StreamWriter(fileName); try { var members = (typeof(CustomAppointmentsBookedModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } var propertyName = memberInfo.Name; var isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } fileWriter.WriteLine(string.Join(",", header.ToArray())); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } if (propInfo.PropertyType == typeof(IEnumerable <RescheduleApplointmentModel>)) { if (model.RescheduleInfo != null && model.RescheduleInfo.Any()) { var rescheduleInfoString = string.Empty; foreach (var rescheduleInfo in model.RescheduleInfo) { rescheduleInfoString += "Rescheduled By: " + rescheduleInfo.RescheduledBy + "\n"; rescheduleInfoString += "Reason: " + rescheduleInfo.Reason + "\n"; if (!string.IsNullOrEmpty(rescheduleInfo.SubReason)) { rescheduleInfoString += "SubReason: " + rescheduleInfo.SubReason + "\n"; } if (!string.IsNullOrEmpty(rescheduleInfo.Notes)) { rescheduleInfoString += "Notes: " + rescheduleInfo.Notes + "\n"; } rescheduleInfoString += "---------------------------\n"; } values.Add(sanitizer.EscapeString(rescheduleInfoString)); } else { values.Add("N/A"); } continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } fileWriter.WriteLine(string.Join(",", values.ToArray())); } _logger.Info("CSV File Export was succesful!"); } catch (Exception ex) { _logger.Error((string.Format("File Write: \n Error {0} \n Trace: {1} \n\n\n", ex.Message, ex.StackTrace))); } finally { fileWriter.Close(); fileWriter.Dispose(); } }
private bool GenerateOutreachCallReportCsv(IEnumerable <HourlyOutreachCallReportModel> modelData, string csvFilePath) { var csvStringBuilder = new StringBuilder(); var members = (typeof(HourlyOutreachCallReportModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <CallCenterNotes>)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } header.Add("Disposition Notes"); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <CallCenterNotes>)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } if (model.Notes != null && model.Notes.Count() > 0) { var notesString = model.Notes.Aggregate("", (current, note) => current + ("[ " + note.DateCreated.ToShortDateString() + " ] - Notes: " + note.Notes + "\n")); values.Add(sanitizer.EscapeString(notesString)); } else { values.Add("N/A"); } csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = _baseReportService.GetResponse(request); return(isGenerated); }
private string WriteCsv(string csvFilePath, IEnumerable <CustomerInfo> modelData) { using (var streamWriter = new StreamWriter(csvFilePath, false)) { var members = (typeof(CustomerInfo)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } streamWriter.Write(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel)) { continue; } if (propInfo.PropertyType == typeof(IEnumerable <string>)) { values.Add(string.Empty); continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } streamWriter.Write(string.Join(",", values.ToArray()) + Environment.NewLine); } streamWriter.Close(); } return("CSV File Export was succesful!"); }
private string WriteCsvCallCenterCallReport(string fileName, IEnumerable <CallCenterCallReportModel> modelData) { var csvFilePath = _tempMediaLocation.PhysicalPath + fileName; var csvStringBuilder = new StringBuilder(); var members = (typeof(CallCenterCallReportModel)).GetMembers(); var header = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <CallCenterNotes>)) { continue; } } else { continue; } string propertyName = memberInfo.Name; bool isHidden = false; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is DisplayNameAttribute) { propertyName = (attribute as DisplayNameAttribute).DisplayName; } } } if (isHidden) { continue; } header.Add(propertyName); } header.Add("Disposition Notes"); csvStringBuilder.Append(string.Join(",", header.ToArray()) + Environment.NewLine); var sanitizer = new CSVSanitizer(); foreach (var model in modelData) { var values = new List <string>(); foreach (var memberInfo in members) { if (memberInfo.MemberType != MemberTypes.Property) { continue; } var propInfo = (memberInfo as PropertyInfo); if (propInfo != null) { if (propInfo.PropertyType == typeof(FeedbackMessageModel) || propInfo.PropertyType == typeof(IEnumerable <CallCenterNotes>)) { continue; } } else { continue; } bool isHidden = false; FormatAttribute formatter = null; var attributes = propInfo.GetCustomAttributes(false); if (!attributes.IsNullOrEmpty()) { foreach (var attribute in attributes) { if (attribute is HiddenAttribute) { isHidden = true; break; } if (attribute is FormatAttribute) { formatter = (FormatAttribute)attribute; } } } if (isHidden) { continue; } var obj = propInfo.GetValue(model, null); if (obj == null) { values.Add(string.Empty); } else if (formatter != null) { values.Add(formatter.ToString(obj)); } else { values.Add(sanitizer.EscapeString(obj.ToString())); } } if (model.Notes != null && model.Notes.Count() > 0) { var notesString = model.Notes.Aggregate("", (current, note) => current + ("[ " + note.DateCreated.ToShortDateString() + " ] - Notes: " + note.Notes + "\n")); values.Add(sanitizer.EscapeString(notesString)); } else { values.Add("N/A"); } csvStringBuilder.Append(string.Join(",", values.ToArray()) + Environment.NewLine); } var request = new GenericReportRequest { CsvFilePath = csvFilePath, Model = csvStringBuilder.ToString() }; var isGenerated = GetResponse(request, RequestSubcriberChannelNames.CallCenterCallReportQueue, RequestSubcriberChannelNames.CallCenterCallReportChannel); if (!isGenerated) { return("CSV File Export failed!"); } DownloadZipFile(_tempMediaLocation, fileName); return("CSV File Export was succesful!"); }