Ricardo Machado
Ricardo Machado

Reputation: 782

Hybris Synchronization calling prepareInterceptor

I wrote a ProductPrepareInterceptor to always run a logic before save product to set a property called "sellable" to true or false depending on it's other properties value.

But when i run the Synchronization of just one product, it calls my interceptor 36 times, always with the same properties in the object.

So my question is: Is this behavior normal? Why the synchronization call the save() function to the same object so many times?

Upvotes: 1

Views: 1286

Answers (1)

alain.janinm
alain.janinm

Reputation: 20065

I don't know if it's normal but I can say that you use an interceptor while you should use dynamic attribute.

When you wan't to have a value calculated depending on the value of other values, then use dynamic attribute.

For exemple in you items definition

            <attribute qualifier="myDynamicAttribute" type="java.lang.String">
                <description>my dynamic attribute</description>
                <modifiers read="true" write="false" search="false" optional="true" />
                <persistence type="dynamic" attributeHandler="myDynamicAttributeBean" />
            </attribute> 

And in your spring your define the handler:

<bean id="myDynamicAttributeBean" class="com.foo.MyDynamicAttributeHandler"/>

Then the class looks like :

public class MyDynamicAttributeHandlerimplements DynamicAttributeHandler<String, ProductModel>
{
    @Override
    public String get(final ProductModel model)
    {
         //your logic here to calculate the value
    }

    @Override
    public void set(final ProductModel model, final String value)
    {
         throw new UnsupportedOperationException();
    }
}

If you need a localized version extends DynamicLocalizedAttributeHandler you'll then have to implement two other methods public String get(final ProductModel model, final Locale locale) and public void set(final ProductModel model, final String value, final Locale locale).

Note that set methods must return UnsupportedOperationException.

Upvotes: 1

Related Questions