public static TemplateReader GetInstance() { { lock (padlock) { if (instance == null) { instance = new TemplateReader(); } return(instance); } } }
/// <summary> /// This method is is main mathod for creating output files /// </summary> /// <param name="InputFields"></param> /// <returns></returns> public ResponseBase CreateOutputFiles(InputFields InputFields) { #region Declaration const string MethodName = "InsuranceRenewalReminder::UIHelper::CreateOutputFiles:: "; ResponseBase Response = new ResponseBase(); EventLogger EventLog = new EventLogger(); #endregion try { //Add Response message from InputField reading if (InputFields.ReturnCode > 0) { Response.ReturnMessage = InputFields.ReturnMessage; } bool FileCreated = false; //Negative use case checking if (InputFields == null || InputFields.InputList == null || InputFields.InputList.Count == 0) { //Console.WriteLine("No Inputs received"); Response.ReturnCode = -1; Response.ReturnMessage = (string.IsNullOrWhiteSpace(Response.ReturnMessage) ? "" : Response.ReturnMessage + "</br>") + "No Inputs received"; return(Response); } //Get input template, As template is common for all users hence implemented Singleton pattern TemplateReader Template = TemplateReader.GetInstance(); string TemplateContent = Template.ReadTemplateFile(); //Create output files for each input foreach (InputField InputData in InputFields.InputList) { //Create customised Output file name string FileName = InputData.ID + "-" + InputData.FirstName + "_" + InputData.Surname + ".txt"; string OutputFilePath = WebConfigurationManager.AppSettings["OutputFilePath"]; //Check if Output file not present previously if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(OutputFilePath + "\\" + FileName))) { string FinalContent = FillTemplateData(TemplateContent.ToString(), InputData); //string FinalContent = FormatTemplateData(TemplateContent.ToString(), InputData); // Alternate method with compromised readability. FileStream FS = new FileStream(System.Web.HttpContext.Current.Server.MapPath(OutputFilePath + "\\" + FileName), FileMode.Create, FileAccess.Write); StreamWriter SW = new StreamWriter(FS); SW.Write(FinalContent.ToString()); SW.Close(); FileCreated = true; } else { //Update response and log warning Response.ReturnCode = 1; Response.ReturnMessage = (string.IsNullOrWhiteSpace(Response.ReturnMessage) ? "" : Response.ReturnMessage + "</br>") + "Record for " + FileName + " already Present, No updates made."; EventLog.LogWarning(MethodName + "Record for " + FileName + " already Present, No updates made."); } } //return success if atleast one fine created. if (FileCreated) { Response.ReturnCode = 0; } } catch (Exception ex) { //Log error & Return response EventLog.LogException(MethodName, ex); Response.ReturnCode = -1; Response.ReturnMessage = Response.ReturnMessage + Environment.NewLine + "Error while generating output file!!!"; return(Response); } return(Response); }