Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static void setCurrentConfiguration(GenericFilter filter, Configuration c
filter.setCurrentConfigurationInternal(currentConfiguration, fromClient);
}

/**
* @deprecated no longer used internally; {@link GenericFilter} now captures the initial data loader
* condition lazily. Retained for backward compatibility.
*/
@Deprecated(since = "3.0", forRemoval = true)
@Internal
public static void updateDataLoaderInitialCondition(GenericFilter genericFilter, @Nullable Condition condition) {
genericFilter.updateDataLoaderInitialCondition(condition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public class GenericFilter extends Composite<JmixDetails>
protected int propertyHierarchyDepth;
protected DataLoader dataLoader;
protected Condition initialDataLoaderCondition;
protected boolean initialDataLoaderConditionInitialized;
protected Condition lastConditionSetByFilter;
protected Predicate<MetaPropertyPath> propertyFiltersPredicate;

protected VerticalLayout contentWrapper;
Expand Down Expand Up @@ -370,15 +372,21 @@ public void setDataLoader(DataLoader dataLoader) {
checkNotNull(dataLoader);

this.dataLoader = dataLoader;
this.initialDataLoaderCondition = dataLoader.getCondition();

LogicalFilterComponent<?> rootLogicalFilterComponent = emptyConfiguration.getRootLogicalFilterComponent();
rootLogicalFilterComponent.setDataLoader(dataLoader);
rootLogicalFilterComponent.setAutoApply(autoApply);
}

/**
* @deprecated no longer used internally; the initial data loader condition is now captured lazily
* in {@link #updateDataLoaderCondition()} before the first filter contribution. Retained for
* backward compatibility.
*/
@Deprecated(since = "3.0", forRemoval = true)
protected void updateDataLoaderInitialCondition(@Nullable Condition condition) {
this.initialDataLoaderCondition = copy(condition);
this.initialDataLoaderConditionInitialized = true;
}

/**
Expand Down Expand Up @@ -830,6 +838,14 @@ protected String getConfigurationName(Configuration configuration) {

protected void updateDataLoaderCondition() {
if (dataLoader != null) {
Condition currentCondition = dataLoader.getCondition();
// Re-capture the loader's own condition only when it was replaced externally (a different
// object than the filter's last output); the filter never adopts its own output.
if (!initialDataLoaderConditionInitialized
|| (lastConditionSetByFilter != null && currentCondition != lastConditionSetByFilter)) {
initialDataLoaderCondition = copy(currentCondition);
initialDataLoaderConditionInitialized = true;
}
LogicalFilterComponent<?> logicalFilterComponent = getCurrentConfiguration().getRootLogicalFilterComponent();
LogicalCondition filterCondition = logicalFilterComponent.getQueryCondition();

Expand All @@ -846,6 +862,7 @@ protected void updateDataLoaderCondition() {
}

dataLoader.setCondition(resultCondition);
lastConditionSetByFilter = resultCondition;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class GroupFilter extends Composite<VerticalLayout>

protected DataLoader dataLoader;
protected Condition initialDataLoaderCondition;
protected boolean initialDataLoaderConditionInitialized;
protected Condition lastConditionSetByFilter;
protected boolean autoApply;

@Internal
Expand Down Expand Up @@ -186,7 +188,6 @@ public void setDataLoader(DataLoader dataLoader) {
checkNotNull(dataLoader);

this.dataLoader = dataLoader;
this.initialDataLoaderCondition = dataLoader.getCondition();

if (!isConditionModificationDelegated()) {
updateDataLoaderCondition();
Expand All @@ -195,15 +196,31 @@ public void setDataLoader(DataLoader dataLoader) {
updateSummaryText();
}

/**
* @deprecated no longer used internally; the initial data loader condition is now captured lazily
* in {@link #updateDataLoaderCondition()} before the first filter contribution. Retained for
* backward compatibility.
*/
@Deprecated(since = "3.0", forRemoval = true)
protected void updateDataLoaderInitialCondition(@Nullable Condition condition) {
this.initialDataLoaderCondition = copy(condition);
this.initialDataLoaderConditionInitialized = true;
}

protected void updateDataLoaderCondition() {
if (dataLoader == null) {
return;
}

Condition currentCondition = dataLoader.getCondition();
// Re-capture the loader's own condition only when it was replaced externally (a different
// object than the filter's last output); the filter never adopts its own output.
if (!initialDataLoaderConditionInitialized
|| (lastConditionSetByFilter != null && currentCondition != lastConditionSetByFilter)) {
initialDataLoaderCondition = copy(currentCondition);
initialDataLoaderConditionInitialized = true;
}

LogicalCondition resultCondition;
if (initialDataLoaderCondition instanceof LogicalCondition initialLogicalCondition) {
resultCondition = ((LogicalCondition) copy(initialLogicalCondition));
Expand All @@ -217,6 +234,7 @@ protected void updateDataLoaderCondition() {
}

dataLoader.setCondition(resultCondition);
lastConditionSetByFilter = resultCondition;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
@Internal
public class GroupFilterUtils {

/**
* @deprecated no longer used internally; {@link GroupFilter} now captures the initial data loader
* condition lazily. Retained for backward compatibility.
*/
@Deprecated(since = "3.0", forRemoval = true)
public static void updateDataLoaderInitialCondition(GroupFilter groupFilter, @Nullable Condition condition) {
groupFilter.updateDataLoaderInitialCondition(condition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ protected void loadDataLoader(GenericFilter component, Element element) {
(dataLoaderId) -> {
DataLoader dataLoader = getContext().getDataHolder().getLoader(dataLoaderId);
component.setDataLoader(dataLoader);

getContext().addInitTask(new AbstractInitTask() {
@Override
public void execute(Context context) {
FilterUtils.updateDataLoaderInitialCondition(resultComponent,
dataLoader.getCondition());
}
});
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking changes is not documented. Was this approved?

The base loader condition must now be set before a configuration is activated. Previously the loader init task ran after onInit, so this captured myBaseCondition:

// in onInit
genericFilter.setCurrentConfiguration(c1);
ordersDl.setCondition(myBaseCondition);   // set AFTER activation

Now the first updateDataLoaderCondition() snapshots the baseline without it, and it's lost on the next switch — a silent regression for anyone relying on the old timing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, valid point. Removed the regression: the filter now adopts the loader's condition as its baseline if it was replaced externally (a different object, not the filter's own output). So setting the base condition after activating a configuration in onInit works again (and the same for a standalone GroupFilter), while configuration switching stays fixed. Covered by tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately: we need to remember to add a clarification to the documentation. To change the base condition, set a new object via DataLoader.setCondition(). If instead, after the first updateDataLoaderCondition (for GenericFilter or GroupFilter), you obtain a reference to the Condition from DataLoader and modify it in place without changing the reference in DataLoader, the filter ignores it: the change is not picked up and is overwritten on the next condition rebuild (the exception being a direct DataLoader.load() before the rebuild, which reads the mutated object, but the filter does not preserve it). This behavior is not changed by this PR — it was the case before as well.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

import io.jmix.flowui.component.filter.FilterComponent;
import io.jmix.flowui.component.logicalfilter.GroupFilter;
import io.jmix.flowui.component.logicalfilter.GroupFilterUtils;
import io.jmix.flowui.component.logicalfilter.LogicalFilterComponent;
import io.jmix.flowui.model.DataLoader;
import io.jmix.flowui.xml.layout.ComponentLoader;
import io.jmix.flowui.xml.layout.inittask.AbstractInitTask;
import io.jmix.flowui.xml.layout.loader.AbstractComponentLoader;
import org.dom4j.Element;

Expand Down Expand Up @@ -55,14 +53,6 @@ protected void loadDataLoader(GroupFilter resultComponent, Element element) {
.ifPresent(dataLoaderId -> {
DataLoader dataLoader = context.getDataHolder().getLoader(dataLoaderId);
resultComponent.setDataLoader(dataLoader);

getContext().addInitTask(new AbstractInitTask() {
@Override
public void execute(Context context) {
GroupFilterUtils.updateDataLoaderInitialCondition(resultComponent,
dataLoader.getCondition());
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2026 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package component.genericfilter

import component.genericfilter.view.GfBaseConditionAfterActivationView
import component.genericfilter.view.GfBaseConditionReviseView
import component.genericfilter.view.GfConfigsNoActivationView
import io.jmix.core.querycondition.Condition
import io.jmix.core.querycondition.LogicalCondition
import io.jmix.core.querycondition.PropertyCondition
import io.jmix.flowui.component.genericfilter.FilterUtils
import io.jmix.flowui.component.genericfilter.GenericFilter
import org.springframework.boot.test.context.SpringBootTest
import test_support.spec.FlowuiTestSpecification

/**
* A base condition set on the data loader after a configuration is activated in {@code onInit}
* must still be applied after switching configurations.
*/
@SpringBootTest
class GenericFilterBaseConditionAfterActivationTest extends FlowuiTestSpecification {

void setup() {
registerViewBasePackages("component.genericfilter.view")
}

def "base condition set after activation in onInit survives a configuration switch"() {
when: "the view opens: c1 activated in onInit, then a base condition on 'amount' set on the loader"
GenericFilter filter = navigateToView(GfBaseConditionAfterActivationView).genericFilter

and: "switching to c2"
filter.setCurrentConfiguration(filter.getConfiguration("c2"))

then: "the base condition (on 'amount') is still applied alongside c2"
hasPropertyConditionOn(filter.dataLoader.condition, "amount")
}

def "base condition revised after activation in onInit is the one preserved on switch"() {
when: "the view opens: base on 'amount', activate c1, then base revised to 'total' — all in onInit"
GenericFilter filter = navigateToView(GfBaseConditionReviseView).genericFilter

and: "switching to c2"
filter.setCurrentConfiguration(filter.getConfiguration("c2"))

then: "the revised base condition (on 'total') is applied, not the pre-activation one"
hasPropertyConditionOn(filter.dataLoader.condition, "total")
}

def "explicitly set initial condition is preserved when a configuration is later activated"() {
given: "the view opens with a configuration built but not activated (filter has not contributed yet)"
GenericFilter filter = navigateToView(GfConfigsNoActivationView).genericFilter

and: "the loader already holds some condition, and a DIFFERENT initial condition is set explicitly"
filter.dataLoader.setCondition(PropertyCondition.equal("number", "X"))
FilterUtils.updateDataLoaderInitialCondition(filter, PropertyCondition.greater("amount", 0))

when: "a configuration is activated (the first filter contribution)"
filter.setCurrentConfiguration(filter.getConfiguration("c1"))

then: "the explicitly set initial condition (on 'amount') is used, not the loader's prior condition"
hasPropertyConditionOn(filter.dataLoader.condition, "amount")
}

def "a logical base condition is preserved and combined with the active configuration"() {
given: "the view opens with a configuration built but not activated"
GenericFilter filter = navigateToView(GfConfigsNoActivationView).genericFilter

and: "the loader has a logical base condition with two properties"
filter.dataLoader.setCondition(LogicalCondition.and(
PropertyCondition.greater("amount", 0),
PropertyCondition.greater("total", 0)))

when: "a configuration is activated"
filter.setCurrentConfiguration(filter.getConfiguration("c1"))

then: "both base conditions are still applied alongside the configuration"
hasPropertyConditionOn(filter.dataLoader.condition, "amount")
hasPropertyConditionOn(filter.dataLoader.condition, "total")
}

protected static boolean hasPropertyConditionOn(Condition condition, String property) {
if (condition instanceof PropertyCondition) {
return property == condition.property
}
if (condition instanceof LogicalCondition) {
return condition.conditions.any { hasPropertyConditionOn(it, property) }
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -853,4 +853,86 @@ class GenericFilterBuilderApiTest extends FlowuiTestSpecification {
then:
noExceptionThrown()
}

def "PropertyFilterBuilder.label() sets the label on the built PropertyFilter"() {
given:
GenericFilter filter = filterWithLoader()

when:
PropertyFilter<String> pf = filter.filterComponentBuilder()
.<String> propertyFilter()
.property("number")
.operation(PropertyFilter.Operation.EQUAL)
.label("Order number")
.build()

then:
pf.label == "Order number"
}

def "JpqlFilterBuilder.join() sets the JOIN clause on the built JpqlFilter"() {
given:
GenericFilter filter = filterWithLoader()

when:
JpqlFilter<String> jf = filter.filterComponentBuilder()
.jpqlFilter(String)
.parameterName("tag")
.where("t.name = ?")
.join("join {E}.tags t")
.build()

then:
jf.queryCondition.join == "join {E}.tags t"
}

def "RunTimeConfigurationBuilder.buildAndRegister() throws when the filter has no DataLoader"() {
given: "a GenericFilter without a DataLoader, with an id set so the DataLoader check is reached"
GenericFilter filter = uiComponents.create(GenericFilter)

when:
filter.runtimeConfigurationBuilder()
.id("noLoader")
.buildAndRegister()

then:
thrown(IllegalStateException)
}

def "RunTimeConfigurationBuilder.add() accepts a non-single filter component (GroupFilter)"() {
given: "a GenericFilter with a DataLoader and a GroupFilter condition"
GenericFilter filter = filterWithLoader()
GroupFilter group = filter.filterComponentBuilder()
.groupFilter()
.add(filter.filterComponentBuilder().jpqlFilter().where("{E}.number = '1'").build())
.build()

when: "adding the GroupFilter (not a SingleFilterComponentBase) to a runtime configuration"
RunTimeConfiguration config = filter.runtimeConfigurationBuilder()
.id("withGroup")
.add(group)
.buildAndRegister()

then: "the GroupFilter is part of the configuration"
config.rootLogicalFilterComponent.filterComponents.contains(group)
}

def "RunTimeConfigurationBuilder.add() of a value-less single component stores no default value"() {
given: "a GenericFilter and a PropertyFilter on a numeric property with no value"
GenericFilter filter = filterWithLoader()
PropertyFilter<BigDecimal> pf = filter.filterComponentBuilder()
.<BigDecimal> propertyFilter()
.property("amount")
.operation(PropertyFilter.Operation.EQUAL)
.build()

when: "adding it without a value (paramName non-null, value null)"
RunTimeConfiguration config = filter.runtimeConfigurationBuilder()
.id("noValue")
.add(pf)
.buildAndRegister()

then: "no default value is recorded for the parameter"
config.getFilterComponentDefaultValue(pf.parameterName) == null
}
}
Loading