Friday 30 November 2018

Interceptors in Hybris


*  Interceptor, which intercept the normal flow of model and execute bussiness logic and allows model to continue its life cycle.
*  Interceptors will execute before saving model or after saving model or while saving model.
*  We use interceptors to validate, add ,change model data.

Hybris provided five interceptors

1. Load Interceptor
2. Init Defaults Interceptor
3. Prepare Interceptor
4. Validate Interceptor

5. Remove Interceptor

1. Load Interceptor:-

*  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.

*  We can create Load Interceptor class by implementing 
LoadInterceptor interface.

public interface LoadInterceptor extends Interceptor{
void onLoad(Object model, InterceptorContext ctx) throws InterceptorException;
}



package co.hybrisinstructive.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.LoadInterceptor;

import co.hybrisinstructive.core.model.TrainingCustomerModel;


public class TrainingLoadInterceptor implements LoadInterceptor
{

@Override
public void onLoad(final Object obj, final InterceptorContext ctx) throws InterceptorException
{
if (obj instanceof TrainingCustomerModel)
{
final TrainingCustomerModel model = (TrainingCustomerModel) obj;

if (model.getCountry() == "INDIA")
{
model.setCountry(model.getCountry() + "_IN");
}
}
}

}


<!-- ############## InitDefaultsInterceptor ########### -->
<bean id="trainingInitDefaultsInterceptor" class="co.hybrisinstructive.core.interceptors.TrainingInitDefaultsInterceptor"  autowire="byName"/>
<bean id="trainingInitDefaultsInterceptorMapping"  
       class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="trainingInitDefaultsInterceptor"/>
<property name="typeCode" value="TrainingCustomer"/>
</bean>





2.Init Defaults Interceptor:-

*  The Init Defaults Interceptor is called when a model is filled with its default values.
*  This will call either when the model 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.

*  We can create Init Defaults Interceptor class by implementing InitDefaultsInterceptor interface.

public interface InitDefaultsInterceptor extends Interceptor{
void onInitDefaults(Object model, InterceptorContext ctx) throws InterceptorException;}



package co.hybrisinstructive.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InitDefaultsInterceptor;
import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;

import org.apache.log4j.Logger;

import co.hybrisinstructive.core.model.TrainingCustomerModel;


public class TrainingInitDefaultsInterceptor implements InitDefaultsInterceptor
{
private static final Logger LOG = Logger.getLogger(TrainingInitDefaultsInterceptor.class);

@SuppressWarnings("boxing")
@Override
public void onInitDefaults(final Object obj, final InterceptorContext ctx) throws InterceptorException
{
LOG.info("################# TrainingInitDefaultsInterceptor ###########");
if (obj instanceof TrainingCustomerModel)
{
final TrainingCustomerModel model = (TrainingCustomerModel) obj;
model.setCountryCode(91);
}
}

}





<!-- ############## InitDefaultsInterceptor ########### -->
<bean id="trainingInitDefaultsInterceptor" class="co.hybrisinstructive.core.interceptors.TrainingInitDefaultsInterceptor" autowire="byName"/>
<bean id="trainingInitDefaultsInterceptorMapping"  
     class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="trainingInitDefaultsInterceptor"/>
<property name="typeCode" value="TrainingCustomer"/>
</bean>




3. Prepare Interceptor:-

*  The Prepare Interceptor is called before a model is saved to the database. 
*  Prepare Interceptor executed first later its validate Validate interceptors.
*  Prepare interceptor is called before the Impex translators.
*  We can create Prepare Interceptor class by implementing 
PrepareInterceptor interface.


public interface PrepareInterceptor extends Interceptor{
void onPrepare(Object model, InterceptorContext ctx) throws InterceptorException;}



package co.hybrisinstructive.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.PrepareInterceptor;

import org.apache.log4j.Logger;

import co.hybrisinstructive.core.model.TrainingCustomerModel;


