如何restrict/write一个impex的验证码?

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

我想知道这是否是一种仅针对 impex 触发我的验证码的方法。我的意思是我的代码应该验证通过 Impex(而不是通过后台)创建的新产品。这是我的代码:

@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:


                }
             }
       }

我的问题是,当我创建新产品时,在 BackOffice 中没有 manufacturerName 和 manufacturerProductId 字段。 谢谢!对不起,如果我说错了什么,我是新来的:)

你说的是"In BackOffice when i create a new Product i don´t have manufacturerName and manufacturerProductId fields"。 Impex 也可能是这种情况。您正在使用的 impex 很可能现在指定了这两个属性,这就是没有问题的原因。

如果需要,您可以将这两个属性设置为强制属性,然后没有人可以在不指定 manufacturerNamemanufacturerProductId[= 的情况下创建产品37=]。 我也相信 backOffice 将更新以在创建期间也包括这两个属性,因为它们是强制性的。

您可以在 {extensionName}-items.xml(根据您的类型定义)中指定一个属性是必需的使用 optional 标志,如下所示:

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

如果这两个属性不是必需的,您必须考虑在没有它们的情况下创建产品的情况(就像您当前的 backOffice 情况)。 但是,您的拦截器应考虑两种情况(当您在创建期间指定这些属性时,当您不)

因此,您可以编辑代码以在使用前验证这两个属性是否为空。您可以在尝试获取制造商名称之前添加它:

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