/// <summary>
 ///   Move the spread sheet File to Quarantine
 /// </summary>
 /// <param name="s"></param>
 public void MoveToQuarantine(ForeCastFileInfo s)
 {
     try
     {
         String DirName     = s.PathName + "\\" + "QUARANTINE\\";
         String XlFile      = s.XlsFile;
         int    nindex      = XlFile.LastIndexOf("\\");
         String filename    = XlFile.Substring(nindex + 1);
         String newfilename = DirName +
                              DateAsStr(DateTime.Now) + "_" +
                              filename;
         /////////////////////////////////////////////
         ///
         ///   IF Quarantine Folder does not exist
         ///   Create a new one
         if (!Directory.Exists(DirName))
         {
             Directory.CreateDirectory(DirName);
         }
         ///////////////////////////////////////////
         ///
         ///  Copy the File to Quarantine
         ///
         if (!ForeCastUpdatorService.CanFilebeAccessed(XlFile))
         {
             throw new Exception(XlFile + " Copy: Cannot be accessed or opened by another app");
         }
         File.Copy(XlFile, newfilename, true);
         //////////////////////////////////////////////
         ///  Delete the file
         ///
         if (!ForeCastUpdatorService.CanFilebeAccessed(XlFile))
         {
             throw new Exception(XlFile + " Cannot be accessed or opened by another app");
         }
         File.Delete(XlFile);
     }
     catch (Exception e)
     {
         ///////////////////////////////////////////////
         ///
         /// Log the Exception
         ///
         System.Diagnostics.EventLog.WriteEntry("ForeCastLog", e.ToString());
         CSyntaxErrorLog.AddLine(e.ToString());
         return;
     }
 }
        /// <summary>
        ///    Backup File
        /// </summary>
        /// <param name="s"></param>

        public void BackupFile(ForeCastFileInfo s)
        {
            try
            {
                String DirName     = s.PathName + "\\" + "BACKUP\\";
                String XlFile      = s.XlsFile;
                int    nindex      = XlFile.LastIndexOf("\\");
                String filename    = XlFile.Substring(nindex + 1);
                String newfilename = DirName +
                                     DateAsStr(DateTime.Now) + "_" +
                                     filename;
                /////////////////////////////////////////////
                ///
                ///  IF Backup folder does not exist
                ///  Create a new one
                if (!Directory.Exists(DirName))
                {
                    Directory.CreateDirectory(DirName);
                }
                //////////////////////////////////////
                ///
                ///  Move to Backup folder
                ///
                ///

                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Copying File");
                if (!ForeCastUpdatorService.CanFilebeAccessed(XlFile))
                {
                    throw new Exception(XlFile + "Copy: Cannot be accessed or opened by another app");
                }
                File.Copy(XlFile, newfilename, true);
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Finished Copying File");
                if (!ForeCastUpdatorService.CanFilebeAccessed(XlFile))
                {
                    throw new Exception(XlFile + "Delete: Cannot be accessed or opened by another app");
                }
                File.Delete(XlFile);
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Finished Deleting File");
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", e.ToString());
                CSyntaxErrorLog.AddLine(e.ToString());
                return;
            }
        }
        /// <summary>
        ///    Scan for Files and Process if at all at least one is found
        /// </summary>
        ///
        private static void ScanAndProcess()
        {
            CForeCastUpdate fcst_up = null;

            try
            {
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Scanning for data");

                if (!ForeCastUpdatorService.CanFilebeAccessed(DirPath + "\\CONFIG.XML"))
                {
                    String file_name = DirPath + "\\CONFIG.XML";
                    System.Diagnostics.EventLog.WriteEntry("ForeCastLog", file_name + "  does  not exist ,Cannot be accessed or opened already by another app");
                    return;
                }


                fcst_up = new CForeCastUpdate(DirPath + "\\CONFIG.XML");

                try
                {
                    System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Going to iterate the folders");
                    fcst_up.Update();
                    System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Finished Scanning for data");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Exception thrown" + ex.ToString());
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Exception thrown" + e.ToString());
            }

            fcst_up = null;
            return;
        }
        /// <summary>
        ///    Call  the Excel content Validator
        /// </summary>
        /// <param name="s"></param>
        public bool ValidateFile(ForeCastFileInfo s)
        {
            CParser        prs = null;
            CExcelDBUpdate upd = null;

            try {
                if (!ForeCastUpdatorService.CanFilebeAccessed(s.XlsFile))
                {
                    throw new Exception(s.XlsFile + " Cannot be accessed or opened by another app");
                }

                String m_Country = this.GetCountryFromPath(s.XlsFile);
                String m_Segment = this.GetSegmentFromPath(s.XlsFile);


                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Processing File ...." + s.XlsFile);
                String Prgs        = (String)GlobalSettings.GetEntry("VALIDATION_SCRIPT");
                String OrclConStr  = (String)GlobalSettings.GetEntry("ORACLE_CONNECTION_STRING");
                String ExcelConStr = "Provider=" + (String)GlobalSettings.GetEntry("EXCEL_PROVIDER");
                ExcelConStr += "Data Source=" + s.XlsFile + ";";
                ExcelConStr += (String)GlobalSettings.GetEntry("EXCEL_PROPERTIES");
                CSemanticErrorLog.AddLine("Excel file = " + s.XlsFile);
                prs = new CParser(Prgs, m_Country, m_Segment);
                bool brs     = prs.CallParser(ExcelConStr, OrclConStr);
                bool ret_val = true;

                if (brs == true)
                {
                    upd     = new CExcelDBUpdate(OrclConStr, prs.GetExcelReader());
                    ret_val = upd.UpdateAll();

                    if (ret_val == false)
                    {
                        CSemanticErrorLog.AddLine("FATAL ERROR WHILE UPDATING FORECAST DATA  ");
                    }
                }



                if (ret_val == true && brs == true)
                {
                    if (GlobalSettings.allow_emails == true)
                    {
                        notify.TO   = prs.GetEmail();
                        notify.Body = "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + prs.GetSemanticLog();
                        notify.SendToExcelEmail(0);
                    }

                    if (GlobalSettings.event_logging == true)
                    {
                        if (!System.Diagnostics.EventLog.SourceExists("ForeCastLog"))
                        {
                            System.Diagnostics.EventLog.CreateEventSource("ForeCastLog",
                                                                          "DoForeCastLog");
                        }

                        System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + prs.GetSemanticLog() + prs.GetSyntaxLog());
                    }

                    if (upd != null)
                    {
                        upd.Cleanup();
                        upd = null;
                    }
                    if (prs != null)
                    {
                        prs.CloseAll();
                        prs = null;
                    }
                    return(ret_val);
                }
                else
                {
                    if (GlobalSettings.event_logging == true)
                    {
                        if (!System.Diagnostics.EventLog.SourceExists("ForeCastLog"))
                        {
                            System.Diagnostics.EventLog.CreateEventSource("ForeCastLog",
                                                                          "DoForeCastLog");
                        }

                        System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + prs.GetSemanticLog() + prs.GetSyntaxLog());
                    }

                    if (GlobalSettings.allow_emails == true)
                    {
                        notify.TO   = prs.GetEmail();
                        notify.Body = "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + prs.GetSemanticLog();
                        notify.SendToExcelEmail(1);

                        notify.Body = "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + prs.GetSemanticLog() + prs.GetSyntaxLog();
                        notify.Send(1);
                    }
                    if (upd != null)
                    {
                        upd.Cleanup();
                        upd = null;
                    }
                    if (prs != null)
                    {
                        prs.CloseAll();
                        prs = null;
                    }

                    return(false);
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.EventLog.WriteEntry("ForeCastLog", e.ToString());
                CSemanticErrorLog.AddLine(e.ToString());
                if (GlobalSettings.event_logging == true)
                {
                    if (!System.Diagnostics.EventLog.SourceExists("ForeCastLog"))
                    {
                        System.Diagnostics.EventLog.CreateEventSource("ForeCastLog",
                                                                      "DoForeCastLog");
                    }


                    System.Diagnostics.EventLog.WriteEntry("ForeCastLog", "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + CSemanticErrorLog.GetLog() + CSyntaxErrorLog.GetLog());
                }

                if (GlobalSettings.allow_emails == true)
                {
                    notify.Body = "Hi" + "\r\n\r\n" + "ForeCast updation status " + "\r\n" + CSemanticErrorLog.GetLog() + CSyntaxErrorLog.GetLog();
                    notify.Send(1);
                }

                if (upd != null)
                {
                    upd.Cleanup();
                    upd = null;
                }
                if (prs != null)
                {
                    prs.CloseAll();
                    prs = null;
                }

                return(false);
            }
        }