public class TrainingPrepareInterceptor implements PrepareInterceptor
{
private static final Logger LOG = Logger.getLogger(TrainingPrepareInterceptor.class);

@Override
public void onPrepare(final Object obj, final InterceptorContext ctx) throws InterceptorException
{
LOG.info("############ TrainingPrepareInterceptor ######################");
if (obj instanceof TrainingCustomerModel)
{
final TrainingCustomerModel model = (TrainingCustomerModel) obj;
model.setCountry("INDIA");
}
}

}




<!-- ############## PrepareInterceptor ########### -->
<bean id="trainingPrepareInterceptor" class="co.hybrisinstructive.core.interceptors.TrainingPrepareInterceptor" autowire="byName"/>
<bean id="trainingPrepareInterceptorMapping"  
    class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="trainingPrepareInterceptor"/>
<property name="typeCode" value="TrainingCustomer"/>
</bean>



4. Validate Interceptor:-

*  The Validate Interceptor is called before a model is saved to the database.
*  Validate Interceptor is executed after Prepare interceptors are executed.
    *  We can create Validate Interceptor class by implementing 
ValidateInterceptor interface.

public interface ValidateInterceptor extends Interceptor{
void onValidate(Object model, InterceptorContext ctx) throws InterceptorException;}



package co.hybrisinstructive.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.ValidateInterceptor;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import co.hybrisinstructive.core.model.TrainingCustomerModel;



public class TrainingValidateInterceptor implements ValidateInterceptor
{
private static final Logger LOG = Logger.getLogger(TrainingValidateInterceptor.class);

@Override
public void onValidate(final Object obj, final InterceptorContext ctx) throws InterceptorException
{
LOG.info("######## TrainingValidateInterceptor #############");

if (obj instanceof TrainingCustomerModel)
{
final TrainingCustomerModel model = (TrainingCustomerModel) obj;
if (StringUtils.isEmpty(model.getCustomerName()))
{
throw new InterceptorException("Training Customer cannot have empty username");
}
}
}
}



<!-- ############## ValidateInterceptor ########### -->
<bean id="trainingValidateInterceptor" class="co.hybrisinstructive.core.interceptors.TrainingValidateInterceptor" autowire="byName"/>
<bean id="trainingValidateInterceptorMapping"  
    class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="trainingValidateInterceptor"/>
<property name="typeCode" value="TrainingCustomer"/>
</bean>









5. Remove Interceptor:-

  *   The Remove Interceptor is called before a model is removed from the database 
* Generally Remove Interceptor is used to execute the logic that Stores the removed model in a separate model.
   *  We can create Remove Interceptor class by implementing 
RemoveInterceptor interface.

public interface RemoveInterceptor extends Interceptor{
void onRemove(Object model, InterceptorContext ctx) throws InterceptorException;}



package co.hybrisinstructive.core.interceptors;

import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.PersistenceOperation;
import de.hybris.platform.servicelayer.interceptor.RemoveInterceptor;

import java.util.Date;

import co.hybrisinstructive.core.jalo.TrainingRemovedCustomers;
import co.hybrisinstructive.core.model.TrainingCustomerModel;
import co.hybrisinstructive.core.model.TrainingRemovedCustomersModel;


public class TrainingRemoveInterceptor implements RemoveInterceptor
{

@Override
public void onRemove(final Object obj, final InterceptorContext ctx) throws InterceptorException
{
if (obj instanceof TrainingCustomerModel)
{
final TrainingCustomerModel model = (TrainingCustomerModel) obj;
final TrainingRemovedCustomers customer = ctx.getModelService().create(TrainingRemovedCustomersModel.class);
customer.setName(model.getCustomerName());
customer.setCountry(model.getCountry());
customer.setRemovedTime(new Date());
ctx.registerElementFor(customer, PersistenceOperation.SAVE);
}
}

}


<!-- ############## RemoveInterceptor ########### -->
<bean id="trainingRemoveInterceptor" class="co.hybrisinstructive.core.interceptors.TrainingRemoveInterceptor" autowire="byName"/>
<bean id="trainingRemoveInterceptorMapping"  
       class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor" ref="trainingRemoveInterceptor"/>
<property name="typeCode" value="TrainingCustomer"/>
</bean>