Implement Custom Mapping

Learn how to define your own mapping.

Context

The recommended way to implement your own integration to an external system into the Personalization Module assumes the following steps.

Define Populators

Define the populators for your mappers.

Procedure

  1. There are several possibilities of creating your own mappers.
    • You can implement the Populator interface as presented below:
      public class CxTestSuffixMapper implements Populator<List<String>, MappingData>
      {
       
          @Override
          public void populate(final List<String> data, final ProfileMappingData target) throws ConversionException
          {
              data.stream().map(s -> s + "-test").forEach(s -> {
                  final SegmentMappingData segment = new SegmentMappingData();
                  segment.setCode(s);
                  target.getSegments().add(segment);
              });
          }
      }
    • You can also simply extend one of the generic mappers provided out of the box:
      public class CxTestScriptProfileMapper extends CxScriptProfileMapper<List<String>>
      {
          //empty
      }
  2. Define your populator in the Spring configuration
    <bean id="cxTestFixedSegmentsProfileMapper" class="de.hybris.platform.personalizationintegration.mapping.mapper.impl.CxTestFixedSegmentsProfileMapper" parent="cxFixedSegmentsProfileMapper"/>
    <bean id="cxTestScriptProfileMapper" class="de.hybris.platform.personalizationintegration.mapping.mapper.impl.CxTestScriptProfileMapper" parent="cxScriptProfileMapper">
        <property name="scriptGroup" value="testGroup"/>
    </bean>
    <bean id="cxTestSuffixMapper" class="de.hybris.platform.personalizationintegration.mapping.mapper.impl.CxTestSuffixMapper"/>

Define Converters

Use the populators you have just defined to implement a new converter.

Procedure

  1. Inject the newly created populators in your own converter.
    <bean id="testIntegrationConverter" parent="abstractPopulatingConverter">
        <property name="targetClass" value="de.hybris.platform.personalizationintegration.mapping.ProfileMappingData"/>
        <property name="populators">
            <list>
                <ref bean="cxTestFixedSegmentsProfileMapper"/>
                <ref bean="cxTestScriptProfileMapper"/>
                <ref bean="cxTestSuffixMapper"/>
            </list>
        </property>
    </bean>
    
  2. Inject your new converter into the CxIntegrationMappingService by using the map merge directive.
    <bean depends-on="cxIntegrationConvertersMap" parent="mapMergeDirective">
        <property name="key" value="testConverterName" />
        <property name="value" ref="testIntegrationConverter" />
    </bean>  
  3. Using the map merge directive registers your converter in DefaultCxIntegrationMappingService. In order to convert some external data using your converter through the service you just need to execute the following :
    defaultCxIntegrationMappingService.mapExternalDataToSegments(testValues,"testConverterName");