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
- <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.
- <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
- <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:
- <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
- 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.
How to write impex for Map Type?
Hi KB,
Is it possible to give default map entries in items.xml itself? If so, then how we have to give it ? I mean any specific syntax for that?
Thanks,
AB
Hi Sir,
Why for Enumtypes. autocrete& generate are true ?
Why in Maptypes, generate is false and auto create is true?
Regards,
Viswanth.
autocreate and generate has nothing to do with enumtypes or maptypes. It is just how do we want to keep our item type as.
autocreate = true : this creates a jalo class for the corresponding type
generate = true : generates the item type and creates a database table for it in hybris platform.
which map object it created internally for and why ?
Hi KB,
Dynamic Enums are not supported by smartedit. Need to configure anything??? Could you please assist on this..
HI KB,
Its mentioned that “Like collection, we don’t need jalo class to be generated for Map type hence set generate as false.” But in the previous section its mentioned as
“make autocreate as true so that collection type gets created in the backend.
make generate as false as we don’t need to generate Jalo class for collection type”.
Yes, Both are same, whats the question ?
For collection, auto generate has to be true or false?
Autocreate should be true and generate does’t play a role for collection.
Thanks KB 🙂
Can you please provide the sample table structure when we created through Map Item type.If possible please provide for remaining items too…
For example if we want to display fixed number of constants in our website we will define those values in items.xml file as enum values….how these values will display in our site..?is there any java code will be there…?
Enum values can be accessed using the entity on which it is associated.
Ex: If Order model has enum for order status, We can get it by loading order model and then calling getStatus(getter method).
You can use enum method to get all the constant values.
Thanks Kb sir…i got it..
you are welcome 🙂
Hi Karibasappa ,
What is the difference between Normal java enum and hybris enum
Java enums will have a fixed set of values.
There will be no type and table created if we use Java enum.
hybris enums can be dynamic as well. which helps in adding run time values without any code change.
There will be type and table created for it in Hybris.
Thanks KB
Hi KB,
Could you please explain in an example adding enum values at runtime?
Can we change value of dynamic enum through backoffice? How to change value on runtime?