예제 #1
0
        /// <summary>
        /// The generate document metadata based report.
        /// </summary>
        private MemoryStreamResult GenerateDocumentMetadataBasedReport()
        {
            this.logger.Log(LogType.Trace, this.GetAssemblyName(), "{0}.GenerateDocumentMetadataBasedReport", this.GetType().Name);
            var resourcesPackage = ResourcePackage.Open(Path.Combine(this.sourceFolder, this.templatePackageName));
            var documentMetadata = DocumentMetadataPackage.Open(Path.Combine(this.sourceFolder, this.metadataPackageName)).DocumentMetadata;
            var generator        = new ExportGenerator();
            var exportParams     = new ExportParameters();

            ExportToMemoryStreamResult result = generator.GenerateDocument(this.dataParts,
                                                                           documentMetadata,
                                                                           resourcesPackage,
                                                                           exportParams);

            if (result.Error != null)
            {
                this.logger.Log(LogType.Fatal, this.GetAssemblyName(), "{0}.GenerateDocumentMetadataBasedReport", result.Error);
            }

            return(new MemoryStreamResult
            {
                MemoryStream = result.MemoryStream,
                Status = result.Error == null ? MemoryStreamResultStatus.Success : MemoryStreamResultStatus.Failure,
                ErrorMessage = result.Error == null ? null : result.Error.Message,
            });
        }
예제 #2
0
        /// <summary>
        /// The pack source data folder.
        /// </summary>
        /// <param name="subFolderName">The sub folder name.</param>
        /// <returns>The zipped file name</returns>
        private string PackSourceMetaDataFolder(string subFolderName)
        {
            this.logger.Log(LogType.Trace, this.GetAssemblyName(), "{0}.PackSourceMetaDataFolder - Packing {1}", this.GetType().Name, subFolderName);
            string directory = Path.Combine(this.sourceFolder, subFolderName);
            string package   = this.isDocumentMetaBased ? DocumentMetadataPackage.Pack(directory) : ExportMetadataPackage.Pack(directory);

            // Extract package name from returned
            return(Path.GetFileName(package));
        }
        public static DocumentMetadataPackage Open(string fileName)
        {
            // avoid contention whilst opening file, should be v quick anyway
            lock (padLock)
            {
                try
                {
                    if (string.IsNullOrEmpty(fileName))
                    {
                        throw new ArgumentNullException("fileName");
                    }

                    var fi = new FileInfo(fileName);
                    if (!fi.Exists)
                    {
                        throw new MetadataException(string.Format("File not found <{0}>", fileName));
                    }

                    Output(string.Format("Starting open of package <{0}>", fileName));

                    DocumentMetadataPackage package = new DocumentMetadataPackage();

                    List <PackagePart> metadataParts = new List <PackagePart>();
                    List <PackagePart> fileParts     = new List <PackagePart>();

                    using (var zip = ZipPackage.Open(fileName, FileMode.Open, FileAccess.Read))
                    {
                        Output("Zip opened");

                        foreach (var part in zip.GetParts())
                        {
                            string uriString = part.Uri.OriginalString;

                            if (uriString.StartsWith(string.Concat(UriDelim, MetadataPathPart)))
                            {
                                metadataParts.Add(part);

                                Output(string.Format("Found Metadata part: Uri <{0}>", uriString));
                            }
                            else if (uriString.StartsWith(string.Concat(UriDelim, FilesPathPart)))
                            {
                                fileParts.Add(part);

                                Output(string.Format("Found TemplateFile part: Uri <{0}>", uriString));
                            }
                        }

                        if (metadataParts.Count == 0)
                        {
                            throw new MetadataException("No metadata files found");
                        }
                        else if (metadataParts.Count > 1)
                        {
                            throw new MetadataException("Only one metadata file allowed in a ExportMetadataPackage");
                        }
                        else
                        {
                            // metadata process
                            // read all the templates files into our internal store
                            var m = metadataParts[0];

                            long   length = m.GetStream().Length;
                            byte[] data   = new byte[length];
                            m.GetStream().Read(data, 0, (int)length);

                            Output(string.Format("Loading Metadata part: Uri <{0}> Size {1}", m.Uri.OriginalString, length));

                            bool hasBomb = false;
                            if (data.Length > 2 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF)
                            {
                                hasBomb = true;
                                Output("Stripping Byte Order Mark");
                            }

                            UTF8Encoding encoding       = new UTF8Encoding(true);
                            string       resourceString = encoding.GetString(hasBomb ? data.Skip(3).ToArray() : data);

                            Output("Stripping Byte Order Mark");

                            package.DocumentMetadata = DocumentMetadataBase.Deserialize(resourceString);

                            Output("Export metadata deserialized");
                        }

                        // template file process
                        if (!string.IsNullOrEmpty(package.DocumentMetadata.TemplateFileName))
                        {
                            Output(string.Format("Checking for existence of the TemplateFilePath <{0}>", package.DocumentMetadata.TemplateFileName));

                            if (fileParts.Count == 0)
                            {
                                throw new MetadataException(string.Format("No file parts found and <{0}> expected", package.DocumentMetadata.TemplateFileName));
                            }
                            else if (fileParts.Count > 1)
                            {
                                throw new MetadataException(string.Format("More than 1 file parts found and only <{0}> expected", package.DocumentMetadata.TemplateFileName));
                            }

                            var f = fileParts[0];

                            string name   = f.Uri.OriginalString.Replace(string.Concat(UriDelim, FilesPathPart, UriDelim), null);
                            long   length = f.GetStream().Length;

                            package.DocumentMetadata.HasTemplate  = true;
                            package.DocumentMetadata.TemplateData = new byte[length];
                            f.GetStream().Read(package.DocumentMetadata.TemplateData, 0, (int)length);

                            Output(string.Format("Loaded TemplateFile data : Uri <{0}> Size {1}", f.Uri.OriginalString, length));
                        }
                    }

                    package.ValidateInternal();

                    return(package);
                }
                catch (Exception ex)
                {
                    Output(ex.ToString());
                    throw;
                }
            }
        }