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

morlandin

Members
  • Posts

    666
  • Joined

  • Last visited

  • Days Won

    2

 Content Type 

Profiles

Forum

Events

Featured Visualizations

Knowledge Base

Documentation (PDF Downloads)

Blog

Documentation (Test Area)

Documentation

Dr. Jaspersoft Webinar Series

Security Advisories

Downloads

Everything posted by morlandin

  1. Changed Resolution from Open to Fixed Changed Status from New to Resolved Changed Assigned User from - to @morlandin
  2. Changed Resolution from Open to Fixed Changed Status from New to Resolved
  3. Changed Status from New to Assigned Changed Assigned User from - to @morlandin
  4. Changed Assigned User from @User_306070 to @morlandin
  5. Changed Status from New to Resolved Changed Assigned User from - to @User_306070
  6. Changed Resolution from Open to Fixed Changed Status from New to Resolved Changed Assigned User from - to @morlandin
  7. Changed Resolution from Open to Fixed Changed Status from Confirmed to Resolved
  8. Changed Status from New to Confirmed Changed Assigned User from - to @morlandin
  9. Changed Status from New to Assigned Changed Assigned User from - to @morlandin
  10. OverviewWhen the user creates a new report, the template chooser proposes several ready to use documents to choose from. These documents can be taken as they are, or can be used with the wizard to create proper textfields, labels and groups starting from an SQL query or another data source. In this tutorial we will see how to create a custom template and add it to the template chooser. NOTE: All the files used and generated in this tutorial are attached. Think the StructureIt's important to have in mind a generic idea of the structure we need to have for the reports generated with the new template. So we need to think where to place images, fields, format and margins of the page and so on. Let's say that we want a report with the title in the center, a logo image on the left of the title, the records one after another and separated by a black line. The page must be in A4 format with a watermark in its center, and also the page number in the bottom-center of every page. Create the StructureEssentially a template is nothing more than a standard report where some elements have a precise and fixed name that is used by the Jaspersoft Studio engine to understood where to place every element. The first thing to do is to create a new report from File -> New -> Jasper Report. We can start with a blank report or also by selecting one of the other templates as starting point. For wath we want to do the Blank A4 is sufficent, so select this template and hit Next. In the next steps define the folder and the name of your report and as data adapter select One Empty Record since we don't need any data for our purpose. Complete the wizard to get your empty report. Now in the new report create a group: right click on the report root node in the outline and select Create Group. In the dialog that will popup leave the default name for the group and hit Finish. At the end you should have something like this: Now we can start to put the elements in the bands but first lets explain how this elements are used by Jaspersoft Studio: Every band will have the same size in the template and in the report generated from the template. For example, if your template has the Detail with height 100 pixels and the Summary with heigth to 0 pixels then every report generated with this template will have by default a Detail height of 100 pixels and a Summary height of 0 pixels. The consideration above is valid also for the page format, if your template is for example an A4 without margins even the report generated with this will be A4 without margins. The content of the bands Summary, Background, Title, Page Header and Page Footer is static. This means that every element that is placed in this bands inside the template will be replicated in every report generated using this template. In the Column Header band there should be only a Static Text element, and its text content must be "Label". The appearance, font and the other attributes of this label will be used to create every label that is inserted in this band. In the Group Header band there should be only a Text Field with a textual expression equal to "GroupField" (with the double quotes since it's the expression of a String). As with the Column Header this will be taken as example to generate every field that goes in this band. In the Detail band there should be only a Text Filed with the text "Field" (remember the double quotes) and as above the appearance of this field will be used as sample. Now we know everything we need to generate the report, and now we can place the elements to create our layout. Follow this steps: Place a Static text in the center of the Detail band and an image of your prefecene on the left. Then double click on the band to set it size in a way to fit exactly its content. Work a little on the Static Text font and color to obtain something nice. Delete the Page Header, Group Footer and the Summary band. Set the size of the Background band to 800 pixels, place inside it an Image element and choose an image. Make this element reasonably big (lets say width and height over 400 pixels) and place it in the center and in the middle of the Summary band. In the Page Footer band put a Page Number element, place it in the center of the band and then double click on the band to resize it and avoid waste of space. You can change the graphical attributes to make it looks better. Place in the Column Footer band a Line element and set it height to 1 pixel and for the width fit the band. As usual double click on the band to resize. Place in the Column Header a Label element with the text "Label" (without the double quote) and optionally change its appearance, like the text color or element borders. Place a Text Field in the Group Header and in the Detail. Set the text of the first Text Field to "GroupField" and for the second one use "Field" (both with the double quotes) and optionally change their look. At the end you should have something like this: Now switch to the Preview mode and look if the result is what you want (even without data you can see how the layout will look) and if it is allright you can continue with the export operation. Export the TemplateTo start the export wizard select click File -> Export as Report Template. In the first dialog you will see all the resources that will be exported. Between these resources you can fine the template and other files used by it, in our example you will see the two used images. In this step you will need also to select the destination folder, where the template and all his resources will be placed. When you have finished press Next. This following step is really important, here you can define the categories where your template will be visible inside the New Report Wizard. You can select any number of categories, between the embedded one or your own personal category. To define a custom category you need only to type its name inside the text area on the bottom and press the Add button. Then your category will be added to Selected Categories list. To move an element from the Selected Categories list to the Available Categories list and viceversa simply double-click on the element. At this point you need to select the correct type of report, for example if it is a Tabular Report or a standard one. It is importanto to select the correct type for the report you have designed, so in our exaple select the Standar Report item and you can proceed to the next step. At this point you can see two different steps: The first one, and the one that should appear follwoing this tutorial, is a congratulations step. It means that the template you designed is valid for the report type you have selected in the previous step.The second one is a list of error messages if the template you designed is not valid for the selected type because of some design errors. For example for a template with Tabular type is expected a table in the summary band. If this table is not found it's probably a design error. In this step all the design errors found are listed so you can look to what is wrong and fix it. In both this cases you can export your report template and all it's resources by clicking finish. But if you are in the second case maybe your template could not work well when it is used to generate the report. Remember that if you edit the report you must save it before to start the export as template wizard. Otherwise any unsaved change will not be taken from the wizard because it read the report file from your disk, not the one in your editor Generate the Preview PictureOptionally you can also generate the thumbnail for your Template, in the Preview view hit the export image button and save it in the same directory of the Template and with the same name. The thumbnail is not mandatory, the new template will be displayed anyway in the Template Selection dialog, with a default image as preview. Anyway it's better to have it, because in this way we could suppose how our report will looks like before to create it. Add the Templates location to StudioNow you have to put your custom template for studio. From Jaspersoft Studio hit Window -> Preferences and in the dialog that will appear expand the category Jaspersoft Studio and hit Template Loactions. Here press New and select the folder where you have put the template (MyTemplate), as in the following image Test the new templateAt this point everytime you have to select a Template for a report Jaspersoft Studio will from all the custom folder every JRXML files inside them and try to use these files as report template. We can try this by creating a new report and in the template selection step of the wizard we will see our new template with the preview image: Select your template and hit Next. When you have to select the data adapter use the Sample DB and use the query "select * from orders" and hit Next. Add to the report some fields and hit Next, then another time Next without creating any group and complete the wizard. The new report will be something like this: Switch to preview to see your report: Since now you have a much significative page for the sample image of the Template you can repeat the procedure to save thumbnail from here, and when you will need to select the location, just overwrite the old image.
  11. Changed Resolution from Open to No Change Required Changed Status from New to Closed Changed Assigned User from - to @User_306070
  12. Hi, in the cheatsheet there was an error, in the step with the text "In the fields Series and Label add the value F{SHIPCITY}", the name of the field should be $F{SHIPCITY}. Anyway this isn't probably your problem since the report dosen't even compile with a wrong variable name. Another thing is that your chart area is just too small, i've added two new steps to the cheatsheet where is explained how enlarge the chart. Anyway you can try to set the height of the summary band to 500 and then right click on the chart, select "Size To Container" and then "Fit Both". Doing this the chart will expands to fill all the summary. Please, let us know if this solved your problem. Thanks!
  13. Changed Status from New to Closed
  14. Changed Resolution from Open to No Change Required it seems to be an eclipse UI bug, i've opened a bug in their tracker
  15. Changed Assigned User from - to @User_306070
  16. Changed Resolution from Open to No Change Required System Message
  17. Changed Assigned User from @User_306070 to @anonymous
  18. Changed Assigned User from - to @User_306070
  19. Overview In the tutorial How to create and use a JRDataSource adapter we have seen how to create a simple data adapter that gets the data from a custom class. One of the problem of that adapter was the managing of the fields. Suppose you want to use a custom JRDataSource provided by someone, how do you know which fields are required? Obviously you can read documentation on the data adapter or ask the author, but we will see another way to provide a data adapter, and integrated with it all these informations like list of fields, with name, description and type. We can do this by defininga custom datasource provider, a java class that implements the interface JRDataSourceProvider. The interface JRDataSourceProvider To create a custom datasource provider we need to implement the interface JRDataSourceProvider, lets see which method this interface require: boolean supportsGetFieldsOperation() : returns true if the provider supports the field retriving operation, using the method getField. By returning true in this method the data source provider indicate that it is able to introspect the data source and discover the available fields. public JRField[] getFields(JasperReport report) : Returns the fields that are available from the data source. The provider can use the passed in report to extract some additional configuration information such as report properties. public JRDataSource create(JasperReport arg0) : Creates and returns a new instance of the provided data source. The provider can use the passed in report to extract some additional configuration information such as report properties. In other words this return the class that implements a JRDataSource interface (like the class MyImplementation seen in How to create and use a JRDataSource adapter) that contains the real data. public void dispose(JRDataSource dataSource) : disposes the data source previously obtained using the create method. This method must close any resources associated with the data source. Now we have a general idea on how this inderface does, but lets see a pratical example. In this example as data adapter for the create method we have used MyImplementation, seen in How to create and use a JRDataSource adapter, inside the implementation of the method create: You can find this example attached. Location of the class This class to work must be placed in the same project folder where you have your custom data adapter (MyImplementation in the example). We can put the class in a new package, so right click in the project folder MyReports and select New->Package. At this point right click on the CustomDataAdapter package and select New -> Class. On the dialog that will appear insert MyDataSource as class name. Then press the button Add to add a new interface and select JRDataSourceProvider, if you have done right the JRDataSourceProvider interface will appear in the list of the used interfaces. Finally hit the Finish button to create the new class. At this point you have to write the data adapter code, but for this example you can just copy and paste the code written before. Creation of the Data Adapter Now you have to create the data adapter that use this class, from select the element File -> New -> Data adapter. From the dialog select the same project folder where you put the class (in this case MyReports) and has name of the file put MyCustomSource and hit Next. Now you have to choose the type of the data adapter, select JasperReports DataSource Provider Class and hit Next. At this point you must provide the information to get the class previously written: As name of the datasource you can put anything you like, in this case we can use MyCustomSource. On the field JasperReports DataSource Provider Class Name hit the button with three dots "...", and in the new dialog search for MyCustomSource and hit Ok. At the end in this field you should find something like CustomDataSource.MyDataSource. After you have compiled all the fields hit the Test button to check if it is all right, and if you obtain a successful response hit the Finish button. Creation of the Report At this point you can use this new data adapter inside a report. Create a new report from the report wizard (File -> New -> Jasper Report), for this example use the template Coffee. Place it in the same Project folder of the data adapter (MyReports) and as data adapter select MyCustomSource. At this point the new datasource should be recognized as a fields provider and you will see the following image: and hit Next. Now to the provider will be requested the available fields, and they will be shown in the left list (in this example Name and Age), add them all to the report and then hit Finish. Now your report will be generated with the two fields already in it. Switch to the preview tab to see the result: mydatasource.java
  20. OverviewSometimes it is necessary to retrieve information from sources unsupported by any data adapters. In this case you can write your own java class to connect and query these sources, and then elaborate the extracted information to make them compatible with JasperReport. To do this you must define a custom data adapter specifying its class that implements the JRDataSource interface and some other information. Doing this, JasperReport will use this class to extract the information and will use it to fill the report. In this example will see how to create a custom JRDataSource adapter. The code in this example will be pretty simple, because all the information provided by the data adapter is hardcoded in the adapter itself. This means that the information provided is embedded and for this reason there is no need of other resources. This is a simplistic approach to show how to create a custom data adapter without creating too complex an example. The Class of the Custom Data AdapterThe first thing to do is decide which data is returned and how it is structured. Let's suppose that we have a series of records where every record is a name and an age. So we need to return this information. Now we need to understand how a data adapter is composed. A custom data adapter is a piece of java code with some characteristics: A class that implements the interface JRDataSource. This is necessary to guarantee the presence of the methods required to retrieve the data. To implement this interface we need to define two methods: public boolean next(): this method tell to JasperReports if there are still records to read. If it returns true, there are other records, otherwise it must return false.public Object getFieldValue(JRField jrField): We already know that a record can have any number of fields. This method is called for every field in the report, and it must return a value for that field. The parameter received by this method is the field that needs to be valorized, and it contains the name of the field, the description and, in general, information about the field. Knowing this information could be useful to identify the field and return the appropriate data.Keep in mind that these methods are called for every record. At first the method next is called and if its result is true then the getFieldValue is called for every field in the report. Then the method next is called again and the cycle will be repeated until it returns false. You then have to define a static method that returns an instance of the class defined in the first point. This is necessary to provide to JasperReport a way to get an already built instance of the class.Now that the concept behind this custom data adapter is explained, we can see the code (the code is also attached to this tutorial): package CustomDataAdapter;import java.util.HashMap;import net.sf.jasperreports.engine.JRDataSource;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRField;public class MyImplementation implements JRDataSource { /** * For this data adapter the informations are embedded in the code */ private static final String[] nameArray = {"Frank", "Joseph", "Marco", "Carl", "Lenny", "Homer", "Teodor", "Leopold"}; private static final Integer[] ageArray = {50,30,40,46,44,26,32,21}; /** * Variable to store how much records were read */ private int counter = -1; /** * Variables to store the number of fields, and their names, in the report */ private HashMap<String, Integer> fieldsNumber = new HashMap<String, Integer>(); /** * Method used to know if there are records to read. */ private int lastFieldsAdded = 0; @Override public boolean next() throws JRException { if (counter<nameArray.length-1) { counter++; return true; } return false; } /** * This method is called for every field defined in the report. So if i have 2 fields it is called 2 times for every record, and * for each of them it must provide a value. * The parameter can be used to understand for which field is requested, in fact it contains the name of the requested field. This * data adapter is done with the goal of return two values, a name and an age. An easy option would be expect a field with the name * "Name" and one with name "Age". But we can do something more flexible, in this case we will enumerate all the fields requested and * and the first two will be assumed to be for name and age, for all the others will be returned an empty string. So we can have a report * with more than two fields, but the name and the age will be returned each time only for the first two. * * If this example is too much complex refer to the method getFieldValue2, where is shown the first explained, and simple solution, where we * expect two fields with a precise name. */ @Override public Object getFieldValue(JRField jrField) throws JRException { Integer fieldIndex; if (fieldsNumber.containsKey(jrField.getName())) fieldIndex = fieldsNumber.get(jrField.getName()); else { fieldsNumber.put(jrField.getName(), lastFieldsAdded); fieldIndex = lastFieldsAdded; lastFieldsAdded ++; } if (fieldIndex == 0) return nameArray[counter]; else if (fieldIndex == 1) return ageArray[counter]; return ""; } /** * Example of a simpler getFieldValue, not actually used */ public Object getFieldValue2(JRField jrField) throws JRException { if (jrField.getName().equals("Name")) return nameArray[counter]; else if (jrField.getName().equals("Age")) return ageArray[counter]; return ""; } /** * Return an instance of the class that implements the custom data adapter. */ public static JRDataSource getDataSource(){ return new MyImplementation(); }}[/code]Location of the ClassAt this point we need only to understand where put this class and how use it. From the designer, right click on a JasperReport project folder and select New -> Package (if you don't see the element package search it under others), then use the name CustomDataAdapter for the new package. At this point right click on the CustomDataAdapter package and select New -> Class. On the dialog that will appear insert MyImplementation as class name. Then press the button Add to add a new interface and select JRDataSource, if you have done right the JRDataSource interface will appear in the list of the used interfaces. Finally hit the Finish button to create the new class. At this point you have to write the data adapter code, but for this example you can just copy and paste the code written before. Creation of the Data AdapterNow you have to create the data adapter that use this class, from select the element File -> New -> Data adapter. From the dialog select the same project folder where you put the class (in this case MyReports) and has name of the file put MyCustomAdapter and hit Next. Now you have to choose the type of the data adapter, select Custom Implementation of JRDataSource and hit Next. At this point you must provide the information to get the class previously written: As name of the datasource you can put anything you like, in this case we can use MyDataSource.On the factory class hit the button with three dots "...", and in the new dialog search for MyImplementation and hit Ok. At the and in the Factory Class field you should find something like CustomDataAdapter.MyImplementation.In the second textfield you must insert the static method that return an instance of your class, in this case simply type getDataSource.After you have compiled all the fields hit the Test button to check if it is all right, and if you obtain a successful response hit the Finish button. Creation of the ReportAt this point you can use this new data adapter inside a report. Create a new report from the report wizard (File -> New -> Jasper Report), for this example use the template Coffee. Place it in the same Project folder of the data adapter (MyReports) and as data adapter select MyDataAdapter and hit Finish. Now create two fields (In the outline view right click on the element Fields and select Create Field, one time for each field), and drag and drop them in the detail band. Then you have to change the Class property of the second field to java.lang.Integer, since by default it is String, but our data adapter return a string for the first value and an integer for the second one. If you don't do this then you will get a ClassCastException from JasperReports when you will run the report, becuase it is not able to do this conversion automatically. Probably you will have to adjust the size of the band and of the frame inside it (you could also remove this frame). Anyway ad the and you will obtein something like this: If you don't have the Automatic Build of your projects enabled (and by default it is disabled), you need to build your class file. To do this simply right-click on your project and select the option "Build Prjoect", if there aren't errors then you can proceed with the preview. NOTE: If after building your project you still get ClassNotFound exeption when executing the report try to close and reopen Jaspersoft Studio and rebuild the project, because in some rare cases it could be possible that the Java Classloader is not able to load correctly a just created class. Finally switch to the Preview tab to compile the report and see the result:
×
×
  • Create New...