SAP Commerce (Hybris) interview questions with answers - Part 3 - Dynamic Attributes

image from wikimedia.org

What is the difference between dynamic and persisted attributes?

Dynamic Attributes allow to add custom logic to the Models attributes without the touching peristence layer.

How can I create a Dynamic Attribute?

1. Need to define the Dynamic Attribute in the items.xml File
  • Add the new Dynamic Attribute
  • Set the persistence type of new attribute to dynamic
  • Provide a custom attributeHandler. If you do not provide the custom bean ID for attributeHandler, it is automatically generated in the following way: ClientName_longNameAttributeHandler
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    <?xml version="1.0" encoding="UTF-8"?>
    <itemtype code="CarInfo"
    extends="GenericItem"
    jaloclass="de.hybris.tutorial.jalo.CarInfo"
    autocreate="true"
    generate="false">
    <attributes>
    <!-- Existing functionality -->
    <attribute autocreate="true" qualifier="brand" type="java.lang.String">
    <persistence type="property" />
    </attribute>
    <attribute autocreate="true" qualifier="carName" type="java.lang.String">
    <persistence type="property" />
    </attribute>
    <attribute autocreate="true" qualifier="bodyType" type="java.lang.String">
    <persistence type="property" />
    </attribute>
    <attribute autocreate="true" qualifier="bodyCode" type="java.lang.String">
    <persistence type="property" />
    </attribute>
    <!-- The new Dynamic Attribute -->
    <attribute type="java.lang.String" qualifier="fullName">
    <persistence type="dynamic" attributeHandler="fullNameHandler" />
    <modifiers read="true" write="false" optional="true" unique="false" />
    </attribute>

    <!-- other attributes -->
    </attributes>
    </itemtype>
2. Create your DynamicAttributeHandler.

The class should implement DynamicAttributeHandler interface and override the getter and setter methods. Because it doesn’t require a setter (see items.xml above) we can just throw an UnsupportedOperationException when calling the setter method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class FullNameHandler implements DynamicAttributeHandler<String, CarInfoModel> {
public static final String VALUE_DELIMITER = " ";

@Override
public String get(final CarInfoModel item) {
if (item == null) {
throw new IllegalArgumentException("Item model is null!!!");https://www.linkedin.com/
}
StringBuilder builder = new StringBuilder();
// Brand Name: BMW
builder.append(item.getBrand().getName());
builder.append(VALUE_DELIMITER);
// Car Name: 1 Series
builder.append(item.getCarName().getName());
builder.append(VALUE_DELIMITER);
// Body Type: 2 door convertible
builder.append(item.getBody().getType().getName());
builder.append(VALUE_DELIMITER);
// Body Code: E88
builder.append(item.getBody().getBodyCode());

return builder.toString();
}

@Override
public void set(final CarInfoModel item, final String value) {
throw new UnsupportedOperationException();
}
}
3. Register the Spring Bean
1
<bean id="fullNameHandler" class="com.carsupplier.core.servicelayer.FullNameHandler"/>
4. Update Running System by hac or the command line
5. Use the attribte
1
2
3
4
5
protected void setFullName(CarInfoModel source, CarInfoData target){
// call the method
String fullName = source.getFullName();
target.setFullName(fullName);
}
6. Check the possible result on the frontend

image from wikimedia.org

Can you localize dynamic attributes?

Yes. You need to use localized type in items.xml and implement the DynamicLocalizedAttributeHandler
Some example from items.xml:

1
2
3
4
5
6
7
8
<!-- other attributes -->

<attribute type="localized:java.lang.String"> qualifier="fullName">
<persistence type="dynamic" attributeHandler="dynamicFullNameHandler" />
<modifiers read="true" write="false" optional="true" unique="false" />
</attribute>

<!-- other attributes -->

Also don’t forget to register the Spring Bean and do the other stuff.

Ok. Let’s look at the code. What’s wrong with this example of using a dynamic attribute?

Just find the main issue here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Override
public String get(final CarInfoModel carInfoModel) {
List<String> addresses = new ArrayList<>();
for (CarModel carModel : carInfoModel.getModels()){
List<DealerModel> dealers = dealerFinder.findDealers(carModel);
for (DealerModel dealerModel : dealers){
PriceRowModel bestPrice = carSellingService.getBestPrice(carModel, dealerModel);
if (bestPrice.getPrice()<TOO_CHEAP){
addresses.add(dealerModel.getAddress());
}
}
}
return listFormatter.format(addresses);
}

The main issue here, as you can see is that this code has a significant performance impact during the call the method. Dynamic attributes are expected to be fast and simple. They shouldn’t contain any complicated logic or perform expensive calls.

Created based on official documentation

Share