How to use a generated Report as attachment

by Laura Cigardi
12,901 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

In WebRatio it's possible to generate PDF, Excel or RTF reports. This article discusses a particular usage of the WebRatio generated reports. The goal is to create a report in order to use it as attachment for a newsletter (or to store it in the database). Suppose you want to send an email to all the customers with the list of the month's offers as attachment. This list is a PDF report in which there are products details and images. Since this list may vary from a month to another, the report can not be a predefined one, but it has to be calculated every month with the current offers.

Here are the steps in order to make a working example:

  1. Download the "Acme Sample Project" using the menu Help -> WebRatio Samples.
  2. Model the "Offer" report page. You can find information about how to model a report in the WebRatio User Guide in the section Tasks -> Modeling Reports With WebRatio. The model should be something like the figure below. N.B. The report page must be in a public Site View. The Protected flag has not be activated neither on the page nor in none of its container model elements (e.g. Area,...).
  1. In order to launch explicitely the report generation, model a Script Unit that, in the operation chain of the newsletter sending, calls the report page, generates the PDF report and converts it into a blob that can be used by the Email Unit. The newsletter sending operation chain can be something like the following figure.

 

This is the Groovy Script for the Script Unit:

#input String reportUrl, String filename



//The reportUrl is the complete url of the report page and must 

//contain all the parameters required by the page in order to be

//correctly computed



/* The reportUrl is the absolute url to the report page (ex. http://localhost:8080/Acme/pageX.do) 

   The filename is the name of the PDF file to store (ex. OfferList.pdf)

*/



import com.webratio.rtx.RTXBLOBData;

import com.webratio.rtx.blob.BLOBData;

import com.webratio.rtx.RTXBLOBService;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.cookie.CookiePolicy;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.io.FileUtils;

import org.apache.commons.io.IOUtils;



HttpClient client = new HttpClient();

client.getParams().setCookiePolicy(CookiePolicy.DEFAULT);



GetMethod authget = new GetMethod(reportUrl);

client.executeMethod(authget);



byte[] PDF_File = authget.getResponseBody();

String uploadDir = rtx.getUploadDirectory();



File PDF = new File(uploadDir + File.separator+ filename);

PDF.createNewFile();

FileUtils.writeByteArrayToFile(PDF , PDF_File);

RTXBLOBService fileService = rtx.getBLOBService();

RTXBLOBData blobFile = new BLOBData(fileService.getRelativePath(PDF), rtx);

return blobFile


N.B. The operation chain used in this article to model the newsletter sending is just an example. It's not the best way to do it. In fact, in this example the operation chain is synchronous with the user navigation and so the user has to wait until the operation is finished. A better way to realize this feature is to use a Job. Please refer to the article "Getting started with Jobs" to get more information about Jobs and to see a more efficient model.

How to pass the required parameters by the report page in order to be correctly computed

The goal of the following example is to create a reports with the list of some specific products in which there are parameters that must be passed to the units in the report page. In this case the products are retrieved by the Key Attribute (OID). In order to accomplish this example, it is necessary to go through the following steps:

  1. In this example, in the Report page there is the "Products" Multi Data Unit with the "Product OID" Key Condition. In this step you have to understand how to identify the Parameters and how to pass them to the "Products" Multi Data Unit. Suppose to have a Page where there is a Multi Data Unit that retrieves the products with the OIDs (4 and 5), the same products that will be shown in the report. in this case you can add a Link that connects the two Units. In this way you can see in the web browser the generated link URL and discover the required parameters and values.

          The modeled Link results in the rendered page:

         

          You can see the following URL:

     http://localhost:8080/Acme/page52.do?kcond15.attN2F94A6=4_5

          where

          - page52.do is the Report Page
        - kcond15.attN2F94A6 is the Key Condition parameter related to the Key Attribute (OID)
        - 4_5 are the OID values.
       

        NOTE: A more generic and simple way to discover the required parameters that will be added to the URL is check and take a note of the Id of the Conditions required by the units. In fact you can refer to the ID Property in the Properties View of each Condition. (kcond, acond, rcond, etc).

         

        

  1. Modify the Groovy script by adding to the reportUrl the parameters above retrieved. This is the modified version of the previous Groovy Script for the Script Unit:
     #input String reportUrl, String filename, int[] oids
     import org.apache.commons.lang.StringUtils;
     ...
     def stringParam="";
     for (p in oids) {
        stringParam += p +"_"
     }
     stringParam = StringUtils.substringBeforeLast(stringParam, "_");
     GetMethod authget = new GetMethod(reportUrl+"?kcond15.attN2F94A6="+stringParam);
     ...

        NOTE: At line 1 you can see the new input array parameter
     int[] oids 
          related to the OIDs of the products and at line 9 the concatenation of the new report URL
     reportUrl+"?kcond15.attN2F94A6="+stringParam
          Now the URL is complete of required parameters required by the page (OIDs) in order to be correctly computed.
  1. The Link of the first step can be removed. There will be other cases where more types of parameters and where other conditions are required, for example you can consider the following: Retrieve all products where the price is between 1000 and 1500, in this case

          - MinPrice (i.e. acond10) is an Attribute Condition and the Predicate is "Greather than"
          - MaxPrice (i.e. acond11) is an Attribute Condition and the Predicate is "Less than"

          and the complete URL will be

     http://localhost:8080/Acme/page52.do?acond10=1000&acond11=1500