How do I skip certain ValidateInterceptor?
Asked Answered
M

3

7

I have created a VariantValueCategory and wanted to skip the ValidateInterceptor as it was not allowing me to create VariantValueCategory either by Impex or by HMC. Can any one suggest me how do I skip ValidateInterceptor or any Interceptor?

Meseems answered 28/2, 2017 at 22:1 Comment(0)
B
19

Answer for hybris >= v6

Check Mouad El Fakir's answer for previous version

You can disable interceptor through code and Impex.

Using code

You can run your save model code using sessionService.executeInLocalViewWithParams and you can use parameters to avoid to use interceptors.

There are 3 types of policies :

  • InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_BEANS : to disable a list of beans
  • InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_TYPES : to disable a kind of interceptor - validator for example
  • InterceptorExecutionPolicy.DISABLED_UNIQUE_ATTRIBUTE_VALIDATOR_FOR_ITEM_TYPES : to disable UniqueAttributesValidatoron a set of type

Example 1 - Disable beans

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_BEANS, ImmutableSet.of("yourDataInterceptorToDisable"));

sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
    @Override
    public void executeWithoutResult()
    {
        //Do your stuff  
        modelService.save(something);   // save successful - yourDataInterceptor interceptor is disabled
    }
});

Example 2 - Disable interceptors type

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_INTERCEPTOR_TYPES,
                ImmutableSet.of(InterceptorExecutionPolicy.DisabledType.VALIDATE));
sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
    @Override
    public void executeWithoutResult()
    {
        //Do your stuff  
        modelService.save(something);    // save successful - all validate interceptors are disabled
    }
});

Example 3 - Disable by type

final Map<String, Object> params = ImmutableMap.of(InterceptorExecutionPolicy.DISABLED_UNIQUE_ATTRIBUTE_VALIDATOR_FOR_ITEM_TYPES, ImmutableSet.of("YourType"));

sessionService.executeInLocalViewWithParams(params, new SessionExecutionBody()
{
    @Override
    public void executeWithoutResult()
    {
        //Do your stuff  
        modelService.save(something);   // save successful - UniqueAttributesValidator not called
    }
});

Using Impex

It's the same thing with impex you can add 3 parameters to achieve the same thing as code

Example 1 - Disable beans [disable.interceptor.beans='yourDataInterceptorToDisable']

INSERT_UPDATE YourType[disable.interceptor.beans='yourDataInterceptorToDisable'];isocode[unique=true];toto;titi;
;something;toto;titi;

Example 2 - Disable interceptors type [disable.interceptor.types=validate]

INSERT_UPDATE YourType[disable.interceptor.types=validate];isocode[unique=true];toto;titi;
;something;toto;titi;

Example 3 - Disable by type [disable.UniqueAttributesValidator.for.types='YourType']

INSERT_UPDATE YourType[disable.UniqueAttributesValidator.for.types='YourType'];isocode[unique=true];toto;titi;
;something;toto;titi;

Ref : https://help.hybris.com/6.3.0/hcd/9ce1b60e12714a7dba6ea7e66b4f7acd.html

Battue answered 1/3, 2017 at 9:52 Comment(2)
Awesome, i didn't know that it's possible to disable a specific interceptor from impex.Depreciatory
@MouadELFakir Yes I guess it's quite new. I've not heard of it while I was working on 5.4. Actually your answer is good for version before 6 because above code probably won't work.Battue
D
10

Actually there are two modes of importing data with ImpEx in Hybris :

  • Active mode : it uses the ServiceLayer to do import. It means that actions like INSERT, UPDATE and REMOVE are performed using ModelService, thus the ServiceLayer infrastructure like interceptors and validators are triggered.
  • Legacy mode : it's a very quick CRUDE import, which means it's bypassing the ServiceLayer of Hybris, hence no interceptors and no validators are invoked.

So how to enable legacy mode ? will You can do this in three different ways :

  1. In local.properties set impex.legacy.mode = true and restart the server.
<!-- local.properties -->

impex.legacy.mode = true
  1. Or if you do import using HAC, check legacy mode checkbox :

enter image description here

  1. Or set the configuration directly into theimpex like this :
INSERT_UPDATE VariantValueCategory[impex.legacy.mode=true] ;myAttribute
...

However if you want to disable completely the interceptor from being called (not just for impexes), you can replace it with a VoidInterceptor.

VoidInterceptor : it's an empty interceptor, it does nothing at all.

So if we suppose that you want to prevent this interceptor variantCategoryValidateInterceptor from being invoked, you can replace it like this :

<!-- in my*-spring.xml -->

<bean id="variantValueCategoryVoidInterceptorMapping" class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">

    <property name="interceptor" ref="VoidInterceptor"/>

    <property name="typeCode" value="VariantValueCategory"/>

    <property name="replacedInterceptors" ref="variantCategoryValidateInterceptor"/>

</bean>
Depreciatory answered 28/2, 2017 at 23:4 Comment(1)
The impex.legacy.mode way was to aggressive, it bypass all interceptors. You can't be precise and just skip one specific validator. There are now cleaner way to disable interceptor. Please check my answer!Battue
A
0

The simpliest way: unregisterInterceptor

Go to HAC -> Scripting Languages -> Groovy

def inteceptorMapping = spring.getBean("yourInterceptorMappingBeanId")
registry = spring.getBean("interceptorRegistry");
registry.unregisterInterceptor(inteceptorMapping);
Astrid answered 8/7, 2020 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.