radu
radu

Reputation: 120

Hybris: How to get the name of an enum from context?

this is my code:

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());
...    
   }

I need the name of enum ManufacturerName to compare it with another String, but my genereted getManufacturerName returns me just the code.What are my options? Here are the -item.xml and my generated get method:

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

and

/**
 * <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);
}

Thanks again!

Upvotes: 0

Views: 3507

Answers (1)

Johannes von Zmuda
Johannes von Zmuda

Reputation: 1822

Use EnumerationService's getEnumerationName() method.

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;
}

Spring declaration

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

Upvotes: 5

Related Questions