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:
-
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:
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:
- run ant all (refresh Eclipse workspace if necessary)
-
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.