radu
radu

Reputation: 120

How to restrict/write a validation code just for impex?

I want to know if it is a way to trigger my Validation Code just for impex. What I mean is that my Code should validate a new Product created through Impex (not through backoffice). Here is my Code:

@Override
public void onValidate(final Object o, final InterceptorContext ctx) throws InterceptorException

    if (o instanceof ProductModel)
    {
       final ProductModel product = (ProductModel) o;
       if (ctx.isNew(product))
        {
            final String manufacturerName = enumerationService.getEnumerationName(product.getManufacturerName()); // if ManufacturerName is Null  enumerationService throw "Parameter plainEnum must not be null!"
            final String code = product.getCode().toString();
            final String manufacturerProductId = product.getManufacturerProductId();
            final int a = productLookupService.getProduct(code, manufacturerName, manufacturerProductId);
                switch (a)
                {
                    case 1:
                        throw new InterceptorException("Product already in ProductLookup");

                    case 2:
                        throw new InterceptorException(
                                "There are more than one product with the same " + code + " number in ProductLookup !");
                    default:


                }
             }
       }

My Problem is that in BackOffice when I create a new Product I don´t have manufacturerName and manufacturerProductId fields. Thanks! and sorry if I said something wrong, I am new in this :)

Upvotes: 0

Views: 702

Answers (1)

dj_frunza
dj_frunza

Reputation: 1593

You said that "In BackOffice when i create a new Product i don´t have manufacturerName and manufacturerProductId fields". This can also be the case for Impex. Most probably the impex you are using is specifying those two attributes now and that is why there is no problem.

If you want, you can make this two attributes mandatory and then nobody will be able to create a product without specifying the manufacturerName ad the manufacturerProductId. I also believe that the backOffice will update to also include these two attributes during creation since they are mandatory.

You can specify that an attribute is mandatory in your {extensionName}-items.xml(under your type definition) using the optional flag like below:

<attribute qualifier="manufacturerProductId" type="String">
  <persistence type="property"/>
  <modifiers optional="false"/>
</attribute>

If these two attributes are not mandatory, you have to consider the case when a product will be created without having them(like your current backOffice situation). However your Interceptor should take into consideration both cases (when you have these attributes specified during creation and when you don't)

Because of this you can edit your code to verify whether this two attributes are null or not before using them. You can add this right before trying to get the manufacturerName:

 if (product.getManufacturerName() == null || product.getManufacturerProductId() == null) {
    return;
}

Upvotes: 1

Related Questions