Merge Simple Providers

Merge Simple Segments Provider and Simple User Segments Provider into a single class.

Context

Both Simple Segments Provider and Simple User Segments Provider operate on customer group segments and they have the same provider ID. We can merge them into a single class which implements both SegmentsProvider and UserSegmentsProvider interfaces.

Procedure

  1. Create the CustomerGroupSegmentsProvider.java class in the personalizationsegmenttrail/src/de/hybris/personalizationsegmenttrail/segment directory. It should implement the SegmentsProvider and UserSegmentsProvider interfaces.
    package de.hybris.personalizationsegmenttrail.segment;
     
    import de.hybris.platform.commercefacades.catalog.PageOption;
    import de.hybris.platform.commercefacades.customergroups.CustomerGroupFacade;
    import de.hybris.platform.commercefacades.user.data.PrincipalData;
    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.SegmentsProvider;
    import de.hybris.platform.personalizationintegration.segment.UserSegmentsProvider;
    import de.hybris.platform.personalizationservices.configuration.CxConfigurationService;
    import de.hybris.platform.personalizationservices.data.BaseSegmentData;
     
    import java.math.BigDecimal;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.stream.Collectors;
     
    import org.springframework.beans.factory.annotation.Required;
     
     
    final public class CustomerGroupSegmentsProvider implements SegmentsProvider, UserSegmentsProvider
    {
       private static final String PROVIDER_ID = "CGSP";
       private CxConfigurationService cxConfigurationService;
       private CustomerGroupFacade customerGroupFacade;
     
       @Override
       public Optional<List<BaseSegmentData>> getSegments()
       {
          final List<BaseSegmentData> segments = getCustomerGroupsUids().stream()
                .map(this::convertStringToBaseSegmentData)
                .collect(Collectors.toList());
     
          return Optional.of(segments);
       }
     
       @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();
       }
     
       @Override
       public String getProviderId()
       {
          return PROVIDER_ID;
       }
     
       private BaseSegmentData convertStringToBaseSegmentData(final String uid)
       {
          final BaseSegmentData segmentData = new BaseSegmentData();
          segmentData.setCode(uid);
          segmentData.setDescription("Customer group: " + uid);
          segmentData.setProvider(PROVIDER_ID);
     
          return segmentData;
       }
     
       private List<String> getCustomerGroupsUids()
       {
          final List<String> uniqueIdentifiers = customerGroupFacade.getAllCustomerGroups(PageOption.createWithoutLimits()) //
                .getUserGroups().stream()
                .map(PrincipalData::getUid)
                .collect(Collectors.toList());
     
          return uniqueIdentifiers;
       }
     
       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);
       }
     
       @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="customerGroupSegmentsProvider"
         class="de.hybris.personalizationsegmenttrail.segment.CustomerGroupSegmentsProvider">
       <property name="cxConfigurationService" ref="cxConfigurationService"/>
       <property name="customerGroupFacade" ref="customerGroupFacade"/>
    </bean>
    ...
  3. Add update actions for your provider.

    To be able configure update actions separately for this provider, you need to extend RecalculationAction enum with values: UPDATE_CGSP and ASYNC_PROCESS_CGSP (where CGSP is your provider identifier returned by getProviderId method).

    We can extend RecalculationAction enum in the personalizationsegmenttrail/resources/personalizationsegmenttrail-beans.xml directory.

    ...
    <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>
    </enum>
    ...
  4. Configure the prefix for your provider.

    There is possibility to automatically add prefix to segment names returned by this provider. It can be done by setting property : personalizationintegration.provider.<PROVIDER_ID>.prefix.

    The CG prefix is added as an example.

    personalizationintegration.provider.CGSP.prefix=CG