cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to define an enum in a bean with associated values?

ccronquillo
Explorer
0 Likes
2,446

Hi! I'm refactoring a part of my code when it comes to user access levels, so I thought of creating an <enum> in one of our *-beans.xml files, i.e.,

<enum class="com.foo.UserRoleEnum>  
  <value>ADMIN</value>  
  <value>SUPER_USER</value> 
  <value>MODERATOR</value>  
  <value>NORMAL_USER</value>  
</enum>

However, in the database, the roles have associated numeric values. For example, the "Admin" can take 90, while Normal user takes 5.

How can I design an enum in such a way that each value is associated to a numeric value? An analogy to what I need is if you defined a Java enum like this way:

public enum UserRoleEnum {
  ADMIN(90), NORMAL_USER(5);

  private int value;

  UserRoleEnum(int v) {
    this.value = v;
  }

  public int getV() {
    return value;
  }
}

Of course, I can just defined this above class in our code and my problem is solved, right? But I'm wondering if something like this could be configured from beans.

Accepted Solutions (1)

Accepted Solutions (1)

mansurarisoy
Contributor

You may achieve what you want by using "template" attribute for the bean. I found an example from OOTB in rulebuilderbackoffice-beans.xml as follows:

<enum class="de.hybris.platform.rulebuilderbackoffice.editors.ValidationInfoSeverity"
	template="resources/validationinfoseverity-template.vm">
	<description>Represents severity level of Rule Validation results</description>
	<value>ERROR("z-icon-times-circle", "ye-validation-error")</value>
	<value>WARN("z-icon-exclamation-triangle", "ye-validation-warn")</value>
	<value>INFO("z-icon-info-circle", "ye-validation-info")</value>
	<value>NONE("", "ye-validation-none")</value>
</enum>

In that example, it's used for a String value, but I believe you can use it for an Integer value also.

Hope this helps,

ccronquillo
Explorer
0 Likes

How does the .vm file look like? I can't seem to locate this if it's OOTB...

mansurarisoy
Contributor
0 Likes

You can find the template file under

Before 1905: bin/ext-backoffice/rulebuilderbackoffice/resources/validationinfoseverity-template.vm

After 1905: bin/modules/rule-engine/rulebuilderbackoffice/resources/validationinfoseverity-template.vm

ccronquillo
Explorer
0 Likes

Got it to work!! Thanks 😄

Answers (2)

Answers (2)

ccronquillo
Explorer

Thanks again guys. And just to share, I ended up with this Velocity template, which is pretty much the thing I wanted the enum to end up with!

It was just a bit frustrating with getting up to speed with Velocity... First time I ever encountered this. But it was nice learning it!

public enum $shortClassName
{
    #foreach($v in $enumValue)
        $v.comment
        ${StringUtils.upperCase($v.name)}#if ($foreach.last)  #else , #end
    #end ;

    private int value;

    $shortClassName(int vv) {
        this.value = vv;
    }

    public int getValue() {
        return value;
    }

}
former_member632827
Participant

Hey,

We can control the data/enum class generation by the template. May it be useful for you

https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/1811/en-US/8bc53579866910149472ccbef022...

Check some OOTB defined enum configurations

<enumclass="de.hybris.platform.ruleengineservices.configuration.Switch"template="resources/string_enum.vm">
<value>CONSUMPTION("ruleengineservices.consumption.enabled")</value>
</enum> <enum class="de.hybris.platform.rulebuilderbackoffice.editors.ValidationInfoSeverity" template="resources/validationinfoseverity-template.vm"> <description>Represents severity level of Rule Validation results</description> <value>ERROR("z-icon-times-circle", "ye-validation-error")</value> <value>WARN("z-icon-exclamation-triangle", "ye-validation-warn")</value> <value>INFO("z-icon-info-circle", "ye-validation-info")</value> <value>NONE("", "ye-validation-none")</value> </enum>