Project Wiki
Views

Cascading input controls

From Wiki

Jump to: navigation, search


Overview

This implementation is designed to support cascading behavior based on query input controls. These controls can use parameter values from other named parameter input controls whose values can resolve to a string. These strings are substituted into the defined query for that cascading input control with the usage of tokens specified in the cascading input control's query. For example, a query such as:

select billing_address_state from accounts where billing_address_country = $P{country} and billing_address_state = $P{state}

Detailed specification

Examples

Country - State - Name cascade

p_country input control

Single select query input control.

select distinct billing_address_country
from accounts
order by billing_address_country

p_state input control

Single select query input control.

select distinct billing_address_state
from accounts
where billing_address_country = $P{p_country} order by billing_address_state

p_name input control

Single select query input control.

select distinct name
from accounts
where billing_address_country = $P{p_country} and billing_address_state = $P{p_state}
order by name

JRXML

....
	<parameter name="p_country" class="java.lang.String">
		<defaultValueExpression><![CDATA["USA"]]></defaultValueExpression>
	</parameter>
	<parameter name="p_state" class="java.lang.String">
		<defaultValueExpression><![CDATA["CA"]]></defaultValueExpression>
	</parameter>
	<parameter name="p_name" class="java.lang.String">
		<defaultValueExpression><![CDATA["Alpha-Murraiin Communications, Inc"]]></defaultValueExpression>
	</parameter>
	<queryString>
		<![CDATA[select * from accounts where name = $P{p_name}]]>
	</queryString>
....

Multi-select control example

Included with sample reports.

Country_multi_select control

Multi-select query control. billing_address_country is the visible and value column of the control.

SQL:

select distinct billing_address_country from accounts order by billing_address_country


Cascading_state_multi_select

Multi-select query control. billing_address_state is the value column. billing_address_state and billing_address_country are the visible columns.

SQL:

select distinct billing_address_state, billing_address_country
from accounts
where $X{IN, billing_address_country, Country_multi_select}
order by billing_address_country, billing_address_state

JRXML

	<parameter name="Country_multi_select" class="java.util.Collection">
		<defaultValueExpression><![CDATA[new ArrayList(Arrays.asList(new String[] {"USA"}))]]></defaultValueExpression>
	</parameter>
	<parameter name="Cascading_state_multi_select" class="java.util.Collection">
		<defaultValueExpression><![CDATA[new ArrayList(Arrays.asList(new String[] {"CA"}))]]></defaultValueExpression>
	</parameter>
	<queryString>
		<![CDATA[
select * from accounts
where $X{IN, billing_address_state, Cascading_state_multi_select}
   and $X{IN, billing_address_country, Country_multi_select}
order by billing_address_country, billing_address_state, name]]>
	</queryString>

Setting up query input controls for cascading


Parameter substitution in query input controls follows the same approach as for JasperReports. It works for queries of all types of query connections. $P, $P! and $X (for SQL queries) parameters are supported.

As now, single select query input controls return single values to reports and queries, and multi-select query input controls return collections.

A number of parameters will always be available for query input controls. "Standard" controls will always be provided to reports (not queries), even if an input control is not defined for them.

Parameter Name Type Notes Standard?
LoggedInUser User Not usable in query input control, but is used as parameter to report Yes
LoggedInUsername String Of logged in user Yes
LoggedInUserFullName String Of logged in user No
LoggedInUserEmailAddress String Of logged in user No
LoggedInUserEnabled Boolean Is logged in user enabled? No
LoggedInUserExternallyDefined Boolean Is logged in user externally defined? ie. authenticated externally No
LoggedInUserTenantId String Of logged in user. Only relevant in Pro/Enterprise. No
LoggedInUserRoles Collection<String> Current set of roles of logged in user. Useful for $X parameter No
LoggedInUserAttributes Map<String, String> Not usable in query input control, but is used as parameter to report. Empty map if no attributes No
LoggedInUserAttributeNames Collection<String> User profile attribute names. Useful for $X parameters. Empty collection if no attributes No
LoggedInUserAttributeValues Collection<String> No
LoggedInUserAttribute_<attribute name> String Attribute value for matched attribute name (like "att1") on the user. Empty string if no match. Only provided if defined in a query or as a report parameter. No


Changes to parameters for reports in JasperServer

The above "always available" parameters are also provided for reports if they are defined as parameters in the JRXML. This adds to the current LoggedInUser and LoggedInUsername parameters that are currently available.


Extension point for introducing additional built-in parameters


Classes that implement BuiltInParameterProvider can be included to provide parameters which are available to reports and queries.

public interface BuiltInParameterProvider {

    /**
     * Generate a set of standard parameters
     *
     * Each element is a JRParameter, value
     *
     * @param context
     * @param jrParameters unchanged
     * @param parameters unchanged
     * @return List<Object[]> [JRParameter, value]
     */
    public List<Object[]> getParameters(ExecutionContext context, List jrParameters, Map parameters);

    /**
     * Generate parameters can be requested by name that are not part of the standard set
     *
     * @param context
     * @param jrParameters unchanged, can be null
     * @param parameters unchanged
     * @param name of parameter
     * @return List<Object[]> [JRParameter, value] or null if the given name is not set by this generator
     */
    public Object[] getParameter(ExecutionContext context, List jrParameters, Map parameters, String name);

}

To include your custom parameter provider, put the class into the WAR (in WEB-INF/classes or WEB-INF/lib) and add an instance of your class into the builtInParameterProviders bean in WEB-INF/applicationContext.xml, as follows:

    <bean id="builtInParameterProviders" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <bean class="com.jaspersoft.jasperserver.api.engine.jasperreports.util.UserProfileBuiltInParameterProvider"/>
                <bean class="your.package.and.class.name.here"/>
            </list>
        </constructor-arg>
    </bean>

Have a look at the UserProfileBuiltInParameterProvider class as an example.

Comments