How to use Enum values in Hybris ?

Requirement :
Need to get enum values from DB and we need to save enum values to DB

Let us see how to do it


For basic understanding of enum, please read Defining-enum types article

Saving Enum value by associating it to required object


Example :

OrderStatus enum defined in Hybris OOTB with values like CREATED,COMPLETED, CANCELLED etc

Now we need to set this enum value to OrderModel, we can do it as below

  1. final OrderModel order = new OrderModel();//You may get order from some method
  2. order.setStatus(OrderStatus.CREATED);//You can set different value conditionally based on requirement
  3.  
  4. modelService.save(order);
final OrderModel order = new OrderModel();//You may get order from some method
order.setStatus(OrderStatus.CREATED);//You can set different value conditionally based on requirement

modelService.save(order);


This will persist the Order with Enum value as “CREATED”


Retrieving enum value associated with specific object and comparing it


Example

We will retrieve OrderStatus enum associated with OrderModel and use it

  1. //Load OrderModel from DB and assume its assigned to order object
  2. OrderModel order = getOrder(100001); //make DB call
  3.  
  4. return OrderStatus.WAIT_FRAUD_MANUAL_CHECK.equals(order.getStatus());
//Load OrderModel from DB and assume its assigned to order object
OrderModel order = getOrder(“100001”); //make DB call

return OrderStatus.WAIT_FRAUD_MANUAL_CHECK.equals(order.getStatus());


In this line, we are retrieving order status and comparing it with order status enum value as per our business logic.

This line returns true if order needs to have Fraud Check and false otherwise.


Send enum value to Frontend for display

We need to write below code in populator in most of the cases

In that case, set status to DTO object and pass it to controller and Frontend

  1. //Load OrderModel from DB and assume its assigned to order object
  2. OrderModel order = getOrder(100001); //make DB call
  3.  
  4. Model.addAttribute(“orderCurrentStatus”,order.getStatus().getCode());
//Load OrderModel from DB and assume its assigned to order object
OrderModel order = getOrder(“100001”); //make DB call

Model.addAttribute(“orderCurrentStatus”,order.getStatus().getCode());


In Jsp/tag file, we can access it as below

  1. ${orderCurrentStatus}
${orderCurrentStatus}


Get all the enum values of a specific enum

Sometime it is required to get all the values of a specific enum

We can get that by using EnumerationService as below

  1. //Inject EnumerationService wherever you are using it
  2. List<OrderStatus> orderStatusList = enumerationService.getEnumerationValues(OrderStatus.class);
//Inject EnumerationService wherever you are using it
List<OrderStatus> orderStatusList = enumerationService.getEnumerationValues(OrderStatus.class);


About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on