10021---Compose and Model the Merchandise Shop

Overview

This chapter is divided into 3 major steps:

  • Modelling the data
  • Applying the model to the items' XML definition
  • Testing the build

Modelling the data

Data modelling is a very important step in your design and of course should always take place prior to the main development effort. Your design and implementation should be driven by your domain. This also keeps a more stable codebase for consumers of the data via the services.

We might want to consider the following changes to the default data model required by our merchandise shop:

  • Extensions to Product attributes
    • internal / public product
  • Extensions to Customer attributes
    • isInternal attribute, holding information about whether a customer is eligible for internal products or not

The following diagram illustrates how the Product data type should be overwritten:

The following diagram shows how the Customer data type should be overwritten. For demonstration purposes, 

the customer's isInternal attribute is modeled as a dynamic attribute. This requires a special bean class which is defined in a later step.

Reflecting the model in the items' XML definition

We need to add the internal flag to the Product item type. Similarly, we need to refine the definition of the Customer item type by adding our isInternal flag.
To do this we have to add the following typegroup to the items types XML definition:

merchandisecore/resources/merchandisecore-items.xml

        <typegroup name="merchandise">
            <itemtype code="Product" autocreate="false" generate="false">
                <description>Extending the Product type from core with additional attributes.</description>
                <attributes>
                    <attribute qualifier="internalOnly" type="java.lang.Boolean">
                        <description>Defines if the product should be sold to hybris employees only.</description>
                        <persistence type="property" />
                    </attribute>
                </attributes>
            </itemtype>
 
            <itemtype code="Customer" generate="false" autocreate="false">
                <attributes>
                    <attribute qualifier="isInternal" type="java.lang.Boolean">
                        <description>Defines if the customer is a hybris internal employee. Aggregated information</description>
                        <persistence type="dynamic" attributeHandler="dynamicHybrisCustomerAttributeBean" />
                        <modifiers read="true" write="true" optional="true"
                            unique="false" />
                    </attribute>
                </attributes>
            </itemtype>
        </typegroup>
Please note, the  isInternal  attribute is dynamic , which means it's not persisted in db, but evaluated in a specialized bean.

Adding Code for Dynamic Attribute

In order to have the dynamic Attribute working follow these steps:

  1. Create the class de.hybris.merchandise.core.model.DynamicHybrisCustomerAttributeBean in the merchandisecore extension with the following content:

/**
 *
 */
package de.hybris.merchandise.core.model;
 
import de.hybris.platform.core.model.user.CustomerModel;
import de.hybris.platform.servicelayer.model.attribute.DynamicAttributeHandler;
 
 
/**
 *
 */
public class DynamicHybrisCustomerAttributeBean implements DynamicAttributeHandler<Boolean, CustomerModel>
{
 
    @Override
    public Boolean get(final CustomerModel model)
    {
        if (model == null)
        {
            throw new IllegalArgumentException("Item model is required");
        }
 
        // Accelerator stores the email in the ID (uid) field
        final String email = model.getUid();
        return Boolean.valueOf(email != null && (email.endsWith("hybris.de") || email.endsWith("hybris.com")));
 
    }
 
    @Override
    public void set(final CustomerModel model, final Boolean value)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}

2. Modify your merchandisecore-spring.xml to configure the bean:

merchandisecore/resources/merchandisecore-spring.xml
<bean id= "dynamicHybrisCustomerAttributeBean"
       class = "de.hybris.merchandise.core.model.DynamicHybrisCustomerAttributeBean" />

Localizing the new attributes

Since we have added new attributes to the type system, we need to localize them.
To do so, add the following lines to the /merchandisecore/resources/localization/merchandisecore-locales_en.properties.

Additionally, you could add localized names for other languages in the corresponding files.

merchandisecore/resources/localization/ merchandisecore-locales_en.properties

type.Customer.isInternal.name=Internal
type.Product.internalOnly.name=Internal only

Testing the extension

Perform the following steps to test your solution:

  1. run ant all (refresh Eclipse workspace if necessary)
  2. as we have modified the data model, we need to update the system. Do the update from the hAC and include the type system localization. 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值