Day 5: Extending Itemtypes
I get confused easily. So whenever somebody asks me whether to add additional attributes directly to OOTB itemtype or create a subtype and add them, I think a lot.
I know this is not an acceptable excuse for someone with my years of experience. :( So putting this down so that I never get confused with these scenarios.
Creating a Subtype and Adding the Attributes to the Subtype
This is the recommended method as it avoids direct dependency with the OOTB itemtypes.
Let’s define a new itemtype extending an OOTB Product.
One important point to note is autocreate should be true. Otherwise it will cause build failures. The first definition of a type should always have this value as true.
Generate can be true or false. But I haven’t encountered any scenario where I didn’t need the getters and setters to avoid the business logic implementation.
<itemtype code="MyProduct" autocreate="true" generate="true" extends="Product" jaloclass="org.training.jalo.MyProduct">
<attributes>
<attribute qualifier="oldPrice" type="java.lang.Double" generate="true">
<persistence type="property"/>
<modifiers read="true" write="true" optional="true"/>
</attribute>
</attributes>
</itemtype>
Adding Attributes to a Type Directly
Not the recommended approach because it creates direct dependency with the OOTB type.
In this case, autocreate element need to be false. Setting the autocreate modifier to true causes a build failure.
<itemtype code="Product" autocreate="false" generate="false">
<attributes>
<attribute qualifier="oldPrice" type="java.lang.Double" generate="true">
<persistence type="property"/>
<modifiers read="true" write="true" optional="true"/>
</attribute>
</attributes>
</itemtype>
Hope now I will be able to recollect it faster :)
Reference :