Skip to main content

Calculated Fields

It is possible to create a ValueField for which the value is not stored in the database, but rather derived based on other values.

First, you will need to define a ValueField with fieldType set to CALCULATED_FIELD:

<field name="totalWeight">
<fieldType>CALCULATED_FIELD</fieldType>
<valueField>
<valueFieldType component="" name="Long"/>
</valueField>
</field>

After expanding this, the cruds class will contain a method stub to implement the calculation.

StarshipCruds
@Override
public CrudsResult<Long> performCalculateTotalWeight(ParameterContext<StarshipData> dataParameter) {
StarshipData starshipData = dataParameter.getValue();
Long result = null;

// anchor:custom-calculation-totalWeight:start
result = starshipData.getComponents().stream()
.mapToLong(StarshipComponentData::getWeight)
.sum();
// anchor:custom-calculation-totalWeight:end

return CrudsResult.success(result);
}

You can then add this Field to a Projection, or as an info Field.