on ‎2021 Aug 06 11:46 AM
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.
Request clarification before answering.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey,
We can control the data/enum class generation by the template. May it be useful for you
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.