/// <summary>
        /// Copy Image Files
        /// </summary>
        /// <param name="documentDetail">documentDetail</param>
        /// <param name="errorMessage">errorMessage</param>
        /// <returns></returns>
        private bool CopyImageFiles(ExportDocumentDetail documentDetail, out string errorMessage)
        {
            #region assertion
            documentDetail.ShouldNotBe(null);
            #endregion

            var result            = true;
            var imageErrorBuilder = new StringBuilder();
            try
            {
                var pageCount = 0;

                if (documentDetail.ImageFiles != null && documentDetail.ImageFiles.Count > 0)
                {
                    var imageFieldId = DocumentBO.GetFieldIdByNameForExportJob(_dataset, _fieldImageFileName, false);
                    var fieldValue   = DocumentBO.GetFieldValueForExportJob(Convert.ToInt64(_exportLoadJobDetailBeo.MatterId), _dataset.CollectionId, documentDetail.DocumentId, imageFieldId);

                    var imageFileName = (!string.IsNullOrEmpty(fieldValue)) ?fieldValue: string.Empty;

                    imageFileName = Regex.Replace(imageFileName, Constants.FileSpecialCharactersRegex, string.Empty); //Remove the illegal character in the filename
                    if (string.IsNullOrEmpty(imageFileName))
                    {
                        imageErrorBuilder.Append(Constants.ExportImageNoValueCopyErrorMessage);
                        errorMessage = imageErrorBuilder.ToString();
                        return(false);
                    }
                    Int64 noOfImageFiles = documentDetail.ImageFiles.Count;
                    var   imgFileNames   = new List <string>();
                    if (_lstBatesAndDpnFieldTypes.Contains(_imageNameFieldType) && imageFileName.Contains(','))//if  field value contains , then it means field is bates field
                    {
                        imgFileNames = GetFileNamesForCommaSeperatedFields(imageFileName, noOfImageFiles, imgFileNames);
                    }
                    else
                    {
                        bool status = GetFileNamesWithStatus(ref imgFileNames, noOfImageFiles, imageFileName, out errorMessage);
                        if (!status)
                        {
                            return(false);
                        }
                    }
                    CopyFilesToTargetPath(documentDetail, ref result, imageErrorBuilder, ref pageCount, imgFileNames);
                }
                else
                {
                    imageErrorBuilder.Append(Constants.ExportImageNoValueCopyErrorMessage);
                    errorMessage = imageErrorBuilder.ToString();
                    return(false);
                }
            }
            catch (Exception ex)
            {
                ex.AddUsrMsg("Export File Copy Worker: Unable to copy image files for job run id: {0}", PipelineId).Trace().Swallow();
                result = false;
            }
            errorMessage = imageErrorBuilder.ToString();
            return(result);
        }
        private void InitializeForProcessing(string exportBootParameter)
        {
            parametersExportLoadFile = GetExportBEO <ExportLoadJobDetailBEO>(exportBootParameter);
            if (parametersExportLoadFile.ExportLoadFileInfo != null)
            {
                columnDelimiter = Convert.ToChar(Convert.ToInt32(parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.Column));
                quoteCharacter  = Convert.ToChar(Convert.ToInt32(parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.Quote));
                contentNewLine  = Convert.ToChar(Convert.ToInt32(parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.NewLine));
                newLine         = Constants.ConcordanceRecordSplitter;
                if (parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat != null)
                {
                    dateFormatForData = (!string.IsNullOrEmpty(parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.DateFormat)) ? parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.DateFormat : string.Empty;
                }

                encoding = (parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileFormat.EncodingType == EncodingTypeSelection.Ansi) ? Encoding.GetEncoding(Constants.Ansi) : Encoding.Unicode;

                if (parametersExportLoadFile.ExportLoadFileInfo.ExportLoadNativeFileLocation != null)
                {
                    isRelativePath = parametersExportLoadFile.ExportLoadFileInfo.ExportLoadNativeFileLocation.RelativePathLocation;
                }

                if (!string.IsNullOrEmpty(parametersExportLoadFile.CreatedBy))
                {
                    createdBy = parametersExportLoadFile.CreatedBy;
                }
            }


            if (exportDocumentCollection.ExportOption != null)
            {
                loadFileName        = exportDocumentCollection.ExportOption.LoadFilePath;
                imageHelperFileName = (!string.IsNullOrEmpty(exportDocumentCollection.ExportOption.LoadFileImageHelperFilePath)) ? exportDocumentCollection.ExportOption.LoadFileImageHelperFilePath : string.Empty;
                textHelperFileName  = (!string.IsNullOrEmpty(exportDocumentCollection.ExportOption.LoadFileTextHelperFilePath)) ? exportDocumentCollection.ExportOption.LoadFileTextHelperFilePath : string.Empty;
                exportFolderPath    = exportDocumentCollection.ExportOption.ExportDestinationFolderPath;
            }

            if (parametersExportLoadFile.ExportLoadFields != null)
            {
                exportFieldsSelection = parametersExportLoadFile.ExportLoadFields;
            }

            if (parametersExportLoadFile.ExportLoadTagInfo != null && !string.IsNullOrEmpty(parametersExportLoadFile.ExportLoadTagInfo.Delimeter))
            {
                tagSeparator = Convert.ToChar(Convert.ToInt32(parametersExportLoadFile.ExportLoadTagInfo.Delimeter));
            }

            if (!string.IsNullOrEmpty(PipelineId) && WorkAssignment != null)
            {
                jobID = WorkAssignment.JobId;
            }

            #region Initialize File writer
            //Initialize File writer for First time
            loadFileIoWriter = new StreamWriter(loadFileName, true, encoding);
            if (exportDocumentCollection.ExportOption.IsText && parametersExportLoadFile.ExportLoadFileInfo.ExportLoadFileOption.Nameselection == TextFileNameSelection.UseOPT)
            {
                textFileIoWriter = new StreamWriter(textHelperFileName, true);
            }
            if (exportDocumentCollection.ExportOption.IsImage || exportDocumentCollection.ExportOption.IsProduction)
            {
                imageFileIoWriter = new StreamWriter(imageHelperFileName, true);
            }
            #endregion

            #region Initialize Sort Order collection
            loadFileSortOrderCollection = new SortedDictionary <string, string>();
            if (exportDocumentCollection.ExportOption.IsText)
            {
                textFileSortOrderCollection = new SortedDictionary <string, string>();
            }
            if (exportDocumentCollection.ExportOption.IsImage || exportDocumentCollection.ExportOption.IsProduction)
            {
                imageFileSortOrderCollection = new SortedDictionary <string, string>();
            }
            #endregion


            foreach (var exportField in exportFieldsSelection)
            {
                if (String.Equals(exportField.DataSetFieldName, "CONTENT", StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }
                var textFieldId = DocumentBO.GetFieldIdByNameForExportJob(_dataset, exportField.DataSetFieldName, false);
                _selectedFieldIds.Add(textFieldId);
            }
        }
        private bool CopyTextFiles(ExportDocumentDetail documentDetail, out string errorMessage)
        {
            var result           = true;
            var textErrorBuilder = new StringBuilder();

            try
            {
                if (documentDetail.TextFiles != null && documentDetail.TextFiles.Count > 0)
                {
                    foreach (var file in documentDetail.TextFiles)
                    {
                        var targetPath = string.Empty;
                        GetFileName(documentDetail, ref result, textErrorBuilder, file, ref targetPath);
                        if (!result)
                        {
                            continue;
                        }
                        bool retry;
                        do
                        {
                            try
                            {
                                if (file.IsTextFieldExportEnabled)
                                {
                                    var contentFieldValue = string.Empty;
                                    var textFieldId       = DocumentBO.GetFieldIdByNameForExportJob(_dataset, _exportDocumentCollection.ExportOption.TextFieldToExport, false);
                                    var field             = _dataset.DatasetFieldList.FirstOrDefault(f => f.Name.ToLower().Equals(_exportDocumentCollection.ExportOption.TextFieldToExport.ToLower()));

                                    if (field != null && field.FieldType != null)
                                    {
                                        if (field.FieldType.DataTypeId == ContentFieldTypeID)
                                        {
                                            contentFieldValue =
                                                DocumentBO.GetDocumentContentFieldsForExportJob(Convert.ToInt64(_exportLoadJobDetailBeo.MatterId), _dataset.CollectionId, documentDetail.DocumentId);
                                            contentFieldValue = DocumentBO.Base64DecodeForExportJob(contentFieldValue);   //Decode the content field
                                        }
                                        else
                                        {
                                            contentFieldValue =
                                                DocumentBO.GetFieldValueForExportJob(Convert.ToInt64(_exportLoadJobDetailBeo.MatterId), _dataset.CollectionId, documentDetail.DocumentId, textFieldId);
                                        }
                                    }

                                    File.WriteAllText(targetPath, contentFieldValue, _encodingType);
                                }
                                else
                                {
                                    File.Copy(file.SourceFilePath, targetPath);
                                }
                                retry = false;
                            }
                            catch (IOException ioEx)
                            {
                                result = false;
                                var hResult = System.Runtime.InteropServices.Marshal.GetHRForException(ioEx);
                                if (hResult == ExceptionHr)  //Capture "File already exists" exception only and generate the new filename and retry to copy it again
                                {
                                    var fileName = GetFileNameForDuplicates(Path.GetFileName(targetPath), file.DestinationFolder);
                                    targetPath = Path.Combine(file.DestinationFolder, fileName);
                                    retry      = true;
                                }
                                else
                                {
                                    textErrorBuilder.Append(Constants.ExportNativeTextCopyErrorMessage);
                                    textErrorBuilder.Append(file.SourceFilePath);
                                    textErrorBuilder.Append(Constants.ExportBreakMessage);
                                    retry = false;
                                }
                            }
                            catch (Exception)
                            {
                                result = false;
                                textErrorBuilder.Append(Constants.ExportNativeTextCopyErrorMessage);
                                textErrorBuilder.Append(file.SourceFilePath);
                                textErrorBuilder.Append(Constants.ExportBreakMessage);
                                retry = false;
                            }
                        } while (retry);
                        file.DestinationFolder = targetPath;
                    }
                }
                else if (_isIncludeText)
                {
                    textErrorBuilder.Append(TextFileError);
                    result = false;
                }
            }
            catch (Exception ex)
            {
                ex.AddUsrMsg("Export File Copy Worker: Unable to copy text files for job run id: {0}", PipelineId).Trace().Swallow();
                result = false;
            }
            errorMessage = textErrorBuilder.ToString();
            return(result);
        }
Exemplo n.º 4
0
        private ExportDocumentDetail ConstructDocumentData(ExportDocumentCollection exportDocumentCollection,
                                                           string documentId, string correlationId, out JobWorkerLog <ExportMetadataLogInfo> metadataLog)
        {
            var exportDocument = new ExportDocumentDetail();

            try
            {
                exportDocument.DocumentId    = documentId;
                exportDocument.CorrelationId = correlationId;
                bool isError;
                bool isErrorInTag = false;

                var document = DocumentBO.GetDocumentDataForExportJob(matterId, _dataset.CollectionId, documentId, out isError);
                if (document != null)
                {
                    exportDocument.DCN = document.DocumentControlNumber;

                    if (!String.IsNullOrEmpty(exportDocumentCollection.ExportOption.FieldForTextFileName))
                    {
                        var textFieldId = DocumentBO.GetFieldIdByNameForExportJob(_dataset,
                                                                                  exportDocumentCollection.ExportOption.FieldForTextFileName, false);
                        exportDocument.TextFileName = DocumentBO.GetFieldValueForExportJob(matterId,
                                                                                           _dataset.CollectionId, documentId, textFieldId);
                    }

                    if (!String.IsNullOrEmpty(exportDocumentCollection.ExportOption.FieldForNativeFileName))
                    {
                        var nativeFieldId = DocumentBO.GetFieldIdByNameForExportJob(_dataset,
                                                                                    exportDocumentCollection.ExportOption.FieldForNativeFileName, false);
                        exportDocument.NativeFileName = DocumentBO.GetFieldValueForExportJob(matterId,
                                                                                             _dataset.CollectionId, documentId, nativeFieldId);
                    }

                    #region "Recreate family group"
                    //2)Attachment Range
                    if (_beginsEnds != null)
                    {
                        Tuple <string, string> beginEnd;
                        _beginsEnds.TryGetValue(document.Id.ToString(), out beginEnd);
                        if (beginEnd != null)
                        {
                            exportDocument.BeginDoc = beginEnd.Item1;
                            exportDocument.EndDoc   = beginEnd.Item2;
                        }
                    }
                    #endregion

                    //3) File Path (Native File & Text)
                    if (document.DocumentBinary.FileList != null)
                    {
                        if (exportDocumentCollection.ExportOption.IsNative)
                        {
                            //Get the tag name to check the tag exists for the document. If exists then export the native files.
                            string tagNameToIncludeNative = exportDocumentCollection.ExportOption.IncludeNativeTagName;
                            if (!string.IsNullOrEmpty(tagNameToIncludeNative)) //If "Include native for tagged document only" option is selected
                            {
                                //Check whether the document tagged with the selected tag. If yes, set the IsNativeTagExists property as true and get all native files to export out.
                                exportDocument.IsNativeTagExists = CheckTagExistsToIncludeNative(document.CollectionId, document.DocumentId, tagNameToIncludeNative);
                                if (exportDocument.IsNativeTagExists) //If document tagged then get the native files list to export.
                                {
                                    exportDocument.NativeFiles = GetFileList(document.DocumentBinary.FileList.Where(f => f.Type == Constants.NativeFileTypeId).ToList());
                                }
                            }
                            else //If "Include native for tagged document only" option not selected, get native files to export out.
                            {
                                exportDocument.NativeFiles = GetFileList(document.DocumentBinary.FileList.Where(f => f.Type == Constants.NativeFileTypeId).ToList());
                            }
                        }
                        if (exportDocumentCollection.ExportOption.IsText)
                        {
                            if (exportDocumentCollection.ExportOption.IsTextFieldToExportSelected && !string.IsNullOrEmpty(exportDocumentCollection.ExportOption.TextFieldToExport))
                            {
                                var fileList = new List <ExportFileInformation>
                                {
                                    new ExportFileInformation()
                                    {
                                        IsTextFieldExportEnabled = true
                                    }
                                };
                                exportDocument.TextFiles = fileList;
                            }
                            else
                            {
                                if (exportDocumentCollection.ExportOption.IsProduction && !string.IsNullOrEmpty(exportDocumentCollection.ExportOption.ProductionSetCollectionId))
                                {
                                    string[] lstOfProductionSets = exportDocumentCollection.ExportOption.ProductionSetCollectionId.Split(',');
                                    foreach (string productionSetId in lstOfProductionSets)
                                    {
                                        var productionDocumentData = GetDocumentData(documentId, productionSetId, out isError);
                                        if (exportDocumentCollection.ExportOption.TextOption1 == Constants.ScrubbedText) //If Text Prority 1 is Printed/Scrubbed text
                                        {
                                            //Get the scrubbed text files for one or more productions...
                                            if (exportDocument.TextFiles == null)
                                            {
                                                //Get the scrubbed text files for the first time....
                                                exportDocument.TextFiles = GetFileList(productionDocumentData.DocumentBinary.FileList.Where(f => f.Type == Constants.ScrubbedFileTypeId).ToList());
                                            }
                                            else
                                            {
                                                //Appnd to existing text files in case user chooses more than one production
                                                exportDocument.TextFiles.AddRange(GetFileList(productionDocumentData.DocumentBinary.FileList.Where(f => f.Type == Constants.ScrubbedFileTypeId).ToList()));
                                            }
                                            if ((exportDocument.TextFiles == null || exportDocument.TextFiles.Count == 0 || !File.Exists(exportDocument.TextFiles.FirstOrDefault().SourceFilePath)) && exportDocumentCollection.ExportOption.TextOption2 == Constants.ExtractedText) //If scrubbed text not exists for this document then get the Extracted text
                                            {
                                                exportDocument.TextFiles = GetFileList(document.DocumentBinary.FileList.Where(f => f.Type == Constants.TextFileTypeId).ToList());
                                                exportDocument.TextFiles.SafeForEach(x => x.SourceFilePath = x.SourceFilePath.Contains(Constants.QuestionMark) ? x.SourceFilePath.Substring(0, x.SourceFilePath.LastIndexOf(Constants.QuestionMark)) : x.SourceFilePath);
                                            }
                                            else
                                            {
                                                exportDocument.TextFiles.ForEach(s => s.IsScrubbedText = true);
                                            }
                                        }
                                        else //Else If Text Prority 1 is Extracted text
                                        {
                                            //Get the extracted text files
                                            exportDocument.TextFiles = GetFileList(document.DocumentBinary.FileList.Where(f => f.Type == Constants.TextFileTypeId).ToList());
                                            exportDocument.TextFiles.SafeForEach(x => x.SourceFilePath = x.SourceFilePath.Contains(Constants.QuestionMark) ? x.SourceFilePath.Substring(0, x.SourceFilePath.LastIndexOf(Constants.QuestionMark)) : x.SourceFilePath);               //Remove the querystring
                                            if ((exportDocument.TextFiles == null || exportDocument.TextFiles.Count == 0 || !File.Exists(exportDocument.TextFiles.FirstOrDefault().SourceFilePath)) && exportDocumentCollection.ExportOption.TextOption2 == Constants.ScrubbedText) //If extracted text not exists for this document then get the Scrubbed text if exists
                                            {
                                                exportDocument.TextFiles = GetFileList(productionDocumentData.DocumentBinary.FileList.Where(f => f.Type == Constants.ScrubbedFileTypeId).ToList());
                                                exportDocument.TextFiles.ForEach(s => s.IsScrubbedText = true);
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    exportDocument.TextFiles = GetFileList(document.DocumentBinary.FileList.Where(f => f.Type == Constants.TextFileTypeId).ToList());
                                    exportDocument.TextFiles.SafeForEach(x => x.SourceFilePath = x.SourceFilePath.Contains(Constants.QuestionMark) ? x.SourceFilePath.Substring(0, x.SourceFilePath.LastIndexOf(Constants.QuestionMark)) : x.SourceFilePath); //Remove the querystring
                                }
                            }
                        }
                    }
                    //2.1) File Path (Images for Imageset or Production Set)
                    exportDocument.ImageFiles = new List <ExportFileInformation>();
                }
                //3) Tag
                if (exportDocumentCollection.ExportOption.IsTag && exportDocumentCollection.ExportOption.TagList != null && exportDocumentCollection.ExportOption.TagList.Count > 0)
                {
                    var tag = GetDocumentTags(documentId, out isErrorInTag);
                    if (tag != null && tag.Count > 0)
                    {
                        exportDocument.Tags = tag.Where(t => exportDocumentCollection.ExportOption.TagList.Contains(t.TagId.ToString(CultureInfo.InvariantCulture)) && t.Status).ToList();
                    }
                }

                #region Log
                metadataLog = ConstructLog(correlationId, (!isError), (!string.IsNullOrEmpty(exportDocument.DCN) ? exportDocument.DCN : string.Empty), isError, isErrorInTag, string.Empty);
                #endregion
            }
            catch (Exception ex)
            {
                ex.Trace().Swallow();
                metadataLog = ConstructLog(correlationId, false, string.Empty, true, true, "Error in get Metadata.");
            }
            return(exportDocument);
        }