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
2
3
4
<enumtype code="Gender" autocreate="true" generate="true">
            <value code="MALE"/>
            <value code="FEMALE"/>
</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
2
3
4
5
6
<enumtype code="PhoneContactInfoType" generate="true" autocreate="true" dynamic="true">
            <description>Phone type</description>
            <value code="MOBILE"/>
            <value code="WORK"/>
            <value code="HOME"/>
        </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
2
3
4
5
<maptype code="addressMap"
                 argumenttype="java.lang.String"
                 returntype="Address"
                 autocreate="true"
                 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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>
<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

Karibasappa G C (KB)
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