/// <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>
        /// This method deserializes and determine the Xml CreateReviewSetJobBEO object
        /// </summary>
        /// <param name="bootParameter"></param>
        private UpdateReviewSetJobBEO GetBootObject(string bootParameter)
        {
            UpdateReviewSetJobBEO bootObject = null;

            if (!string.IsNullOrEmpty(bootParameter))
            {
                //Creating a stringReader stream for the bootparameter
                var stream = new StringReader(bootParameter);

                //Ceating xmlStream for xmlserialization
                var xmlStream = new XmlSerializer(typeof(UpdateReviewSetJobBEO));

                //Deserialization of bootparameter to get ProductionDetailsBEO
                bootObject = (UpdateReviewSetJobBEO)xmlStream.Deserialize(stream);
            }
            return(bootObject);
        }
        /// <summary>
        /// Constructs and returns the document search query entity
        /// </summary>
        /// <param name="jobParameters">The job parameters.</param>
        /// <param name="datasetEntity">The dataset entity.</param>
        /// <returns></returns>
        private DocumentQueryEntity GetQueryEntity(UpdateReviewSetJobBEO jobParameters,
                                                   DatasetBEO datasetEntity, int startIndex, int documentCount, List <Field> outputFields)
        {
            var documentQueryEntity = new DocumentQueryEntity
            {
                QueryObject = new SearchQueryEntity
                {
                    MatterId = datasetEntity.Matter.FolderID,
                    IsConceptSearchEnabled = jobParameters.DocumentSelectionContext.SearchContext.IsConceptSearchEnabled,
                    DatasetId   = datasetEntity.FolderID,
                    ReviewsetId = jobParameters.ReviewSetId
                }
            };

            documentQueryEntity.DocumentStartIndex = startIndex;
            documentQueryEntity.DocumentCount      = documentCount;
            documentQueryEntity.SortFields.Add(new Sort {
                SortBy = Constants.Relevance
            });

            // Include families & duplicates is obsolete...So no need to include families & duplicates as part of search-engine search.. Always set to false;
            documentQueryEntity.IgnoreDocumentSnippet = true;
            documentQueryEntity.IncludeDuplicates     = true;
            documentQueryEntity.IncludeFamilies       = true;
            documentQueryEntity.TotalRecallConfigEntity.IsTotalRecall = true;

            if (outputFields != null && outputFields.Any())
            {
                documentQueryEntity.OutputFields.AddRange(outputFields);
            }

            var tmpQuery       = string.Empty;
            var selectionQuery = string.Empty;

            if (!string.IsNullOrEmpty(jobParameters.DocumentSelectionContext.SearchContext.Query))
            {
                tmpQuery = jobParameters.DocumentSelectionContext.SearchContext.Query;
            }

            switch (jobParameters.DocumentSelectionContext.GenerateDocumentMode)
            {
            case DocumentSelectMode.UseSelectedDocuments:
            {
                jobParameters.DocumentSelectionContext.SelectedDocuments.ForEach(d =>
                                                                                 selectionQuery += string.Format("{0}:\"{1}\" OR ", EVSystemFields.DocumentKey, d));
                if (!string.IsNullOrEmpty(selectionQuery))
                {
                    selectionQuery = selectionQuery.Substring(0, selectionQuery.LastIndexOf(" OR "));
                    tmpQuery       = string.Format("({0} AND {1})", tmpQuery, selectionQuery);
                }

                break;
            }

            case DocumentSelectMode.QueryAndExclude:
            {
                jobParameters.DocumentSelectionContext.DocumentsToExclude.ForEach(d =>
                                                                                  selectionQuery += string.Format("(NOT {0}:\"{1}\") AND ", EVSystemFields.DocumentKey, d));
                if (!string.IsNullOrEmpty(selectionQuery))
                {
                    selectionQuery = selectionQuery.Substring(0, selectionQuery.LastIndexOf(" AND "));
                    tmpQuery       = string.Format("({0} AND {1})", tmpQuery, selectionQuery);
                }
                break;
            }
            }
            documentQueryEntity.QueryObject.QueryList.Clear();
            documentQueryEntity.QueryObject.QueryList.Add(new Query {
                SearchQuery = tmpQuery
            });
            return(documentQueryEntity);
        }
        /// <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>
 /// 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>
        /// Constructs and returns the document search query entity
        /// </summary>
        /// <param name="jobParameters">The job parameters.</param>
        /// <param name="datasetEntity">The dataset entity.</param>
        /// <returns></returns>
        private DocumentQueryEntity GetQueryEntity(UpdateReviewSetJobBEO jobParameters,
            DatasetBEO datasetEntity, int startIndex, int documentCount, List<Field> outputFields)
        {
            var documentQueryEntity = new DocumentQueryEntity
            {
                QueryObject = new SearchQueryEntity
                {
                    MatterId = datasetEntity.Matter.FolderID,
                    IsConceptSearchEnabled = jobParameters.DocumentSelectionContext.SearchContext.IsConceptSearchEnabled,
                    DatasetId = datasetEntity.FolderID,
                    ReviewsetId = jobParameters.ReviewSetId
                }
            };

            documentQueryEntity.DocumentStartIndex = startIndex;
            documentQueryEntity.DocumentCount = documentCount;
            documentQueryEntity.SortFields.Add(new Sort {SortBy = Constants.Relevance});

            // Include families & duplicates is obsolete...So no need to include families & duplicates as part of search-engine search.. Always set to false;
            documentQueryEntity.IgnoreDocumentSnippet = true;
            documentQueryEntity.IncludeDuplicates = true;
            documentQueryEntity.IncludeFamilies = true;
            documentQueryEntity.TotalRecallConfigEntity.IsTotalRecall = true;

            if (outputFields != null && outputFields.Any())
            {
                documentQueryEntity.OutputFields.AddRange(outputFields);
            }

            var tmpQuery = string.Empty;
            var selectionQuery = string.Empty;

            if (!string.IsNullOrEmpty(jobParameters.DocumentSelectionContext.SearchContext.Query))
            {
                tmpQuery = jobParameters.DocumentSelectionContext.SearchContext.Query;
            }

            switch (jobParameters.DocumentSelectionContext.GenerateDocumentMode)
            {
                case DocumentSelectMode.UseSelectedDocuments:
                {
                    jobParameters.DocumentSelectionContext.SelectedDocuments.ForEach(d =>
                        selectionQuery += string.Format("{0}:\"{1}\" OR ", EVSystemFields.DocumentKey, d));
                    if (!string.IsNullOrEmpty(selectionQuery))
                    {
                        selectionQuery = selectionQuery.Substring(0, selectionQuery.LastIndexOf(" OR "));
                        tmpQuery = string.Format("({0} AND {1})", tmpQuery, selectionQuery);
                    }

                    break;
                }
                case DocumentSelectMode.QueryAndExclude:
                {
                    jobParameters.DocumentSelectionContext.DocumentsToExclude.ForEach(d =>
                        selectionQuery += string.Format("(NOT {0}:\"{1}\") AND ", EVSystemFields.DocumentKey, d));
                    if (!string.IsNullOrEmpty(selectionQuery))
                    {
                        selectionQuery = selectionQuery.Substring(0, selectionQuery.LastIndexOf(" AND "));
                        tmpQuery = string.Format("({0} AND {1})", tmpQuery, selectionQuery);
                    }
                    break;
                }
            }
            documentQueryEntity.QueryObject.QueryList.Clear();
            documentQueryEntity.QueryObject.QueryList.Add(new Query {SearchQuery = tmpQuery});
            return documentQueryEntity;
        }
        /// <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);
        }