Sample Publication Grouping Handler

Assume that there is a canonical item with an attribute containing a collection of values. When that item is published, you want to turn it into a collection of target items with a single value in place of the collection attribute. Here is how it is done.

To reach the goal, write an implementation of the PublicationGroupingHandler:

src/main/java/com/mycorp/datahub/grouping/CollectionSplitterPublicationHandler.java

public class CollectionSplitterPublicationHandler extends ConfigurablePublicationGroupingHandler
{
   private String targetTypeCode;
   private String canonicalAttributeName;

   /**
    * Instantiates this handler.
    *
    * @param targetType type of the target item, for which this handler should apply.
    * @param canonicalAttr name of the canonical item collection attribute, based on which the item will
    * be split into multiple items.
    */
   public CollectionSplitterPublicationHandler(final Class<? extends TargetItem> targetType, final String canonicalAttr)
   {
      targetTypeCode = targetType != null ? targetType.getSimpleName() : "";
      canonicalAttributeName = canonicalAttr != null && !canonicalAttr.trim().isEmpty()
            ? canonicalAttr : "<null>";
   }

   @Override
   public <T extends CanonicalItem> List<T> group(final T item, final TargetItemCreationContext context)
   {
      final Collection<?> splitValues = (Collection<?>) item.getField(canonicalAttributeName);
      return containsMultipleValues(splitValues)
            ? multiplyForValues(item, splitValues) : groupOriginalItem(item);
   }

   private boolean containsMultipleValues(final Collection<?> splitValues)
   {
      return splitValues != null && splitValues.size() > 1;
   }

   private <T extends CanonicalItem> List<T> multiplyForValues(final T item, final Collection<?> splitValues)
   {
      final List<T> group = new ArrayList<>(splitValues.size());
      for (final Object splitValue : splitValues)
      {
         group.add(copy(item, splitValue));
      }
      return group;
   }

   private <T extends CanonicalItem> T copy(final T item, final Object collValue)
   {
      try
      {
         final T copy = (T) item.clone();
		 // cloning is the recommended best practice
         copy.setField(canonicalAttributeName, Arrays.asList(collValue));
         return copy;
      }
      catch (final CloneNotSupportedException e)
      {
         // this is dead code because clone is supported by CanonicalItem
         throw new UnsupportedOperationException(e);
      }
   }

   private <T extends CanonicalItem> List<T> groupOriginalItem(final T item)
   {
      return Arrays.asList(item);
   }

   @Override
   public boolean isApplicable(final CanonicalItem item, final TargetItemCreationContext context)
   {
      final String ctxTypeCode = context != null ? context.getTargetItemTypeCode() : null;
      return targetTypeCode.equals(ctxTypeCode);
   }
}

In this implementation, the applicable target item type and the collection attribute name are configurable through constructor injection. By extending the com.hybris.datahub.grouping.impl.ConfigurablePublicationGroupingHandler, you can rely on the super class implementation of the getOrder() method.

Configuring the Publication Grouping Handler

To make the handler implementation eligible for publication, configure it in your extension's Spring configuration file:

src/main/resources/datahub-extension-mycorp-spring.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
   ...
   <bean name="publicationItemSplitter" class="com.mycorp.datahub.grouping.CollectionSplitterPublicationHandler">
      <constructor-arg value="com.mycorp.datahub.domain.TargetProduct" />
      <constructor-arg value="categories" />
      <property name="order" value="5" />
   </bean>
   ...
</beans>