private async void readTemplate(FileInfo info) { TemplateMessage = "Loading template..."; OnPropertyChanged(() => TemplateMessage); FileStream stream; try { stream = info.OpenRead(); } catch (Exception) { TemplateMessage = "Error: file in use"; OnPropertyChanged(() => TemplateMessage); return; } byte[] buf = new byte[stream.Length]; int x = await stream.ReadAsync(buf, 0, buf.Length); await reprepo.SaveTemplateAsync(ReportDefinition.ID, buf); IReportType rt = reportingService.GetReportType(ReportDefinition.ReportType); Template = rt.CreateTemplate(buf); ReportDefinition.TemplateFileName = Path.GetFileName(info.FullName); if (SectionsCV != null) { SectionsCV.CurrentChanged -= SectionsCV_CurrentChanged; } SectionsCV = new ListCollectionView(new List <string>(Template.SectionNames)); SectionsCV.CurrentChanged += SectionsCV_CurrentChanged; raise(); }
private async void ExecuteDefaultTemplate() { byte[] buf; IReportType rt = reportingService.GetReportType(ReportDefinition.ReportType); if (rt.GetDefaultTemplate(out buf)) { TemplateMessage = "Loading template..."; OnPropertyChanged(() => TemplateMessage); await reprepo.SaveTemplateAsync(ReportDefinition.ID, buf); Template = rt.CreateTemplate(buf); ReportDefinition.TemplateFileName = "DefaultTemplate"; if (SectionsCV != null) { SectionsCV.CurrentChanged -= SectionsCV_CurrentChanged; } SectionsCV = new ListCollectionView(new List <string>(Template.SectionNames)); SectionsCV.CurrentChanged += SectionsCV_CurrentChanged; raise(); } else { TemplateMessage = "Sorry no default template defined for " + rt.Name + ". Best to design a template manually then load it in."; } }
public async Task <bool> FillReportAsync(ITemplate t, IReportType rt, Dictionary <string, dynamic> stuffing, Stream output) { if (t == null) { throw new ArgumentNullException("t"); } if (output == null) { throw new ArgumentNullException("output"); } bool ok = await rt.Filler.FillAsync(t, stuffing, output); return(ok); }
private async Task <string> writeReportAsync(ITemplate t, IReportType rt, Dictionary <string, dynamic> stuffing, string preferredFilename, string outputFolder, string subfolder = null) { if (string.IsNullOrWhiteSpace(preferredFilename)) { throw new ArgumentException("give me a valid preferredFilename"); } // find a valid output file path string outputPath = PrepareOutputFilePath(preferredFilename, outputFolder, subfolder); using (var stream = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) { bool ok = await FillReportAsync(t, rt, stuffing, stream); stream.Flush(); } return(outputPath); }
public async void Load(ReportInfo specifiedReport) { clearOut(); // track changes this.originalInfo = specifiedReport.Copy(); this.ReportDefinition = specifiedReport; // parameters if (Parameters != null) { Parameters.CollectionChanged -= paramsCollection_CollectionChanged; } Parameters = ReportDefinition.Parameters; Parameters.CollectionChanged += paramsCollection_CollectionChanged; // template if (SectionsCV != null) { SectionsCV.CurrentChanged -= SectionsCV_CurrentChanged; } byte[] buf = await reprepo.GetTemplateAsync(ReportDefinition.ID); if (buf == null) { Template = null; TemplateMessage = string.Format("No template selected"); SectionsCV = null; } else { IReportType rt = reportingService.GetReportType(ReportDefinition.ReportType); Template = rt.CreateTemplate(buf); SectionsCV = new CollectionView(Template.SectionNames); SectionsCV.CurrentChanged += SectionsCV_CurrentChanged; } // notify raise(); }
//imm: the immutable way ? //public async Task<Dictionary<string, dynamic>> WriteReportsAsync(ReportInfo report, Dictionary<string, dynamic> stuffing, string outputFolder, Stream stdout = null) /// <summary> /// handles single documents, multiple documents in a folder, or all that in many folders. /// The passed in stuffing is updated with actual paths used /// folder: overwritten /// file: unchanged /// filePath: full path to the generated file /// The folder and file dictionaries and lists are converted to List[Dictionary[string,dynamic]] /// for simpler management if you need to read it and post-process the generated files. /// The deeper lists and dictionaries below the single document level are untouched. /// </summary> /// <param name="report"></param> /// <param name="stuffing"></param> /// <param name="outputFolder"></param> /// <param name="stdout"></param> /// <returns></returns> public async Task WriteReportsAsync(ReportInfo report, Dictionary <string, dynamic> stuffing, string outputFolder, Stream stdout = null) { if (stuffing.ContainsKey("folders")) { // recurse into MULTIPLE document generation, for MULTIPLE folders /* * [folders] * [dic] * [folder] * [files] * [dic]... */ //imm: Dictionary<string, dynamic> newResult = new Dictionary<string, dynamic>(); List <Dictionary <string, dynamic> > newFoldersList = new List <Dictionary <string, dynamic> >(); var folders = stuffing["folders"] as IEnumerable <object>; foreach (var folderDic in folders) { var goodDic = getDictionary(folderDic); await WriteReportsAsync(report, goodDic, outputFolder, stdout); newFoldersList.Add(goodDic); } stuffing["folders"] = newFoldersList; //imm: newResult["folders"] = newFoldersList; } // convention: if there is a ["folder"] of string, and a ["files"] of IEnumerable<IDictionary<?,?>> then generate many files in to folder else if (stuffing.ContainsKey("folder") && stuffing.ContainsKey("files")) { // MULTIPLE document generation for a single script run IReportType rt = GetReportType(report.ReportType); byte[] templateBytes = await reprepo.GetTemplateAsync(report.ID); if (templateBytes == null) { throw new Exception("Can't generate a report with no template."); } var t = rt.CreateTemplate(templateBytes); List <Dictionary <string, dynamic> > newFilesList = new List <Dictionary <string, dynamic> >(); string folder = safeFolder((string)stuffing["folder"]); var files = stuffing["files"] as IEnumerable <object>; // the list of single document context dictionaries foreach (var f1 in files) { var dic = getDictionary(f1); string preferredFilename = null; // convention: ["file"] is the desired file name (required) if (dic.ContainsKey("file")) { preferredFilename = dic["file"] as string; } if (preferredFilename == null) { throw new Exception("[file] not set"); } dic["filePath"] = await writeReportAsync(t, rt, dic, preferredFilename, outputFolder, folder); newFilesList.Add(dic); } stuffing["folder"] = folder; // safe folder stuffing["files"] = newFilesList; // standardised list of dic } else { // SINGLE document generation for a single script run string preferredFilename = null; if (stuffing.ContainsKey("file")) { preferredFilename = stuffing["file"] as string; } // allow auto name if just one document IReportType rt = GetReportType(report.ReportType); byte[] templateBytes = await reprepo.GetTemplateAsync(report.ID); if (templateBytes == null) { throw new Exception("Can't generate a report with no template."); } var t = rt.CreateTemplate(templateBytes); if (preferredFilename == null) { preferredFilename = report.Name + "." + rt.DocumentType; } stuffing["filePath"] = await writeReportAsync(t, rt, stuffing, preferredFilename, outputFolder); } }
public Task <bool> FillReportAsync(ITemplate t, IReportType rt, Dictionary <string, dynamic> stuffing, Stream output) { return(baseService.FillReportAsync(t, rt, stuffing, output)); }