An enumeration can be created at runtime by defining the enumeration type in the Easy type manifest file easytypes.json.
This document describes, how the Easy Enumeration Types can be configured in your Easy Extension.
Defining an easy enumeration type
An easy enumeration type definition can be created in easytypes.json manifest file. The manifest allows to define the following for an easy enumeration type:
Code of the easy enumeration type.
Localized name of easy enumeration type
Enumeration Values for easy enumeration type
Code of the enumeration value
Localized name of the enumeration value
For example: The easy enumeration type EasyUsageEnum defined below is an easy enumeration type having enumeration values as TEST and LIVE.
Using an easy enumeration type in an easy item type
While defining the easy item type definition for the easy item type, the attribute (of type easy enumeration type) definition must use the code of the easy enumeration type as the type of the attribute.
For example: the blow definition defines a new easy item type EasyItem and an attribute as usage of type as EasyUsageEnum easy enumeration type. This means that the EasyItem item holds either the value as TEST or LIVE in the attribute usage as defined in the easy enumeration type EasyUsageEnum.
Defining the enum class for an easy enumeration type
The implementation for the enum class for an easy enumeration type can be created by defining a Groovy class. Following considerations are important while defining a Groovy enum class:
For new easy enumerations types, Name of the class must be same as the Code of the easy enumeration type.
Your Groovy class must implement HybrisEnumValue interface
Create constants _TYPECODE for the easy item type having its value as the Code of easy item type
Create constants SIMPLE_CLASSNAME for the easy item type having its value as the Code of easy item type
Create a cache to hold the enumeration values for the keys.
Create constants for the enumeration values defined in easy enumeration type
Create the properties code and codeLowercase of type String
Defined a perameterized constructor with parameter as String to create an instance by the enumeration value code. The constructor must set the code parameter to code property, and it's lowercase value to codeLowerCase property.
Implement the getType() method to return the SIMPLE_CLASSNAME constant value.
Implement the getCode() method to return the code property value.
Define static methos valueOf(String) to convert the value from a String to an enumeration value.
Implement the equals(), hashcode() and toString() methods.
For example: Below is the implementation of the enum class for the easy enumeration type EasyUsageEnum
package com.sap.cx.boosters.easy.extension.easytype.enums
import de.hybris.platform.core.HybrisEnumValue
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap
class EasyUsageEnum implements HybrisEnumValue
{
public final static String _TYPECODE = "EasyUsageEnum"
public final static String SIMPLE_CLASSNAME = "EasyUsageEnum"
private static final ConcurrentMap<String, EasyUsageEnum> cache = new ConcurrentHashMap<String, EasyUsageEnum>()
public static final EasyUsageEnum TEST = valueOf("TEST")
public static final EasyUsageEnum LIVE = valueOf("LIVE")
private final String code
private final String codeLowerCase
private static final long serialVersionUID = 0L
private EasyUsageEnum(final String code)
{
this.code = code.intern();
this.codeLowerCase = this.code.toLowerCase().intern()
}
@Override
String getType() {
return SIMPLE_CLASSNAME
}
@Override
String getCode() {
return this.code
}
static EasyUsageEnum valueOf(final String code)
{
final String key = code.toLowerCase();
EasyUsageEnum result = cache.get(key);
if (result == null)
{
EasyUsageEnum newValue = new EasyUsageEnum(code)
EasyUsageEnum previous = cache.putIfAbsent(key, newValue)
result = previous != null ? previous : newValue
}
return result;
}
@Override
boolean equals(final Object obj)
{
try
{
final HybrisEnumValue enum2 = (HybrisEnumValue) obj
return this.is(enum2) || (enum2 != null && !this.getClass().isEnum() && !enum2.getClass().isEnum() && this.getType().equalsIgnoreCase(enum2.getType()) && this.getCode().equalsIgnoreCase(enum2.getCode()))
}
catch (final ClassCastException e)
{
return false
}
}
@Override
int hashCode()
{
return this.codeLowerCase.hashCode()
}
@Override
String toString()
{
return this.code.toString()
}
}
Defining the model class for an item having an attribute of easy enumeration type
While defining the model class of an easy item type that has an attribute of type easy enumeration type, must ensure to define the gettter and setter of the attribute with return/argument type as then enum class for the easy enumeration type.
For example: The model class definition below defined the getter and setter for the property usage having the return/argument types as EasyUsageEnum
package com.sap.cx.boosters.easy.extension.easytype.model
import de.hybris.bootstrap.annotations.Accessor
import de.hybris.platform.core.model.ItemModel
import de.hybris.platform.servicelayer.model.ItemModelContext
import com.sap.cx.boosters.easy.extension.easytype.enums.EasyUsageEnum
class EasyItemModel extends ItemModel{
public static final String _TYPECODE = "EasyItem"
public static final String CODE = "code"
public static final String NAME = "name"
public static final String DESCRIPTION = "description"
public static final String USAGE = "usage"
EasyItemModel(){
super()
}
EasyItemModel(final ItemModelContext ctx)
{
super(ctx)
}
@Accessor(qualifier = CODE, type = Accessor.Type.GETTER)
String getCode()
{
return getPersistenceContext().getPropertyValue(CODE)
}
@Accessor(qualifier = CODE, type = Accessor.Type.SETTER)
void setCode(final String value)
{
getPersistenceContext().setPropertyValue(CODE, value)
}
@Accessor(qualifier = NAME, type = Accessor.Type.GETTER)
String getName()
{
return getName(null)
}
@Accessor(qualifier = NAME, type = Accessor.Type.GETTER)
String getName(final Locale loc)
{
return getPersistenceContext().getLocalizedValue(NAME, loc)
}
@Accessor(qualifier = NAME, type = Accessor.Type.SETTER)
void setName(final String value)
{
setName(value, null)
}
@Accessor(qualifier = NAME, type = Accessor.Type.SETTER)
void setName(final String value, final Locale loc)
{
getPersistenceContext().setLocalizedValue(NAME, loc, value)
}
@Accessor(qualifier = DESCRIPTION, type = Accessor.Type.GETTER)
String getDescription()
{
return getDescription(null)
}
@Accessor(qualifier = DESCRIPTION, type = Accessor.Type.GETTER)
String getDescription(final Locale loc)
{
return getPersistenceContext().getLocalizedValue(DESCRIPTION, loc)
}
@Accessor(qualifier = DESCRIPTION, type = Accessor.Type.SETTER)
void setDescription(final String value)
{
setDescription(value, null)
}
@Accessor(qualifier = DESCRIPTION, type = Accessor.Type.SETTER)
void setDescription(final String value, final Locale loc)
{
getPersistenceContext().setLocalizedValue(DESCRIPTION, loc, value)
}
@Accessor(qualifier = USAGE, type = Accessor.Type.GETTER)
EasyUsageEnum getUsage()
{
return getPersistenceContext().getPropertyValue(USAGE)
}
@Accessor(qualifier = USAGE, type = Accessor.Type.SETTER)
void setUsage(final EasyUsageEnum value)
{
getPersistenceContext().setPropertyValue(USAGE, value)
}
}
Define the property to map the enum class with an easy enumeration type
if your model classes are not defined in the package com.sap.cx.boosters.easy.extension.easytype, then add the following property to easy.properties. This informs the easy extension framework to load the model classes from the package where you defined the model classes.
easyextension.<easy extension id>.easy.type.base.models.package=<Root package of model/enum classes>
# For example
# easyextension.easytype.easy.type.base.models.package=com.sap.cx.boosters.easy.extension.easytype
To inform the model class of an easy item type to the easy extensibility framework, add the following property to easy.properties
easytype.<Code of the Easy enumeration type>.modelClass = <Fully qualified name of the enum class>
Using easy enumerations in Groovy classes
While using the easy item types in the development of an easy extension the indirect referencing must be used to avoid any issues. Following are some of the examples:
Getting enumeration value to for an easy enumeration type