/// <summary>
 /// UpdateDocument
 /// </summary>
 /// <param name="document"></param>
 /// <param name="fileData"></param>
 /// <param name="approvalRequired"></param>
 private void UpdateDocument(Document document, byte[] fileData, bool approvalRequired)
 {
     Document oldDocument = eCollabroDbContext.Repository<Document>().Query().Include(inc => inc.FileObject).Filter(qry => qry.DocumentId.Equals(document.DocumentId)).Get().FirstOrDefault();
     if (oldDocument != null)
     {
         if (approvalRequired && oldDocument.ApprovalStatus.Equals(WorkflowConstants.ApprovedStatus)) // Save to Queue
         {
             document.ModifiedById = UserContextDetails.UserId;
             document.ModifiedOn = DateTime.UtcNow;
             document.ApprovalStatus = WorkflowConstants.ApprovalWaitingStatus;
             TempDocument tempDocument = Mapper.Map<Document, TempDocument>(document);
             if (fileData != null)
             {
                 tempDocument.DocumentFile = fileData;
             }
             _workflowManager.SaveToQueue<TempDocument>(tempDocument, ContextEnum.Document, document.DocumentId);
             _workflowManager.CreateWorkflowTask(ContextEnum.Document, document.DocumentId, "New Document [" + document.DocumentTitle + "] ", "Document Description : " + document.DocumentDescription);
         }
         else // Record is new and not in Queue
         {
             oldDocument.DocumentTitle = document.DocumentTitle;
             oldDocument.DocumentDescription = document.DocumentDescription;
             if (!string.IsNullOrEmpty(document.DocumentFileName))
                 oldDocument.DocumentFileName = document.DocumentFileName;
             if (fileData != null)
             {
                 if (oldDocument.FileObject != null)
                 {
                     oldDocument.FileObject.ModifiedById = UserContextDetails.UserId;
                     oldDocument.FileObject.ModifiedOn = DateTime.UtcNow;
                 }
                 else
                 {
                     oldDocument.FileObject = new FileObject();
                     oldDocument.FileObject.CreatedOn = DateTime.UtcNow;
                     oldDocument.FileObject.CreatedById = UserContextDetails.UserId;
                 }
                 oldDocument.FileObject.FileObjectData = fileData;
             }
             oldDocument.DocumentLibraryId = document.DocumentLibraryId;
             oldDocument.IsActive = document.IsActive;
             oldDocument.ModifiedById = UserContextDetails.UserId;
             oldDocument.ModifiedOn = DateTime.UtcNow;
             oldDocument.IsAnomynousAccess = document.IsAnomynousAccess;
             oldDocument.IsCommentsAllowed = document.IsCommentsAllowed;
             oldDocument.IsLikeAllowed = document.IsLikeAllowed;
             oldDocument.IsRatingAllowed = document.IsRatingAllowed;
             oldDocument.IsVotingAllowed = document.IsVotingAllowed;
             oldDocument.ApprovalStatus = document.ApprovalStatus;
             oldDocument.ApproveRejectDate = document.ApproveRejectDate;
             oldDocument.ApproveRejectById = document.ApproveRejectById;
             eCollabroDbContext.Save();
             if (approvalRequired) // approval required // create or update task
             {
                 _workflowManager.CreateWorkflowTask(ContextEnum.Document, document.DocumentId, "New Document [" + document.DocumentTitle + "] ", "Document Description : " + document.DocumentDescription);
             }
         }
     }
     else
     {
         throw new DBConcurrencyException();
     }
 }
        /// <summary>
        /// AddDocument
        /// </summary>
        /// <param name="document"></param>
        /// <param name="fileData"></param>
        /// <param name="approvalRequired"></param>
        private void AddDocument(Document document, byte[] fileData, bool approvalRequired)
        {
            document.CreatedById = UserContextDetails.UserId;
            document.CreatedOn = DateTime.UtcNow;
            if (approvalRequired)
                document.ApprovalStatus = WorkflowConstants.ApprovalWaitingStatus;

            if (fileData != null)
            {
                document.FileObject = new FileObject();
                document.FileObject.CreatedById = UserContextDetails.UserId;
                document.FileObject.CreatedOn = DateTime.UtcNow;
                document.FileObject.ContextId = (int)ContextEnum.Document;
                document.FileObject.FileObjectData = fileData;
            }
            eCollabroDbContext.Repository<Document>().Insert(document);
            eCollabroDbContext.Save();

            if (approvalRequired)
            {
                _workflowManager.CreateWorkflowTask(ContextEnum.Document, document.DocumentId, "New Document [" + document.DocumentTitle + "] ", "Document Description : " + document.DocumentDescription);
            }
        }
 /// <summary>
 /// MapDocumentQueueToDocument
 /// </summary>
 /// <param name="document"></param>
 /// <param name="documentinQueue"></param>
 private void MapDocumentQueueToDocument(Document document, TempDocument documentinQueue)
 {
     document.DocumentTitle = documentinQueue.DocumentTitle;
     document.DocumentDescription = documentinQueue.DocumentDescription;
     document.DocumentLibraryId = documentinQueue.DocumentLibraryId;
     if (!string.IsNullOrEmpty(documentinQueue.DocumentFileName))
         document.DocumentFileName = documentinQueue.DocumentFileName;
     if (documentinQueue.DocumentFile != null)
     {
         if (document.FileObject == null)
         {
             document.FileObject = new FileObject();
             document.FileObject.CreatedById = documentinQueue.CreatedById;
             document.FileObject.CreatedOn = documentinQueue.CreatedOn;
         }
         else
         {
             document.FileObject.ModifiedById = documentinQueue.ModifiedById = document.ModifiedById;
             document.FileObject.ModifiedOn = document.ModifiedOn;
         }
         document.FileObject.FileObjectData = documentinQueue.DocumentFile;
     }
     document.ModifiedOn = documentinQueue.ModifiedOn;
     document.ModifiedById = documentinQueue.ModifiedById;
     document.IsAnomynousAccess = documentinQueue.IsAnomynousAccess;
     document.IsCommentsAllowed = documentinQueue.IsCommentsAllowed;
     document.IsLikeAllowed = documentinQueue.IsLikeAllowed;
     document.IsRatingAllowed = documentinQueue.IsRatingAllowed;
     document.IsVotingAllowed = documentinQueue.IsVotingAllowed;
     document.IsActive = documentinQueue.IsActive;
     document.ApprovalStatus = documentinQueue.ApprovalStatus;
     document.ApproveRejectById = documentinQueue.ApproveRejectById;
     document.ApproveRejectDate = documentinQueue.ApproveRejectDate;
 }
        /// <summary>
        /// SaveDocument
        /// </summary>
        /// <param name="document"></param>
        /// <param name="fileData">file data if file need to be saved in DB</param>
        public void SaveDocument(Document document, byte[] fileData)
        {
            #region Check Permission

            List<PermissionEnum> userPermissions = _securityManager.GetUserFeaturePermissions(UserContextDetails.UserId, FeatureEnum.DocumentLibrary);
            if ((document.DocumentId.Equals(0) && !userPermissions.Contains(PermissionEnum.AddContent)) || (!document.DocumentId.Equals(0) && !userPermissions.Contains(PermissionEnum.EditContent)))
                throw new BusinessException(_coreValidationResourceManager.GetString(CoreValidationMessagesConstants.UnAuthorized), CoreValidationMessagesConstants.UnAuthorized);

            #endregion

            //check contentSetting
            SecurityManager securityManager = new SecurityManager();
            List<SiteContentSettingResult> siteContentSettingResults = securityManager.GetSiteFeatureSettings(FeatureEnum.DocumentLibrary);

            bool approvalRequired = siteContentSettingResults.Where(qry => qry.ContentSettingId.Equals((int)FeatureSettingEnum.ApprovalRequired)).FirstOrDefault().IsAssigned;
            //self approved in case approval not required
            if (!approvalRequired)
            {
                document.ApprovalStatus = WorkflowConstants.ApprovedStatus;
                document.ApproveRejectDate = DateTime.UtcNow;
                document.ApproveRejectById = UserContextDetails.UserId;
            }
            else
            {
                document.ApprovalStatus = WorkflowConstants.ApprovalWaitingStatus;
                document.ApproveRejectById = null;
                document.ApproveRejectDate = null;
            }

            if (document.DocumentId.Equals(0)) // New Document
            {
                AddDocument(document, fileData, approvalRequired);
            }
            else  // Update Document
            {
                UpdateDocument(document, fileData, approvalRequired);
            }
        }