Define Complex User Segments Provider

Create a complex user Segments Provider, using the mapper mechanism to convert data to user segments.

Procedure

  1. Create the ComplexUserSegmentProvider.java class in the personalizationsegmenttrail/src/de/hybris/personalizationsegmenttrail/segment directory. Make sure the class implements the UserSegmentsProvider interface. This provider uses CustomerModel as data based on which the mappers assign segments to customer. Usually such data comes from an external system.
    package de.hybris.personalizationsegmenttrail.segment;
     
    import de.hybris.platform.core.model.user.CustomerModel;
    import de.hybris.platform.core.model.user.UserModel;
    import de.hybris.platform.personalizationintegration.mapping.MappingData;
    import de.hybris.platform.personalizationintegration.mapping.SegmentMappingData;
    import de.hybris.platform.personalizationintegration.segment.UserSegmentsProvider;
    import de.hybris.platform.personalizationintegration.service.CxIntegrationMappingService;
     
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
     
    import org.springframework.beans.factory.annotation.Required;
     
     
    public class ComplexUserSegmentProvider implements UserSegmentsProvider
    {
       private static final String PROVIDER_ID = "COMPLEX";
       public static final String CONVERTER_NAME = "CSC";
     
       private CxIntegrationMappingService cxIntegrationMappingService;
     
       @Override
       public List<SegmentMappingData> getUserSegments(final UserModel user)
       {
          if (user instanceof CustomerModel)
          {
             //get segment data (usually from external system)
             final CustomerModel segmentData = getSegmentData(user);
     
             //map data to segment
             final Optional<MappingData> mappingData = cxIntegrationMappingService.mapExternalData(segmentData, CONVERTER_NAME);
     
             //return segment list
             return mappingData.map(MappingData::getSegments).orElseGet(() -> Collections.emptyList());
          }
          return null;
       }
     
       private CustomerModel getSegmentData(final UserModel user)
       {
          return (CustomerModel) user;
       }
     
       @Override
       public String getProviderId()
       {
          return PROVIDER_ID;
       }
     
       protected CxIntegrationMappingService getCxIntegrationMappingService()
       {
          return cxIntegrationMappingService;
       }
     
       @Required
       public void setCxIntegrationMappingService(final CxIntegrationMappingService cxIntegrationMappingService)
       {
          this.cxIntegrationMappingService = cxIntegrationMappingService;
       }
    }
  2. Create Mapper for CITY segments.

    In this example, the segments are assigned to user based on CustomerModel, so you need to create a mapper, which maps the CustomerModel to a desired segment. In this case it is the CITY segment, assigned to the customer based on the address. Create the CitySegmentMapper.java class in the personalizationsegmenttrail/src/de/hybris/personalizationsegmenttrail/segment/mapper directory. Make sure the class implements Populator <CustomerModel, MappingData> inteface.

    package de.hybris.personalizationsegmenttrail.segment.mapper;
     
    import de.hybris.platform.converters.Populator;
    import de.hybris.platform.core.model.user.AddressModel;
    import de.hybris.platform.core.model.user.CustomerModel;
    import de.hybris.platform.personalizationintegration.mapping.MappingData;
    import de.hybris.platform.personalizationintegration.mapping.SegmentMappingData;
    import de.hybris.platform.servicelayer.dto.converter.ConversionException;
     
    import java.math.BigDecimal;
     
     
    public class CitySegmentMapper implements Populator<CustomerModel, MappingData>
    {
     
       @Override
       public void populate(final CustomerModel customer, final MappingData mappingData) throws ConversionException
       {
          if (customer.getAddresses() != null)
          {
             customer.getAddresses().stream()//
                   .map(this::createMapping)//
                   .forEach(mappingData.getSegments()::add);
          }
       }
     
       private SegmentMappingData createMapping(final AddressModel address)
       {
          final SegmentMappingData data = new SegmentMappingData();
          data.setCode("CITY_" + address.getTown());
          data.setAffinity(BigDecimal.ONE);
     
          return data;
       }
    }
  3. Define your provider and mappers in the Spring context.

    Add the beans definition to the personalizationsegmenttrail/resources/personalizationsegmenttrail-spring.xml file.

    The beans definition includes the provider, converter, and the following mappers:
    • city mapper: a class for this mapper was defined in previous step it simply map customer address to CITY segment.
    • fixed mapper: one of the mappers provided out of the box. It returns segment names defined in the personalizationintegration.mappers.cxfixedsegmentsprofilemapper.segments property.
    • script mapper: one of the mappers provided out of the box. It uses scripts to interpret the segment data.

    More information about standard mappers: Implement Custom Mapping.

    ...
    <bean id="complexUserSegmentProvider" class="de.hybris.personalizationsegmenttrail.segment.ComplexUserSegmentProvider">
        <property name="cxIntegrationMappingService" ref="cxIntegrationMappingService"/>
    </bean>
     
    <!-- Converter gathering segment data mappers -->
    <bean id="customerSegmentConverter" parent="faultTolerantConverter">
        <property name="targetClass" value="de.hybris.platform.personalizationintegration.mapping.MappingData"/>
        <property name="populators">
            <list>
                <ref bean="fixedSegmentMapper"/>
                <ref bean="scriptSegmentMapper"/>
                <ref bean="citySegmentMapper" />
            </list>
        </property>
    </bean>
     
    <!-- Adding converter to map used by CxIntegrationMappingService -->
    <!-- Pay attention that key is the same value which we use in ComplexUserSegmentProvider when calling
    cxIntegrationMappingService.mapExternalData -->
     
    <bean depends-on="cxIntegrationConvertersMap" parent="mapMergeDirective">
        <property name="key" value="CSC" />
        <property name="value" ref="customerSegmentConverter" />
    </bean>
     
    <!-- Mappers -->
    <bean id="fixedSegmentMapper" parent="cxFixedSegmentsProfileMapper"/>
    <bean id="scriptSegmentMapper" parent="cxScriptProfileMapper">
        <property name="scriptGroup" value="complexProviderGroup"/>
    </bean>
    <bean id="citySegmentMapper" class="de.hybris.personalizationsegmenttrail.segment.mapper.CitySegmentMapper"/>
    ...
  4. Add update actions for your provider.

    To configure update actions separately for this provider, extend RecalculationAction enum in personalizationsegmenttrail/resources/personalizationsegmenttrail-beans.xml with the follwing values: UPDATE_COMPLEX and ASYNC_PROCESS_COMPLEX (where COMPLEX is the provider identifier returned by the getProviderId method).

    You can extend RecalculationAction enum in the personalizationsegmenttrail/resources/personalizationsegmenttrail-beans.xml file:

    ...
    <enum class="de.hybris.platform.personalizationservices.RecalculateAction">
        <value>UPDATE_SIMPLE</value>
        <value>ASYNC_PROCESS_SIMPLE</value>
        <value>UPDATE_CGSP</value>
        <value>ASYNC_PROCESS_CGSP</value>
        <value>UPDATE_COMPLEX</value>
        <value>ASYNC_PROCESS_COMPLEX</value>
    </enum>
    ...
  5. Configure prefix for your provider.

    To automatically add prefix to segment names returned by this provider, set the personalizationintegration.provider.<PROVIDER_ID>.prefix property for the system. The C prefix is added as an example.

    personalizationintegration.provider.COMPLEX.prefix=C