cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Null Value Decorator Support for Enumeration Attributes

former_member348752
Participant
0 Kudos
2,342

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?

Accepted Solutions (0)

Answers (1)

Answers (1)

andyfletcher
Active Contributor
0 Kudos

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.