[SCC Practice 2] CMS Component Visitor
 
  
                
The Visitor helps us to catch the change value on the reference attributes of components.
NOTE: It won’t work for partof=”true” attributes.
Steps:
1, Create a visitor java file.
2, Register the visitor bean into context.
3, Map the cms visitor with the Component model
Let’s go.
1, Create a visitor java file.
OOTB also has some existing visitors. Example: CMSNavigationNodeModelVisitor, ContentPageModelVisitor…
As usual, the visitor is putting in the facade extension.
Define class visitor:
- Implement ItemVisitor interface and override the visit() method.
public class YourCMSComponentModelVisitor implements ItemVisitor<YourCMSComponentModel>
{
	@Override
	public List<ItemModel> visit(YourCMSComponentModel source, List<ItemModel> path, Map<String, Object> ctx)
	{
		final List<ItemModel> collectedItems = newLinkedList();
		
		collectedItems.addAll(source.getEntries()); // input the attribute of component model you want to tracking.
		
		return collectedItems;
	}
}
- Extends AbstractCMSComponentModelVisitor abstract class and override the visit() method.
public class YourCMSComponentModelVisitor extends AbstractCMSComponentModelVisitor<YourCMSComponentModel> {
    @Override
    public List<ItemModel> visit(YourCMSComponentModel source, List<ItemModel> path, Map<String, Object> ctx) {
        final List<ItemModel> collectedItems = super.visit(source, path, ctx);
        collectedItems.addAll(source.getEntries()); // input the attribute of component model you want to tracking.
        return collectedItems;
    }
}
2, Register the visitor bean into context.
Under resources/config directory, create an XML file name yourfacades-synchronization-visitors-spring.xml
If the config folder doesn’t exist, please create a new folder name config. Then add the following definition:
For visitor class Implement ItemVisitor interface:
<alias name="defaultYourCMSComponentModelVisitor" alias="yourCMSComponentModelVisitor"/>
<bean name="defaultYourCMSComponentModelVisitor" class="de.hybris.platform.cmsfacades.synchronization.itemvisitors.impl.YourCMSComponentModelVisitor"/>
For visitor class Extends AbstractCMSComponentModelVisitor abstract class:
 	<alias name="defaultYourCMSComponentModelVisitor" alias="yourCMSComponentModelVisitor"/>
	<bean name="defaultYourCMSComponentModelVisitor" class="de.hybris.platform.cmsfacades.synchronization.itemvisitors.impl.YourCMSComponentModelVisitor" parent="abstractCMSComponentModelVisitor"/>
3, Map the cms visitor with the Component model
<bean depends-on="cmsVisitors" parent="mapMergeDirective">
		<property name="key">
			<util:constant static-field="de.hybris.platform.cms2.model.navigation.YourCMSComponentModel._TYPECODE"/>
		</property>
		<property name="value" ref="yourCMSComponentModelVisitor"/>
</bean>
The total XML file will be like this:
<?xml version="1.0" encoding="UTF-8"?>
<!--
 Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:util="http://www.springframework.org/schema/util"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<!--cms component visitors-->
	<alias name="defaultYourCMSComponentModelVisitor" alias="yourCMSComponentModelVisitor"/>
	<bean name="defaultYourCMSComponentModelVisitor" class="de.hybris.platform.cmsfacades.synchronization.itemvisitors.impl.YourCMSComponentModelVisitor" parent="abstractCMSComponentModelVisitor"/>
	<bean depends-on="cmsVisitors" parent="mapMergeDirective">
		<property name="key">
			<util:constant static-field="de.hybris.platform.cms2.model.navigation.YourCMSComponentModel._TYPECODE" />
		</property>
		<property name="value" ref="yourCMSComponentModelVisitor" />
	</bean>
</beans>
Include the xml file into the yourfacades-spring.xml file. In the the yourfacades-spring.xml file add the import resource file:
<import resource="config/yourfacades-synchronization-visitors-spring.xml"/>
Do ant all then start the server to verify the result.
Happy Codding.!!! <3
