Skip to content

Instantly share code, notes, and snippets.

Created May 13, 2015 15:57
Show Gist options
  • Save anonymous/67a2ae0192257ac51f39 to your computer and use it in GitHub Desktop.
Save anonymous/67a2ae0192257ac51f39 to your computer and use it in GitHub Desktop.
using CASImporter.dal.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CASImporter.CsvParser
{
public class AlertObjectFactory
{
private static Alert mainAlert = new Alert();
/// <summary>
/// Main Method which will return the constructed Alert Object.
/// </summary>
/// <returns>returns a constructed Alert Object.</returns>
public static Alert GetAlertObject()
{
if (DoFileChecks() == true)
{
//File Integrity Seems ok, lets continue.
SetReportGenerationDateTime();
SetDefaultObjectProperties();
ImportAlertRecords();
}
Console.WriteLine("Alert Object construction Complete! Returning to Main Method");
return mainAlert;
}
/// <summary>
/// Takes the time the file was generated and populates it in the object.
/// </summary>
private static void SetReportGenerationDateTime()
{
//@TODO: Do we need to check against the DB to see if this file has already been imported?
//@TODO: Do we need to check if this file has been modified/created within the last....1 hour?
mainAlert.AlertDatetime = DateTime.Now; //@TODO: Write proper code to find files creation datetime.
}
private static void ImportAlertRecords()
{
if(CheckForFileContents() == true)
{
Console.WriteLine("Records Exist in Cas Alert Report!");
mainAlert.Records = AlertRecordImporter.GetAlertObjects();
mainAlert.AlertHasRecords = true; //state in the object that we do have records!
}
else
{
mainAlert.Records = null;
mainAlert.AlertHasRecords = false;
}
}
private static bool CheckForFileContents()
{
Console.WriteLine("Checking for Alert Records In CSV");
return true;
}
/// <summary>
/// Checks for the files existance.
/// </summary>
/// <returns></returns>
private static bool DoFileChecks()
{
bool errorCheck = true; //@TODO: write some code to physcally check the file for existance,etc..
Console.WriteLine("Checking For file Existance");
if (errorCheck)
{
Console.WriteLine("File Does Exist!");
//The checks seem to be ok! lets signal that in the object.
mainAlert.Error = false;
return true; //tell the calling method Everything is ok!
}
else
{
Console.WriteLine("File Does Not Exist!");
mainAlert.Error = true;
return false; //Tell the calling method there is a problem!
}
}
/// <summary>
/// Sets the default Object properties such a creation date and the intital read state.
/// </summary>
private static void SetDefaultObjectProperties()
{
mainAlert.Unread = true;
mainAlert.ImportDateTime = DateTime.Now;
}
}
}
using CASImporter.dal.Helpers;
using CASImporter.dal.Models;
using CsvHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CASImporter.CsvParser
{
public class AlertRecordImporter
{
/// <summary>
/// Imports the CSV file and will loop through, adding each item into a final list which will be returned
/// </summary>
/// <returns>List of "AlertRecord"</returns>
public static List<AlertRecord> GetAlertObjects()
{
string filePath = "C:\\TestData\\testCas1.csv";
List<AlertRecord> finalList = new List<AlertRecord>(); //The object we are building up and then returning back.
AlertRecord BuilderRecord = new AlertRecord(); //A Single AlertRecord we will use to add content into prior to adding it to the finallist.
using (var sr = new StreamReader(filePath))
{
try
{
var reader = new CsvReader(sr);
Console.WriteLine("importing AlertRecords Into object.");
int counter = 1;
while (reader.Read())
{
Console.WriteLine("Importing Record: "+ counter.ToString());
//Lets loop through the CSVReader and manually map CSV Fields to Model Properties.
BuilderRecord.HospitalNumber = reader.GetField<string>(0);
BuilderRecord.ForeName = reader.GetField<string>(1);
BuilderRecord.SurName = reader.GetField<string>(2);
BuilderRecord.DateOfBirth = DatetimeManipulator.ConvertHSSDate(reader.GetField<string>(3)); //Take the value from the CSV and do some magic to make it fit into a DateTime Object!
BuilderRecord.EventDateTime = DatetimeManipulator.ConvertHSSDateTime(reader.GetField<string>(4), reader.GetField<string>(5));
BuilderRecord.CrisNumber = reader.GetField<string>(6);
//Now we are done adding values into the AlertRecord, lets add the builder to the final list.
finalList.Add(BuilderRecord);
counter++;
}
Console.WriteLine("import Complete, Returning Final List of AlertRecords!");
return finalList; //return the final list to the user.
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment