Tuesday, March 11, 2014

DynamicReports in Java (2) - Using ad hoc data types

1. Create a simple tabular report

      import net.sf.dynamicreports.adhoc.AdhocManager;
      import net.sf.dynamicreports.adhoc.configuration.AdhocColumn;
      import net.sf.dynamicreports.adhoc.configuration.AdhocConfiguration;
      import net.sf.dynamicreports.adhoc.configuration.AdhocReport;
      import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
      import net.sf.dynamicreports.report.builder.component.Components;
      import net.sf.dynamicreports.report.constant.HorizontalAlignment;
      import net.sf.dynamicreports.report.datasource.DRDataSource;
      import net.sf.dynamicreports.report.exception.DRException;
      import net.sf.jasperreports.engine.JRDataSource;

      public class AdhocReportTest1 {
           public AdhocReportTest1() {
                build();
           }
 
           public void build() {
               //Creating the AdhocConfiguration for the report
               AdhocConfiguration config = new AdhocConfiguration();
               AdhocReport report = new AdhocReport();
               config.setReport(report);

               //Creating the columns for the report
               AdhocColumn column = new AdhocColumn();
              column.setName("Name");
              report.addColumn(column);
     
              column = new AdhocColumn();
              column.setName("Price");
               report.addColumn(column);
     
              column = new AdhocColumn();
              column.setName("Quantity Ordered");
              report.addColumn(column);
     
              try {
                  //Can save the configuration and reload in the future
//                  AdhocManager.saveConfiguration(config, new FileOutputStream(new File("c:/temp/configuration.xml")));
//                  AdhocConfiguration loadedConfiguration = AdhocManager.loadConfiguration(new FileInputStream("c:/temp/configuration.xml"));

                  JasperReportBuilder reportBuilder = AdhocManager.createReport(config.getReport());

                  //Setting the title of the report
                   reportBuilder.addTitle(Components.text("List of Orders\n------------")
                          .setHorizontalAlignment(HorizontalAlignment.CENTER));

                  //Setting the data source
                  reportBuilder.setDataSource(createDataSource());
                  reportBuilder.show();
               } catch (DRException e){
                      e.printStackTrace();
//              } catch (FileNotFoundException e) {
//                   e.printStackTrace();
               }
          }
 
          private JRDataSource createDataSource() {
              DRDataSource dataSource = new DRDataSource("Name", "Price", "Quantity Ordered");
              dataSource.add("-----", "-----", "-----");
              dataSource.add("Apple", 1.29, 120.00);
              dataSource.add("Apple", 1.69, 150);
              dataSource.add("Orange", 0.99, 130);
              dataSource.add("Orange", 0.96, 100);
              dataSource.add("Mange", 2.99, "three hundred");
              return dataSource;
          }
 
          public static void main(String[] args){
              new AdhocReportTest1();
          }
      }

2. Create a customized tabular report

import net.sf.dynamicreports.adhoc.AdhocManager;
import net.sf.dynamicreports.adhoc.configuration.AdhocCalculation;
import net.sf.dynamicreports.adhoc.configuration.AdhocColumn;
import net.sf.dynamicreports.adhoc.configuration.AdhocConfiguration;
import net.sf.dynamicreports.adhoc.configuration.AdhocGroup;
import net.sf.dynamicreports.adhoc.configuration.AdhocReport;
import net.sf.dynamicreports.adhoc.configuration.AdhocSort;
import net.sf.dynamicreports.adhoc.configuration.AdhocStyle;
import net.sf.dynamicreports.adhoc.configuration.AdhocSubtotal;
import net.sf.dynamicreports.adhoc.configuration.AdhocSubtotalPosition;
import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;
import net.sf.dynamicreports.report.datasource.DRDataSource;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

public class AdhocReportTest2 {
    public AdhocReportTest2() {
        build();
    }

    public void build() {
        //Creating the AdhocConfiguration for the report
        AdhocConfiguration config = new AdhocConfiguration();
        AdhocReport report = new AdhocReport();
        config.setReport(report);

        AdhocColumn column = new AdhocColumn();
        column.setName("Price");
        report.addColumn(column);

        column = new AdhocColumn();
        column.setName("Quantity");
        report.addColumn(column);
     
        //Creating group by
        AdhocGroup group = new AdhocGroup();
        group.setName("Name");
        report.addGroup(group);
     
        //Creating the subtotals
        AdhocSubtotal subtotal = new AdhocSubtotal();
        subtotal.setGroupName("Name");
        subtotal.setLabel("Average Price");
        subtotal.setName("Price");
        subtotal.setCalculation(AdhocCalculation.AVERAGE);
        subtotal.setPosition(AdhocSubtotalPosition.GROUP_FOOTER);
        report.addSubtotal(subtotal);
     
        subtotal = new AdhocSubtotal();
        subtotal.setGroupName("Name");
        subtotal.setName("Quantity");
        subtotal.setLabel("Total ordered");
        subtotal.setCalculation(AdhocCalculation.SUM);
        subtotal.setPosition(AdhocSubtotalPosition.GROUP_FOOTER);
        report.addSubtotal(subtotal);
     
        //Creating sort by
        AdhocSort sort = new AdhocSort();
        sort.setName("Name");
        report.addSort(sort);

        try {
            JasperReportBuilder reportBuilder = AdhocManager.createReport(config.getReport());

            //Setting the title of the report
            reportBuilder.addTitle(Components.text("Summary of Orders")
                    .setHorizontalAlignment(HorizontalAlignment.CENTER));

            //Setting the data source
            reportBuilder.setDataSource(createDataSource());
            reportBuilder.show();
        } catch (DRException e) {
            e.printStackTrace();
        }
    }

    private JRDataSource createDataSource() {
        DRDataSource dataSource = new DRDataSource("Name", "Price", "Quantity");
        dataSource.add("Apple", 1.29, 120);
        dataSource.add("Apple", 1.69, 150);
        dataSource.add("Orange", 0.99, 130);
        dataSource.add("Orange", 0.96, 100);
        dataSource.add("Mange", 2.99, 300);
        return dataSource;
    }

    public static void main(String[] args) {
        new AdhocReportTest2();
    }
}

          Previous<      >Next

---------------------------------------------------------------------------------------------------------------
       
                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
--------------------------------------------------------------------------------------------------------------------------

References:

1.DynamicReports in Java (1) - Using defined data type and the steps for a basic report
2. DynamicReports in java (3) - Concatenating reports
3. DynamicReports in java (4) - Setting/formatting the title
4. DynamicReports in java (5) - subreport and page break
5. Getting started
6. Reporting In Java Using DynamicReports And JasperReports

No comments:

Post a Comment