Defining Enum and Map types in Hybris


Enum Type


Enum types are used to define the set of constant values.

Example:
Set of Color like Red,Blue and Set of status like Created,InProgress,Submitted etc.

Enum for Gender to specify Male or Female

  1. <enumtype code="Gender" autocreate="true" generate="true">
  2.             <value code="MALE"/>
  3.             <value code="FEMALE"/>
  4. </enumtype>
<enumtype code="Gender" autocreate="true" generate="true">
            <value code="MALE"/>
            <value code="FEMALE"/>
</enumtype>


Enum for Phone type to specify its Office or Home or Mobile phone.

  1. <enumtype code="PhoneContactInfoType" generate="true" autocreate="true" dynamic="true">
  2.             <description>Phone type</description>
  3.             <value code="MOBILE"/>
  4.             <value code="WORK"/>
  5.             <value code="HOME"/>
  6.         </enumtype>
<enumtype code="PhoneContactInfoType" generate="true" autocreate="true" dynamic="true">
            <description>Phone type</description>
            <value code="MOBILE"/>
            <value code="WORK"/>
            <value code="HOME"/>
        </enumtype>

All the Enum types are stored in one table.

It will be inside the deployment of EnumerationValue item type.

We need Enum type and Enum class to be generated for the Enum type,hence make both autocreate and generate as true.

Dynamic in Enum

Dynamic in Enum is completely different from Dynamic attributes.

If an Enum type is dynamic then values can be added at the run time.
We can make Enum type as Dynamic by specifying dynamic=true in the Enum type definition.

If an Enum type is non-dynamic (by default, dynamic=”false”) we are not allowed to add new values at runtime.

If we add any non-dynamic Enum type without values,build will fail as it does not have any effect.

So if you want to add new values at run time we have to make dynamic=”true” for an Enum type.

We can change the flag anytime but enforces a system update.

If dynamic=”false” the servicelayer generates real java enums (having a fixed set of values).

If dynamic=”true” it generates hybris enums which can be used without fixed values(means we can add run time values).

Map Type

We know that Map is a collection of Key value pair.

Key is also called argument and value is also called return value.

We can define Map as below

  1. <maptype code="addressMap"
  2.                  argumenttype="java.lang.String"
  3.                  returntype="Address"
  4.                  autocreate="true"
  5.                  generate="false"/>
<maptype code="addressMap"
                 argumenttype="java.lang.String"
                 returntype="Address"
                 autocreate="true"
                 generate="false"/>

we can reference the code defined above as a type to any attribute in the item type definition.

Example:

  1. <itemtype code="CustomUser" autocreate="true" generate="false">
  2.                 <deployment table="CustomUser" typecode="11001" />
  3.                 <attributes>
  4.                     <attribute qualifier="userId" type="java.lang.String">
  5.                         <modifiers optional="false" unique="true"/>
  6.                         <persistence type="property" />
  7.                     </attribute>
  8.                     <attribute qualifier="languages" type="LanguageList">
  9.                         <modifiers unique="false" read="false"/>
  10.                         <persistence type="property" />
  11.                     </attribute>
  12.                     <attribute qualifier="nickNames" type="StringCollection">
  13.                         <modifiers unique="false" read="false"/>
  14.                         <persistence type="property" />
  15.                     </attribute>
  16.                     <attribute qualifier="userAddressMap" type="addressMap">
  17.                         <modifiers unique="false" read="false"/>
  18.                         <persistence type="property" />
  19.                     </attribute>
  20.                    
  21.                 </attributes>
  22.             </itemtype>
<itemtype code="CustomUser" autocreate="true" generate="false">
				<deployment table="CustomUser" typecode="11001" />
				<attributes>
					<attribute qualifier="userId" type="java.lang.String">
						<modifiers optional="false" unique="true"/>
						<persistence type="property" />
					</attribute>
					<attribute qualifier="languages" type="LanguageList">
						<modifiers unique="false" read="false"/>
						<persistence type="property" />
					</attribute>
					<attribute qualifier="nickNames" type="StringCollection">
						<modifiers unique="false" read="false"/>
						<persistence type="property" />
					</attribute>
					<attribute qualifier="userAddressMap" type="addressMap">
						<modifiers unique="false" read="false"/>
						<persistence type="property" />
					</attribute>
					
				</attributes>
			</itemtype>

Now we have defined the Map and injected it into userAddressMap attribute of CustomUser

Do the Build using ant all

Check CustomUserModel.java to have below entry

  1. private Map<String,AddressModel> _userAddressMap;
private Map<String,AddressModel> _userAddressMap;

So we have created a Map of String and Address inside CustomUser item type.

Like collection, we don’t need jalo class to be generated for Map type hence set generate as false.
Set autocreate as true so that item type for Map will be generated.

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