public Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest BuildDocumentImportChartRequest(string sBase64)
        {
            try
            {
                // declare new instnce of each object
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();

                oRequest.Credentials = new Greenway.PrimeSuite.DataContracts.API.Credentials();
                oRequest.Credentials.PrimeSuiteCredential = new Greenway.PrimeSuite.DataContracts.API.PrimeSuiteCredential();
                oRequest.Credentials.VendorCredential = new Greenway.PrimeSuite.DataContracts.API.VendorCredential();
                oRequest.Header = new Greenway.PrimeSuite.DataContracts.API.Header();

                // populate the Vendor Credentials
                oRequest.Header.PrimeSuiteUserID = 1038; // pull this from the table in the long run.

                oRequest.Base64Document = sBase64;
                oRequest.DocumentFileType = Greenway.PrimeSuite.DataContracts.Document.DocumentFileType.PDF;
                oRequest.DocumentStatus = Greenway.PrimeSuite.DataContracts.Document.DocumentImportDocumentStatusDocumentImportDocumentStatusEnum.Authenticated;

                return oRequest;
            }
            catch (Exception ex)
            {
                //do something
                throw ex;
            }
        }
        public Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest BuildDocumentImportChartRequest(string sBase64)
        {
            try
            {
     

                // declare new instnce of each object
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();

                oRequest.Credentials = new Greenway.PrimeSuite.DataContracts.API.Credentials();
                oRequest.Credentials.PrimeSuiteCredential = new Greenway.PrimeSuite.DataContracts.API.PrimeSuiteCredential();
                oRequest.Credentials.VendorCredential = new Greenway.PrimeSuite.DataContracts.API.VendorCredential();
                oRequest.Header = new Greenway.PrimeSuite.DataContracts.API.Header();

                // populate the PrimeSuite Credentials
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteSiteId = "asdf";
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserAlias = "";
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = "******";
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = "******";

                // populate the Vendor Credentials
                oRequest.Credentials.VendorCredential.VendorLogin = "******";
                oRequest.Credentials.VendorCredential.VendorPassword = "******";

                // populate the Vendor Credentials
                oRequest.Header.SourceSiteID = "";
                oRequest.Header.PrimeSuiteUserID = 1038; // pull this from the table in the long run.
                oRequest.Header.DestinationSiteID = "asdf";

                oRequest.Base64Document = sBase64;
                oRequest.DocumentDescription = " Imported by you";
                oRequest.DocumentFileType = Greenway.PrimeSuite.DataContracts.Document.DocumentFileType.PDF;
                oRequest.DocumentName = "imported pdf";
                oRequest.DocumentSignerID = 1038; // this needs to be admin or the user you create. Make sure in PS the user you create has all rights to sign and import.
                oRequest.DocumentStatus = Greenway.PrimeSuite.DataContracts.Document.DocumentImportDocumentStatusDocumentImportDocumentStatusEnum.Authenticated;
                oRequest.DocumentTypeID = 15; // you will need to look the DocumentType from PS.
                oRequest.FileCreateDate = DateTime.Now.ToString();
                oRequest.PrimeSuitePatientId = 18261;
                oRequest.VisitID = 0;
              

                return oRequest;
            }
            catch (Exception ex)
            {
                //do something
                throw ex;
            }
        }
        private static void ProcessBarcodedDocuments(ImportDocument newDocumentToImport)
        {
            PostOfficev2.API.DataAccess.DocumentRepository documentData = new PostOfficev2.API.DataAccess.DocumentRepository();
            CurrentDocuments currentDocument = documentData.GetCurrentDocumentPathPDF(Utility.GetUserName());
            newDocumentToImport.FullPath = currentDocument.PathToCurrentPDFDocument;

            int pdfPageCount = 0;
            Document pdfDocument = new Document(newDocumentToImport.FullPath);
            pdfPageCount = pdfDocument.Pages.Count;

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

                try
                {
                    inFile = new System.IO.FileStream(newDocumentToImport.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)
                {
                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    return;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    //LogErrors("Binary data array is null.");
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.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.Header = new Greenway.PrimeSuite.DataContracts.API.Header();

                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);

                oRequest.PrimeSuitePatientId = newDocumentToImport.PatientID;
                oRequest.DocumentTypeID = newDocumentToImport.DocTypeId;

                oRequest.DocumentName = "Lab Result -";
                oRequest.DocumentDescription = string.Empty;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

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

                oResponse = ocall.SendPDF(oRequest, greenwayServer);

                ArchiveDocument archiveDocument = new ArchiveDocument();

                string[] listOfDocumentsToArchive = currentDocument.CurrentDocumentList.Split('~');

                for (int i = 0; i < listOfDocumentsToArchive.Length - 1; i++)
                {
                    string[] documentName = listOfDocumentsToArchive[i].Split('\\');
                    archiveDocument.ArchiveTheDocument(documentName[documentName.Length - 1], listOfDocumentsToArchive[i], Utility.GetUserName());
                    File.Delete(listOfDocumentsToArchive[i]);
                }

                return;
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                return;
            }
        }
        // POST: api/ProcessErrorDocument
        public IHttpActionResult Post([FromBody]ScannedDocument barCodeText)
        {
            //string s1 = value.ArchiveID;
            DocumentTypeData documentTypeData = new DocumentTypeData();
            DocumentsData documentsData = new DocumentsData();
            string[] patientId = barCodeText.PatientID.Split(' ');
            string[] tabId = barCodeText.TabID.Split('~');
            barCodeText.ID = documentsData.GetDocumentIDFromPath(System.Configuration.ConfigurationManager.AppSettings["pathToWatchedFolder"] + barCodeText.FullPath);
            barCodeText.ArchiveID = documentsData.GetArchiveIDFromPath(System.Configuration.ConfigurationManager.AppSettings["pathToWatchedFolder"] + barCodeText.FullPath);

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

                try
                {
                    inFile = new System.IO.FileStream(System.Configuration.ConfigurationManager.AppSettings["pathToErrorFolder"] + 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)
                {
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    return BadRequest(er.ToString());
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException)
                {
                    string[] fileNameArray = barCodeText.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    return BadRequest();
                }

                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(patientId[0]);
                oRequest.DocumentTypeID = int.Parse(tabId[0]);
                oRequest.DocumentName = documentTypeData.GetDocumentNameBasedOnDocumentType(tabId[0]);
                oRequest.DocumentSignerID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["providerSignerID"]);
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = System.Configuration.ConfigurationManager.AppSettings["greenwayUserID"];
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = System.Configuration.ConfigurationManager.AppSettings["greenwayUserPassword"];
                EmployeeDesktop.API.Document ocall = new EmployeeDesktop.API.Document();
                oResponse = ocall.SendPDF(oRequest, System.Configuration.ConfigurationManager.AppSettings["greenwayServer"]);
                documentTypeData.UpdateScannedDocuments(barCodeText.ID, oResponse.DocumentID.ToString());
                documentTypeData.UpdateDocumentArchive(barCodeText.ArchiveID, "T");
                File.Delete(System.Configuration.ConfigurationManager.AppSettings["pathToErrorFolder"] + barCodeText.FullPath);
                return Ok();
            }
            catch (Exception er)
            {
                return BadRequest(er.ToString());
            }
        }
        private static OperationResult ProcessBarcodedDocuments(ImportDocument newDocumentToImport)
        {

            OperationResult operationResult = new OperationResult();

            Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Start Import");
            PostOffice.Api.DataAccess.DocumentRepository documentData = new PostOffice.Api.DataAccess.DocumentRepository();
            CurrentDocuments currentDocument = documentData.GetCurrentDocumentPathPdf(Utility.GetUserName());
            newDocumentToImport.FullPath = currentDocument.PathToCurrentPdfDocument;

            Logging.LogErrors(ConfigurationValues.ErrorLogPath, "Start Creating Base 64");
            try
            {
                System.IO.FileStream inFile;
                byte[] binaryData;

                try
                {
                    inFile = new System.IO.FileStream(newDocumentToImport.FullPath,
                                              System.IO.FileMode.Open,
                                              System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    inFile.Read(binaryData, 0,
                                         (int)inFile.Length);
                    inFile.Close();
                }
                catch (Exception er)
                {
                    Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }

                string base64String;
                try
                {
                    base64String =
                      System.Convert.ToBase64String(binaryData,
                                             0,
                                             binaryData.Length);
                }
                catch (System.ArgumentNullException er)
                {
                    //LogErrors("Binary data array is null.");
                    string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                    string fileName = fileNameArray[fileNameArray.Length - 1];
                    File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                    operationResult.Success = false;
                    operationResult.AddMessage(er.ToString());
                    return operationResult;
                }

                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "End Base 64");

                ContractBuilder oBuilder = new ContractBuilder();
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "1");
                Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest oRequest = new Greenway.PrimeSuite.DataContracts.Document.DocumentImportChartRequest();
                oRequest.Header = new Greenway.PrimeSuite.DataContracts.API.Header();
                oRequest = oBuilder.BuildDocumentImportChartRequest(base64String);

                oRequest.PrimeSuitePatientId = newDocumentToImport.PatientID;
                oRequest.DocumentTypeID = newDocumentToImport.DocTypeId;
                oRequest.DocumentName = newDocumentToImport.DocumentName;
                oRequest.DocumentDescription = newDocumentToImport.DocumentName + " " + newDocumentToImport.Initials;
                oRequest.DocumentSignerID = int.Parse(ConfigurationValues.ProviderSignerID);
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, "3");
                if (newDocumentToImport.ProviderId.Length > 0)
                {
                    oRequest.DocumentStatus = Greenway.PrimeSuite.DataContracts.Document.DocumentImportDocumentStatusDocumentImportDocumentStatusEnum.InProgress;
                    oRequest.DocumentSignerID = int.Parse(newDocumentToImport.ProviderId);
                }
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserName = greenwayUserID;
                oRequest.Credentials.PrimeSuiteCredential.PrimeSuiteUserPassword = greenwayUserPassword;

                PostOffice.Api.Document ocall = new PostOffice.Api.Document();

                ocall.SendPdf(oRequest, greenwayServer);
                ArchiveDocument archiveDocument = new ArchiveDocument();

                string[] listOfDocumentsToArchive = currentDocument.CurrentDocumentList.Split('~');
                for (int i = 0; i < listOfDocumentsToArchive.Length - 1; i++)
                {
                    try
                    {
                        string[] documentName = listOfDocumentsToArchive[i].Split('\\');
                        archiveDocument.ArchiveTheDocument(documentName[documentName.Length - 1], listOfDocumentsToArchive[i], Utility.GetUserName());
                        File.Delete(listOfDocumentsToArchive[i]);
                    }
                    catch (Exception er)
                    {
                        Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                        operationResult.Success = true;
                        operationResult.AddMessage("None");
                        return operationResult;
                    }
                }

                operationResult.Success = true;
                operationResult.AddMessage("None");
                return operationResult;
            }
            catch (Exception er)
            {
                Logging.LogErrors(ConfigurationValues.ErrorLogPath, er.ToString());
                string[] fileNameArray = newDocumentToImport.FullPath.Split('\\');
                string fileName = fileNameArray[fileNameArray.Length - 1];
                File.Copy(newDocumentToImport.FullPath, pathToErrorFolder + fileName, true);
                operationResult.Success = false;
                operationResult.AddMessage(er.ToString());
                return operationResult;
            }
        }
        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;
            }
        }