Extending the Item Synchronization API
The Item Synchronization API provides operations to synchronize items and to retrieve the status of a synchronization for an item and its dependencies. You can extend the Item Synchronization API to return synchronization statuses for dependent items. You can also extend the API by collecting related items other than the default items when performing synchronization actions by augmenting the list of synchronization visitors.
Getting Return Statuses for Dependent Items
You can extend the Item Synchronization API to recursively return the synchronization status for dependent items. To do this, you must do the following:
- Implement your own Item Collector using the de.hybris.platform.cmsfacades.common.itemcollector.ItemCollector interface.
- Modify the current Spring configuration.
Implementing an Item Collector
The Item Collector specifies which dependent items will be returned in the response to the client. For example, in the case of a content page, you can create an Item Collector that only collects restrictions, and not content slots as it is in the default implementation. The following code excerpt shows an example of how to implement an Item Collector that only collects restrictions:
public class AbstractPageItemRestrictionCollector implements ItemCollector<AbstractPageModel>
{
@Override
public List<? extends ItemModel> collect(final AbstractPageModel item)
{
return item.getRestrictions();
}
}
Modifying the Spring Configuration
Once you have created your Item Collector, you must decide if this newly-collected item will be part of the selected dependencies or the shared dependencies. The following table provides the Spring aliases and describes which item collector bean is to be used for selected dependencies and which for shared dependencies:
| Spring Alias | Description |
|---|---|
| basicSynchronizationItemCollectorRegistry | Overrides the items collected for the selected dependencies. |
| sharedSynchronizationItemCollectorRegistry | Overrides the items collected for the shared dependencies. |
In the example above where we created an Item Collector to collect only restrictions, this Item Collector would most obviously be the selected dependencies. You must therefore override the basicSynchronizationItemCollectorRegistry as shown in the following example:
<!-- Item Collector Registries -->
<alias name="customBasicSynchronizationItemCollectorRegistry" alias="basicSynchronizationItemCollectorRegistry"/>
<bean name="customBasicSynchronizationItemCollectorRegistry" class="de.hybris.platform.cmsfacades.common.itemcollector.impl.DefaultItemCollectorRegistry">
<property name="typeService" ref="typeService"/>
<property name="itemCollectorMap">
<map>
<entry value-ref="abstractPageItemRestrictionCollector">
<key>
<util:constant static-field="de.hybris.platform.cms2.model.pages.AbstractPageModel._TYPECODE"/>
</key>
</entry>
</map>
</property>
</bean>
<alias name="customAbstractPageItemRestrictionCollector" alias="abstractPageItemRestrictionCollector"/>
<bean name="customAbstractPageItemRestrictionCollector" class="de.hybris.platform.myextension.synchronization.itemcollector.AbstractPageItemRestrictionCollector"/>
Optionally, you can control the maximum depth of the structure created by the collectors by overriding the current configuration as shown in the following example:
<alias name="customCmsSyncItemStatusConfig" alias="cmsSyncItemStatusConfig" /> <bean id="customCmsSyncItemStatusConfig" class="de.hybris.platform.cmsfacades.data.SyncItemStatusConfig"> <property name="maxDepth" value="2" /> </bean>
Collecting Related Items
You can also extend the Item Synchronization API to collect other related items before getting the synchronization status or performing synchronizations. By other related items, we mean related items that are not automatically collected by the default implementation of the Items Synchronization API, such as restrictions. You can do this by augmenting the list of synchronization visitors.
Implementing the Item Visitor
To implement an item vistor:
-
Create an item visitor for the Item Type that you want to collect for other related items.
If an Item Visitor is already defined the Item Type, you should extend the current Visitor Java class. In the following example, we have redefined the Item Visitor for CMSLinkComponentModel to collect the Items already defined and have extended it to include Category and all Actions:
public class CustomCMSLinkComponentModelVisitor extends CMSLinkComponentModelVisitor { @Override public List<ItemModel> visit(CMSLinkComponentModel source, List<ItemModel> path, Map<String, Object> ctx) { final List<ItemModel> collectedItems = super.visit(source, path, ctx); collectedItems.add(source.getCategory()); collectedItems.addAll(source.getActions()); return collectedItems; } } -
Modify the Spring configuration.
To maintain the CMS Item synchronization behavior, you must augment the existing map that maps Item Types to Item Visitors, and not completely override it, as shown in the following code excerpt:
<bean depends-on="cmsVisitors" parent="mapMergeDirective"> <property name="key"> <util:constant static-field="de.hybris.platform.cms2.model.contents.components.CMSLinkComponentModel._TYPECODE"/> </property> <property name="value" ref="cmsLinkComponentModelVisitor"/> </bean> <alias name="customCMSLinkComponentModelVisitor" alias="cmsLinkComponentModelVisitor"/> <bean name="customCMSLinkComponentModelVisitor" class="de.hybris.platform.myextension.synchronization.itemvisitors.impl.CustomCMSLinkComponentModelVisitor"/>