0

I made a report using JasperReports and it is already working, but I want to construct a String from the values of a Set type object. I'm using the following expression to fill my Text Field:

$F{resultat}.getTypeResultats().iterator().next().getDescription()+ ". " +
(($F{resultat}.getTypeResultats().iterator().hasNext()) ? 
$F{resultat}.getTypeResultats().iterator().next().getDescription() : "")

I'm getting the expected value from the Field, but it always gets the first element in the Set.

How can I iterate my set?

Thanks for your help.

Here's the subreport:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 3.7.5  -->
<!-- 2017-03-27T14:34:04 -->
<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="FicheEpisode_Declarations" pageWidth="552" pageHeight="792" columnWidth="552" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>
    <property name="ireport.zoom" value="1.771561000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <import value="org.apache.commons.lang.StringUtils"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <subDataset name="Dataset1">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
    </subDataset>
    <field name="dateDeclaration" class="java.util.Date"/>
    <field name="dateReception" class="java.util.Date"/>
    <field name="declarantAutre" class="java.lang.String"/>
    <field name="casMaladie" class="com.openpole.mado.bo.CasMaladie"/>
    <field name="modeDeclaration" class="com.openpole.mado.bo.ModeDeclaration"/>
    <field name="medecinDeclarant" class="com.openpole.mado.bo.Medecin"/>
    <field name="laboratoireDeclarant" class="com.openpole.mado.bo.Laboratoire"/>
    <field name="resultat" class="com.openpole.mado.bo.Resultat"/>
    <background>
        <band height="11" splitType="Stretch"/>
    </background>
    <title>
        <band height="1" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="86" splitType="Stretch">
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="3" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.typeDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="18" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.dateDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="46" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.methodeDeclaration} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="32" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.decl.dateReception} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy-MM-dd" isBlankWhenNull="true">
                <reportElement key="fldDateDeclaration" mode="Transparent" x="154" y="18" width="92" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{dateDeclaration}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy-MM-dd" isBlankWhenNull="true">
                <reportElement key="fldDateReception" mode="Transparent" x="154" y="32" width="92" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.util.Date"><![CDATA[$F{dateReception}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldModeDeclaration" mode="Transparent" x="154" y="46" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{modeDeclaration}.getDescription()]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldNomDeclarant" mode="Transparent" x="154" y="3" width="273" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{medecinDeclarant} != null ?
  $R{rapport.ficheEpisode.decl.medecin} + " (" + $F{medecinDeclarant}.getPrenom() + " " + $F{medecinDeclarant}.getNom() + ")":
$F{laboratoireDeclarant} != null ?
  $R{rapport.ficheEpisode.decl.laboratoire} + " (" + $F{laboratoireDeclarant}.getDescription() + ")":
$R{rapport.ficheEpisode.decl.autre} + " (" + StringUtils.trimToEmpty($F{declarantAutre}) + ")"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="60" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.res.sourceInfection} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="lblIdentification" positionType="Float" mode="Transparent" x="0" y="75" width="101" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$R{rapport.ficheEpisode.res.typeResultat} + " :"]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldMicroOrganisme-1" mode="Transparent" x="154" y="61" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{resultat}.getMicroOrganismes().iterator().next().getDescription()]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="" isBlankWhenNull="true">
                <reportElement key="fldMicroOrganisme-1" mode="Transparent" x="154" y="75" width="275" height="11" forecolor="#000000" backcolor="#FFFFFF"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single" markup="none">
                    <font fontName="SansSerif" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{resultat}.getTypeResultats().iterator().next().getDescription()+ ". " +(($F{resultat}.getTypeResultats().iterator().hasNext()) ? $F{resultat}.getTypeResultats().iterator().next().getDescription() : "")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>
  • You don't need to iterate manually. The JR engine do it itself. You should post the simple small sample to reproduce the problem: *jrxml* (master + subreport) + *Java* code if you have – Alex K Mar 27 '17 at 18:26
  • @AlexK unfortunely the source code for my main report has more lines than the allowed by stackoverflow. I hope this is usefull enough. Thanks for the help – Fabio Sarmiento Gómez Mar 27 '17 at 18:36
  • 1
    Create a [mcve], hence create a small example of what you are trying to do, that we easily can reproduce. – Petter Friberg Mar 27 '17 at 20:52
  • How exactly do you want to print the vales in the set (when there's more than one value)? On the same line, concatenated with a separator? Or on different lines? – dada67 Mar 28 '17 at 08:01
  • @dada67 exactly, when there's more than one value separated by a comma. I have a set with the objetcs that contain the info I want to display. I'm already getting the value of the first objetct. But I wonder if it's possible to iterate a java set using a JR expression because I always get the first element of the set – Fabio Sarmiento Gómez Mar 28 '17 at 15:45

1 Answers1

0

You can concatenate values from a list using Java 8 methods and lambda expressions. See here and here how to enable lambda expressions in reports.

The expression that you need would be something like

<textFieldExpression><![CDATA[$F{resultat}.getTypeResultats().stream().map(e -> e.getDescription()).collect(java.util.stream.Collectors.joining(", "))]]></textFieldExpression>
Community
  • 1
  • 1
dada67
  • 4,723
  • 17
  • 19