Sunday 31 March 2019

Dynamic Attribute

The following example shows the configuration of the Dynamic Attribute in the items.xml file:

<itemtype code="Accommodation" autocreate="true" generate="true" jaloclass="de.hybris.platform.test.Accommodation">
<attributes>
                <!-- ... -->
                <attribute type="java.lang.String" qualifier="daysHumanReadable">
                <persistence type="dynamic" attributeHandler="accommodationDays" />
                <modifiers read="true" write="false" />
                </attribute>
                <!-- ... -->
</attributes>
</itemtype>

AccommodationDays.java


public class AccommodationDays implements DynamicAttributeHandler<String, AccommodationModel>
{

 @Override
 public String get(final AccommodationModel model)
 {
                String daysWord;
  final int days = model.getDays();
                if (days == 1)
                {
                       daysWord = "day";
                } 
                else
                {
                       daysWord = "days";
                }
                return days + " " + daysWord;
 }

 @Override
 public void set(final AccommodationModel model, final String value)
 {
  throw new UnsupportedOperationException();
 }

}


It is recommended to implement the above method to throw an exception, because the write modifier of the daysHumanReadable attribute is set to false. Thus there is no setter generated in the AccommodationModel.

we can get the value based on some business logic.

No comments:

Post a Comment