/// <summary>
        /// Calculates the number of documents to be set for every review set
        /// For every review set in the list
        /// 1. Check if original document count for the review set is greater than calculated number of documents
        /// 2. If so, check if the document count need to be obtained from family duplicate grouping
        /// 3. If yes, then set family/ duplicate grouping count to the review set
        /// 4. If no, and it belongs to orphan group then also add the orphan group to the same review set
        /// 5. If no, and there is no orphan group then add the original count to the review set
        /// </summary>
        /// <param name="documentGroup"></param>
        /// <param name="reviewsetList"></param>
        /// <param name="useFamilyCount"></param>
        private void CalculateNumberOfDocuments(DocumentGroupById documentGroup, List<ReviewsetDetails> reviewsetList, bool useFamilyCount)
        {
            reviewsetList.ShouldNotBe(null);
            for (int i = 0; i < reviewsetList.Count; i++)
            {
                if (reviewsetList[i].OriginalNoOfDocs > reviewsetList[i].CalculatedNoOfDocs)
                {
                    if (useFamilyCount)
                    {
                        reviewsetList[i].CalculatedNoOfDocs += documentGroup.DocumentCount;
                    }
                    else
                    {
                        if (documentGroup != null && documentGroup.DocumentCount > 0)
                        {
                            //if there is orphan family, that can be splitted between review sets, calculated number of docs will increase by the difference between
                            //original docs and calculated number. Document group count has to be reduced by the same difference
                            if ((reviewsetList[i].CalculatedNoOfDocs + documentGroup.DocumentCount) > reviewsetList[i].OriginalNoOfDocs)
                            {
                                reviewsetList[i].CalculatedNoOfDocs += (reviewsetList[i].OriginalNoOfDocs - reviewsetList[i].CalculatedNoOfDocs);
                                documentGroup.DocumentCount -= (reviewsetList[i].OriginalNoOfDocs - reviewsetList[i].CalculatedNoOfDocs);
                                continue;
                            }
                            else
                            {
                                reviewsetList[i].CalculatedNoOfDocs += documentGroup.DocumentCount;
                                break;
                            }
                        }
                        else
                        {
                            reviewsetList[i].CalculatedNoOfDocs += reviewsetList[i].OriginalNoOfDocs;
                        }
                    }
                    if ((reviewsetList[i].CalculatedNoOfDocs > reviewsetList[i].OriginalNoOfDocs))
                    {
                        if (i + 1 < reviewsetList.Count)
                        {
                            reviewsetList[i + 1].OriginalNoOfDocs -= (reviewsetList[i].CalculatedNoOfDocs - reviewsetList[i].OriginalNoOfDocs);

                            //if the next set count becomes zero then pull from next set in the list
                            if ((reviewsetList[i + 1].OriginalNoOfDocs <= 0) && (i + 2 < reviewsetList.Count))
                            {
                                reviewsetList[i + 1].OriginalNoOfDocs = reviewsetList[i + 2].OriginalNoOfDocs;
                            }
                        }
                    }
                    if (useFamilyCount)
                    {
                        break;
                    }
                }
            }
        }
 /// <summary>
 /// Convert ReviewsetDocumentBEOs to BulkDocumentInfoBEOs
 /// </summary>
 /// <param name="reviewsetDocuments">List of ReviewsetDocumentBEOs</param>
 /// <returns>List of BulkDocumentInfoBEOs</returns>
 private List<BulkDocumentInfoBEO> ConvertToBulkDocumentInfoBEO(List<ReviewsetDocumentBEO> reviewsetDocuments)
 {
     reviewsetDocuments.ShouldNotBe(null);
     var currentUser = EVSessionManager.Get<UserSessionBEO>(Constants.UserSessionInfo);
     var bulkTagDocuments = new List<BulkDocumentInfoBEO>();
     reviewsetDocuments.ForEach(document => bulkTagDocuments.Add(new BulkDocumentInfoBEO
     {
         CreatedBy = currentUser != null ? currentUser.UserGUID : string.Empty,
         DocumentId = document.DocumentId,
         DCN = document.DCN,
         FamilyId = document.FamilyId,
         FromOriginalQuery = true
     }));
     return bulkTagDocuments;
 }
        protected void FetchDocumentFromDCB(int documentNumber,
            List<DocumentDetail> documentDetailList, FamiliesInfo familiesInfo, JobWorkerLog<DcbParserLogInfo> dcbParserLogEntry)
        {
            #region Precondition asserts
            documentDetailList.ShouldNotBe(null);
            dcbParserLogEntry.ShouldNotBe(null);
            #endregion
            RVWDocumentBEO evDocument = new RVWDocumentBEO();
            try
            {
                //Get the document from DcbFacade
                Document currentDcbDocument = DcbFacade.GetDocument(documentNumber);

                //Throw exception if GetDocument fails
                currentDcbDocument.ShouldNotBe(null);

                //Create the target EV document
                evDocument.DocumentId = Guid.NewGuid().ToString().Replace("-", "").ToUpper();
                dcbParserLogEntry.LogInfo.DocumentId = evDocument.DocumentId;
                evDocument.CollectionId = DcbOpticonJobBEO.TargetDatasetId;
                evDocument.MatterId = DcbOpticonJobBEO.MatterId;

                //Add the fields required for casemap
                RVWDocumentFieldBEO evDocumentAccessionNumField = new RVWDocumentFieldBEO
                                                   {
                                                       FieldId = Convert.ToInt32(DcbOpticonJobBEO.SysDocId),
                                                       FieldName = EVSystemFields.DcbId,
                                                       IsSystemField = true,
                                                       IsRequired = true,
                                                       FieldValue = Convert.ToString(currentDcbDocument.UUID, CultureInfo.InvariantCulture)
                                                   };
                evDocument.FieldList.Add(evDocumentAccessionNumField);
                evDocument.FieldList.Add(_evDocumentSysImportTypeField);

                //Set the fields from field mapping except content field
                foreach (FieldMapBEO fieldMap in DcbOpticonJobBEO.FieldMappings)
                {
                    Field dcbField = currentDcbDocument.FieldItems.Find(o => (o.Code == fieldMap.SourceFieldID));

                    //Profile fieldmapping has duplicates
                    RVWDocumentFieldBEO evDocumentFieldBEO = evDocument.FieldList.Find(o => o.FieldId.Equals(fieldMap.DatasetFieldID));
                    if ((null != dcbField) && (evDocumentFieldBEO == null) && (fieldMap.DatasetFieldID != _contentFieldId))
                    {
                        RVWDocumentFieldBEO evDocuemtnField = new RVWDocumentFieldBEO
                                                                  {
                                                                      FieldId = fieldMap.DatasetFieldID,
                                                                      FieldName = fieldMap.DatasetFieldName
                                                                  };


                        FieldBEO evfieldDef = _dataset.DatasetFieldList.Find(o => o.ID == evDocuemtnField.FieldId);
                        evDocuemtnField.FieldValue = evfieldDef.FieldType.DataTypeId == Constants.DateDataType
                                                         ? GetDateFiedlValue(dcbField, dcbParserLogEntry)
                                                         : Regex.Replace(dcbField.Value, "\r\n", "\n");
                        evDocument.FieldList.Add(evDocuemtnField);
                    }
                }

                //Separate logic for content
                StringBuilder sbContent = new StringBuilder();
                if (DcbOpticonJobBEO.ContentFields != null)
                {
                    foreach (string contentfield in DcbOpticonJobBEO.ContentFields.Field)
                    {
                        Field dcbContentField = currentDcbDocument.FieldItems.Find(o => (o.Name.Equals(contentfield)));
                        if (null != dcbContentField)
                        {
                            sbContent.Append(dcbContentField.Value);
                        }
                    }
                }
                string text = sbContent.ToString().Replace("\r\n", "\n");
                //evDocument.DocumentBinary.Content = Regex.Replace(sbContent.ToString(), "\r\n", "\n");

                if (!DumpTextToFile(evDocument, text, dcbParserLogEntry))
                {
                    return;
                }

                //Set the native file path if selected
                evDocument.NativeFilePath = GetNativeFilePath(currentDcbDocument);

                if (!String.IsNullOrEmpty(evDocument.NativeFilePath) && File.Exists(evDocument.NativeFilePath))
                {
                    FileInfo fileInfo = new FileInfo(evDocument.NativeFilePath);
                    //Tracer.Trace("DcbParcer located native document {0} for DocumentId = {1} and the file length is {2}",
                    //    evDocument.NativeFilePath, evDocument.DocumentId, fileInfo.Length);
                    if (fileInfo.Length > 0)
                    {
                        evDocument.FileSize = (int)Math.Ceiling(fileInfo.Length / 1024.0);
                    }
                    else
                    {
                        evDocument.FileSize = 0;
                    }

                    evDocument.MD5HashValue = DocumentHashHelper.GetMD5HashValue(evDocument.NativeFilePath);
                    evDocument.SHAHashValue = DocumentHashHelper.GetSHAHashValue(evDocument.NativeFilePath);
                }

                //Set the MIME type
                string extn = string.Empty;
                string newExtn = string.Empty;
                extn = Path.GetExtension(evDocument.NativeFilePath);
                if (!String.IsNullOrEmpty(extn))
                    newExtn = extn.Remove(0, 1);
                evDocument.MimeType = GetMimeType(newExtn);
                evDocument.FileExtension = extn;

                string createdByGuid = String.Empty;
                if (null != ProfileBEO && null != ProfileBEO.CreatedBy)
                {
                    createdByGuid = ProfileBEO.CreatedBy;
                }
                evDocument.CreatedBy = createdByGuid;
                evDocument.ModifiedBy = createdByGuid;

                if (File.Exists(evDocument.NativeFilePath))
                {
                    //Calculating size of file in KB
                    FileInfo fileInfo = new FileInfo(evDocument.NativeFilePath);
                    evDocument.FileSize = (int)Math.Ceiling(fileInfo.Length / Constants.KBConversionConstant);

                    if (evDocument.DocumentBinary == null) { evDocument.DocumentBinary = new RVWDocumentBinaryBEO(); }
                    RVWExternalFileBEO nativeFile = new RVWExternalFileBEO
                    {
                        Type = NATIVE_FILE_TYPE,
                        Path = evDocument.NativeFilePath
                    };
                    evDocument.DocumentBinary.FileList.Add(nativeFile);
                }

                DocumentDetail documentDetail = new DocumentDetail
                                                    {
                                                        // CorrId is the same as TaskId and it is 1 based.
                                                        CorrelationId = checked(documentNumber + 1).ToString(CultureInfo.InvariantCulture),
                                                        IsNewDocument = true,
                                                        docType = DocumentsetType.NativeSet,
                                                        document = evDocument
                                                    };
                documentDetailList.Add(documentDetail);

                //Add Tags
                if (DcbOpticonJobBEO.IncludeTags && null != currentDcbDocument.TagItems && currentDcbDocument.TagItems.Count > 0)
                {
                    if (null == documentDetail.DcbTags)
                    {
                        documentDetail.DcbTags = new List<DcbTags>();
                    }
                    DcbDocumentTags dcbDocumentTags = new DcbDocumentTags
                                                          {
                        compositeTagNames = currentDcbDocument.TagItems,
                        DatasetId = DcbOpticonJobBEO.TargetDatasetId,
                        MatterId = DcbOpticonJobBEO.MatterId,
                        DocumentId = evDocument.DocumentId
                    };
                    documentDetail.DcbTags.Add(dcbDocumentTags);
                }

                // Add notes
                AddComments(documentDetail, evDocument, currentDcbDocument);

                //Add Images
                if (DcbOpticonJobBEO.ImportImages)
                {
                    RVWDocumentBEO images = ImportDocumentImages(evDocument.DocumentId, currentDcbDocument);
                    if (null != images)
                    {
                        DocumentDetail imageDocumentDetail = new DocumentDetail
                                                                 {
                                                                     // CorrId is the same as TaskId and it is 1 based.
                                                                     CorrelationId = checked(documentNumber + 1).ToString(CultureInfo.InvariantCulture),
                                                                     IsNewDocument = true,
                                                                     docType = DocumentsetType.ImageSet,
                                                                     document = images
                                                                 };
                        documentDetailList.Add(imageDocumentDetail);
                        dcbParserLogEntry.LogInfo.AddedImages = images.DocumentBinary.FileList.Count;
                    }

                    //Add Redlines
                    //ImportDocumentRedlines();
                }

                //Add Document Relation
                if (DcbOpticonJobBEO.IsImportFamilies)
                {
                    ImportDocumentRelationship(evDocument.DocumentId, currentDcbDocument, familiesInfo);
                }
                #region Postcondition asserts
                documentDetailList.ShouldNotBe(null);
                #endregion
            }
            catch (Exception ex)
            {
                //TaskLogInfo.AddParameters(Constants.ErrorDoAtomicWork + "<br/>" + ex.Message);
                //TaskLogInfo.StackTrace = ex.Source + "<br/>" + ex.Message + "<br/>" + ex.StackTrace;
                //TaskLogInfo.IsError = true;

                ex.Trace().Swallow();
                dcbParserLogEntry.Success = false;
                if (ex.ToUserString().Contains(Constants.DiskFullErrorMessage))
                {
                    dcbParserLogEntry.LogInfo.Message = "There is not enough space on the disk";
                    throw;
                }
                else
                {
                    dcbParserLogEntry.LogInfo.Message = ex.ToUserString();
                }
            }
        }
        /// <summary>
        /// Tag the documents with not reviewed tag
        /// </summary>
        /// <param name="reviewsetRecord">Reviewset Record</param>
        /// <param name="tempList">List of documents added to reviewset</param>
        private void TagDocumentsNotReviewed(DocumentRecordCollection reviewsetRecord,
            List<ReviewsetDocumentBEO> tempList)
        {
            reviewsetRecord.ShouldNotBe(null);
            tempList.ShouldNotBe(null);
            var matterId = reviewsetRecord.ReviewsetDetails.MatterId.ToString(CultureInfo.InvariantCulture);
            var collectionId = reviewsetRecord.ReviewsetDetails.CollectionId;

            //var currentUser = EVSessionManager.Get<UserSessionBEO>(Constants.UserSessionInfo);
            var documentTagObjects = new List<DocumentTagBEO>();

            var _binderDetail = BinderVaultManagerInstance.GetBinderSpecificDetails(matterId,
                reviewsetRecord.ReviewsetDetails.BinderId);
            _binderDetail.ShouldNotBe(null);
            _binderDetail.NotReviewedTagId.ShouldBeGreaterThan(0);
            _binderDetail.ReviewedTagId.ShouldBeGreaterThan(0);
            reviewsetRecord.ReviewsetDetails.NotReviewedTagId = _binderDetail.NotReviewedTagId;
            reviewsetRecord.ReviewsetDetails.ReviewedTagId = _binderDetail.ReviewedTagId;


            var docsInfo = ConvertToBulkDocumentInfoBEO(tempList);

            //dictionary to hold list of documents to update
            var documentList = docsInfo.ToDictionary(a => a.DocumentId, a => a);

            //get Effective Tags for the give tag
            var effectiveTagList =
                BulkTagBO.GetEfectiveTags(matterId, new Guid(collectionId), _binderDetail.BinderId,
                    _binderDetail.NotReviewedTagId, Constants.One).ToList();

            var currentState = new List<DocumentTagBEO>();
            var newState = new List<DocumentTagBEO>();

            //get the list of tags with current status for the documents to be tagged
            currentState.AddRange(DocumentVaultManager.GetDocumentTags(matterId, new Guid(collectionId),
                documentList.Keys.ToList()));
            //for every tag from the effective list of tags to be updated
            foreach (var tag in effectiveTagList)
            {
                newState.AddRange(
                    documentList.Values.Select(
                        x =>
                            new DocumentTagBEO
                            {
                                TagId = tag.TagId,
                                Status = tag.TagState,
                                DocumentId = x.DocumentId,
                                DCN = x.DCN,
                                TagTypeSpecifier = tag.type,
                                TagName = tag.TagName
                            }));
            }

            //get all the documents that is not part of current documents list and update to vault
            var vaultChanges = newState.Except(currentState, new DocumentTagComparer()).Distinct().ToList();
            //Remove the entries that are already untagged but are marked for untagging
            vaultChanges.RemoveAll(document => document.Status == Constants.Untagged &&
                                               currentState.Exists(
                                                   y =>
                                                       y.TagId == document.TagId &&
                                                       String.Compare(y.DocumentId, document.DocumentId,
                                                           StringComparison.OrdinalIgnoreCase) == 0) == false);

            /* The following statement will take the untagged documents in the new state and 
             * make it as list of tagged documents so that except operator in the next statement 
             * remove all the tagged documents in the current state that have 
             * been untagged now.*/
            var newstateUntaggedDocuments = newState.Where(t => t.Status != Constants.Tagged).Select(t =>
                new DocumentTagBEO {TagId = t.TagId, DocumentId = t.DocumentId, Status = Constants.Tagged}).ToList();

            //Determine Tag changes to update in search index..This has to be supplied for IndexTaggerWorker to take care in search-engine
            var indexChanges = reviewsetRecord.DocumentTags = currentState.FindAll(currentStateOfDocument =>
                (newstateUntaggedDocuments.Find(newStateOfDocument =>
                    currentStateOfDocument.TagId == newStateOfDocument.TagId &&
                    currentStateOfDocument.DocumentId == newStateOfDocument.DocumentId
                    && currentStateOfDocument.Status == newStateOfDocument.Status) == null)).Union(newState).
                Distinct(new DocumentTagBEO()).ToList();

            DocumentVaultManager.UpdateDocumentTags(matterId, new Guid(collectionId), _binderDetail.BinderId,
                vaultChanges, indexChanges, _createdBy);
            Send(reviewsetRecord);
        }