Interceptors in SAP Commerce(Hybris)

Interceptors are called when we perform an operation on a particular object/model. They can be called at different lifecycles of ModelService object.

As we know the Models have it’s own lifecycle managed by ModelService. The predefined methods which represents the Model life cycle are:

Create: to create model instance;

Update: to update/modify the properties of the model;

Save: when we create or update Model we save back the model to update the database. If we have used a new model, a new record is created in the database, otherwise, the existing record is updated;

Load: to get existing model from the database;

Remove: to delete no longer needed records from the database.

We can use interceptors to modify the model, raise an exception, to interrupt the current step or publish an event. Interceptors can interrupt the lifecycle of a model and execute some logic and then lifecycle of the model can continue its process.

Types of interceptors in Hybris

1) Init Defaults Interceptor

This Interceptor is called when we create a new model filled with its default values calling modelService.initDefaults() or modelService.create(). We can use this interceptor to fill the model with additional default values.

To use this interceptor we must implement InitDefaultsInterceptor interface and then override onInitDefaults() method.

After implementing an interceptor, register it as a Spring bean:

1. In customextension-spring.xml add the following line

2. And add this line in the same file to do Interceptor mapping:

2) Prepare Interceptor

This Interceptor is called before a model is saved to the database(modelService.save()) and before it is validated by Validate interceptors. We can use this Interceptor to add values to the model or modify existing ones before they are saved. It is always called before the impex translators.

To use this interceptor we must implement PrepareInterceptor interface and then override onPrepare() method.

Then register and map the CustomProductPrepareInterceptor as bean in Spring like the first example.

3) Validate Interceptor

This Interceptor is called before a model is saved to the database(modelService.save()) after is been prepared by the Prepare interceptors,. You can use Validate Interceptors to validate values of the model and raise an InterceptorException if any values are not valid. An exception raised during execution prevents the model from being saved.

To use this interceptor we must implement ValidateInterceptor interface and then override onValidate() method.

Then register and map the ProductValidateInterceptor as bean in Spring like the first example.

4) Load Interceptor

This Interceptor is called after a model is retrieved from the database using modelService.get(). We can use this interceptor if we want to change to change the values of the model even after being loaded from the Database

To use this interceptor we must implement LoadInterceptor interface and then override onLoad() method.

Then register and map the ProductLoadInterceptor as bean in Spring like the first example.

5) Remove Interceptor

This Interceptor is called before a model is removed from the database using modelService.remove(). We can use remove interceptor whenever we need to remove models that are related to the model but are not in the model context or to prevent the removal of the model by raising an InterceptorException.

To use this interceptor we must implement RemoveInterceptor interface and then override onRemove() method.

Then register and map the ProducRemoveInterceptor as bean in Spring like the first example.

--

--

--

Software Development Lead @Coca-Cola HBC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Accelerating the debugging process by using a hybrid environment

Try Mito Python Package: The Data Scientist’s Spreadsheet

Software Deployment Strategies

Introduction to Python Image Library

Apache Kafka: What Is and How It Works

Binary data travelling through

Setup On-premise Kubernetes with Kubeadm, MetalLB, Traefik and Vagrant

How to set up USB-serial program connection #1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nuray Fahri

Nuray Fahri

Software Development Lead @Coca-Cola HBC

More from Medium

Let’s dive into head first Java

Method Overloading and Method Overriding in Java

Introduction to Java Basics

Teach Me OOP