I was recently tasked to clean an old repo which includes the following Jasper report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Main Report" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4123fb0b-673b-4db3-9fe3-29adda726395">
<import value="org.thmasker.jasper.accessor.StatusJasperAccessor"/>
<columnHeader>
<band height="25" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<staticText>
<reportElement mode="Opaque" x="339" y="0" width="84" height="20" forecolor="#FFFFFF" backcolor="#0D9DDB" uuid="44984c48-65ae-4d88-afb1-5567a8ff122d">
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="com.jaspersoft.studio.unit.y" value="px"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
</reportElement>
<box>
<pen lineStyle="Solid" lineColor="#0D9DDB"/>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
</box>
<textElement textAlignment="Center">
<font size="8"/>
</textElement>
<text><![CDATA[ID #]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField textAdjust="StretchHeight">
<reportElement positionType="Float" stretchType="ElementGroupHeight" x="339" y="-5" width="84" height="15" uuid="8a4cef1f-0fa3-4abf-994f-b0bf11d4a156">
<property name="net.sf.jasperreports.export.xls.auto.fit.column" value="true"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="com.jaspersoft.studio.unit.leftIndent" value="px"/>
<property name="com.jaspersoft.studio.unit.spacingBefore" value="px"/>
</reportElement>
<box>
<pen lineColor="#0D9DDB"/>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#0D9DDB"/>
</box>
<textElement verticalAlignment="Justified">
<font size="8"/>
<paragraph leftIndent="3" spacingBefore="3"/>
</textElement>
<textFieldExpression><![CDATA[StatusJasperAccessor.getStatus()]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
and this is the corresponding StatusJasperAccessor:
@Component
@RequiredArgsConstructor
public class StatusJasperAccessor implements Serializable {
private static StatusService statusService;
private final StatusService tStatusService;
@PostConstruct
public void init() {
statusService = tStatusService;
}
@Autowired
public static String getStatus() {
return statusService.getStatus();
}
}
Now, I'm trying to get rid of this awful piece of code, but I can't make any alternative work. How can I make this work on the XML file?
@Component
@RequiredArgsConstructor
public class StatusJasperAccessor implements Serializable {
private final StatusService statusService;
@Autowired
public String getStatus() {
return statusService.getStatus();
}
}
Found a solution:
I need to add this in the report XML:
<import value="org.thmasker.jasper.accessor.StatusJasperAccessor"/>
<parameter name="statusAccessor" class="org.thmasker.jasper.accessor.StatusJasperAccessor"/>
and use it with:
<textFieldExpression><![CDATA[$P{statusAccessor}.getStatus()]]></textFieldExpression>
Accessor can be:
@Component
@RequiredArgsConstructor
public class StatusJasperAccessor implements Serializable {
private final StatusService statusService;
@Autowired
public String getStatus() {
return statusService.getStatus();
}
}
and I need to also add this bean as a parameter before filling the report:
@Autowired
StatusJasperAccessor accessor;
Map<String, Object> params = new HashMap<>();
params.put("statusAccessor", accessor);
JasperPrint jp = JasperFillManager.fillReport(jasperReport, params, dataSource);