Extending Data Objects

You can extend the data model and data transfer object for Commerce Services using an AddOn.

Extending the Data Model

Procedure

  1. In the extensionname-items.xml file, replace the extensionname part in the file with the name of the actual extension.

    The sample AddOn extends the CustomerModel class with two properties:

    • nickname
    • workOfficeAddress
  2. Verify that you see the correct results in the *-items.xml file.

    The *-items.xml file looks like the following sample.

    occaddon-items.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <items 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    			xsi:noNamespaceSchemaLocation="items.xsd">
    	<itemtypes>	
    		<typegroup name="Customer">	
    			<itemtype code="Customer" autocreate="false" generate="false">
    				<description>Extending Customer type with additional attributes.</description>
    				<attributes>
    					<attribute autocreate="true" qualifier="nickname" type="java.lang.String">
    						<modifiers read="true" write="true" optional="true" />
    						<persistence type="property" />
    						<description>Customer nickname</description>
    					</attribute>
    		            <attribute autocreate="true" qualifier="workOfficeAddress" type="Address" isSelectionOf="addresses">
                   		   <modifiers read="true" write="true" search="false" optional="true" />
            		       <persistence type="property" qualifier="WorkOfficeAddress"/>
                		</attribute>					
    				</attributes>
    			</itemtype>
    		</typegroup>
    	</itemtypes>
    </items>

Extending the Data Transfer Object

Procedure

  1. In the extensionname-beans.xml file, replace the <extensionname> part with the name of the current extension.

    The sample AddOn extends the CustomerData with two properties:

    • nickname
    • workOfficeAddress
  2. Verify that you see the correct results in the *-beans.xml file.

    occaddon-beans.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:noNamespaceSchemaLocation="beans.xsd">
    	<!-- Additional nickname and workofficeAddress property for CustomerData -->
    	<bean class="de.hybris.platform.commercefacades.user.data.CustomerData" extends="de.hybris.platform.commercefacades.user.data.PrincipalData">
    		<property name="nickname" type="String" />
    		<property name="workOfficeAddress" type="de.hybris.platform.commercefacades.user.data.AddressData"/>	
    	</bean>
    </beans>

Populating Data

Context

The Converter objects in the <commerce>-* extensions are configured in Spring only and have no separate concrete implementation classes. During the conversion process, they use a list of Populator objects. It is possible to add a Populator to an existing Converter without having to redeclare the Converter. Use the modifyPopulatorList method.

This procedure provides the steps to populate new attributes.

Procedure

  1. Define the Populator Class in OccaddonCustomerPopulator.java.
    public class OccaddonCustomerPopulator implements Populator<CustomerModel, CustomerData>
    {
        private Converter<AddressModel, AddressData> addressConverter;
        @Override
        public void populate(final CustomerModel source, final CustomerData target) throws ConversionException
        {
            Assert.notNull(source, "Parameter source cannot be null.");
            Assert.notNull(target, "Parameter target cannot be null.");
            target.setNickname(source.getNickname());
            if (source.getWorkOfficeAddress() != null)
            {
                target.setWorkOfficeAddress(getAddressConverter().convert(source.getWorkOfficeAddress()));
            }
        }
        protected Converter<AddressModel, AddressData> getAddressConverter()
        {
            return addressConverter;
        }
        @Required
        public void setAddressConverter(final Converter<AddressModel, AddressData> addressConverter)
        {
            this.addressConverter = addressConverter;
        }
        }
  2. Add the Populator to the Converter in occaddon-spring.xml.
    ...
    	<alias name="defaultOccaddonCustomerPopulator" alias="occaddonCustomerPopulator"/>
    	<bean id="defaultOccaddonCustomerPopulator" class="de.hybris.platform.occaddon.customer.converters.populator.OccaddonCustomerPopulator">
    		<property name="addressConverter" ref="addressConverter"/>
    	</bean>
    
    	<bean parent="modifyPopulatorList">
        		<property name="list" ref="customerConverter"/>
        		<property name="add" ref="occaddonCustomerPopulator"/>
    	</bean>
    ...

Next Steps

The same actions must be performed to the reverse Converter.

Localizing Attributes

Procedure

Add <key>=<localized string> entries in the following type system localization file: occaddon/resources/localization/occaddon-locales_en.properties.

occaddon-locales_en.properties

type.Customer.nickname.name=Nickname
type.Customer.nickname.description=Nickname description
type.Customer.workOfficeAddress.name=Work office address
type.Customer.workOfficeAddress.description=Work office address

Extending the Data Transfer Object (DTO) for v2

Context

For v2 of Commerce Services, there is an additional DTO layer. It was created to improve the stability and configurability of the response data.

To use the new DTO and configure responses, perform the following steps:

Procedure

  1. Extend the DTO in occaddon-beans.xml.
    ...
        <!-- Additional nickname and workofficeAddress property for UserWsDTO -->
        <bean class="de.hybris.platform.commercewebservicescommons.dto.user.UserWsDTO"
              extends="de.hybris.platform.commercewebservicescommons.dto.user.PrincipalWsDTO">
            <property name="nickname" type="String" />
            <property name="workOfficeAddress" type="de.hybris.platform.commercewebservicescommons.dto.user.AddressWsDTO"/>    
        </bean>
        ...
  2. Add new fields to the predefined field level configurations in occaddon-web-spring.xml.
    Remember that you need to do this in the <addonname>-web-spring.xml file localized in the resource directory, which is added to the Commerce Services context.
    ...
    	<bean parent="fieldSetLevelMapping">
            <property name="dtoClass"
                value="de.hybris.platform.commercewebservicescommons.dto.user.UserWsDTO" />
            <property name="levelMapping">
                <map>
                    <entry key="BASIC" value="nickname" />
                    <entry key="DEFAULT" value="nickname" />
                    <entry key="FULL" value="nickname,workOfficeAddress(FULL)" />
                </map>
            </property>
        </bean>
        ...

    Additional Resources

  3. Populate data between commerce data and web services DTO. Populating data from the commerce layer to web services DTO is done with the help of Orika - a popular Java Bean mapper framework. Fields with the same names are populated automatically by methods from version v2 with the help of the DataMapper object.

    UsersController.java

    ...
        public UserWsDTO getUser(@RequestParam(defaultValue = "BASIC") final String fields)
        {
            final CustomerData customerData = customerFacade.getCurrentCustomer();
            final UserWsDTO dto = dataMapper.map(customerData, UserWsDTO.class, fields);
            return dto;
        }
        ...
    You can also use the DataMapper in the AddOn controllers. The default implementation of DataMapper is defined in the commercewebservicescommons extension. To use it in an AddOn, define the dependency for this extension and add the proper resource in the controller:

    ...Controller.java

    ...
        @Resource(name = "dataMapper")
        protected DataMapper dataMapper;
        ...