Search code examples
attributesdefault-valuesap-commerce-clouditems

How to set Default Value for a Map attribute of an item in Items.xml Hybris?


I need to initialize a maptyped attribute of a item with defaultvalues of map.

lets say that we have defined a maptype

<maptype code="dummyMap" argumenttype="java.lang.String" returntype="java.lang.String" autocreate="true" generate="false" />

and we have declared a itemtype as

<itemtype code="dummyItem" autocreate="true" ...>
  <attributes>
    <attribute qualifier="dummyAttribute" type="dummyMap">
        <defaultvalue>???</defaultvalue>  <<<<<========= How should we initialize ?????
    </attribute>
  </attributes>
</itemtype>

As an example in similar case for an enum type attribute we define the default Value as

<defaultvalue>em().getEnumerationValue("dummyEnum","dummyEnum_Value")</defaultvalue>

How do we apply the same for a Maptyped attribute. Please let me know on how to initiale the attribute with a map value.


Solution

  • With the latset version of Hybris, you can try passing a java.util.Collections.singletonMap e.g.

    <defaultvalue>java.util.Collections.singletonMap("one", java.math.BigDecimal.ONE)</defaultvalue>
    

    When I tested it with Hybris v1811 (as shown below),

    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="items.xsd">
    
        <maptypes>
            <maptype code="DummyMap"
                     argumenttype="java.lang.String"
                     returntype="java.math.BigInteger"
                     autocreate="true"
                     generate="false"/>
        </maptypes>
        <itemtypes>
            <itemtype code="DummyItem" autocreate="true">
                <deployment table="DummyItem" typecode="30001" />
                <attributes>
                    <attribute qualifier="uname" type="java.lang.String">
                        <modifiers read="true" write="true" search="true" initial="true" optional="false"/>
                        <defaultvalue>"Hello"</defaultvalue>
                        <persistence type="property"></persistence>
                    </attribute>
                    <attribute qualifier="dummyAttribute" type="DummyMap">
                        <modifiers read="true" write="true" search="true" initial="true" optional="false"/>
                        <defaultvalue>java.util.Collections.singletonMap("one", java.math.BigDecimal.ONE)</defaultvalue>
                        <persistence type="property"></persistence>
                    </attribute>
                </attributes>
            </itemtype>
        </itemtypes>
    </items>
    

    the XML Representation of DummyItem in the backoffice showed:

    <itemtype code="DummyItem" extends="GenericItem" jaloclass="org.training.jalo.DummyItem" generate="true" singleton="false" jaloonly="false" autocreate="true">
        <deployment table="dummyitem" typecode="30001"/>
        <attributes>
            <attribute generate="true" autocreate="true" qualifier="dummyAttribute" type="DummyMap"><!-- could not export defaultvalue '{one=1}' -->
    
                <persistence type="property" qualifier=""/>
                <modifiers read="true" write="true" search="true" encrypted="false" optional="false" removable="true" initial="true" unique="false" private="false" partof="false"/>
            </attribute>
            <attribute generate="true" autocreate="true" qualifier="uname" type="java.lang.String">
                <defaultvalue>
    new java.lang.String( "Hello" )
                </defaultvalue>
                <persistence type="property" qualifier=""/>
                <modifiers read="true" write="true" search="true" encrypted="false" optional="false" removable="true" initial="true" unique="false" private="false" partof="false"/>
            </attribute>
        </attributes>
    </itemtype>
    

    As you can see, it was able to pass new java.lang.String( "Hello" ) as the default value for the attribute, uname but for the attribute, dummyAttribute, it shows, <!-- could not export defaultvalue '{one=1}' -->.