Publication examples

Ths document gives various examples of business object publications.

Print template types

Publication templates can use 3 processing mechanism:

The 2 first examples bellow are using substitutions, the last examples are using custom methods.

vCard publication

If a business objects includes identity data it may be published as a vCard to be imported on contact management systems, e-mail clients, mobile devices, ...

An example of vCard publication (using a dedicated publication template) is provided on the standard User object:

[BEGIN]
BEGIN:VCARD
VERSION:2.1
N:[VALUE:usr_last_name];[VALUE:usr_first_name]
TEL;CELL:[VALUE:usr_cell_num]
TEL;WORK;VOICE:[VALUE:usr_work_num]
TEL;HOME;VOICE:[VALUE:usr_home_num]
EMAIL;PREF;INTERNET:[VALUE:usr_email]
UID:[VALUE:usr_login]
ADR;WORK:;[VALUE:usr_address2];[VALUE:usr_address1];[VALUE:usr_city];[VALUE:usr_state];[VALUE:usr_zipcode];[VALUE:usr_country]
PHOTO;ENCODING=BASE64:[EXPR:res = DocumentDB.toBase64(obj.getField("usr_image_id").getValue(), obj.getGrant());]
END:VCARD
[END]

Note the usage of a DocumentDB API for inlining the user picture as a base 64 encoded string, the global [BEGIN] / [END] publication template statements allows to export multiple vCards at the same time (beware that this bulk import is not possible with every e-mail clients).

For instance the import of published data renders as follows in Microsoft Outlook®:

RSS publication

The simplest approach for publishing business object data as a RSS feed is to configure a publication template such as the one used by default for the WebNewsobject (publication template name is WebNewsAsRSS):

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>[LABEL:WebNews]</title>
        <description>[LABEL:WebNews]</description>
        <generator>Simplicite</generator>
        <ttl>15</ttl>
[BEGIN]
        <item>
            <title><![CDATA[[VALUE:nws_title]]]></title>
            <description><![CDATA[[VALUE:nws_description]]]></description>
            <pubDate>[VALUE:nws_date]</pubDate>
        </item>
[END]
    </channel>
</rss>

If the RSS is to be public the object must be granted in read mode to the PUBLIC group, so as the publication template.

Custom method for text content

The example bellow is for a simple printable HTML content:

MyObject.myPublicationMethod = function(pt) {
    var t = this.getFieldValue("myTitleField");
    var out = HTMLTool.openSimplePage(t, null, null);
    out += HTMLTool.cssBlock("@media print { @page { size: portrait A4; margin: 0; }");
    out += "<h1>" + pt.getLabel() + ": " + t + "</h1>";
    out += "<p>" + this.getFieldValue("myHTMLContentField") + "</p>";
    out += HTMLTool.closePage();
    return out;
};

For text contents the return type of method must be String

Custom method for binary content

The example bellow is for a simple PDF content using the iText® library and its wrapper tool (publication template MIME type must be set to PDF):

MyObject.myPublicationMethod = function(pt) {
    importPackage(Packages.com.lowagie.text);
    try {
        var out = new java.io.ByteArrayOutputStream();
        var pdf = PDFTool.open(out);
        pdf.add(new Phrase(this.getFieldValue("myTextContentField")));
        PDFTool.close(pdf);
        return out.toByteArray();
    } catch(e) {
        console.error(e);
    }
};

The example bellow is for a simple Microsoft Excel® content using the Apache POI® library and its wrapper tool (publication template MIME type must be set to XLS):

MyObject.myPublicationMethod = function(pt) {
    try {
        var xls = new ExcelPOITool(); // or ExcelPOITool(true); as of version 4.0, the true argument means using XLSX format
        var s = xls.newSheet(this.getLabel());
        var r = xls.newRow(0);
        r.add(xls.newCell(0, this.getFieldValue("myLabelField")));
        r.add(xls.newCell(1, this.getField("myValueField").getDouble()));
        s.add(r);
        xls.add(s);
        return xls.generateToByteArray();
    } catch(e) {
        console.error(e);
    }
};

The example bellow is for a simple Microsoft Word® content using the Docx4j® library (publication template MIME type must be set to DOCX):

MyObject.myPublicationMethod = function(pt) {
    importPackage(Packages.org.docx4j.openpackaging.packages);
    importPackage(Packages.org.docx4j.openpackaging.parts.WordprocessingML);
    try {
        var pkg = WordprocessingMLPackage.createPackage();
        var doc = pkg.getMainDocumentPart();
        doc.addStyledParagraphOfText("Title", this.getDisplay());
        doc.addParagraphOfText(this.getFieldValue("myTextField"));
        var res = new java.io.ByteArrayOutputStream();
        pkg.save(res);
        return res.toByteArray()
    } catch(e) {
        console.error(e);
    }
};

For binary contents the return type of method must be byte[]