on 2017 May 10 8:04 PM
Hybris 6.x has support for Null Value Decorators (or nullDecorators, depending on the documentation) that ensure that an item model attribute accessor/getter never returns null.
The given example uses the use case of a boolean (if the underlying persistence layer is null, the attribute calculated returns false)
<model>
<getter default="true" name="calculated">
<nullDecorator>Boolean.valueOf(false)</nullDecorator>
</getter>
</model>
Can the nullDecorator be used with enumerations? If so, how can it be used with enumerations?
Request clarification before answering.
Don't you just put the value of the decorator enum in there? I think it just gets placed a string into the model method so it just needs to be something that will compile.
e.g. (adding a new myStatus attribute on AbstractOrder)
<itemtype code="AbstractOrder" autocreate="false" generate="false">
<attributes>
<attribute qualifier="myStatus" type="OrderStatus">
<persistence type="property" />
<model>
<getter default="true" name="myStatus">
<nullDecorator>OrderStatus.CREATED</nullDecorator>
</getter>
</model>
</attribute>
</attributes>
</itemtype>
would generate
public OrderStatus getMyStatus() {
final OrderStatus value = getPersistenceContext().getPropertyValue(MYSTATUS);
return value != null ? value : OrderStatus.CREATED;
}
I've just tried this and generates compilable code.
Thanks for pointing this out. I've not come across it before. Pretty handy for when adding attributes to types with existing data. Now we don't need to write the callers so defensively or remember to set a default on existing data when deploying.
Is it v6 thing? I can't find any reference to it in the v5 docs but then the search is pretty ropey so it might just be missing it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.