Hybris:如何从上下文中获取枚举的名称?

时间:2018-04-27 11:42:58

标签: hybris

这是我的代码:

public void onValidate(final Object o, final InterceptorContext ctx) throws InterceptorException
{
    if (o instanceof ProductModel)
    {
        final ProductModel product = (ProductModel) o;
        if (!ctx.isNew(product))
        {

            if (StringUtils.isEmpty(product.getCode()))
            {
                throw new InterceptorException("The Code must not be empty!");
            }
            if (StringUtils.isEmpty(product.getManufacturerName().toString()))
            {
                throw new InterceptorException("The ManufacturerName must not be empty!");
            }               
            if (ctx.isModified(product, ProductModel.MANUFACTURERPRODUCTID)
                    || ctx.isModified(product, ProductModel.MANUFACTURERNAME))
            {

                final boolean b = ProductLookupService.getProductforManufacturerName(product.getManufacturerName().toString());
...    
   }

我需要enum ManufacturerName的名称来将它与另一个String进行比较,但我生成的getManufacturerName只返回代码。我的选择是什么?这是-item.xml和我生成的get方法:

<enumtype code="ManufacturerName" generate="true" autocreate="true" dynamic="true"/>
<itemtype code="Product" generate="false" autocreate="false">
    <attributes>
       <attribute type="ManufacturerName" qualifier="manufacturerName" generate="true">
                    <description> </description>
                    <persistence type="property" />
                </attribute>
...
</attributes>

/**
 * <i>Generated method</i> - Getter of the <code>Product.ManufacturerName</code> attribute defined at extension <code>myextension</code>. 
 * @return the manufacturerName
 */
@Accessor(qualifier = "manufacturerName", type = Accessor.Type.GETTER)
public ManufacturerName getManufacturerName()
{
    return getPersistenceContext().getPropertyValue(MANUFACTURERNAME);
}

再次感谢!

1 个答案:

答案 0 :(得分:3)

使用EnumerationService的getEnumerationName()方法。

import de.hybris.platform.enumeration.EnumerationService;

...

private EnumerationService enumerationService;

...

// Returns current session language name
String name = enumerationService.getEnumerationName(product.getManufacturerName());
// Returns name for a given locale
String englishName = getEnumerationName(product.getManufacturerName(), Locale.ENGLISH);

...

@Required 
public void setEnumerationService(EnumerationService enumerationService) {
    this.enumerationService = enumerationService;
}

春天宣言

<bean id="myClass" ...>
    <property name="enumerationService" ref="enumerationService" />
</bean>