An interceptor addresses a particular step in the life cycle of a model. When the life cycle reaches a certain step, you can activate a corresponding interceptor. An interceptor can modify the model, raise an exception to interrupt the current step or publish an event if the model matches certain criteria. For example, you could check that an attribute contains certain values before saving the model.
In easy words, we can say that anything that intercepts something else is called an interceptor. We can take one example to explain interceptor concept in a real-life scenario. We take one example where a person wants to watch a movie in the cinema hall. Now we will see how an interceptor is used in this scenario.
In the above diagram, we can see that one person went to the cinema hall and his journey intercepted at the ticket checking counter. Here Diamond(Has ticket) works as an interceptor. If a person has a ticket then he is permitted to watch a movie. If he doesn’t have a ticket then he is redirected to buy a ticket at the ticket counter.
What is the model’s life cycle?
This question is asked in hybris interviews. Here I am trying to explain this so it can help you in cracking hybris interview.
A model represents a state in the database. The representation is not live, that means that modified model values are not written to the database automatically. Instead, when you modify a model, you must explicitly save it to the database to have its state reflected there.
Phases in the Model’s life cycle
There are four phases in the model’s life cycle.
- Instantiating the model
- Modifying model values
- Saving Model Values
- Removing the model
1. Instantiating the model
This can be done by either creating a new Model instance or by loading a Model from the database.
- Creating a model instance:- Visit link Model service create method vs new operator.
- Loading an existing model from the database is possible either by using pk or by using a query expression.
Example:- UserModel user = modelService.get(pk); [loading using primary key]
2. Modifying model values
We can modify the properties of the model if required.
3. Saving model values
If a model is created or modified. We save back the model to update the database. If we have used a new model, a new record is inserted/created in the database, otherwise, the existing record is updated.
4. Removing the model
If the model is no longer needed, the database record is deleted.
We can use interceptors to hook into the model’s life cycle.
Types of interceptors
The load interceptor is called whenever a model is loaded from the database. You may want to use this interceptor if you want to change the values of the model after load. An exception raised during execution prevents the model from being loaded. To use this interceptor we must implement LoadInterceptor interface.
The InitDefaults interceptor is called when a model is filled with its default values. This happens either when it is created via the modelService.create method or when the modelService.initDefaults method is called. You can use this interceptor to fill the model with additional default values defined in the items.xml. To use this interceptor we must implement InitDefaultsInterceptor interface.
The Prepare interceptor is called before a model is saved to the database before it is validated by Validate interceptors. Use this to add values to the model or modify existing ones before they are saved. An exception is raised during execution prevents the model from being saved. Prepare interceptor is called before the Impex translators. To use this interceptor we must implement PrepareInterceptor.
The Validate interceptor is called before a model is saved to the database after is been prepared by the Prepare interceptors. We can use Validate interceptor to validate values of the model and raise an InterceptorException if any values are not valid. To use this interceptor we must implement ValidateInterceptor.
The Remove interceptor is called before a model is removed from the database. We can use this interceptor to prevent the removal of the model by raising an InterceptorException. To use this interceptor we must implement RemoveInterceptor.
How to register an interceptor?
After implementing an interceptor, we need to register it as a spring bean. The steps involved in registering an interceptor as followed:-
- Navigate to customExtension->resources and open customExtension-spring.xml. Add the following line of code to this file.
- In this step, we will do Interceptor mapping as shown in below image.