items.xml in SAP Commerce [Hybris]

The items.xml file specifies types of an extension for defining the data model. By editing the items.xml file, you can define new types or extend existing types. In addition, you can define, override, and extend attributes of the types.

Location of the items.xml file:

The items.xml is located in the resources directory of an extension. The items.xml files are prefixed with the name of their respective extension in the form of extension name-items.xml. For example:

  • For the core extension, the file will be core-items.xml.
  • For the catalog extension, the file will be catalog-items.xml.


Basic structure of the items.xml file:

The items.xml defines the types for an extension in XML format and the basic structure is as follows:

<items   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

              xsi:noNamespaceSchemaLocation="items.xsd"> 

              <atomictypes/>

              <collectiontypes/> 

              <enumtypes/> 

              <maptypes/> 

              <relations/> 

              <itemtypes/>

</items>


  • items.xml file is validated against an XSD file (items.xsd), so the order of type definitions must conform to this order. If order that doesn’t conform to the items.xsd causes SAP Commerce to fail the extension build.
  • We must define types in order of inheritance i.e. more abstract types need to be defined at the beginning of the items.xml file and more concrete types need to be defined more to the end.

Defining Item types in Hybris:     
        To create an item type, define the type and its attributes in items.xml file.
We can define an item type in 3 ways in Hybris. We can decide any one of the approach based on the requirement as shown below:
  1. Define a new Item type without extending any existing Item type.
  2. Define a new Item type by extending existing Item type.
  3. Define an existing Item type again with new attributes.

1)Define a new Item type without extending any existing Item type:
    In this approach, we define a new item type without extending any existing item type in items.xml file as shown below:
 

<itemtype code="JobMonitoringLogs"

          jaloclass="com.hybris.training.core.jalo.JobMonitoringLogs"

          autocreate="true"

          generate="true">

    <deployment table="JobMonitoringLogs" typecode="11000"/>

    <attributes>

        <attribute qualifier="code" type="java.lang.String" autocreate="true" generate="true">

            <persistence type="property"/>

            <modifiers optional="false" unique="true"/>

        </attribute>

        <attribute qualifier="name" type="java.lang.String" autocreate="true" generate="true">

            <persistence type="property"/>

            <modifiers/>

        </attribute>

                             <attribute qualifier="lastExecutionTime" type="java.util.Date" autocreate="true" generate="true">

            <persistence type="property"/>

            <modifiers/>

        </attribute>

    <attributes>     

</itemtype>


Here autocreate = true indicates that, in hybris db a new database entry will be created during system initialization or update process.
generate = true means, it generates jalo and model classes for the new item type. 
We must set autocreate=true and generate=true for the first time of the item type definition, else it will throw the build error.

 
2)Define a new Item type by extending existing Item type:

     In this approach, we define a new item type by extending any existing item type in items.xml file as shown below:

<itemtype code="ApparelProduct" extends="Product"

          autocreate="true" generate="true"

          jaloclass="com.hybris.training.core.jalo.ApparelProduct">

    <description>Base apparel product extension that contains additional attributes.</description>

    <attributes>

        <attribute qualifier="genders" type="GenderList">

            <description>List of genders that the ApparelProduct is designed for</description>

            <modifiers/>

            <persistence type="property"/>

        </attribute>

    </attributes>

</itemtype>



3)Define an existing Item type again with new attributes:

    In this approach, we define an item type again in items.xml file by adding new attributes as shown below:

<itemtype code="Product"

          autocreate="false" generate="false"

          jaloclass="com.hybris.training.core.jalo.Product">

    <attributes>

        <attribute qualifier="europe1Taxes" type="PriceRowCollectionTupe">

            <description>Eeurope1 Taxes </description>

            <modifiers/>

            <persistence type="property"/>

        </attribute>

    </attributes>

</itemtype>


Since it is not a new item type and we define again with new attributes, we must set autocreate=false and generate=false in the items.xml file. It means we have added new attributes directly to the existing item type.


If you have any questions or need further clarification, feel free to reach out to me at rameshvanka8@gmail.com. I’m happy to help and would love to hear your feedback!  

Happy Learning,

Ramesh



Comments

Popular posts from this blog

Type System in SAP Commerce [Hybris]

Differences between Extension and AddOn in SAP Hybris