Convert Collection/Multiselect input control parameter to string using custom delimiter

0

I have a multiselect input control that i need to convert to string and then process the values selected. 

I can do this using: $P{Parameter_Name}.toString() . 

This will result in

[ value1,value2,value3,...]

The problem is that some ove the values defined in the input control List of values have ,(comma) in them, which makes it impossible to split the resulted string propperly. What i would like to do is to convert the collection of values to string and be able to specify a diferent delimiter so i can get for example something like [value1|value2|value3|.....]

Is there any way of doing this?

Alex42's picture
31
Joined: Dec 7 2016 - 4:24am
Last seen: 16 hours 32 min ago

3 Answers:

0

I think you should end all the Input Control Values with a specific character in order to achieve this.

Check this jrxml .

I'm passing 3 input control values

  • This is my First Value^/
  • This is my, Second Value^/
  • This is my Third Value^/

Observe that all my input controls are ending with 2 special characters.

These will be used for checking if the string is ending or not.

In the report, I'm replacing ^/, with a pipe delimiter.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2017-04-19T15:21: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="Blank_A4_Landscape_6" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9eb747b4-5176-48bc-acde-ce1f01e95901">
    <parameter name="ArrayList" class="java.util.Collection">
        <defaultValueExpression><![CDATA[java.util.Arrays.asList("This is my First Value^/","This is my, Second Value^/", "This is my Third Value^/")]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="802" height="30" uuid="79255c56-d765-48e4-91dd-08fd44c9b6bb"/>
                <textElement>
                    <font size="11"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{ArrayList}.toString().replace("^/,", "|").replace("^/", "").replace("[","").replace("]","")]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="36" splitType="Stretch"/>
    </summary>
</jasperReport>

reportdev's picture
601
Joined: Oct 12 2015 - 12:05pm
Last seen: 8 hours 51 min ago
0

Thank you for the solution. It is indeed an option, unfortunately in my case it is the Nuclear Option. Since the input control in question is widely used, adding the custom separator as you suggested(or adding a non printable character as delimiter to not affect the display) would imply making changes in all the queries of all the repoprts that use said input control. There are over 100 reports, at least. 

I am still hoping there is a way to change the delimiter. If not it would be easyer to clean-up the data from the DB

Alex42's picture
31
Joined: Dec 7 2016 - 4:24am
Last seen: 16 hours 32 min ago
0

You could use a report scriptlet to create a String [value1|value2|value3|.....] parameter based on your input control. For guidance see

http://community.jaspersoft.com/wiki/jasperreports-library-tutorial#Scriptlets

and

http://jasperreports.sourceforge.net/sample.reference/scriptlet/index.html#scriptlet

shertage's picture
1495
Joined: Sep 26 2006 - 8:06pm
Last seen: 1 day 19 hours ago
Feedback