/// <summary>
        /// Gets the E mail and entry id pairs from give EdrmManager object. This information can be used for email message generation
        /// </summary>
        /// <param name="outlookEdrmManager">The outlook edrm manager.</param>
        /// <param name="edrmFilePath">The edrm file path.</param>
        /// <returns>E mail and entry id pairs </returns>
        private IEnumerable <KeyValuePair <string, string> > GetEMailAndEntryIdPairs(OutlookEdrmManager outlookEdrmManager, string edrmFilePath)
        {
            if (outlookEdrmManager.OutlookEmailDocumentEntities != null)
            {
                List <KeyValuePair <string, string> > EntryIdEMailMessagePair = new List <KeyValuePair <string, string> >();
                foreach (OutlookEMailDocumentEntity outlookEmailDocumentEntity in outlookEdrmManager.OutlookEmailDocumentEntities)
                {
                    string externalFileName = string.Empty;

                    // Take first file in external file entities, use same name but different extension for msg file.
                    // This msg file doesn't exist here - but will be created later in the flow with the given name, decided here..
                    if (outlookEmailDocumentEntity.Files != null && outlookEmailDocumentEntity.Files.Count > 0 &&
                        outlookEmailDocumentEntity.Files[0].ExternalFile != null && outlookEmailDocumentEntity.Files[0].ExternalFile.Count > 0)
                    {
                        ExternalFileEntity externalFile = outlookEmailDocumentEntity.Files[0].ExternalFile[0];
                        externalFileName = CreateFileURIForEmailMessage(Path.GetDirectoryName(edrmFilePath), externalFile.FilePath, externalFile.FileName);
                    }

                    EntryIdEMailMessagePair.Add(new KeyValuePair <string, string>(outlookEmailDocumentEntity.EntryId, externalFileName));
                }

                return(EntryIdEMailMessagePair);
            }
            return(null);
        }
        /// <summary>
        /// Gets the E mail and entry id pairs from give EdrmManager object. This information can be used for email message generation
        /// </summary>
        /// <param name="outlookEdrmManager">The outlook edrm manager.</param>
        /// <param name="edrmFilePath">The edrm file path.</param>
        /// <returns>E mail and entry id pairs </returns>
        private IEnumerable<KeyValuePair<string, string>> GetEMailAndEntryIdPairs(OutlookEdrmManager outlookEdrmManager, string edrmFilePath)
        {
            if (outlookEdrmManager.OutlookEmailDocumentEntities != null)
            {
                List<KeyValuePair<string, string>> EntryIdEMailMessagePair = new List<KeyValuePair<string, string>>();
                foreach (OutlookEMailDocumentEntity outlookEmailDocumentEntity in outlookEdrmManager.OutlookEmailDocumentEntities)
                {
                    string externalFileName = string.Empty;

                    // Take first file in external file entities, use same name but different extension for msg file.
                    // This msg file doesn't exist here - but will be created later in the flow with the given name, decided here..
                    if (outlookEmailDocumentEntity.Files != null && outlookEmailDocumentEntity.Files.Count > 0 &&
                        outlookEmailDocumentEntity.Files[0].ExternalFile != null && outlookEmailDocumentEntity.Files[0].ExternalFile.Count > 0)
                    {
                        ExternalFileEntity externalFile = outlookEmailDocumentEntity.Files[0].ExternalFile[0];
                        externalFileName = CreateFileURIForEmailMessage(Path.GetDirectoryName(edrmFilePath), externalFile.FilePath, externalFile.FileName);
                    }

                    EntryIdEMailMessagePair.Add(new KeyValuePair<string, string>(outlookEmailDocumentEntity.EntryId, externalFileName));
                }

                return EntryIdEMailMessagePair;
            }
            return null;
        }
        /// <summary>
        /// Transforms Outlook specific EDRM file to Document Business Entity
        /// </summary>
        /// <param name="edrmFileName">EDRM file to be transformed</param>
        /// <param name="currentDocumentIndex"> Index of current document in list of documents being extracted
        /// Null able column - can be null when percent complete need not be calculated.
        /// </param>
        /// <returns>
        /// EvDocumentDataEntity that abstracts documents and relationships
        /// </returns>
        private EvDocumentDataEntity TransformOutlookEDRMToDocumentBusinessEntity(string edrmFileName, string mailStorePath, int? currentDocumentIndex)
        {
            try
            {
                OutlookEdrmManager outlookEdrmManager = new OutlookEdrmManager(edrmFileName);
                List<RelationshipBEO> relationships = new List<RelationshipBEO>();
                outlookEdrmManager.GetDocumentIdFromConversationIndex += GetUniqueEmailDocumentIDFromConversationIndex;
                List<RVWDocumentBEO> documents = outlookEdrmManager.GetDocuments(FilterByMappedFields, DatasetBeo.Matter.FolderID,
                                                                    DatasetBeo.CollectionId.ToString(), ref relationships);
                //Replace EVCorlib Mail Message file extension with CNEV file extension
                documents.ForEach
                    (doc => doc.FileExtension = doc.FileExtension.Replace(Constants.EVCorlibEMailMessageExtension, Constants.CNEvEmailMessageExtension));
                UpdateNativeDocumentFileNameForEMailMessages(documents);

                // Adding created By property for each documents
                documents.ForEach(x => x.CreatedBy = DatasetBeo.CreatedBy);

                #region Create Ev Document Data Entity and raise event for processing/importing available documents
                EvDocumentDataEntity evDocumentDataEntity = new EvDocumentDataEntity()
                {
                    Documents = documents,
                    OutlookMailStoreDataEntity = new OutlookMailStoreEntity
                    {
                        EntryIdAndEmailMessagePairs = GetEMailAndEntryIdPairs(outlookEdrmManager, edrmFileName),
                        PSTFile = new FileInfo(mailStorePath)
                    },
                    PercentComplete = currentDocumentIndex ?? (100.00 / FileProcessorInstance.EntryIdsCount) * currentDocumentIndex.Value
                };
                #endregion Create Ev Document Data Entity and raise event for processing/importing available documents

                // raise event as the document processing completes
                if (EvDocumentDataEntityAvailable != null)
                    EvDocumentDataEntityAvailable(evDocumentDataEntity);

                #region insert temporary records for thread calculation

                List<EmailThreadingEntity> lstEmailThreadEntities = new List<EmailThreadingEntity>();
                if (relationships != null && relationships.Count > 0)
                {
                    foreach (var relation in relationships)
                    {
                        EmailThreadingEntity ete = relation.ToDataAccesEntity(batchIdentifier, ThreadingConstraint, ThreadRelationshipEntity.RelationshipType.OutlookAttachment);
                        lstEmailThreadEntities.Add(ete);
                    }
                }

                IEnumerable<OutlookEMailDocumentEntity> outlookEmails = outlookEdrmManager.OutlookEmailDocumentEntities;
                // store temporary record for calculating email relationship
                if (outlookEmails != null && outlookEmails.Any())
                {
                    List<OutlookEMailDocumentEntity> lstOutLookEmails = new List<OutlookEMailDocumentEntity>();
                    foreach (OutlookEMailDocumentEntity outLookEmail in outlookEmails)
                    {
                        if ((!string.IsNullOrEmpty(outLookEmail.ConversationIndex)) && outLookEmail.ConversationIndex.Length >= 44)
                        {
                            lstOutLookEmails.Add(outLookEmail);
                        }
                    }
                    lstOutLookEmails.ForEach(outLookEmail =>
                                                  lstEmailThreadEntities.Add(
                                                        ToEmailThreadEntity(
                                                                    outLookEmail,
                                                                    batchIdentifier,
                                                                    ThreadingConstraint,
                                                                    ThreadRelationshipEntity.RelationshipType.OutlookEmailThread)));



                }
                //Used Bulk Add Temp Relationships in EmailThreadingHelper
                if (lstEmailThreadEntities.Any())
                {
                    //EmailThreadingHelper.BulkAddRelationshipTemporaryRecord(lstEmailThreadEntities);
                    _documentRelationships.AddRange(lstEmailThreadEntities);
                }

                #endregion

                // Cleanup if delete non native files is set
                if (IsDeleteTemporaryFiles == DeleteExtractedFilesOptions.DeleteNonNativeFiles)
                {
                    outlookEdrmManager.DeleteNonNativeFiles();
                }

                return evDocumentDataEntity;
            }
            catch (EVException evException)
            {
                ((Exception)evException).Trace().Swallow();
            }
            catch (Exception exception)
            {
                string errorCode = ErrorCodes.OutlookEDRMToDocumentTransformFailure;
                exception.AddErrorCode(errorCode).Trace().Swallow();
            }

            return null;
        }
        /// <summary>
        /// Transforms Outlook specific EDRM file to Document Business Entity
        /// </summary>
        /// <param name="edrmFileName">EDRM file to be transformed</param>
        /// <param name="currentDocumentIndex"> Index of current document in list of documents being extracted
        /// Null able column - can be null when percent complete need not be calculated.
        /// </param>
        /// <returns>
        /// EvDocumentDataEntity that abstracts documents and relationships
        /// </returns>
        private EvDocumentDataEntity TransformOutlookEDRMToDocumentBusinessEntity(string edrmFileName, string mailStorePath, int?currentDocumentIndex)
        {
            try
            {
                OutlookEdrmManager     outlookEdrmManager = new OutlookEdrmManager(edrmFileName);
                List <RelationshipBEO> relationships      = new List <RelationshipBEO>();
                outlookEdrmManager.GetDocumentIdFromConversationIndex += GetUniqueEmailDocumentIDFromConversationIndex;
                List <RVWDocumentBEO> documents = outlookEdrmManager.GetDocuments(FilterByMappedFields, DatasetBeo.Matter.FolderID,
                                                                                  DatasetBeo.CollectionId.ToString(), ref relationships);
                //Replace EVCorlib Mail Message file extension with CNEV file extension
                documents.ForEach
                    (doc => doc.FileExtension = doc.FileExtension.Replace(Constants.EVCorlibEMailMessageExtension, Constants.CNEvEmailMessageExtension));
                UpdateNativeDocumentFileNameForEMailMessages(documents);

                // Adding created By property for each documents
                documents.ForEach(x => x.CreatedBy = DatasetBeo.CreatedBy);

                #region Create Ev Document Data Entity and raise event for processing/importing available documents
                EvDocumentDataEntity evDocumentDataEntity = new EvDocumentDataEntity()
                {
                    Documents = documents,
                    OutlookMailStoreDataEntity = new OutlookMailStoreEntity
                    {
                        EntryIdAndEmailMessagePairs = GetEMailAndEntryIdPairs(outlookEdrmManager, edrmFileName),
                        PSTFile = new FileInfo(mailStorePath)
                    },
                    PercentComplete = currentDocumentIndex ?? (100.00 / FileProcessorInstance.EntryIdsCount) * currentDocumentIndex.Value
                };
                #endregion Create Ev Document Data Entity and raise event for processing/importing available documents

                // raise event as the document processing completes
                if (EvDocumentDataEntityAvailable != null)
                {
                    EvDocumentDataEntityAvailable(evDocumentDataEntity);
                }

                #region insert temporary records for thread calculation

                List <EmailThreadingEntity> lstEmailThreadEntities = new List <EmailThreadingEntity>();
                if (relationships != null && relationships.Count > 0)
                {
                    foreach (var relation in relationships)
                    {
                        EmailThreadingEntity ete = relation.ToDataAccesEntity(batchIdentifier, ThreadingConstraint, ThreadRelationshipEntity.RelationshipType.OutlookAttachment);
                        lstEmailThreadEntities.Add(ete);
                    }
                }

                IEnumerable <OutlookEMailDocumentEntity> outlookEmails = outlookEdrmManager.OutlookEmailDocumentEntities;
                // store temporary record for calculating email relationship
                if (outlookEmails != null && outlookEmails.Any())
                {
                    List <OutlookEMailDocumentEntity> lstOutLookEmails = new List <OutlookEMailDocumentEntity>();
                    foreach (OutlookEMailDocumentEntity outLookEmail in outlookEmails)
                    {
                        if ((!string.IsNullOrEmpty(outLookEmail.ConversationIndex)) && outLookEmail.ConversationIndex.Length >= 44)
                        {
                            lstOutLookEmails.Add(outLookEmail);
                        }
                    }
                    lstOutLookEmails.ForEach(outLookEmail =>
                                             lstEmailThreadEntities.Add(
                                                 ToEmailThreadEntity(
                                                     outLookEmail,
                                                     batchIdentifier,
                                                     ThreadingConstraint,
                                                     ThreadRelationshipEntity.RelationshipType.OutlookEmailThread)));
                }
                //Used Bulk Add Temp Relationships in EmailThreadingHelper
                if (lstEmailThreadEntities.Any())
                {
                    //EmailThreadingHelper.BulkAddRelationshipTemporaryRecord(lstEmailThreadEntities);
                    _documentRelationships.AddRange(lstEmailThreadEntities);
                }

                #endregion

                // Cleanup if delete non native files is set
                if (IsDeleteTemporaryFiles == DeleteExtractedFilesOptions.DeleteNonNativeFiles)
                {
                    outlookEdrmManager.DeleteNonNativeFiles();
                }

                return(evDocumentDataEntity);
            }
            catch (EVException evException)
            {
                ((Exception)evException).Trace().Swallow();
            }
            catch (Exception exception)
            {
                string errorCode = ErrorCodes.OutlookEDRMToDocumentTransformFailure;
                exception.AddErrorCode(errorCode).Trace().Swallow();
            }

            return(null);
        }