Code
You can download the project for this lesson.
You can also copy the code used in the video here:
In
controllers.xml file:
<controller name="Product">
<extends controller="Invoicing"/>
<action name="printProductDetail"
class="com.yourcompany.invoicing.actions.PrintProductAction"
mode="detail"
icon="printer"/>
</controller>
In
PrintProductAction.java file:
public class PrintProductAction extends JasperReportBaseAction {
private Product product;
@Override
protected JRDataSource getDataSource() throws Exception {
return new JREmptyDataSource();
}
@Override
protected String getJRXML() throws Exception {
return "ProductDetail.jrxml";
}
@Override
protected Map getParameters() throws Exception {
Messages errors = MapFacade.validate("Product", getView().getValues());
if (errors.contains()) throw new ValidationException(errors);
Map parameters = new HashMap();
parameters.put("id", (getProduct().getNumber()));
parameters.put("description", getProduct().getDescription());
parameters.put("author", getProduct().getAuthor().getName());
parameters.put("isbn", getProduct().getIsbn());
parameters.put("category", getProduct().getCategory().getDescription());
parameters.put("price", getProduct().getPrice());
return parameters;
}
private Product getProduct() throws Exception {
if (product == null) {
int number = getView().getValueInt("number");
product = XPersistence.getManager().find(Product.class, Integer.valueOf(number));
}
return product;
}
}
Transcription
Hello, I'm Monica. In this lesson, we will see a simple example of how
to send parameters from OpenXava and use them in the report. For this,
we will use the last lesson of the OpenXava course.
First, let's import the project from lesson 26 of the OpenXava course
from the repository github.com/openxava/openxava-course_en. In Code,
download the project as a ZIP file.
In OpenXava Studio, right-click in the package explorer panel, and click
on Import. Select Project from Folder or Archive from General. Click
Next. Here, in archive, open the explorer and select the file you just
downloaded. It will show the available projects or folders to import.
Select only the option with Eclipse project in Import as. Click Finish.
Done, we have the project, but there are still a few steps to start it.
Right-click on the project, Run As, Maven install. Once it says Success,
right-click on the project again, Maven, Update project. Now, go to the
package com.yourcompany.invoicing.run and start the project. Right-click
on invoicing, Run As, Java Application. Copy the link and open it in the
browser. Log in with Admin, admin. We can see that there are already
records to take advantage of.
Let's go to Jaspersoft Studio and create a new report called Product
detail. Remove the sections that we won't need. Our goal is to show some
of the properties of the product from the parameters we receive. In the
last lesson, we saw how to use static text; we will use it for ID. Then
drag a TextField and double-click on it. We can see that this element
offers much more than static text; it can be used for something as
simple as text, as well as for code with simple logic, which in our case
would be Java code. For now, let's just leave the text, remember it
should be in double quotes. Thus, we create several TextFields and
include the labels of each property we want to show: Name, Price,
Category, Author, ISBN.
In OpenXava Studio, we need to create an action to print the report, so
first, let's define it in its controller. Since the Product module
doesn't have a controller yet, we will create one. Extend from the
Invoicing controller. For the action, name it Print Product Detail,
leave the class empty, and set the mode to detail so that the action
appears there. Finally, choose an icon for the action, for example, a
printer. Almost done, we need to create the action class and then
specify its path in the empty class field. In the package
com.yourcompany.invoicing.actions, create a new class called
PrintProductAction. Once created, extend it from JasperReportBaseAction
and add the unimplemented methods. In getDataSource, return new
JREmptyDataSource. In getJRXML, put the report name. Here in
getParameters, we will define the parameters to send. Add an error
message to display if the Product values are not found in the view where
we call the action.
Import from openxava.validators. Create a map to store the parameters.
There are different ways to obtain the information, such as from the
database or directly from the view where the action is called. We will
choose the first option: perform a JPA query using the product number,
which in this case is its ID, retrieve all product information, and add
the necessary data to the map. For example, we want to add the product
number to the key ID. To do this, define a Product and create a get
method for that product. If the product is null when we try to obtain
it, we take the number from the current view and use it to search for
the product in our database using JPA. With getView, we get the current
view and from that view we want to get an int value with the parameter
number. In this case, we are obtaining it from here. This parameter
should be named the same as the property declared in the class. Now
perform the JPA query using the number we obtained and finally return
the product. In the parameters, we will set "id" as the key and
getProduct().getNumber() as its value. Do the same for all the
information we want to send. There are two things to note: the product
number is of type int, so we are sending an int type in the map. Also,
for the author and category, we are sending more relevant information
directly, not just their IDs. Return the map and save it. Go back to the
controller and in the class field, put the full path of the action.
Now we have the action that sends parameters to the report. In the
report, we also need to create parameters. Do this from the Outline
panel. Right-click on Parameters and select Create Parameter. Select the
created Parameter1 element, it should already be selected. In the
properties panel of the element, name it ID and set its class to
Integer. Each parameter we send from the action should have a
corresponding parameter in the report to receive it. For example, if we
sent an ID parameter of type int, then the report should have a
parameter named ID and of class Integer. Create the other six parameters
in the report in the same way. The id text in the report, as it was
static text, should be shown using a TextField to display the parameter
value. In the TextField, use the dollar sign, P for parameter, and the
parameter name in curly braces. For other fields, since we already had a
TextField, add a plus sign before the parameter, as if constructing a
String in Java. You can also drag the parameter element directly from
the Outline panel and then add the text. Once everything is ready, save
and copy the report to the reports folder, which you should create in
the project. Now you can start the application. Test with one of the
records and see the results.
Working with parameters is interesting; you can send whatever you want
as a parameter: lists, collections, and more. Then, in the report, you
receive them with the appropriate data type to work with. If you have
any questions about this lesson, you can ask us on the forum. You can
also download the code for this lesson from the repository link, both
links are in the video description. See you in the next lesson where we
will learn how to work with images in the report. Bye.