Is there such thing as a "Calculated Attribute" for an entity?

Hi,

Apologies for the numerous questions and thank you for your detailed answers always.

Having worked with MS Lightswitch previously, there was a possibility to define a “calculated attribute” for an entity.
This allowed for setting the attribute’s value via code, without specifying any event / timing when this code should be run.

Does this exist in Cuba?

As background for this question, this is what I am trying to achieve:
Let’s say I have a Customer entity with a Rank attribute.
The Rank attribute is a related entity (combination), with RankName and EffectiveDate attributes.

So, Customer “Joe” could have 2 Rank attributes:
(1) RankName “Standard”, EffectiveDate “2015/5/1”
(2) RankName “Premium”, EffectiveDate “2016/8/1”

I would like to show in one table my Customer attributes AND the CURRENT (most recent rank smaller or equal to TODAY) rank.
So I was thinking of adding a “Calculated Attribute” in my Customer entity fetching the current effective rank.

Alternatives I can think of are:

  • Create a persistent attribute for Customer, and fetch current rank before table display
  • Use addGeneratedColumn?
    Which would you recommend?

Thank you for your help,

Matthis

2 Likes

Hi Matthis,
You can create a non-persistent attribute in the Customer entity: set Transient and Read only checkboxes when creating the attribute in Studio. After that, Studio will generate a field and a getter like these:


    @Transient
    @MetaProperty
    protected String fullName;

    public String getFullName() {
        return fullName;
    }

In you case, the field is not needed, so remove it and set @MetaProperty annotation on the getter:


    @MetaProperty
    public String getFullName() {
        return firstName + " " + lastName;
    }

Keep in mind, that when you access the non-persistent attribute, the persistent attributes that are used in its calculations must be loaded. So include them in appropriate view.

Such non-persistent attributes can be used everywhere in UI and business logic. If you need the calculated attribute only in one UI table, a generated column can be a viable alternative.

1 Like

Hi Aleksey,

this is pretty interesting. I didn’t know about that. One side question here: Is it possible to do filtering and sorting on these attributes (through the generic filter UI)? In case of filtering, how does the database actually know about this filter criteria or will it be filtered in memory after the DB gave you all the necessary data?

bye,
Mario

Hi Mario,

The generic Filter component cannot use non-persistent attributes because it does filtering on the database level and no in-memory processing is implemented yet.
However sorting is possible, let me quote the docs for Table:
“If the column refers to a non-persistent entity attribute, the database sorting will be performed by attributes defined in the related() parameter of the @MetaProperty annotation. If no related attributes are specified, the sorting will be performed in memory only within the current page.”
For my example above, the annotation can be as follows:


@MetaProperty(related = {"firstName", "lastName"})
public String getFullName() {
    return firstName + " " + lastName;
}
1 Like

Hi Konstantin, thank you for your reply.
Glad to hear this is easily doable in Cuba!

Many thanks,
Matthis