/// <summary>
 /// Updates the reviewset status to Active in DB
 /// </summary>
 /// <param name="reviewsetRecord"></param>
 private void UpdateReviewSetStatus(ReviewsetRecord reviewsetRecord)
 {
     var reviewsetEntity = ConvertToReviewsetBusinessEntity(reviewsetRecord);
     reviewsetEntity.IsAuditable = false;
     using (var transScope = new EVTransactionScope(TransactionScopeOption.Suppress))
     {
         //if all the documents are added successfully then update the status of review set
         ReviewSetBO.UpdateReviewSet(reviewsetEntity, false);
     }
 }
        /// <summary>
        /// This method converts the ReviewsetSearchRecord type to DocumentRecordCollection
        /// </summary>
        /// <param name="searchRecord">ReviewsetSearchRecord</param>
        /// <param name="documentRecordCollection">ReviewsetSearchRecord</param>
        /// <returns></returns>
        private void ConvertReviewsetSearchRecordToDocumentRecordCollection
            (ReviewsetSearchRecord searchRecord, out DocumentRecordCollection documentRecordCollection)
        {
            documentRecordCollection = new DocumentRecordCollection();
            var reviewsetRecord = new ReviewsetRecord
            {
                Activity = searchRecord.ReviewsetDetails.Activity,
                CreatedBy = searchRecord.ReviewsetDetails.CreatedBy,
                DatasetId = searchRecord.ReviewsetDetails.DatasetId,
                MatterId = searchRecord.QueryEntity.QueryObject.MatterId,
                ReviewSetId = searchRecord.ReviewsetDetails.ReviewSetId,
                BinderFolderId = searchRecord.ReviewsetDetails.BinderFolderId,
                BinderId = searchRecord.ReviewsetDetails.BinderId,
                BinderName = searchRecord.ReviewsetDetails.BinderName,
                ReviewSetName = searchRecord.ReviewsetDetails.ReviewSetName,
                ReviewSetDescription = searchRecord.ReviewsetDetails.ReviewSetDescription,
                DueDate = searchRecord.ReviewsetDetails.DueDate,
                KeepDuplicatesTogether = searchRecord.ReviewsetDetails.KeepDuplicatesTogether,
                KeepFamilyTogether = searchRecord.ReviewsetDetails.KeepFamilyTogether,
                ReviewSetGroup = searchRecord.ReviewsetDetails.ReviewSetName,
                ReviewSetLogic = searchRecord.ReviewsetDetails.ReviewSetLogic,
                SearchQuery = searchRecord.ReviewsetDetails.SearchQuery,
                SplittingOption = searchRecord.ReviewsetDetails.SplittingOption,
                StartDate = searchRecord.ReviewsetDetails.StartDate,
                NumberOfDocuments = searchRecord.ReviewsetDetails.NumberOfDocuments,
                NumberOfReviewedDocs = searchRecord.ReviewsetDetails.NumberOfReviewedDocs,
                NumberOfDocumentsPerSet = searchRecord.ReviewsetDetails.NumberOfDocumentsPerSet,
                NumberOfReviewSets = searchRecord.ReviewsetDetails.NumberOfReviewSets,
                CollectionId = searchRecord.ReviewsetDetails.CollectionId,
                AssignTo = searchRecord.ReviewsetDetails.AssignTo
            };

            reviewsetRecord.ReviewSetUserList.AddRange(searchRecord.ReviewsetDetails.ReviewSetUserList);
            reviewsetRecord.DsTags.AddRange(searchRecord.ReviewsetDetails.DsTags);
            documentRecordCollection.ReviewsetDetails = reviewsetRecord;
            documentRecordCollection.TotalDocumentCount = searchRecord.TotalDocumentCount;

        }
 /// <summary>
 /// Updates the reviewset status to Active in DB
 /// </summary>
 /// <param name="reviewsetRecord"></param>
 /// <param name="status"></param>
 /// <param name="isAuditable"></param>
 private void UpdateReviewSetStatus(ReviewsetRecord reviewsetRecord, int status,bool isAuditable=true)
 {
     ReviewsetDetailsBEO reviewsetEntity = ConvertToReviewsetBusinessEntity(reviewsetRecord, status);
     reviewsetEntity.IsAuditable = isAuditable;
     using (EVTransactionScope transScope = new EVTransactionScope(TransactionScopeOption.Suppress))
     {
         //if all the documents are added successfully then update the status of review set
         ReviewSetBO.UpdateReviewSet(reviewsetEntity, false);
     }
 }
        /// <summary>
        /// converts to review set business entity to update the status
        /// </summary>
        /// <param name="reviewsetRecord"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private ReviewsetDetailsBEO ConvertToReviewsetBusinessEntity(ReviewsetRecord reviewsetRecord, int status)
        {
            ReviewsetDetailsBEO returnReviewset = new ReviewsetDetailsBEO();
            returnReviewset.ReviewSetId = reviewsetRecord.ReviewSetId;
            returnReviewset.ReviewSetName = reviewsetRecord.ReviewSetName;
            returnReviewset.StatusId = status;
            returnReviewset.ModifiedBy = reviewsetRecord.CreatedBy;
            returnReviewset.DatasetId = reviewsetRecord.DatasetId;

            returnReviewset.Description = reviewsetRecord.ReviewSetDescription;
            returnReviewset.DueDate = reviewsetRecord.DueDate;
            returnReviewset.KeepDuplicates = reviewsetRecord.KeepDuplicatesTogether;
            returnReviewset.KeepFamily = reviewsetRecord.KeepFamilyTogether;
            returnReviewset.ReviewSetGroup = reviewsetRecord.ReviewSetGroup;
            returnReviewset.ReviewSetLogic = reviewsetRecord.ReviewSetLogic;
            returnReviewset.SearchQuery = reviewsetRecord.SearchQuery;
            returnReviewset.SplittingOption = reviewsetRecord.SplittingOption;
            returnReviewset.StartDate = reviewsetRecord.StartDate;

            returnReviewset.NumberOfDocuments = reviewsetRecord.NumberOfDocuments;
            returnReviewset.NumberOfReviewedDocs = reviewsetRecord.NumberOfReviewedDocs;

            return returnReviewset;

        }
 /// <summary>
 /// This method converts the CreateReviewSetJobBEO to ReviewsetRecord
 /// </summary>
 /// <param name="reviewSetJobBEO">CreateReviewSetJobBEO/param>
 /// <returns>ReviewsetRecord</returns>
 private ReviewsetRecord ConvertToReviewSetRecord(CreateReviewSetJobBEO reviewSetJobBEO)
 {
     ReviewsetRecord rSetRecord = new ReviewsetRecord
     {
         Activity = reviewSetJobBEO.Activity,
         DatasetId = reviewSetJobBEO.DatasetId,
         DueDate = reviewSetJobBEO.DueDate,
         KeepDuplicatesTogether = reviewSetJobBEO.KeepDuplicates,
         KeepFamilyTogether = reviewSetJobBEO.KeepFamily,
         NumberOfDocuments = reviewSetJobBEO.NumberOfDocuments,
         NumberOfDocumentsPerSet = reviewSetJobBEO.NumberOfDocumentsPerSet,
         NumberOfReviewedDocs = reviewSetJobBEO.NumberOfReviewedDocs,
         NumberOfReviewSets = reviewSetJobBEO.NumberOfReviewSets,
         ReviewSetDescription = reviewSetJobBEO.ReviewSetDescription,
         ReviewSetGroup = reviewSetJobBEO.ReviewSetGroup,
         ReviewSetId = reviewSetJobBEO.ReviewSetId,
         BinderFolderId = reviewSetJobBEO.BinderFolderId,
         BinderId = _binderEntity.BinderId,
         BinderName = _binderEntity.BinderName,
         ReviewSetLogic = reviewSetJobBEO.ReviewSetLogic,
         ReviewSetName = reviewSetJobBEO.ReviewSetName,
         SplittingOption = reviewSetJobBEO.SplittingOption,
         StartDate = reviewSetJobBEO.StartDate,
         StatusId = reviewSetJobBEO.StatusId,
         CreatedBy = reviewSetJobBEO.JobScheduleCreatedBy,
         CollectionId = reviewSetJobBEO.CollectionId
     };
     rSetRecord.DsTags.AddRange(reviewSetJobBEO.DsTags);
     return rSetRecord;
 }
        /// <summary>
        /// Absorb the boot parameters, deserialize and pass on the messages to the Search Worker
        /// </summary>
        public void DoBeginWork(string bootParameter)
        {
            bootParameter.ShouldNotBeEmpty();
            // Deserialize and determine the boot object
            _bootObject = GetBootObject(bootParameter);

            // Assert condition to check for jobscheduled by
            _bootObject.JobScheduleCreatedBy.ShouldNotBeEmpty();
            _bootObject.BinderFolderId.ShouldNotBe(0);

            // Get Dataset Details to know about the Collection id and the Matter ID details
            _datasetEntity = DataSetBO.GetDataSetDetailForDataSetId(_bootObject.datasetId);
            //Assert condition to check for dataset details
            _datasetEntity.ShouldNotBe(null);

            _binderEntity = BinderBO.GetBinderDetails(_bootObject.BinderFolderId.ToString());
            _binderEntity.ShouldNotBe(null);

            _reviewSetRecord = ConvertToReviewSetRecord(_bootObject);

            // Construct the document query entity to determine the total documents
            _docQueryEntity = GetQueryEntity(_bootObject, _datasetEntity, 0, 1, null);

            // Mock the user session
            MockSession();

            _docQueryEntity.TransactionName = _docQueryEntity.QueryObject.TransactionName = "ReviewsetStartupWorker - DoBeginWork (GetCount)";

            var reviewsetLogic = _reviewSetRecord.ReviewSetLogic.ToLower();
            if (reviewsetLogic == "all" || reviewsetLogic == "tag")
            {
                var searchQuery = !string.IsNullOrEmpty(_bootObject.SearchQuery)? _bootObject.SearchQuery.Replace("\"", ""): string.Empty;
                _totalDocumentCount = DocumentBO.GetDocumentCountForCreateReviewsetJob(_datasetEntity.Matter.FolderID, _datasetEntity.CollectionId,
                    reviewsetLogic, searchQuery);
            }
            else
            {
                // Retrieve the total documents qualified
                _totalDocumentCount = ReviewerSearchInstance.GetDocumentCount(_docQueryEntity.QueryObject);
            }

            Tracer.Info("Reviewset Startup Worker : {0} matching documents determined for the requested query", _totalDocumentCount);
            if (_totalDocumentCount < 1)
            {
                var message = String.Format("Search server does not return any documents for the reviewset '{0}'", _reviewSetRecord.ReviewSetName);
                throw new ApplicationException(message);
            }

            LogMessage(true, string.Format("{0} documents are qualified", _totalDocumentCount));

            // Construct the document query entity to write the resultant documents in xml file
            var outputFields = new List<Field>();
            outputFields.AddRange(new List<Field>()
            {
                    new Field { FieldName = EVSystemFields.DcnField},
                    new Field { FieldName = EVSystemFields.FamilyId},
                    new Field { FieldName = EVSystemFields.DuplicateId}
            });
            _docQueryEntity = GetQueryEntity(_bootObject, _datasetEntity, 0, Convert.ToInt32(_totalDocumentCount), outputFields);
        }
        /// <summary>
        /// converts review set record to reviewset business entity to save to reviewset master details in vault DB
        /// </summary>
        /// <param name="reviewsetRecord"></param>
        /// <returns></returns>
        private CreateReviewSetTaskBEO ConverttoReviewsetBusinessEntity(ReviewsetRecord reviewsetRecord)
        {
            var reviewSetBusinesssEntity = new CreateReviewSetTaskBEO();

            reviewSetBusinesssEntity.ReviewSetName = reviewsetRecord.ReviewSetName;
            reviewSetBusinesssEntity.ReviewsetDescription = reviewsetRecord.ReviewSetDescription;
            reviewSetBusinesssEntity.DatasetId = reviewsetRecord.DatasetId;
            reviewSetBusinesssEntity.ReviewSetId = reviewsetRecord.ReviewSetId;
            reviewSetBusinesssEntity.BinderId = reviewsetRecord.BinderId;
            reviewSetBusinesssEntity.BinderName = reviewsetRecord.BinderName;
            reviewSetBusinesssEntity.DueDate = reviewsetRecord.DueDate;
            reviewSetBusinesssEntity.KeepDuplicates = reviewsetRecord.KeepDuplicatesTogether;
            reviewSetBusinesssEntity.KeepFamily = reviewsetRecord.KeepFamilyTogether;
            reviewSetBusinesssEntity.ReviewSetGroup = reviewsetRecord.ReviewSetGroup;
            reviewSetBusinesssEntity.ReviewSetLogic = reviewsetRecord.ReviewSetLogic;
            reviewSetBusinesssEntity.SearchQuery = reviewsetRecord.SearchQuery;
            reviewSetBusinesssEntity.SplittingOption = reviewsetRecord.SplittingOption;
            reviewSetBusinesssEntity.StartDate = reviewsetRecord.StartDate;
            reviewSetBusinesssEntity.NumberOfDocuments = reviewsetRecord.NumberOfDocuments;
            reviewSetBusinesssEntity.NumberOfReviewedDocs = reviewsetRecord.NumberOfReviewedDocs;
            reviewSetBusinesssEntity.ReviewSetUserList.AddRange(reviewsetRecord.ReviewSetUserList);
            if (PipelineType==null
                ||PipelineType.Moniker==null
                ||String.IsNullOrEmpty(PipelineType.Moniker))
                
            return reviewSetBusinesssEntity;

            if (PipelineType.Moniker.Equals(Constants.SplitReviewsetPipeLineType))
                reviewSetBusinesssEntity.Action = Constants.Spilt;
            return reviewSetBusinesssEntity;
        }
        private void Send(ReviewsetRecord reviewsetRecord)
        {
            var message = new PipeMessageEnvelope
            {
                Body = reviewsetRecord
            };

            if (null != OutputDataPipe)
            {
                OutputDataPipe.Send(message);
            }
        }
 /// <summary>
 /// Converts document record to review set document BEO
 /// </summary>
 /// <param name="documentList"></param>
 /// <param name="createdBy"> </param>
 /// <returns></returns>
 private List<ReviewsetDocumentBEO> ConvertDocumentRecordtoReviewsetDocument(
     List<DocumentIdentityRecord> documentList, ReviewsetRecord reviewset)
 {
     var convertedDocuments = new List<ReviewsetDocumentBEO>();
     foreach (var document in documentList)
     {
         convertedDocuments.Add(new ReviewsetDocumentBEO
         {
             DocumentId = document.DocumentId,
             CollectionViewId = document.ReviewsetId,
             BinderId = reviewset.BinderId,
             DCN = document.DocumentControlNumber,
             CreatedBy = reviewset.CreatedBy
         }
             );
     }
     return convertedDocuments;
 }
        /// <summary>
        /// converts to review set business entity to update the status
        /// </summary>
        /// <param name="reviewsetRecord"></param>
        /// <returns></returns>
        private ReviewsetDetailsBEO ConvertToReviewsetBusinessEntity(ReviewsetRecord reviewsetRecord)
        {
            var returnReviewset = new ReviewsetDetailsBEO
            {
                ReviewSetId = reviewsetRecord.ReviewSetId,
                ReviewSetName = reviewsetRecord.ReviewSetName,
                StatusId = Constants.Active,
                ModifiedBy = reviewsetRecord.CreatedBy,
                DatasetId = reviewsetRecord.DatasetId,
                Description = reviewsetRecord.ReviewSetDescription,
                DueDate = reviewsetRecord.DueDate,
                KeepDuplicates = reviewsetRecord.KeepDuplicatesTogether,
                KeepFamily = reviewsetRecord.KeepFamilyTogether,
                ReviewSetGroup = reviewsetRecord.ReviewSetGroup,
                ReviewSetLogic = reviewsetRecord.ReviewSetLogic,
                SearchQuery = reviewsetRecord.SearchQuery,
                SplittingOption = reviewsetRecord.SplittingOption,
                StartDate = reviewsetRecord.StartDate,
                NumberOfDocuments = reviewsetRecord.NumberOfDocuments,
                NumberOfReviewedDocs = reviewsetRecord.NumberOfReviewedDocs
            };

            return returnReviewset;
        }
 /// <summary>
 /// This method converts the CreateReviewSetJobBEO to ReviewsetRecord
 /// </summary>
 /// <param name="reviewSetJobBEO">CreateReviewSetJobBEO/param>
 /// <returns>ReviewsetRecord</returns>
 private ReviewsetRecord ConvertToReviewSetRecord(UpdateReviewSetJobBEO reviewSetJobBEO, DatasetBEO dataset)
 {
     var rSetRecord = new ReviewsetRecord
     {
         Activity = "Split",
         DatasetId = reviewSetJobBEO.DatasetId,
         MatterId = dataset.Matter.FolderID,
         BinderFolderId = reviewSetJobBEO.BinderFolderId,
         BinderId = _binderEntity.BinderId,
         BinderName = _binderEntity.BinderName,
         DueDate = reviewSetJobBEO.DueDate,
         KeepDuplicatesTogether = reviewSetJobBEO.KeepDuplicates,
         KeepFamilyTogether = reviewSetJobBEO.KeepFamily,
         NumberOfDocuments = reviewSetJobBEO.NumberOfDocuments,
         NumberOfDocumentsPerSet = reviewSetJobBEO.NumberOfDocumentsPerSet,
         NumberOfReviewedDocs = reviewSetJobBEO.NumberOfReviewedDocs,
         NumberOfReviewSets = reviewSetJobBEO.NumberOfReviewSets,
         ReviewSetDescription = reviewSetJobBEO.ReviewSetDescription,
         ReviewSetGroup = reviewSetJobBEO.ReviewSetGroup,
         ReviewSetId = reviewSetJobBEO.ReviewSetId,
         ReviewSetLogic = reviewSetJobBEO.ReviewSetLogic,
         ReviewSetName = reviewSetJobBEO.ReviewSetName,
         SplittingOption = reviewSetJobBEO.SplittingOption,
         StartDate = reviewSetJobBEO.StartDate,
         StatusId = reviewSetJobBEO.StatusId,
         CreatedBy = reviewSetJobBEO.CreatedByGUID,
         CollectionId = dataset.CollectionId,
         AssignTo = reviewSetJobBEO.AssignTo
     };
     rSetRecord.DsTags.AddRange(reviewSetJobBEO.DsTags);
     rSetRecord.ReviewSetUserList.AddRange(reviewSetJobBEO.ReviewSetUserList);
     return rSetRecord;
 }
        /// <summary>
        /// Absorb the boot parameters, deserialize and pass on the messages to the Search Worker
        /// </summary>
        public void DoBeginWork(string bootParameter)
        {
            bootParameter.ShouldNotBeEmpty();
            // Deserialize and determine the boot object
            _bootObject = GetBootObject(bootParameter);

            // Assert condition to check for jobscheduled by
            _bootObject.CreatedByGUID.ShouldNotBeEmpty();

            // Get Dataset Details to know about the Collection id and the Matter ID details
            _datasetEntity = DataSetBO.GetDataSetDetailForDataSetId(_bootObject.datasetId);
            _bootObject.BinderFolderId.ShouldNotBe(0);

            _binderEntity = BinderBO.GetBinderDetails(_bootObject.BinderFolderId.ToString());
            _binderEntity.ShouldNotBe(null);

            //Assert condition to check for dataset details
            _datasetEntity.ShouldNotBe(null);
            _datasetEntity.Matter.ShouldNotBe(null);

            _reviewSetRecord = ConvertToReviewSetRecord(_bootObject, _datasetEntity);


            // Construct the document query entity to determine the total documents
            _docQueryEntity = GetQueryEntity(_bootObject, _datasetEntity, 0, 1, null);
            _docQueryEntity.TransactionName = _docQueryEntity.QueryObject.TransactionName =
                "ReviewsetStartupWorker - DoBeginWork (GetCount)";

            // Mock the user session
            MockSession();


            var reviewSetDetails = ReviewSetBO.GetReviewSetDetails(_datasetEntity.Matter.FolderID.ToString(),
                _bootObject.ReviewSetId);

            if (reviewSetDetails != null)
            {
                reviewSetDetails.Action = _reviewSetRecord.Activity;
                reviewSetDetails.BinderName = _binderEntity.BinderName;
                //Audit Logging for existing review set
                ReviewSetBO.UpdateReviewSet(reviewSetDetails, false, false);
            }
            // Retrieve the total documents qualified
            _totalDocumentCount = ReviewerSearchInstance.GetDocumentCount(_docQueryEntity.QueryObject);


            Tracer.Info("Split Reviewset Startup Worker : {0} matching documents determined for the requested query",
                _totalDocumentCount);
            if (_totalDocumentCount < 1)
            {
                var message = String.Format("Search engine does not return any documents for Reviewset {0}",
                    _reviewSetRecord.ReviewSetName);
                throw new ApplicationException(message);
            }

            // Construct the document query entity to write the resultant documents in xml file
            var outputFields = new List<Field>();
            outputFields.AddRange(new List<Field>
            {
                new Field {FieldName = EVSystemFields.FamilyId},
                new Field {FieldName = EVSystemFields.DocumentKey},
                new Field {FieldName = EVSystemFields.ReviewSetId},
                new Field {FieldName = EVSystemFields.DuplicateId},
                new Field {FieldName = EVSystemFields.Tag.ToLower()},
                new Field {FieldName = _datasetEntity.DocumentControlNumberName}
            });
            _docQueryEntity = GetQueryEntity(_bootObject, _datasetEntity, 0, Convert.ToInt32(_totalDocumentCount),
                outputFields);
        }
 /// <summary>
 /// Converts document record to review set document BEO
 /// </summary>
 /// <param name="documentList"></param>
 /// <param name="createdBy"> </param>
 /// <returns></returns>
 private List<ReviewsetDocumentBEO> ConvertDocumentRecordtoReviewsetDocument(
     List<DocumentIdentityRecord> documentList, ReviewsetRecord reviewset)
 {
     var convertedDocuments = new List<ReviewsetDocumentBEO>();
     foreach (var document in documentList)
     {
         convertedDocuments.Add(new ReviewsetDocumentBEO
         {
             // Assign document numeric identifier
             Id = document.Id,
             DocumentId = document.DocumentId,
             CollectionViewId = document.ReviewsetId,
             BinderId = reviewset.BinderId,
             TotalRecordCount = _totalDocumentCount,
             FamilyId = document.FamilyId,
             DCN = document.DocumentControlNumber,
             CreatedBy = reviewset.CreatedBy
         }
         );
     }
     return convertedDocuments;
 }
        /// <summary>
        /// Unassigns the reviewset id for the list of documents from search server
        /// </summary>
        /// <param name="documents"></param>
        /// <param name="reviewset">ReviewsetRecord</param>
        private void UnAssignReviewsetInSearchIndex(List<ReviewsetDocumentBEO> documents, ReviewsetRecord reviewset)
        {
            const int batchSize = 1000;
            var processedCount = 0;

            var fields = new Dictionary<string, string>
            {
                {EVSystemFields.ReviewSetId, reviewset.SplitReviewSetId},
                {EVSystemFields.BinderId, reviewset.BinderId}
            };
            var indexManagerProxy = new IndexManagerProxy(reviewset.MatterId, reviewset.CollectionId);
            while (processedCount != documents.Count)
            {
                List<ReviewsetDocumentBEO> tmpDocuments;
                if ((documents.Count - processedCount) < batchSize)
                {
                    tmpDocuments = documents.Skip(processedCount).Take(documents.Count - processedCount).ToList();
                    processedCount += documents.Count - processedCount;
                }
                else
                {
                    tmpDocuments = documents.Skip(processedCount).Take(batchSize).ToList();
                    processedCount += batchSize;
                }
                var docs = tmpDocuments.Select(doc => new DocumentBeo() 
                { Id = doc.DocumentId, Fields = fields }).ToList();

                indexManagerProxy.BulkUnAssignFields(docs);
            }
        }