Anatomy of an Extension

Extensions are required to customize Data Hub. Let's review the structure and content of a extension.xml file, a Java source file, and a Spring file.

A Sample Extension XML File

The extension XML file is used to define the data structure. As mentioned previously, Data Hub uses three main data structures: raw, canonical, and target. Each one has its own XML file. Following is an abbreviated canonical extension XML file. The key tags in the canonical item XML file are:

  • The extension tag is the root element for the extension definition. The attribute name is mandatory, and must contain your extension name. Other extensions refer to your extension by this name, so be thoughtful about the name you choose. The name should be descriptive enough that other developers, teams, or companies know what business domain your extension addresses. It should also be unique in a way that other teams or companies do not pick an identical name for their extension. If the version attribute is used, it should help you identify the version of the extension you are deploying.
  • The dependencies tag declares any possible dependencies your extension may have on other extensions. If your extension is dependent on another extension, load the other extension first. If your extension has no dependencies on other extensions, omit this element in the extension.xml file.
  • The canonicalItems tag declares canonical item types used by your extension. Each type is represented by the item element and, besides the type name, contains definitions of the item attributes. It also defines how those attributes can be populated from the declared raw items and their attributes.
<extension xmlns="http://www.hybris.com/schema/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hybris.com/schema/ datahub-metadata-schema-1.0.0.xsd" name="customer-b2b-datahub-extension">
 
    <dependencies>
        <dependency>
            <extension>name_of_an_extension_this_extension_is_dependent_on</extension>
        </dependency>
        <dependency>
            <extension>name_of_another_extension_this_extension_is_dependent_on</extension>
        </dependency>
    </dependencies>

	<canonicalItems>
        <item>
            <type>SequenceCanonicalItem</type>
            <attributes>
                <attribute>
                    <name>identifier</name>
                    <model>
                        <localizable>false</localizable>
                        <collection>false</collection>
                        <type>String</type>
                        <primaryKey>true</primaryKey>
                    </model>
                    <transformations>
                        <transformation>
                            <rawSource>SequenceRawItem</rawSource>
                            <expression>sourceId</expression>
                        </transformation>
                    </transformations>
                </attribute>
            </attributes>
            </type>
        </item>
	</canonicalItems>

</extension>

Java Source Code in an Extension

While Java source is optional in an extension, it is common. You can do some data manipulation within the XML file, but for intricate work or faster bulk work, java source code is more powerful.

// The following java code is for a composition handler
package com.hybris.datahub.composition.impl;
import com.hybris.datahub.composition.CompositionRuleHandler;
import com.hybris.datahub.domain.AddressType;
import com.hybris.datahub.domain.CanonicalAttributeDefinition;
import com.hybris.datahub.model.CanonicalItem;
import com.hybris.datahub.model.CompositionGroup;
import com.hybris.datahub.model.RawItem;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

class CustomerAddressRelCompositionHandler extends AbstractCompositionRuleHandler implements CompositionRuleHandler
{
    private static final String CANONICAL_TYPE = "CanonicalCustomerAddressRel";
    private static final String CANONICAL_ADDRESS_TYPE = "addressType";
    private static final String CANONICAL_ADDRESS_ID = "addressId";
    private static final String RAW_IDENTIFIER = "sourceId";
    private static final String RAW_BILL_TO = "billTo";
    private static final String RAW_SHIP_TO = "shipTo";
    @Override
    public <T extends CanonicalItem> T compose(final CanonicalAttributeDefinition attribute,
            final CompositionGroup<? extends RawItem> compositionGroup, final T canonicalItem)
    {
        assert compositionGroup != null;
        assert canonicalItem != null;
        assert compositionGroup.getItems() != null;
        // these fields are used to create the combined canonical integration key
        final String customerId = findFirstPopulatedValue(compositionGroup.getItems(), RAW_IDENTIFIER);
        String addressId;
        addressId = findFirstPopulatedValue(compositionGroup.getItems(), RAW_BILL_TO);
        if (addressId != null)
        {
            // this is a bill to address
            canonicalItem.setField(CANONICAL_ADDRESS_ID, addressId);
            canonicalItem.setField(CANONICAL_ADDRESS_TYPE, AddressType.BILL_TO.toString());
        }
        else
        {
            addressId = findFirstPopulatedValue(compositionGroup.getItems(), RAW_SHIP_TO);
            if (addressId != null)
            {
                // this is a ship to address
                canonicalItem.setField(CANONICAL_ADDRESS_ID, addressId);
                canonicalItem.setField(CANONICAL_ADDRESS_TYPE, AddressType.SHIP_TO.toString());
            }
        }
        if (addressId == null)
        {
            // this is a default address
            addressId = customerId;
            canonicalItem.setField(CANONICAL_ADDRESS_ID, addressId);
            canonicalItem.setField(CANONICAL_ADDRESS_TYPE, AddressType.DEFAULT.toString());
        }
        return canonicalItem;
    }
    private String findFirstPopulatedValue(final List<? extends RawItem> rawItemList, final String attributeName)
    {
        for (final RawItem customer : rawItemList)
        {
            final String value = (String) customer.getField(attributeName);
            if (StringUtils.isNotBlank(value))
            {
                return value;
            }
        }
        return null;
    }
    @Override
    public boolean isApplicable(final CanonicalAttributeDefinition attribute)
    {
        return attribute.getCanonicalAttributeModelDefinition().getAttributeName().equals(CANONICAL_ADDRESS_TYPE) && attribute
                .getCanonicalAttributeModelDefinition().getCanonicalItemMetadata().getItemType().equals(CANONICAL_TYPE);
    }
}

Spring XML in an Extension

In the code snippet below, the order property of the handler, CustomerAddressRelCompositionHandler, defined in the Java source described previously, is set to 10 upon instantiation of the CustomerAddressRelCompositionHandler class. The order property controls the execution order of the composition handler relative to other composition handlers.

<bean parent="abstractCompositionRuleHandler" class="com.hybris.datahub.composition.impl.CustomerAddressRelCompositionHandler" id="customerAddressRelCompositionRuleHandler">
    <property value="10" name="order"/>
</bean>