/// <summary>
        /// Save Document to document server and also save details to the database
        /// This method requires to work with actual files, the reason is
        /// If the existing document is not encrypted, and if user updates the document details by setting
        /// Encryption flag to true, then the actual document is to be encrypted.
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="docSearchItem"></param>
        /// <returns></returns>
        public DocumentReturnValue EditMatterDocumentDetails(Guid logonId, DocumentSearchItem docSearchItem)
        {
            DocumentReturnValue returnValue = new DocumentReturnValue();
            string errorMessage = string.Empty;

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                            // Check permission
                            if (!UserSecuritySettings.GetUserSecuitySettings(9))
                                throw new Exception("You do not have sufficient permissions to carry out this request");
                            break;
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            throw new Exception("Access denied");
                        default:
                            throw new Exception("Access denied");
                    }

                    returnValue.Success = false;

                    SrvDocument srvDoc = new SrvDocument();
                    srvDoc.Id = docSearchItem.Id;
                    // If document details is edited then Load default values in SrvDocument
                    srvDoc.Load(docSearchItem.Id);

                    srvDoc.DocumentModuleId = docSearchItem.ProjectId;
                    byte[] bytesToUpdate = new byte[0];
                    srvDoc.BytesToUpdate = bytesToUpdate;

                    // Hardcoded
                    srvDoc.Type = DmEnums.DmPMSDocType.Project;
                    srvDoc.ApplicationId = (int)DataConstants.Application.PMS;
                    srvDoc.FolderName = "Documents";

                    srvDoc.TypeId = docSearchItem.TypeId;
                    srvDoc.Description = docSearchItem.FileDescription;
                    srvDoc.Notes = docSearchItem.Notes;

                    srvDoc.IsPublic=docSearchItem.IsPublic;
                    srvDoc.IsUsedVersioning = docSearchItem.UseVersioning;
                    srvDoc.CreationDate = docSearchItem.CreatedDate;
                    srvDoc.IsLocked = docSearchItem.IsLocked;
                    srvDoc.FeeEarnerId = docSearchItem.FeeEarnerId;

                    // Don't set it while Editing Document
                    srvDoc.VolumeId = 0;

                    #region File Encryption
                    if (!srvDoc.IsEncrypted && docSearchItem.IsEncrypted)
                    {
                        // Encrypt the file and save to the document server
                        if (!string.IsNullOrEmpty(srvDoc.VolumeLocation))
                        {
                            DmEncryptionWeb dmEncryption = new DmEncryptionWeb();
                            string tempDocPath = string.Empty;

                            string savedDocPath = Path.Combine(srvDoc.VolumeLocation, srvDoc.FileName);
                            tempDocPath = Path.Combine(Path.GetTempPath(), Path.GetFileName(srvDoc.FileName));

                            dmEncryption.CopyAndEncryptFile(savedDocPath, tempDocPath, false);

                            File.Copy(tempDocPath, savedDocPath, true);
                            File.Delete(tempDocPath);
                        }
                        else
                        {
                            throw new Exception("Upload document failed on the server.");
                        }
                        srvDoc.IsEncrypted = docSearchItem.IsEncrypted;
                    }
                    #endregion

                    returnValue.Success = srvDoc.Save(out errorMessage);

                    docSearchItem.Id = srvDoc.Id;
                    docSearchItem.FileName = srvDoc.FileName;

                    returnValue.Document = docSearchItem;
                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                returnValue.Success = false;
                returnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                returnValue.Success = false;
                returnValue.Message = ex.Message;
            }

            return returnValue;
        }
        /// <summary>
        /// Document Upload Complete
        /// </summary>
        /// <param name="logonId"></param>
        /// <param name="TransferId"></param>
        /// <param name="docSearchItem"></param>
        /// <returns></returns>
        public DocumentReturnValue DocumentUploadComplete(Guid logonId, Guid TransferId)
        {
            DocumentReturnValue ReturnValue = new DocumentReturnValue();

            try
            {
                // Get the logged on user from the current logons and add their
                // ApplicationSettings the list of concurrent sessions.
                Host.LoadLoggedOnUser(logonId);

                try
                {
                    Functions.RestrictRekoopIntegrationUser(UserInformation.Instance.DbUid);
                    switch (UserInformation.Instance.UserType)
                    {
                        case DataConstants.UserType.Staff:
                        case DataConstants.UserType.Client:
                        case DataConstants.UserType.ThirdParty:
                            // Can do everything
                            break;
                        default:
                            throw new Exception("Access denied");
                    }

                    FileUploadInfo FileUploadInfo = FileTransfer.UploadComplete(logonId, TransferId);
                    DocumentStorageData DocumentStorageData = Host.GetDocumentStorageData(logonId);

                    SrvDocument srvDoc = new SrvDocument();

                    DocumentSearchItem docSearchItem = (DocumentSearchItem)DocumentStorageData.DocDetails;

                    if (!DocumentStorageData.ExitingDocument)
                    {
                        docSearchItem.FileName = FileUploadInfo.TempFileName;

                        string errorMessage = string.Empty;
                        string tempUploadFilePath = string.Empty;
                        string tempFileName = string.Empty;

                        #region File Encryption & Saving Document to its original location
                        try
                        {
                            if (!string.IsNullOrEmpty(DocumentStorageData.VolumeLocation))
                            {
                                //If it wants to be encrypted, encrypt and copy to a temp directory
                                if (docSearchItem.IsEncrypted)
                                {
                                    tempFileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(docSearchItem.FileName));

                                    docSearchItem.FileName = tempFileName;
                                }

                                tempUploadFilePath = docSearchItem.FileName;

                                FileInfo file = new FileInfo(docSearchItem.FileName);
                                if (File.Exists(DocumentStorageData.VolumeLocation) == false)
                                {
                                    // Attempt to copy the file.
                                    file.CopyTo(DocumentStorageData.VolumeLocation, true);
                                }

                                // Get New File Name
                                docSearchItem.FileName = DocumentStorageData.FileName;

                                // Encrypt the file and save to the document server
                                if (docSearchItem.IsEncrypted)
                                {
                                    string decryptFileName = DocumentStorageData.VolumeLocation;

                                    DmEncryptionWeb dmEncryption = new DmEncryptionWeb();
                                    dmEncryption.CopyAndEncryptFile(decryptFileName, tempFileName, false);

                                    // Move encrypted File to the original Documenet Server Path
                                    File.Copy(tempFileName, DocumentStorageData.VolumeLocation, true);
                                    File.Delete(tempFileName);
                                }
                            }
                            else
                            {
                                throw new Exception("Upload document failed on the server.");
                            }
                        }
                        finally
                        {
                            if (!string.IsNullOrEmpty(tempUploadFilePath))
                            {
                                // Delete the temp file which was created when uploaded from client
                                if (File.Exists(tempUploadFilePath))
                                {
                                    File.Delete(tempUploadFilePath);
                                }
                            }
                        }
                        #endregion

                        #region Save Document Details
                        if (docSearchItem.ProjectId != Guid.Empty)
                        {
                            srvDoc.DocumentModuleId = docSearchItem.ProjectId;
                            srvDoc.Type = DmEnums.DmPMSDocType.Project;
                        }
                        else if (docSearchItem.MemberId != Guid.Empty)
                        {
                            srvDoc.DocumentModuleId = docSearchItem.MemberId;
                            srvDoc.Type = DmEnums.DmPMSDocType.Member;
                        }
                        else if (docSearchItem.OrganisationId != Guid.Empty)
                        {
                            srvDoc.DocumentModuleId = docSearchItem.OrganisationId;
                            srvDoc.Type = DmEnums.DmPMSDocType.Org;
                        }

                        byte[] bytesToUpdate = new byte[0];
                        srvDoc.BytesToUpdate = bytesToUpdate;

                        // Hardcoded
                        srvDoc.ApplicationId = (int)DataConstants.Application.PMS;
                        if (string.IsNullOrEmpty(docSearchItem.FolderName))
                        {
                            srvDoc.FolderName = "Documents";
                        }
                        else
                        {
                            srvDoc.FolderName = docSearchItem.FolderName;
                        }

                        srvDoc.TypeId = docSearchItem.TypeId;
                        srvDoc.Description = docSearchItem.FileDescription;
                        srvDoc.Notes = docSearchItem.Notes;

                        srvDoc.IsUsedVersioning = docSearchItem.UseVersioning;
                        srvDoc.CreationDate = docSearchItem.CreatedDate;
                        srvDoc.IsLocked = docSearchItem.IsLocked;
                        srvDoc.FeeEarnerId = docSearchItem.FeeEarnerId;

                        srvDoc.IsEncrypted = docSearchItem.IsEncrypted;
                        srvDoc.FileName = docSearchItem.FileName;
                        srvDoc.VolumeId = DocumentStorageData.VolumeId;
                        srvDoc.VolumeLocation = DocumentStorageData.VolumeLocation;

                        //Document must become public for Clients and Third Party users
                        if (UserInformation.Instance.UserType == DataConstants.UserType.Client || UserInformation.Instance.UserType == DataConstants.UserType.ThirdParty)
                            srvDoc.IsPublic = true;
                        else
                            srvDoc.IsPublic = docSearchItem.IsPublic;

                        ReturnValue.Success = srvDoc.Save(out errorMessage);

                        if (ReturnValue.Success)
                        {
                            docSearchItem.Id = srvDoc.Id;
                            docSearchItem.FileName = srvDoc.FileName;

                            srvDoc.AddDocumentStatus(srvDoc.Id, DataConstants.DocStatus.ReadyforReview, string.Empty);
                        }
                        #endregion

                        // Ensure the volume location of the saved file is returned
                        docSearchItem.VolumeLocation = DocumentStorageData.VolumeLocation;

                        ReturnValue.Document = docSearchItem;
                    }
                    else
                    {
                        docSearchItem.FileName = FileUploadInfo.TempFileName;
                        ReturnValue = this.DocumentReuploaded(docSearchItem);
                    }

                    FileTransfer.UploadReset(logonId, TransferId);

                }
                finally
                {
                    // Remove the logged on user's ApplicationSettings from the
                    // list of concurrent sessions
                    Host.UnloadLoggedOnUser();
                }
            }
            catch (System.Data.SqlClient.SqlException)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = Functions.SQLErrorMessage;
            }
            catch (Exception ex)
            {
                ReturnValue.Success = false;
                ReturnValue.Message = ex.Message;
            }

            return ReturnValue;
        }