Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Navigate to GREG_HOME/ samples/handler/src to find the source code of the Handler Sample.
  2. Add the following dependency to your POM file. Make sure you update the current version of the registry (e.g. 4.4.3) and carbon governance (e.g. 4.5.1). 

    Info

    Project Object Model (POM) is an XML representation of a Maven project held in a file named pom.xml. You should find POM file by the name pom.xml inside GREG_HOME/ samples/handler/src.

    Code Block
    languagehtml/xml
    <dependency>
    <groupId>org.wso2.carbon.registry</groupId>
    <artifactId>org.wso2.carbon.registry.reporting</artifactId>
    <version>
    {current version of the registry<version>${carbon.registry.version}</version>
    </dependency>
    <dependency>
    <groupId>org.wso2.carbon<carbon.commons</groupId>
    <artifactId>org.wso2.carbon.reporting.api</artifactId>
    <version>4<version>${carbon.2commons.0<version}</version>
    </dependency>
    <dependency>
    <groupId>org.wso2.carbon<carbon.commons</groupId>
    <artifactId>org.wso2.carbon.reporting.util</artifactId>
    <version>4<version>${carbon.2commons.0<version}</version>
    </dependency>
    <dependency>
    <groupId>org.wso2.carbon.governance</groupId>
    <artifactId>org.wso2.carbon.governance.api</artifactId>
    <version>{current version of the governance<version>${carbon.governance.version}</version>
    </dependency>
  3. Add a new Java Class named ServiceReportGenerator at GREG_HOME/samples/handler/src/src/main/java/org/wso2/carbon/registry/samples/reporting/ServiceReportGenerator.java with the following source:

    Code Block
    languagejava
    package org.wso2.carbon.registry.samples.reporting;
     
    import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
    import org.wso2.carbon.governance.api.services.ServiceManager;
    import org.wso2.carbon.governance.api.services.dataobjects.Service;
    import org.wso2.carbon.governance.api.util.GovernanceUtils;
    import org.wso2.carbon.registry.core.Registry;
    import org.wso2.carbon.registry.core.session.CurrentSession;
    import org.wso2.carbon.registry.core.utils.RegistryUtils;
    import org.wso2.carbon.registry.reporting.AbstractReportGenerator;
    import org.wso2.carbon.reporting.util.JasperPrintProvider;
    import org.wso2.carbon.reporting.util.ReportParamMap;
    import org.wso2.carbon.reporting.util.ReportStream;
     
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.LinkedList;
    import java.util.List;
     
    public class ServiceReportGenerator extends AbstractReportGenerator {
     
        public ByteArrayOutputStream execute(String template, String type)
                throws IOException {
     
            try {
                Registry registry = getRegistry();
     
                // Read Template
                String templateContent = RegistryUtils.decodeBytes(
                        (byte[]) registry.get(template).getContent());
     
                // Create Report Bean Collection
                ServiceManager manager = new ServiceManager(
                        GovernanceUtils.getGovernanceUserRegistry(registry, CurrentSession.getUser()));
                List<ReportBean> beanList = new LinkedList<ReportBean>();
                for (Service artifact : manager.getAllServices()) {
                    beanList.add(new ReportBean(artifact.getAttribute("overview_name"),
                            artifact.getAttribute("overview_version"),
                            artifact.getAttribute("overview_description")));
                }
     
                // Generate Report Stream
                return new ReportStream().getReportStream(new JasperPrintProvider().createJasperPrint(
                        new JRBeanCollectionDataSource(beanList), templateContent,
                        new ReportParamMap[0]), type);
     
            } catch (Exception e) {
                e.printStackTrace();
            }
     
            return new ByteArrayOutputStream(0);
        }
     
        public static class ReportBean {
            private String overview_name;
            private String overview_version;
            private String overview_description;
     
            public ReportBean(String overview_name, String overview_version,
                              String overview_description) {
                this.overview_name = overview_name;
                this.overview_version = overview_version;
                this.overview_description = overview_description;
            }
     
            public String getOverview_name() {
                return overview_name;
            }
     
            public String getOverview_version() {
                return overview_version;
            }
     
            public String getOverview_description() {
                return overview_description;
            }
        }
     
    }

     

    Info

    The WSO2 Governance Registry uses the Jasper Reporting Engine. Learn more on Jasper Reporting APIs to generate more elegant reports.

     

  4. Compile the source code by running the following command inside GREG_HOME/ samples/handler/src:

    Code Block
    mvn clean install

     

    Info

    The command mvn clean install will trigger an Apache Maven Build in your command line. This requires you having installed Apache Maven. See Installation Prerequisites for links on how to install it.

     A successful run of Apache Maven will generate a report similar to the following:

  5. Copy the GREG_HOME/ samples/handler/src/target/ org.wso2.carbon.registry.samples.handler-4.5.0.jar into GREG_HOME/repository/components/dropins.
  6. Start the WSO2 Governance Registry. See Running the Product for more information.
  7. Navigate to the Resource Browser of Governance Registry and add a new resource as text content with the following parameters:
    1. Name - /_system/governance/repository/components/org.wso2.carbon.governance/templates/service_template.jrxml
    2. Media Type - application/xml
    3. Content - (Copy the content below)

      Code Block
      languagehtml/xml
      <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Service List Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
      	<property name="ireport.zoom" value="1.3310000000000068"/>
      	<property name="ireport.x" value="0"/>
      	<property name="ireport.y" value="0"/>
              <field name="overview_name" class="java.lang.String"/>
              <field name="overview_version" class="java.lang.String"/>
              <field name="overview_description" class="java.lang.String"/>
      	<background>
      		<band height="802" splitType="Stretch"/>
      	</background>
      	<title>
      		<band height="52" splitType="Stretch">
      			<staticText>
      				<reportElement x="0" y="9" width="555" height="27"/>
      				<textElement textAlignment="Center" verticalAlignment="Middle">
      					<font fontName="SansSerif" size="20"/>
      				</textElement>
      				<text><![CDATA[Service List Report]]></text>
      			</staticText>
      		</band>
      	</title>
      	<pageHeader>
      		<band splitType="Stretch"/>
      	</pageHeader>
      	<columnHeader>
      		<band height="29" splitType="Stretch">
      			<staticText>
      				<reportElement x="0" y="0" width="150" height="21"/>
      				<textElement textAlignment="Left" verticalAlignment="Middle">
      					<font fontName="SansSerif" size="12" isBold="true"/>
      				</textElement>
      				<text><![CDATA[Name]]></text>
      			</staticText>
      			<staticText>
      				<reportElement x="150" y="0" width="305" height="21"/>
      				<textElement textAlignment="Center" verticalAlignment="Middle">
      					<font fontName="SansSerif" size="12" isBold="true"/>
      				</textElement>
      				<text><![CDATA[Version]]></text>
      			</staticText>
      			<line>
      				<reportElement x="0" y="21" width="555" height="1"/>
      			</line>
      			<staticText>
      				<reportElement x="455" y="0" width="100" height="21"/>
      				<textElement textAlignment="Center" verticalAlignment="Middle">
      					<font fontName="SansSerif" size="12" isBold="true"/>
      				</textElement>
      				<text><![CDATA[Description]]></text>
      			</staticText>
      		</band>
      	</columnHeader>
      	<detail>
      		<band height="30" splitType="Stretch">
      			<textField>
      				<reportElement x="0" y="4" width="305" height="20"/>
      				<textElement textAlignment="Center" verticalAlignment="Middle"/>
      				<textFieldExpression class="java.lang.String"><![CDATA[$F{overview_name}]]></textFieldExpression>
      			</textField>
      			<textField>
      				<reportElement x="305" y="4" width="150" height="20"/>
      				<textElement textAlignment="Left" verticalAlignment="Middle"/>
      				<textFieldExpression class="java.lang.String"><![CDATA[$F{overview_version}]]></textFieldExpression>
      			</textField>
      			<textField>
      				<reportElement x="455" y="4" width="100" height="20"/>
      				<textElement textAlignment="Center" verticalAlignment="Middle"/>
      				<textFieldExpression class="java.lang.String"><![CDATA[$F{overview_description}]]></textFieldExpression>
      			</textField>
      		</band>
      	</detail>
      	<columnFooter>
      		<band splitType="Stretch"/>
      	</columnFooter>
      	<pageFooter>
      		<band height="29" splitType="Stretch">
      			<staticText>
      				<reportElement x="0" y="3" width="204" height="18"/>
      				<textElement verticalAlignment="Top">
      					<font size="8"/>
      				</textElement>
      				<text><![CDATA[Created with WSO2 Governanace Registry]]></text>
      			</staticText>
      			<line>
      				<reportElement x="0" y="1" width="555" height="1"/>
      			</line>
      			<textField>
      				<reportElement x="246" y="2" width="80" height="20"/>
      				<textElement textAlignment="Right">
      					<font size="8"/>
      				</textElement>
      				<textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
      			</textField>
      			<textField evaluationTime="Report">
      				<reportElement x="327" y="2" width="69" height="20"/>
      				<textElement>
      					<font size="8"/>
      				</textElement>
      				<textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
      			</textField>
      			<textField pattern="dd/MM/yyyy HH.mm.ss">
      				<reportElement x="456" y="3" width="99" height="18"/>
      				<textElement textAlignment="Center">
      					<font size="8"/>
      				</textElement>
      				<textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
      			</textField>
      		</band>
      	</pageFooter>
      	<summary>
      		<band splitType="Stretch"/>
      	</summary>
      </jasperReport>
  8. Click the "Add" button to finish adding the text content.

    Read more on Text Content Creation.

    Info

    The WSO2 Governance Registry uses the Jasper Reporting Engine. The Template File that you created is a Jasper Report Template (.jrxml). Learn more on how to use tools like iReport to generate these template files.

  9. Now, you need to Add a new Report using the following parameters:
    1. Report Name - Service Report
    2. Template - /_system/governance/repository/components/org.wso2.carbon.governance/templates/service_template.jrxml
    3. Report Type - PDF
    4. Report Class - org.wso2.carbon.registry.samples.reporting.ServiceReportGenerator
  10. Click the "Add" button to finish adding the report.

    See the section on Reports to learn how to add reports and manage existing reports.
  11. Add a few Services to Governance Registry. Read more on /wiki/spaces/~yohanna@wso2.com/pages/21233672.
  12. Go to the Manage Reports screen, and select "Generate" to open the Generate Report Screen.
  13. Finally, click on the "Generate" button to generate the report.

    You should now find a PDF file named "Service Report.pdf" getting downloaded.

...