XML Publisher Example

Example of using PeopleCode to generate XML and then merge with a predefined RTF template.
/* GUSAFN02 - HEUP Project, Michael Nitschke 27.11.2007 */
import PSXP_RPTDEFNMANAGER:*;
import GU_EN_ENROLMENTS:*;

Declare Function DeleteLocalFile PeopleCode PSXPFUNCLIB.FUNCLIB FieldFormula;
Declare Function GetDirSeparator PeopleCode PSXPFUNCLIB.FUNCLIB FieldFormula;

/* Function to populate a node based on given SQL.
&node: Create the node in the location you want and pass through to be populated.
&recName: Record that node data will be based on. Must also match data structure returned from &sql.
&sql: SQL that will populate &rec and the node. */
Function AddNodeFromSQL(&node As XmlNode, &recName As
string, &sqlName As string)
Local Record &rec = CreateRecord(@(
"Record." | &recName));
Local SQL &sql = GetSQL(@(
"SQL." | &sqlName), &emplid);
While &sql.Fetch(&rec)
Local XmlNode &nodeRow = &node.AddElement(&node.NodeName |
"Row");
For &f = 1 To &rec.FieldCount
Local XmlNode &nodeFld = &nodeRow.AddElement(&node.NodeName |
"." | &rec.GetField(&f).Name);
&nodeFld.NodeValue = &rec.GetField(&f).Value;
End-For;
End-While;
End-Function;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

&emplid = %EmployeeId;
Local PSXP_RPTDEFNMANAGER:ReportDefn &oRptDefn;
Local XmlDoc &xmlDoc;
&xmlDoc = CreateXmlDoc(
"<?xml version='1.0'?><ACADEMIC_CHARGES/>");

/* Create header, config, programs, charges, unapplied charges and discounts xml elements, including data. */
Local XmlNode &nodeHeader = &xmlDoc.DocumentElement.AddElement(
"Header");
AddNodeFromSQL(&nodeHeader,
"GU_STMT_HDR_DW", "GU_STMT_HDR");
Local XmlNode &nodeConfig = &xmlDoc.DocumentElement.AddElement(
"Config");
AddNodeFromSQL(&nodeConfig,
"GU_STMT_CFG_DTL", "GU_STMT_CFG_DTL_AC");
Local XmlNode &nodeProgram = &xmlDoc.DocumentElement.AddElement(
"Program");
AddNodeFromSQL(&nodeProgram,
"GU_STMT_PROG_DW", "GU_STMT_PROG");
Local XmlNode &nodeCharges = &xmlDoc.DocumentElement.AddElement(
"Charges");
AddNodeFromSQL(&nodeCharges,
"GU_STMT_CHRG_DW", "GU_STMT_CHARGES");
Local XmlNode &nodeChargesGST = &xmlDoc.DocumentElement.AddElement(
"ChargesGST");
AddNodeFromSQL(&nodeChargesGST,
"GU_STMT_CGST_DW", "GU_STMT_CHARGES_GST");
Local XmlNode &nodeUnappliedCharges = &xmlDoc.DocumentElement.AddElement(
"UnappliedPayments");
AddNodeFromSQL(&nodeUnappliedCharges,
"GU_STMT_CHRG_DW", "GU_STMT_UNAPPLIED_PAYMENTS");
Local XmlNode &nodeDiscounts = &xmlDoc.DocumentElement.AddElement(
"Discounts");
AddNodeFromSQL(&nodeDiscounts,
"GU_STMT_CHRG_DW", "GU_STMT_DISCOUNTS");
Local XmlNode &nodeMessages = &xmlDoc.DocumentElement.AddElement(
"Messages");
AddNodeFromSQL(&nodeMessages,
"GU_STMT_MSG_DW", "GU_STMT_MESSAGES");

/* Create report definition and connect to the generated XML document. */
&rptDefn = create PSXP_RPTDEFNMANAGER:ReportDefn(
"GU_ACAD_CHRG");
&rptDefn.Get();
&rptDefn.SetRuntimeDataXMLDoc(&xmlDoc);

/* Create output directories. */
&sDirSep = GetDirSeparator();
&RptOutputDir = GetEnv(
"PS_SERVDIR") | &sDirSep | "files" | &sDirSep | "XMLP" | &sDirSep | UuidGen();
&OutputDir = &RptOutputDir | &sDirSep |
"RptInst";
&DataDir = &RptOutputDir | &sDirSep |
"Data";
CreateDirectory(&OutputDir, %FilePath_Absolute);
CreateDirectory(&DataDir, %FilePath_Absolute);

/* Process. */
&rptDefn.OutDestination = &RptOutputDir;
&rptDefn.ProcessReport(
"", %Language_User, %Date, "");
CommitWork();

/* Display. */
&rptDefn.DisplayOutput();

/* Cleanup. */
DeleteLocalFile(&sOutputFile, %FilePath_Absolute);

3 comments:

Pat said...

Nutters
Maybe use
RemoveDirectory(&RptOutputDir, %FilePath_Absolute + %Remove_Subtree);

To remove both file and random directory.

Pat

Michael Nitschke said...

I'm not sure (as I wrote this 7 years ago and haven't got an example handy right now to test) but might removing the output directory and file remove the output directory and file, leaving you with nothing to publish???

Pat said...

Call DisplayOutput() before removing file and directory