'My PrepareInterceptor is being executed many times with null fields

I'm trying to use my PrepareInterceptor to see if my B2BUnit is receiving addresses, if not I want to set the B2BUnit as not active.

This is my code;

@Override
public void onPrepare(final B2BUnitModel model, final InterceptorContext ctx) throws InterceptorException
{

    if (!haveMandatoryFields(model))
    {
        if (!ctx.isNew(model) && ctx.isModified(model, B2BUnitModel.ACTIVE) && model.getActive() == true)
        {
            throw new InterceptorException(InterceptorsErrorMessages.B2BUnit.MANDATORY_FIELDS);
        }
        else
        {
            model.setActive(false);
        }
    }
}

I'm having two problems here.

  1. I'm adding just one B2BUnit with 1 address and the PrepareInterceptor is being executed 3 times...
  2. Each execution come with new fields, and I need them all on the first one. The 'model.getAddresses()' are null until 3rd execution and my interceptorContext.getDirtyAttributes(model) is bringing all my B2BUnit fields except for addresses at 1st execution, readableCatalogVersions at 2nd one, and readableCatalogVersions again with addresses at 3st one.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source