Method-based property and collections and maps

When I write this code.

@JmixProperty
@DependsOnProperties("stakeholders")
fun getCurrentOwners(): List<InspectionCaseStakeholder> = stakeholders.filter { it.type?.code == InspectionStakeholderType.OWNER && it.current == true }

I get this error
Method-based property InspectionCase.getCurrentOwners doesn't support collections and maps

How to write a computed attribute which return a Collection or Map of Entity ?
The logic could be complex so I don’t want to write it more than once… but I want to use this attribute in some screens and reports

It’s currently not supported. But you can define a method-based property and fill it on entity load:

@DependsOnProperties({"stakeholders"})
@JmixProperty
@Transient
private List<InspectionCaseStakeholder> currentOwners;

public List<InspectionCaseStakeholder> getCurrentOwners() {
    return currentOwners;
}

@PostLoad
public void postLoad() {
    if (stakeholders != null) {
        currentOwners = stakeholders.stream()...
    }
}

Also, I’ve filed an issue: Support for method-based collection properties · Issue #3128 · jmix-framework/jmix

Hi,
thank you for the solution you presented. It is useful when dealing with constants.
In our case the values can be modified in numerous places in the code. Which means we need to udate the value every time. I had the idea to take advantage of kotlin’s capability to define property getter.
I tried this. It seams to work well. :tada:

    @DependsOnProperties("stakeholders")
    @JmixProperty
    @Transient
    var currentOwners: List<InspectionCaseStakeholder> =
        emptyList() // this list is never used, the getter re-compute the value each time
        get() = getCurrentStakeholders(InspectionStakeholderType.OWNER)
1 Like