Beispiel #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,
            });
        }
Beispiel #2
0
        /// <summary>
        /// Gets resource data contained in a <see cref="ResourcePackage" />  which has a specified uri.
        /// </summary>
        /// <param name="resourcePackage">The resource package.</param>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        private static IEnumerable <ResourceData> GetResourceDataForUri(ResourcePackage resourcePackage, string uri)
        {
            var matches = from rd in resourcePackage.ResourceStore.ResourceDataDictionary.Values
                          where rd.Uri.CompareTo(uri) == 0
                          select rd;

            return(matches);
        }
Beispiel #3
0
        /// <summary>
        /// The pack source templates folder.
        /// </summary>
        /// <param name="subFolderName">The sub folder name.</param>
        /// <returns>The zipped file name</returns>
        private string PackSourceTemplatesFolder(string subFolderName)
        {
            this.logger.Log(LogType.Trace, this.GetAssemblyName(), "{0}.PackSourceTemplatesFolder - Packing {1}", this.GetType().Name, subFolderName);
            string directory = Path.Combine(this.sourceFolder, subFolderName);
            string package   = this.isDocumentMetaBased ? ResourcePackage.Pack(directory) : ExcelTemplatePackage.Pack(directory);

            // Extract package name from returned
            return(Path.GetFileName(package));
        }
Beispiel #4
0
        /// <summary>
        /// Generates the document.
        /// </summary>
        /// <param name="dataParts">The data parts.</param>
        /// <param name="metadata">The metadata.</param>
        /// <param name="resourcePackage">The resource package.</param>
        /// <param name="exportParameters">The export parameters.</param>
        /// <returns></returns>
        public ExportToMemoryStreamResult GenerateDocument(IEnumerable <IDataPart> dataParts, DocumentMetadataBase metadata, ResourcePackage resourcePackage, ExportParameters exportParameters = null)
        {
            Guard.IsNotNull(dataParts, "dataParts");
            Guard.IsNotNull(metadata, "metadata");
            Guard.IsNotNull(resourcePackage, "resourcePackage");

            var result = new ExportToMemoryStreamResult();

            try
            {
                if (metadata.DocumentMetadataType == DocumentMetadataType.ExcelDocument)
                {
                    result.MemoryStream = this.GenerateExcelInternal(exportParameters, (ExcelDocumentMetadata)metadata, dataParts, resourcePackage);
                }
                else
                {
                    throw new ExportException("Unknown document metadata type");
                }
            }
            catch (Exception ex)
            {
                result.Error = ex;
            }
            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Opens the specified file name.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public static ResourcePackage Open(string fileName, out string errors)
        {
            // avoid contention whilst opening file, should be v quick anyway
            lock (padLock)
            {
                try
                {
                    errors      = null;
                    writeOutput = false;

                    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));

                    ResourcePackage package = new ResourcePackage();

                    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));
                            }
                        }

                        // read all the templates files into our internal store
                        foreach (var m in metadataParts)
                        {
                            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");
                            }

                            string resourceString = System.Text.UTF8Encoding.UTF8.GetString(hasBomb ? data.Skip(3).ToArray() : data);

                            string error = null;
                            if (!package.TryLoadResourceString(m.Uri.OriginalString, resourceString, out error))
                            {
                                if (string.IsNullOrEmpty(errors))
                                {
                                    errors = error;
                                }
                                else
                                {
                                    errors += error;
                                }
                                errors += Environment.NewLine;
                            }
                        }

                        foreach (var f in fileParts)
                        {
                            string name   = f.Uri.OriginalString.Replace(string.Concat(UriDelim, FilesPathPart, UriDelim), null);
                            long   length = f.GetStream().Length;
                            byte[] data   = new byte[length];
                            f.GetStream().Read(data, 0, (int)length);

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

                            package.LoadDesignerFile(name, data);
                        }
                    }

                    package.ValidateInternal();

                    return(package);
                }
                catch (Exception ex)
                {
                    Output(ex.ToString());
                    throw;
                }
                finally
                {
                    writeOutput = false;
                }
            }
        }
Beispiel #6
0
 public void MergeResources(ResourcePackage package)
 {
     this.ResourceStore.ResolveReferences(this.Resources.MergeResources, package);
 }
Beispiel #7
0
        /// <summary>
        /// Extracts the resources.
        /// </summary>
        /// <param name="resourceMetadataList">List of all the <see cref="ResourceMetadata" /> from which resources will be extracted into this <see cref="ResourceStore" /></param>
        /// <param name="resourcePackage"><see cref="ResourcePackage" /> which holds keyed xaml resources</param>
        private void ExtractResources(IEnumerable <ResourceMetadata> resourceMetadataList, ResourcePackage resourcePackage)
        {
            // Go through list of Resource files specified in the metadata package
            foreach (var rm in resourceMetadataList)
            {
                if (string.IsNullOrEmpty(rm.Source))
                {
                    continue;
                }

                // Get Resources from the supplied package
                var searchUri = string.Format("/Metadata/{0}.xaml", rm.Source);
                var matches   = GetResourceDataForUri(resourcePackage, searchUri);

                // Build a list of designer files specified in those resource files
                List <string> designerFiles = new List <string>();
                foreach (var match in matches)
                {
                    // Add the matching resource to this store.
                    this.Add(match);

                    // If the resource has a DesignerFile, then add to a list, so we can load data about those designer files.
                    if (!string.IsNullOrEmpty(match.DesignerFileName) && !designerFiles.Contains(match.DesignerFileName))
                    {
                        designerFiles.Add(match.DesignerFileName);
                    }
                }

                // Go through the list of designer files, adding the designer file
                foreach (var fileName in designerFiles)
                {
                    if (resourcePackage.ResourceStore.OpenXmlPackageDictionary.ContainsKey(fileName) &&
                        this.OpenXmlPackageDictionary.ContainsKey(fileName) == false)
                    {
                        this.AddDesignerFileData(fileName, resourcePackage.ResourceStore.OpenXmlPackageDictionary[fileName].Data);
                    }
                }
            }
        }
Beispiel #8
0
 /// <summary>
 /// Resolves the references.
 /// </summary>
 /// <param name="resourceMetadataList">List of all the <see cref="ResourceMetadata" /> from which resources will be extracted into this <see cref="ResourceStore" /></param>
 /// <param name="resourcePackage"><see cref="ResourcePackage" /> which holds keyed xaml resources</param>
 internal void ResolveReferences(IEnumerable <ResourceMetadata> resourceMetadataList, ResourcePackage resourcePackage)
 {
     this.ExtractResources(resourceMetadataList, resourcePackage);
     this.PopulateModels();
 }