Jump to content
We've recently updated our Privacy Statement, available here ×

thangalin

Members
  • Posts

    322
  • Joined

  • Last visited

 Content Type 

Profiles

Forum

Events

Featured Visualizations

Knowledge Base

Documentation (PDF Downloads)

Blog

Documentation (Test Area)

Documentation

Dr. Jaspersoft Webinar Series

Downloads

Posts posted by thangalin

  1. For Jaspersoft Studio 6.2, the solution outlined here appears to work.

     

    Create Variable


    Create a variable as follows:

     

    1. Create a variable called `V_CURRENT_PAGE_NUMBER`
    2. Select the variable to open its properties (illustrated below)
    3. Set **Expression** to: `1`
    4. Set **Initial Value Expression** to: `$V{PAGE_NUMBER} + 1`
    5. Set **Reset type** to: `Page`

    These settings are illustrated in the following figure:

     


     

    Setting the **Expression** to `1` prevents its value from being `null`. That is, if the footer shows *Page **null** of 4* it probably means that the **Expression** hasn't been set.

     

    The variable is created.

     

    Add Page Footer


    Add a **Page Footer** band as follows:

     

    1. Select the report in the outline panel
    2. Check **Summary With Page Header And Footer** to ensure the page footer appears on the summary page.
    3. Add a **Page Footer** band.

    The footer is added.

     

    Create Text Field


    Create a text field as follows:

     

    1. Drag and drop a *single text field* onto the **Page Footer** band.
    2. Select the text field.
    3. Set **Expression** to: `msg("Page {0} of {1}", $V{V_CURRENT_PAGE_NUMBER}, $V{PAGE_NUMBER})`
    4. Set **Evalutation Time** to: `Auto`

    These settings are illustrated in the following figure:

     


     

    The single text field is created.

     

    Preview Report


    For a report with three pages plus a summary page, previewing the report shows:

     


     

    The summary page shows:

     


     


  2. This answer makes the following assumptions:

    • The class files and resource bundles are located in the "build" directory (should created by default when writing custom Java classes).
    • The distribution directory for the Java archive will be named "dist".
    • The images used by the reports are in an "images" directory and in SVG format.
    • The report templates have been compiled and are in the main project folder.

    Now:

    1. In the project folder create a file named "build_dist.xml"
    2. Copy the following content into build_dist.xml:
    <?xml version="1.0"?>
    <project name="DistributionLibrary" default="dist" basedir=".">
      <description>Archives templates, bundles, class files.</description>
     
      <property name="build" location="build" />
      <property name="dist" location="dist" />
     
      <target name="dist" description="Build distribution archive.">
        <mkdir dir="${dist}" />
     
        <jar destfile="${dist}/templates.jar">
          <fileset dir="${build}" includes="**/*" />
          <fileset dir="." includes="images/*.svg" />
          <fileset dir="." includes="**/*.jasper"/>
        </jar>
      </target>
    </project>
     
    1. Right-click on the project name.
    2. Select Properties.
    3. Select Builders.
    4. Click New.
    5. Select Ant Builder.
    6. Click OK to display the Edit Configuration dialog.
    7. Set Buildfile to: ${project_loc}/build_dist.xml
    8. Set Base Directory to: ${project_loc}
    9. Click the Targets tab.
    10. Click Set Targets beside "After a 'Clean'".
    11. Uncheck dist, if it is checked.
    12. Click OK.
    13. Click Set Targets beside "Manual Build".
    14. Check dist, if it is unchecked.
    15. Click OK.
    16. Click OK to close the Edit Configuration dialog.
    17. Click OK to close the Project properties dialog.

    The archive is now bulit automatically after each successful project build. Try it as follows:

    1. Select Project >> Clean.
    2. Click OK.

    In the Console panel you should see BUILD SUCCESSFUL.

    Once the build is successful, the dist directory will contain a file called templates.jar that contains an archive that can be used as a resource for reports (in a Java application).

  3. The following seems to work:
     
    1. Click Window >> Prefereces
    2. Select Capabilities
    3. Check Development
    4. Click OK

    Next:

    1. Open the Project Explorer
    2. Right-click Project name
    3. Select Properties
    4. Select Java Build Path
    5. Click the Source tab
    6. Click Add Folder
    7. Select Create New Folder
    8. Set Folder Name to: src
    9. Click Finish
    10. Select src
    11. Click OK
    12. Set Default output folder: Project Name/build
    13. Click OK

    Create a report as usual (with a text field that uses a date, either a parameter or a field), then:

    1. Select the report in the Outline panel
    2. Open the Properties panel
    3. Set Format Factory Class to: com.company.reports.ReportFormatFactory

    Next, create some source code inside the "src" directory in a package (folder) named "com.company.reports". Paste the following into a file named "ReportFormatFactory.java" that is saved in that directory:

    import java.text.DateFormat;
    import java.util.Locale;
    import java.util.TimeZone;
     
    import net.sf.jasperreports.engine.util.DefaultFormatFactory;
     
    /**
     * Delegates creation of date and number formatters to JasperReports' default
     * formatters. This class ensures that dates are formatted consistently across
     * all reports.
     */
    public class ReportFormatFactory extends DefaultFormatFactory {
      /**
       * Returns a DateFormat instance that creates dates in YYYY/MM/dd format.
       *
       * @param pattern Unused.
       * @param locale Passed to the DefaultFormatFactory instance.
       * @param timezone Passed to the DefaultFormatFactory instance.
       *
       * @return An object that can format dates.
       */
      @Override
      public DateFormat createDateFormat(
        String pattern, Locale locale, TimeZone timezone ) {
        return super.createDateFormat( "YYYY/MM/dd", locale, timezone );
      }
    }

    When you run the report, the date should be formatted as YYYY/MM/dd.

    If you want to change the format (e.g., to "dd/MM/YYYY"), update the date format line in the source code and then restart Jaspersoft Studio (the classloader does not seem to reload the ReportFormatFactory class after modification).

    To avoid having to restart each time the date format changes, use a resource bundle:

    1. Create a new project folder called i18n
      1. ​Right-click on the project name
      2. Select New >> Folder
      3. Set Folder name to i18n
      4. Click Finish
    2. Right-click on i18n
    3. Select New >> Other
    4. Expand Messages Editor
    5. Select ResourceBundle
    6. Click Next
    7. Set the name to ReportsLocale
    8. Add a Locale (e.g., en_US)
    9. Click Finish

    Add the i18n directory to the build process:

    1. Right-click i18n
    2. Select Build Path >> Configure Build Path
    3. Click Add Folder
    4. Check i18n
    5. Click OK
    6. Click OK again

    Change the createDateFormat method:

      @Override
      public DateFormat createDateFormat(
        String pattern, Locale locale, TimeZone timezone ) {
        String dateFormat = DATE_FORMAT_DEFAULT;
     
      try {
       ResourceBundle rb = ResourceBundle.getBundle( "ReportsBundle" );
         String df = rb.getString( DATE_FORMAT );
     
         if( df != null ) {
           dateFormat = df;
         }
      }
      catch( Exception e ) {
       // If the resource bundle isn't found, use the default date format.
       // TODO: Pass this exception into a logger.
      }
     
        return super.createDateFormat( dateFormat, locale, timezone );
      }

    And add these constants to the class definition (immediately after the publc class declaration, around line 15/16):

      private final static String DATE_FORMAT = "date.format";
      private final static String DATE_FORMAT_DEFAULT = "YYYY/MM/dd";
     
    Restart Jaspersoft Studio, then:
     
    1. Edit the ReportsLocale file
    2. Add a date.format property
    3. Set the property value to: dd/MM/YYYY
    4. Set the property value for all locales.

    When the report is run, the date should look like 29/02/1976, for example.

  4. Background

    Sometimes many reports and subreports must use the same default format (e.g., "YYYY/MM/dd") while only a handful of fields are an exception (e.g., "YYYY/MM").

    Question

    What are the exact steps to ensure all date fields use the same date format, which can be changed in one location, without having to modify each field individually, such that individual fields can easily override the default?

    Research

    Here is a list of resources that fail to answer this question:

    The Definitive Guide seems to copy the Javadocs verbatim, which doesn't offer any additional insight.

    Ideas

    Here's how I thought it might be possible to set the formatFactoryClass attribute for a given report:

    1. Create a new Project
    2. Open Project Explorer
    3. Right-click on project name
    4. Select Properties
    5. Expand Jaspersoft Studio
    6. Click Properties
    7. Click Configure Workspace Settings button
    8. Click Add
    9. Set property name to any of: {net.sf.jasperreports.formatFactoryClass, or formatFactoryClass, or format.factory.class}
    10. Set value to (supply your own instance): com.package.reports.ReportFormatFactory
    11. Click OK to close Properties Dialog
    12. Click OK to close Preferences
    13. Click OK to close Properties for Project

    This would also require a Library that includes the com.package.reports.ReportFormatFactory class inside.

  5. Hi, Arun.

    See this answer: http://stackoverflow.com/a/5520421/59087

    1. Paths must be absolute.
    2. Only compile .jrxml files to .jasper files if the .jrxml is being modified. Usually you can just load the .jasper file and skip compilation altogether. It is much faster.
    3. Store .jasper and .jrxml files outside of your web root.
    4. Create the following variables throughout all your reports:
      • ROOT_DIR = "/full/path/to/reports/"
      • IMAGE_DIR = $P{ROOT_DIR} + "images/"
      • STYLES_DIR = $P{ROOT_DIR} + "styles/"
      • SUBREPORT_DIR = $P{ROOT_DIR} + "subreports/"
      • COMMON_DIR = $P{ROOT_DIR} + "common/"
    5. Reference items relative to $P{ROOT_DIR} (e.g., $P{IMAGE_DIR} is defined in terms of $P{ROOT_DIR}).
    6. Pass the value of $P{ROOT_DIR} in from your environment.

    --
    Read chapter 15 of Indispensable, a book on JasperReports, available at:
    http://www.whitemagicsoftware.com/books/indispensable/

  6. Hi, Sandee.

    1. Visit http://whitemagicsoftware.com/books/indispensable/
    2. Download the Software (v1.0.0) - it's free.
    3. Take a look at com/whitemagicsoftware/report/ReportImpl.java

    See also the attached file for a minimal example of how to create a PDF using JasperReports. You will have to supply your own .jasper file (compiled from a .jrxml file). This presumes a database connection to a local postgresql database is possible. You will have to change the database connectivity to match your database configuration and software.

    Read the JasperReports API to find out how to compile .jrxml files to .jasper files.

    --
    Read chapter 15 of Indispensable, a book on JasperReports, available at:
    http://www.whitemagicsoftware.com/books/indispensable/

  7. You can use two lists (use java.util.List).

     

    The first list contains the names of the parameters.

     

    The second list contains the values of the parameters.

     

    See also the Report Integration Framework that illustrates how to pass these parameters generically.

     

    http://www.whitemagicsoftware.com/software/java/rif/

     

    --

     

    Read chapter 15 of Indispensable, a book on JasperReports, available at:

     

    http://www.whitemagicsoftware.com/books/indispensable/

  8. Many JasperReports integrations based on JSP or JSF technology have two dangerous security flaws:

    • Database connection credentials.
    • Expose SQL statements.

    Database Connection Credentials

    Across various websites, I see answers to questions about JSP and JSF report integrations similar to the following:

    <%@ page import="java.sql.DriverManager"%><%String url = "jdbc:sqlserver://localhost:1433;DatabaseName=MIDAS10DB";Cnnection connection = DriverManager.getConnection(url, "username", "password" );[/code]

    Even though JasperReports needs a database connection, the user name and password must never be embedded in the JSP or JSF page itself. A mis-configuration of the web server, or a software bug, could accidentally expose key user names and passwords to anyone.

    Expose SQL Statements

    JasperReports uses compiled report templates (.jasper files) to generate the final report, such as a PDF. These templates contain SQL statements. If a malcious hacker were to guess the file name and download the file directly, it would expose a database attack vector.

    Quite often, SQL statements leverage database-specific features. By reading the SQL, a sufficiently knowledgeable hacker can make an educated guess on the database being used and possibly the database version. This can indirectly expose other information, such as the web server's operating system. (If the SQL uses Microsoft Access features, then chances are the operating system is Windows-based.)

    Most systems store the .jrxml and the .jasper files in the same directory as the JSP or JSF files, allowing them to be downloaded. Developers should avoid this situation.

    The Solution

    The solution for the second problem is easy: store the report template files outside of the web server's directory.

    The solution for the first problem is not simple. I have written a development framework that abstracts how the database connection is established. The implementation uses JNDI, but could equally leverage a different technology, without requiring any changes to the web application.

    http://www.whitemagicsoftware.com/software/java/rif/

    http://www.whitemagicsoftware.com/software/java/rif/api/

    Read Chapter 15 (available for free) of Indispensable to see how it works:

    http://www.whitemagicsoftware.com/books/indispensable/

     

  9. Ah, I thought you were using JasperReports, not JasperServer. The integration framework isn't so much a new technology as a layer of abstraction that simplifies integrating JasperReports with web development. (Essentially, people around the world are all hard-coding the same problems -- including some fairly severe security issues -- into their web applications; this API shields the web application from such tight coupling.)




    Post Edited by thangalin at 04/14/2011 19:51
  10. http://www.whitemagicsoftware.com/software/java/rif/

    http://www.whitemagicsoftware.com/software/java/rif/api/com/whitemagicsoftware/report/Parameters.html#PARAM_REPORT_FILENAME

    The idea is that you make the file name part of a parameter that is filled in by the HTTP FORM POST action.

    If you need to include a programmatic item (such as the date), you override a single method to provide the appropriate value.

    If you have questions, let me know.



    Post Edited by thangalin at 04/13/2011 23:47
×
×
  • Create New...