private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                this._timer.Stop();
                Log("Timer Stoped");
                CheckToSeeIfProblemWithRecords();

                string[] listOfFiles = Directory.GetFiles(pathToWatchedFolder);

                for (int i = 0; i < listOfFiles.Length; i++)
                {
                    if (File.Exists(listOfFiles[i]))
                    {
                        string pathTofile = Path.GetFullPath(listOfFiles[i]);
                        Console.WriteLine(pathTofile);
                        ScannedDocument scannedDocument = new ScannedDocument();
                        scannedDocument.PageCount = 0;
                        scannedDocument = GetDocumentInformation(pathTofile);

                        if (scannedDocument.PageCount > 0)
                        {
                            Console.WriteLine(scannedDocument.PageCount.ToString());
                            scannedDocument.FullPath = pathTofile;
                            ProcessBarcodedDocuments(scannedDocument);
                        }
                    }
                }
                Log("Timer Started");
                this._timer.Start();
            }
            catch (Exception er)
            {
                LogErrors(er.ToString());
                //Exceptions.ExceptionHandling.InsertErrorMessage(er.ToString());
                //Exceptions.ExceptionHandling.SendErrorEmail(er.ToString(), ConfigurationValues.EmailFromFriendly, ConfigurationValues.EmailSendToFriendly, ConfigurationValues.EmailSubject);
                this._timer.Start();
            }
        }
        private static string CreateDocumentBasedOnPageCount(ScannedDocument barCodeText)
        {
            Document pdfDocument1 = new Document(barCodeText.FullPath);
            Document pdfDocument2 = new Document();
            string returnDocumentName = string.Empty;

            for (int i = 1; i <= barCodeText.PageCount; i++)
            {
                pdfDocument2.Pages.Add(pdfDocument1.Pages[i]);

            }

            returnDocumentName = pathToWorkingFolder + Utility.GetFileNameFromDateTimeString() + ".pdf";
            pdfDocument2.Save(returnDocumentName);
            pdfDocument1.Dispose();

            Document pdfDocument3 = new Document(barCodeText.FullPath);

            for (int i = 1; i <= barCodeText.PageCount; i++)
            {
                pdfDocument3.Pages.Delete(1);
            }

            File.Delete(barCodeText.FullPath);
            pdfDocument3.Save(barCodeText.FullPath);
            pdfDocument3.Dispose();
            return returnDocumentName;
        }
        private static ScannedDocument GetDocumentInformation(string filePath)
        {
            ScannedDocument scannedDocument = new ScannedDocument();

            try
            {
                using (IDbConnection db = new SqlConnection(deskTopConnection))
                {
                    const string query = "SELECT [ID],[UserName],[DocumentName],[PageCount]"
                        + " ,[PatientID],[TabID],[DateCreated],[ArchiveID] as ArchiveID"
                        + " FROM [ScannedDocuments]"
                        + "  where documentname = @documentname";
                    scannedDocument = db.Query<ScannedDocument>(query, new { @documentname = filePath }).Single();
                    return scannedDocument;
                }
            }
            catch (Exception er)
            {
                LogErrors(er.ToString());
                //Exceptions.ExceptionHandling.InsertErrorMessage(er.ToString());
                //Exceptions.ExceptionHandling.SendErrorEmail(er.ToString(), ConfigurationValues.EmailFromFriendly, ConfigurationValues.EmailSendToFriendly, ConfigurationValues.EmailSubject);
                return scannedDocument;
            }
        }
        private static void ProcessBarcodedDocuments(ScannedDocument barCodeText)
        {
            int pdfPageCount = 0;
            int pdfPageCountCheck = 0;
            Document pdfDocument = new Document(barCodeText.FullPath);
            pdfPageCount = pdfDocument.Pages.Count;
            pdfPageCountCheck = pdfPageCountCheck + barCodeText.PageCount;

            if (pdfPageCount != pdfPageCountCheck)
            {
                string[] fileNameArray = barCodeText.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                UpdateDocumentArchive(barCodeText.ArchiveID, "F");
                File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                File.Delete(barCodeText.FullPath);
                return;
            }

            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(barCodeText.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    LogErrors(er.ToString());
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                    AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    LogErrors("Binary data array is null.");
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                    AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                    return;
                }

                ContractBuilder oBuilder = new ContractBuilder();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse oResponse = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartResponse();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);
                oRequest.PrimeSuitePatientId = int.Parse(barCodeText.PatientID);
                oRequest.DocumentTypeID = int.Parse(barCodeText.TabID);
                oRequest.DocumentName = GetDocumentNameBasedOnDocumentType(barCodeText.TabID);
                oRequest.DocumentSignerID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["providerSignerID"]);
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

                GreenwayDocuments.DocImporter.Document ocall = new GreenwayDocuments.DocImporter.Document();

                int countOfRecordsFromGreenway = GetDocumentCountGreenway(oRequest.PrimeSuitePatientId.ToString(),
                    oRequest.DocumentSignerID.ToString(),
                    oRequest.DocumentTypeID.ToString(), "Imported - " + oRequest.DocumentName);

                int countOfRecordsFromScannedDocuments = GetDocumentCountScannedDocuments(oRequest.PrimeSuitePatientId.ToString(),
                    oRequest.DocumentSignerID.ToString(),
                    oRequest.DocumentTypeID.ToString(), "Imported - " + oRequest.DocumentName);

                oResponse = ocall.SendPDF(oRequest, greenwayServer);
                Log("The document id is: " + oResponse.DocumentID.ToString());
                UpdateScannedDocuments(barCodeText.ID, oResponse.DocumentID.ToString());
                UpdateDocumentArchive(barCodeText.ArchiveID, "T");
                File.Delete(barCodeText.FullPath);

                CheckToSeeIfProblemWithRecords();
            }
            catch (Exception er)
            {
                LogErrors("Error is : " + er.ToString() + "PatienID: " + barCodeText.PatientID + " <br />"
                        + "Document Name: " + GetDocumentNameBasedOnDocumentType(barCodeText.TabID) + " <br />"
                        + "Document Type ID:    " + barCodeText.TabID + " <br />"
                        + "Page Count:    " + barCodeText.PageCount.ToString() + " <br />");
                string[] fileNameArray = barCodeText.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                UpdateDocumentArchive(barCodeText.ArchiveID, "F");
                File.Copy(barCodeText.FullPath, pathToErrorFolder + fileName, true);
                AddErrorDocument(pathToErrorFolder + fileName, barCodeText.User);
                return;
            }
        }