Define Simple User Segments Provider

Create a simple User Segments Provider to assign segments to a customer based on the customer groups.

Procedure

  1. Create the SimpleUserSegmentProvider.java class in the personalizationsegmenttrail/src/de/hybris/personalizationsegmenttrail/segment directory. Make sure the class implements the UserSegmentsProvider interface. For details on method implementation, see Define Segments Provider.
    package de.hybris.personalizationsegmenttrail.segment;
     
    import de.hybris.platform.commercefacades.customergroups.CustomerGroupFacade;
    import de.hybris.platform.commercefacades.user.data.UserGroupData;
    import de.hybris.platform.core.model.user.CustomerModel;
    import de.hybris.platform.core.model.user.UserModel;
    import de.hybris.platform.personalizationintegration.mapping.SegmentMappingData;
    import de.hybris.platform.personalizationintegration.segment.UserSegmentsProvider;
    import de.hybris.platform.personalizationservices.configuration.CxConfigurationService;
     
    import java.math.BigDecimal;
    import java.util.Collections;
    import java.util.List;
    import java.util.stream.Collectors;
     
    import org.springframework.beans.factory.annotation.Required;
     
     
    final public class SimpleUserSegmentProvider implements UserSegmentsProvider
    {
       private static final String PROVIDER_ID = "SIMPLE";
       private CxConfigurationService cxConfigurationService;
       private CustomerGroupFacade customerGroupFacade;
     
       @Override
       public List<SegmentMappingData> getUserSegments(final UserModel user)
       {
          if (user instanceof CustomerModel)
          {
             final List<UserGroupData> customerGroups = customerGroupFacade.getCustomerGroupsForUser(user.getUid());
     
             final List<SegmentMappingData> userSegments = customerGroups.stream()
                   .map(this::getSegmentMapping)
                   .collect(Collectors.toList());
     
             return userSegments;
          }
     
          return Collections.emptyList();
       }
     
       private SegmentMappingData getSegmentMapping(final UserGroupData userGroupData)
       {
          final SegmentMappingData data = new SegmentMappingData();
          data.setCode(userGroupData.getUid());
          data.setAffinity(getAffinity());
     
          return data;
       }
     
       private BigDecimal getAffinity()
       {
          return cxConfigurationService.getMinAffinity().add(BigDecimal.ONE);
       }
     
       @Override
       public String getProviderId()
       {
          return PROVIDER_ID;
       }
     
       @Required
       public void setCxConfigurationService(final CxConfigurationService cxConfigurationService)
       {
          this.cxConfigurationService = cxConfigurationService;
       }
     
       @Required
       public void setCustomerGroupFacade(final CustomerGroupFacade customerGroupFacade)
       {
          this.customerGroupFacade = customerGroupFacade;
       }
    }
  2. Define your provider in the Spring context. Add the bean definition to the personalizationsegmenttrail/resources/personalizationsegmenttrail-spring.xml file.
    ...
    <bean id="simpleUserSegmentProvider"
             class="de.hybris.personalizationsegmenttrail.segment.SimpleUserSegmentProvider">
       <property name="cxConfigurationService" ref="cxConfigurationService"/>
       <property name="customerGroupFacade" ref="customerGroupFacade"/>
    </bean>
    ...
  3. 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_SIMPLE and ASYNC_PROCESS_SIMPLE (where SIMPLE 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>
    </enum>
    ...
  4. In case you haven't done so in the Define Simple Segments Provider scenario, set the personalizationintegration.provider.<PROVIDER_ID>.prefix property for the system to automatically add prefix to segment names returned by this provider. The S prefix is added as an example.
    personalizationintegration.provider.SIMPLE.prefix=S