Accessing and modifying a GenericFilter from the controller

I need to set the value of fields in the property filters of a generic filter and perform an initial filtering in the controller, but I am unable to access the filter components. Can you help me?
Sin título

Hi!

For example, if you create a By username configuration in the genericFilter and set the parameter name to usernamejDSJwvYM , the following code will set the parameter value to ad whenever you select this configuration:
image

Java:

    @Subscribe("genericFilter")
    public void onFilterConfigurationChange(GenericFilter.ConfigurationChangeEvent event) {
        Configuration configuration = event.getNewConfiguration();
        if ("By username".equals(configuration.getName())) {
            setFilterComponentValue(configuration, "usernamejDSJwvYM", "ad");
        }
    }

    private static void setFilterComponentValue(Configuration configuration,
                                                String name, Object value) {
        configuration.getRootLogicalFilterComponent().getFilterComponents().stream()
                .filter(filterComponent ->
                        filterComponent instanceof SingleFilterComponent<?> singleFilterComponent
                                && name.equals(singleFilterComponent.getParameterName()))
                .findAny()
                .ifPresent(filterComponent ->
                        ((SingleFilterComponent<Object>) filterComponent).getValueComponent().setValue(value));
    }

Also, depending on your task, you can set a default configuration with the propertyFilter which has a default value using XML markup if this suits your case:

XML:

        <genericFilter id="genericFilter"
                       dataLoader="usersDl">
            <properties include=".*"/>
            <configurations>
                <configuration id="defaultConfig" default="true">
                    <propertyFilter property="username" operation="EQUAL" defaultValue="admin"/>
                </configuration>
            </configurations>
        </genericFilter>

Best regards,
Dmitriy

1 Like