I'm creating a report in "Jaspersoft iReport designer 5.6.0". What I'm trying to do is add a parameter (a static list that contains values such as: 6 months, 3 months ...). When the user chooses one of these options, I should be able in the Report query, to get the value that the user has chosen, so I can deliver the results upon that selection. I don't want to use jasper server.
Is this possible?
this is my query:
SELECT
DISTRICT."DKEY" AS DISTRICT_DKEY,
DISTRICT."PROVINCE_ID" AS DISTRICT_PROVINCE_ID,
DISTRICT."DISTRICT" AS DISTRICT_DISTRICT
DISTRICT."DURATION" AS DISTRICT_DURATION
FROM
"dbo"."DISTRICT" DISTRICT
where DISTRICT."DKEY" = $P{parameter1}
In iReport you can not create a static list (combobox) of your different month's you can only prompt for the insertion of parameter.
User will need to input manually 3,6 ecc.
<parameter name="parameter1" class="java.lang.Integer" isForPrompting="true">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
iReport is not developed to be used by your client, but to be used by you when developing the report.
If you do not wish to use the jasperserver, you can develop your own application for user to select data ecc. Below you find an example of a java swing application, asking to select month and generating preview of the report (you need to fix the connection settings and provide correct path for the jrxml file):
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.util.*;
import javax.swing.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JRViewer;
public class JasperReportInterface extends JFrame {
private static final long serialVersionUID = 5430239481089683268L;
private JComboBox<MonthItem> selectMonts;
public JasperReportInterface() {
super("Jasper Report Interface");
jbInit();
}
private void jbInit() {
this.getContentPane().setLayout(new GridBagLayout());
selectMonts = new JComboBox<MonthItem>();
selectMonts.addItem(new MonthItem(3));
selectMonts.addItem(new MonthItem(6));
this.getContentPane().add(new JLabel("Select month:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2,
2), 0, 0));
this.getContentPane().add(selectMonts, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2,
2), 0, 0));
JButton btnReport = new JButton("Generate report");
btnReport.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnReport_actionPerformed(e);
}
});
this.getContentPane().add(btnReport,new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2,
2), 0, 0));
}
protected void btnReport_actionPerformed(ActionEvent e) {
String jasperFilePath = "jasper/myJasperFile.jrxml";
Map<String, Object> parameters = new HashMap<String, Object>();
Object v = selectMonts.getSelectedItem();
if (v instanceof MonthItem) {
parameters.put("parameter1", ((MonthItem) v).getMonth());
}
Connection conn = null; // Pass the connection to database or datasource
JasperPrint report;
try {
JasperDesign jasperDesign = JRXmlLoader.load(jasperFilePath);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
report = JasperFillManager.fillReport(jasperReport, parameters, conn);
} catch (JRException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(this, "Error creating report: " + e1.getMessage());
return;
}
JRViewer jrv = new JRViewer(report);
JDialog viewer = new JDialog(this);
viewer.setTitle("Print preview");
viewer.getContentPane().add(jrv);
viewer.pack();
viewer.setSize(new Dimension(840, 600));
viewer.setLocationRelativeTo(null);
viewer.setVisible(true);
}
class MonthItem {
private int month;
protected MonthItem(int month) {
this.month = month;
}
public String toString() {
return month + " months";
}
public int getMonth() {
return month;
}
}
public static void main(String[] args) {
JasperReportInterface ri = new JasperReportInterface();
ri.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ri.setSize(400,100);
ri.setLocationRelativeTo(null);
ri.setVisible(true);
}
}
Naturally you can also develop a similar web application.