private static ReportJobParametersStruct ReturnReportJobParametersStruct(string M3CompanyCode, string M3CompanyTreePath, string TargetOutputUNCPath, string M3ReportLabelOrTitlePattern) { ReportJobParametersStruct NewReportJobParametersStruct = new ReportJobParametersStruct(); string NewM3CompanyTreePath = M3CompanyTreePath; // trim leading or trailing slashes while (NewM3CompanyTreePath.Length > 0 && NewM3CompanyTreePath.Substring(0, 1) == "/") { NewM3CompanyTreePath = NewM3CompanyTreePath.Substring(1); } while (NewM3CompanyTreePath.Length > 0 && NewM3CompanyTreePath.Substring(NewM3CompanyTreePath.Length - 1, 1) == "/") { NewM3CompanyTreePath = NewM3CompanyTreePath.Substring(0, NewM3CompanyTreePath.Length - 1); } NewReportJobParametersStruct.M3CompanyCode = M3CompanyCode; NewReportJobParametersStruct.M3CompanyTreePath = "/UIRoot/CompanySets/Companies/" + NewM3CompanyTreePath; NewReportJobParametersStruct.TargetOutputUNCPath = TargetOutputUNCPath; NewReportJobParametersStruct.M3ReportLabelOrTitlePattern = M3ReportLabelOrTitlePattern; return(NewReportJobParametersStruct); }
// *** RUN REPORTS FROM TREE *** /// <summary> /// Runs reports existing in an M3 tree folder using a path override option and a partial-text label or name filter</summary> /// <param name="M3CompanyTreePath"> M3 company-level tree path, such as @"/Reporting/Preprocess Reports"; The M3 class prefixes "/UIRoot/CompanySets/Companies/") </param> /// <param name="TargetOutputUNCPath"> UNC path that overrides any existing report output folder specification, such as @"\\HOST3\Millennium Shared Files\Temp" (blank = existing path)</param> /// <param name="M3ReportLabelOrTitlePattern"> This is a pattern-matching filter to select specific reports by looking for text contained within the label or name (blank = no filter, running all reports within the folder)</param> public async Task RunReportsFromTreeAsync(string M3CompanyTreePath, string TargetOutputUNCPath, string M3ReportLabelOrTitlePattern) //treePath As String, uncPath As String, label As String { string classMsgKeyTag = "RunReportsFromTreeAsync"; string MPIMsgKeyTag = "MPIRunReport"; if (M3ClassCancellationNotice.IsCancellationRequested) { return; } int reportsCount = 0; ReportJobParametersStruct TricoreReportJob = ReturnReportJobParametersStruct(M3Company.co, M3CompanyTreePath, TargetOutputUNCPath, M3ReportLabelOrTitlePattern); //MILLREPORTLib.RReport M3Report; try { M3TreeHelper = (MILLTREELib.TreeHelper)M3System.TreeHelper; // uses the system tree with login access M3FolderNode = M3TreeHelper.GetContextNodeFromPath(TricoreReportJob.M3CompanyTreePath, M3Company.guidColumn); // MILLTREELib.IMPICollection get_ContextContents(MILLTREELib.IMPIEntity pCo) - It seems possible access this same node structure (if found) within other companies M3FolderNodeItemsCollection = M3FolderNode.ContextContents[(MILLTREELib.IMPIEntity)M3Company]; // collect the items in the folder -- M3FolderNode.Parent ? } catch (Exception e) { LogMessage(classMsgKeyTag, "Could not find the M3 tree path: " + TricoreReportJob.M3CompanyTreePath); LogMessage(MPIMsgKeyTag, e.Message); this.MessageToPublishEvent("Status", "Could not find the M3 tree path: " + TricoreReportJob.M3CompanyTreePath); this.MessageToPublishEvent("Status", e.Message); this.M3ClassCancellationNotice.Cancel(); return; } if (M3ClassCancellationNotice.IsCancellationRequested) { return; } this.MessageToPublishEvent("Status", "Running reports..."); // ****************************** ////This was a neat approach but it fire reports before Millennium can process them... //List<Task> RunReportsTaskList = new List<Task>(); //Task RunReportsTask; //foreach (MILLTREELib.TNode M3Node in M3FolderNodeItemsCollection) //{ // if (M3Node.NodeType.label == "Report" // && (TricoreReportJob.M3ReportLabelOrTitlePattern == "" // || M3Node.label.Contains(TricoreReportJob.M3ReportLabelOrTitlePattern) // || M3Node.description.Contains(TricoreReportJob.M3ReportLabelOrTitlePattern) // ) // ) // { // M3Report = (MILLREPORTLib.RReport)M3Node.AsReport; // if (TricoreReportJob.TargetOutputUNCPath != "") // { // if (M3Report.PhysicalReport.label.Substring(0, 5) == "TRI_x") // Tricore specific custom reporting system // { // M3Report.Properties.addlFormValueByName["YYYUNCPath"] = TricoreReportJob.TargetOutputUNCPath; // } // else // { // M3Report.Properties.outputDirSpecific = TricoreReportJob.TargetOutputUNCPath; // } // } // RunReportsTask = Task.Factory.StartNew(() => // { // try // { // M3Report.RunReport((MILLREPORTLib.IMPIEntity)M3Company, "NoPrint"); // Tricore specific no-print printer // reportsCount++; // this.MessageToPublishEvent("Reports Count", reportsCount.ToString("N0", ci)); // } // catch (Exception e) // { // this.MessageToPublishEvent("Status", "Could not run " + M3Report.AsNode.DescFieldValue); // this.MessageToPublishEvent("Status", e.Message); // } // }); // RunReportsTaskList.Add(RunReportsTask); // <-- this fires reports before Millennium can process them - some don't run // } //} //RunReportsFromTreeAsyncTask = Task.WhenAll(RunReportsTaskList); // ****************************** RunReportsFromTreeAsyncTask = Task.Factory.StartNew(() => { foreach (MILLTREELib.TNode M3Node in M3FolderNodeItemsCollection) { if (M3ClassCancellationNotice.IsCancellationRequested) { return; } if (M3Node.NodeType.label == "Report" && (TricoreReportJob.M3ReportLabelOrTitlePattern == "" || M3Node.label.Contains(TricoreReportJob.M3ReportLabelOrTitlePattern) || M3Node.description.Contains(TricoreReportJob.M3ReportLabelOrTitlePattern) ) ) { M3Report = (MILLREPORTLib.RReport)M3Node.AsReport; if (TricoreReportJob.TargetOutputUNCPath != "") { if (M3Report.PhysicalReport.label.Substring(0, 5) == "TRI_x") // Tricore specific custom reporting system { M3Report.Properties.addlFormValueByName["YYYUNCPath"] = TricoreReportJob.TargetOutputUNCPath; } else { M3Report.Properties.outputDirSpecific = TricoreReportJob.TargetOutputUNCPath; } //M3Report.Properties.outputFormat = "PDF"; //M3Report.Properties.outputDirFlag = 1; //M3Report.Properties.outputDirSpecific = TricoreReportJob.TargetOutputUNCPath; //M3Report.Properties.outputFilenameFlag = 1; //M3Report.Properties.outputFilenameSpecific = "[COMPANY.CO] [REPORT.CHECKDATESTART(%m-%d-%y)].pdf"; //NOT NECESSARY TO SAVE TO RUN //M3Report.Properties.SaveChanges(); // not necessary to fire this way here } try { // Millennium seems to prefer a synchronous type use of RunReport vs something like RunReportsTaskList.Add(RunReportsTask), which can use WhenAll M3Report.RunReport((MILLREPORTLib.IMPIEntity)M3Company, "NoPrint"); // Tricore specific no-print printer reportsCount++; this.MessageToPublishEvent("Reports Count", reportsCount.ToString("N0", ci)); } catch (Exception e) { this.MessageToPublishEvent("Status", "Could not run " + M3Report.AsNode.DescFieldValue); this.MessageToPublishEvent("Status", e.Message); } } } }); //this works to pickup the end of the task await RunReportsFromTreeAsyncTask.ContinueWith(t => Task.Factory.StartNew(() => { LogMessage(classMsgKeyTag, reportsCount.ToString("N0", ci) + " report" + (reportsCount == 1 ? "" : "s") + " queued"); this.MessageToPublishEvent("Status", reportsCount.ToString("N0", ci) + " report" + (reportsCount == 1 ? "" : "s") + " queued"); })); }