Code
You can download the project for this lesson. You can also copy the code used in the video here:
In
PrintInvoiceListAction.java file:
public class PrintInvoiceListAction extends JasperReportBaseAction {
@Inject
private Tab tab;
@Override
protected JRDataSource getDataSource() throws Exception {
List invoiceList = new ArrayList();
if (tab.getSelectedKeys().length > 0) {
for (Map key : tab.getSelectedKeys()) {
Invoice invoice = (Invoice) MapFacade.findEntity("Invoice", key);
invoiceList.add(invoice);
}
} else {
for (int i = 0; i<tab.getTableModel().getRowCount(); i++) {
Invoice invoice = (Invoice) MapFacade.findEntity("Invoice", (Map) tab.getTableModel().getObjectAt(i));
invoiceList.add(invoice);
}
}
return new JRBeanCollectionDataSource(invoiceList);
}
@Override
protected String getJRXML() throws Exception {
return "InvoiceList.jrxml";
}
@Override
protected Map getParameters() throws Exception {
return null;
}
}
In
controllers.xml file:
<action name="printInvoiceList"
class="com.yourcompany.invoicing.actions.PrintInvoiceListAction"
mode="list"
icon="printer"/>
Transcription
Hello, I’m Mónica. In this lesson, you will learn how to design custom reports for list mode.
Currently, OpenXava offers the ability to generate reports in list mode for each module. Although you can add columns, remove them, and sort the records before generating the report, they always have the same design. So, in this lesson, we will see how to generate our own report from the list mode of the Invoice. For this, we need to create an action first. We rename the action, change the class to another one we will create later, and in "mode", we must specify "list" so that the action appears in the list view. Then we create the PrintInvoiceListAction. We extend from JasperReportBaseAction. We won’t be working with parameters this time. Instead, we will directly send a collection, in this case, the list of records. To work with the table we see in the list, we must declare a Tab with the @Inject annotation. We import it from OpenXava tab. If the user has selected any rows, we take the keys of the selected rows and look them up one by one based on the key, then add them to the list. If the user hasn’t selected anything, we go through the entire table and use the key of each row to find the record and add it to the list. Finally, we send that list as the data source.
In Jaspersoft, we create a new report called InvoiceList. We create a title and date in the Title section and remove the sections we won’t be using. Then, we create the fields—remember, they must have the same data type as the one they were sent with. Once we have it ready, we drag them into the detail 1 section and add some formatting to keep it organized. That’s it. We save it and copy it to the project before restarting it. We will generate a report without selecting records, so all the records in the list will be shown. On the other hand, if we select some, only those will be displayed.
This is a simple example. But in your report, you can add styles, images, and other elements. For example, I want the report to highlight invoice rows where the total amount exceeds 100. So, I can do something like this: I drag a rectangle element into the detail section, adjust it to be about the same size as a row, change its background color, and in the logic, I add that if totalAmount is greater than 100, it shows the element. Otherwise, it doesn’t. Since totalAmount is a BigDecimal, I need to add intValue to get the integer value. Finally, I make the element appear in the background of the row. We save it, copy the report to the project, and restart the application. That’s how it would look.
With what you’ve learned in this lesson, you can now access each record in list mode, work with it, and then send it to the report. We’ve used a data source to send a list to the report, but you can also do it with parameters. If you have any questions or issues, you can ask us on the forum, and you can also download the code from this lesson through the repository link, both links are in the video description.
Bye.