Saturday, 14 May 2016

different types of interceptors in hybris ?

1. Model Interceptors
2. Attribute Handler
3. Validation services and Constraints


                                                Model Interceptors


Interceptors intercept the behavior of the lifecycle of Models. Model lifecycle consists of loading from Database, saving to Database and deleting or removing from the Database.
In the lifecycle of a model, interceptors can intercept and modify the model data. It includes auditing, validating and even restricting the data from being removed/deleted from database if certain conditions are not met.


                                Hybris provides five types of Interceptors:


1. LoadInterceptor: Invoked whenever a model is loaded from the database
void onLoad (Object model, InterceptorContext ctx) throws InterceptorException;

2. InitDefaultsInterceptor : Invoked during modelService.create() & modelService.initDefaults()
void onInitDefaults(Object model, InterceptorContext ctx) throws InterceptorException;

3. PrepareInterceptor : Invoked before a model is saved to database & before ValidateInterceptor
void onPrepare(Object model, InterceptorContext ctx) throws InterceptorException;

4. ValidateInterceptor : Invoked before a model is saved to database & after PrepareInterceptor
void onValidate(Object model, InterceptorContext ctx) throws InterceptorException;

5. RemoveInterceptor : Invoked before a model is removed from the database.
void onRemove(Object model, InterceptorContext ctx) throws InterceptorException;


                                              How to implement?


Implement an interface (LoadInterceptor, InitDefaultsInterceptor, PrepareInterceptor, ValidateInterceptor and RemoveInterceptor)

Register an Interceptor in spring.xml:

<bean id=”myValidateInterceptor” class=”mypackage.MyValidateInterceptor”
autowire=”byName”/>
<bean id=”MyValidateInterceptorMapping”
class=”de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping”>
<property name=”interceptor” ref=”myValidateInterceptor”/>
<property name=”typeCode” value=”MyType”/>
<! — The order property is only effective with 4.1.1 and later –>
<property name=”order” value=”5000″/>
</bean>
Attribute Handler
Dynamic Attributes:
Dynamic Attributes enable you to add attributes to the Hybris models and to create a custom logic behind them, without altering the Hybris Model class itself.
A Dynamic Attribute is defined for a type in the items.xml, have non-persistent values. Since Hybris 4.4.0 version, Dynamic Attributes replace Jalo attributes.

How to implement?

1. In items.xml, add the following definitions:
<itemtype code=”Accommodation” autocreate=”true” generate=”true”>
<attributes>
<!– … –>
<attribute type=”java.lang.String” qualifier=”daysHumanReadable”>
<persistence type=”dynamic” attributeHandler=”accommodationDays” />
<modifiers read=”true” write=”false” />
</attribute>
<!– … –>
</attributes>
</itemtype>
2.Register the newly created attributeHandler in Spring context:
<bean id=”accommodationDays”
class=”de.hybris.platform.servicelayer.test.AccomodationDays”   />
3. Class AccommodationDays must implement DynamicAttributeHandler interface
Validation Services & Constraints
The Hybris Data Validation Framework is based on the JSR 303 Java validation specification. It offers an easy and extensible way to validate data. Only validated data is passed on to the persistence layer. Constraint definitions are written in Java and XML.


                        Hybris Data Validation consists of the following three areas:


The Validation Service is a service in the Service Layer which defines constraints and performs data validation.

The Administration Cockpit allows you to create and manage constraints.

Cockpit integration provides validation feedback to the user.

The Validation Service loads the validation engine with constraints. Data is validated based on a set of validation constraints. All modelService.save calls are intercepted aborting save operations wherever necessary. Validation methods may be called explicitly.

No comments:

Post a Comment