Extending the CMS Structure API

The CMS Structure API provides information about CMS component types and their attributes. You can extend the CMS Structure API by exposing additional types and attributes.

To extend the CMS Structure API, you require the following:

  • Knowledge of how to extend generated beans. This document doesn’t describe this; for information, see the Generating Beans and Enums document.
  • Component Type Structure
  • Component Type Attribute Structure
  • Component Type Structure Registry

Component Type Structure

A ComponentTypeStructure is a POJO bean representation of a CMS component type's structure, as shown in the following example:

/**
 * Represents meta-information about a <code>ComposedTypeModel</code> and the populators required to convert this
 * information to a <code>ComponentTypeData</code>.
 */
public interface ComponentTypeStructure
{
	/**
	 * Get the typecode identifying the <code>ComposedTypeModel</code>.
	 *
	 * @return the typecode
	 */
	String getTypecode();


	/**
	 * Sets the typecode identifying the <code>ComposedTypeModel</code>.
	 *
	 * @param typecode
	 *           - the typecode
	 */
	void setTypecode(String typecode);

	/**	
	 * Get the category in which this type belongs to.
	 * By default and for backwards compatibility, all categories are set to COMPONENT enum type.
	 * @return the category
	 */
	StructureTypeCategory getCategory();

	/**
	 * Get the attributes that should be considered by the ComponentTypeStructure.
	 *
	 * @return the attributes or an empty set
	 */
	Set<ComponentTypeAttributeStructure> getAttributes();

	/**
	 * Get the populators to be used when converting the representing <code>ComposedTypeModel</code> to a
	 * <code>ComponentTypeData</code>.
	 *
	 * @return the populators
	 */
	Set<Populator<ComposedTypeModel, ComponentTypeData>> getPopulators();

	/**
	 * Sets the populators to be used when converting the representing <code>ComposedTypeModel</code> to a
	 * <code>ComponentTypeData</code>.
	 *
	 * @param populators
	 *           - the populators
	 */
	void setPopulators(Set<Populator<ComposedTypeModel, ComponentTypeData>> populators);

}

Component Type Attribute Structure

A ComponentTypeAttributeStructure is a POJO bean representation of a CMS component type attribute's structure. It’s shown in the following example:

/**
 * Represents meta-information about an <code>AttributeDescriptorModel</code> and the populators required to convert
 * this information to a <code>ComponentTypeAttributeData</code>.
 */
public interface ComponentTypeAttributeStructure
{
	/**
	 * Get the qualifier identifying the <code>AttributeDescriptorModel</code>.
	 *
	 * @return the qualifier
	 */
	String getQualifier();

	/**
	 * Sets the qualifier identifying the <code>AttributeDescriptorModel</code>.
	 *
	 * @param qualifier
	 *           - the qualifier
	 */
	void setQualifier(String qualifier);

	/**
	 * Get the typecode identifying the <code>ComposedTypeModel</code> that this attribute belongs to.
	 *
	 * @return the typecode
	 */
	String getTypecode();

	/**
	 * Sets the typecode identifying the <code>ComposedTypeModel</code> that this attribute belongs to.
	 *
	 * @param typecode
	 *           - the typecode
	 */
	void setTypecode(String typecode);

	/**
	 * Get the populators to be used when converting the representing <code>AttributeDescriptorModel</code> to a
	 * <code>ComponentTypeAttributeData</code>.
	 *
	 * @return the populators
	 */
	Set<Populator<AttributeDescriptorModel, ComponentTypeAttributeData>> getPopulators();

	/**
	 * Sets the populators to be used when converting the representing <code>AttributeDescriptorModel</code> to a
	 * <code>ComponentTypeAttributeData</code>.
	 *
	 * @param populators
	 *           - the populators
	 */
	void setPopulators(Set<Populator<AttributeDescriptorModel, ComponentTypeAttributeData>> populators);


}

Service Interfaces Exposed by the CMS Structure API

The following diagram shows the interfaces exposed by the CMS Structure API and how they’re used to leverage the custom types defined in the registries. The GenericComponentTypeService depends on the RegistryComponentTypeStructureService. Both services implement the same interface, the ComponentTypeStructureService. The services do the following:

  • GenericComponentTypeStructureService: Gets the structure types from all CMS item models. It automatically introspects the type and its associated attributes from the type's definition in the items.xml file.
  • RegistryComponentTypeStructureService: Gets all types and attributes that must be modified or added to the generated list of type structures.

This dependency makes it possible for the CMS Structure API to decorate the types that are built with the types that are defined in the ComponentTypeStructureRegistry. The types that are defined in the ComponentTypeStructureRegistry always take precedence over the types collected through introspection.

Supporting New CMS Structure Types

If you need to support additional CMS structure types, you must create a new CmsStructureTypeComponentTypeAttributePopulator bean and specify the type. The following example shows how to create a new CMS structure type called Number:

<bean id="numberComponentTypeAttributePopulator" class="de.hybris.platform.cmswebservices.types.facade.populator.CmsStructureTypeComponentTypeAttributePopulator">
	<property name="cmsStructureType" value="Number" />
</bean>

Exposing New Component Types

If you want to expose additional component types, you must register a new ComponentTypeStructure bean for the new component type. You must also register a new ComponentTypeAttributeStructure bean for each attribute you want to expose.

Code Examples of How to Expose New Component Types

The following code examples show how to expose the CMS Image Component in the CMS Structure API.

  • The following code excerpt shows how the CMSImageComponent is declared in the items.xml file:

    <itemtype code="CMSImageComponent" generate="true" jaloclass="de.hybris.platform.cms2.jalo.contents.components.CMSImageComponent" extends="SimpleCMSComponent" autocreate="true">
    	<attributes>
    		<attribute qualifier="media" type="localized:Media">
    			<persistence type="property" />
    		</attribute>
    	</attributes>
    </itemtype>
  • The following code excerpt shows the response from the CMS Structure API when a request for the structure of the CMSImageComponent is made:

        {
            "code": "CMSImageComponent",
            "i18nKey": "type.cmsimagecomponent.name",
            "name": "Image Component",
            "attributes": [
            		{
                    	"qualifier": "media",
                        "cmsStructureType": "Media",
                        "i18nKey": "type.cmsimagecomponent.media.name",
                        "localized": true
                    }
            ]
        }

Exposing Your New Component Types

To expose your new component types, you must do the following:

  1. Declare a component type structure.
  2. Declare a component type attribute structure.

Declaring a Component Type Structure

To expose a new component type in the CMS Structure API, you must declare a new ComponentTypeStructure Spring bean. You can do this by registering an anonymous Spring bean that implements the DefaultComponentTypeStructure, as shown in the following code excerpt:

<bean class="de.hybris.platform.cmswebservices.types.service.impl.DefaultComponentTypeStructure"
      p:typecode="CMSImageComponent" 
      p:populators-ref="defaultComponentTypePopulators" 
      p:category="COMPONENT"/>	

The following table provides the properties that you must include when you declare a new bean:

Property Description
category An optional property. By default the category is StructureTypeCategory.COMPONENT..
populators A reference to a set of populators to be used to populate the ComponentType properties.
typecode The typecode of the CMS component model that was declared in the items.xml file.

For more information about ComponentType properties and the available ComponentType populators, see the ComponentTypeProperties and ComponentTypePopulators sections in the CMS Structure API.

Declaring a Component Type Attribute Structure

For each attribute in the new component type that you want to expose in the CMS Structure API, you need a corresponding ComponentTypeAttributeStructure. This allows you to specify how to populate the attribute's properties. For example, the following code shows how to expose the media attribute of the CMSImageComponent:

<bean class="de.hybris.platform.cmswebservices.types.service.impl.DefaultComponentTypeAttributeStructure" p:typecode="CMSImageComponent" p:qualifier="media">
	<property name="populators">
		<set>
			<ref bean="basicComponentTypeAttributePopulator" />
			<ref bean="i18nComponentTypeAttributePopulator" />
			<ref bean="mediaComponentTypeAttributePopulator" />
		</set>
	</property>
</bean>

The following table describes the properties that you must include when you define a Component Type Attribute Structure:

Property Description
populators A reference to a set of populators to be used to populate the ComponentTypeAttribute properties.
qualifier The identifier of the attribute as declared in the items.xml file.
typecode The typecode of the CMS component attribute that was declared in the items.xml file.

For more information about ComponentTypeAttribute properties and the available ComponentTypeAttribute populators, see the ComponentTypeAttributeProperties and the ComponentTypeAttributePopulator sections of the CMS Structure API.

Exposing Additional Component Type Attribute Properties

The ComponentTypeAttributeData POJO supports a default set of properties. You can extend the solution if you want to expose additional properties.

If you want the ComponentTypeAttributeData POJO to expose more than these default properties, you must do the following:

  1. Declare a value-label bean.
  2. Extend the ComponentTypeAttributeData.
  3. Create a media format service.
  4. Create a ComponentTypeAttributePopulator.
  5. Register the property populator.

Code Examples of How to Expose Additional Component Type Attribute Properties

If you want to specify which media formats are supported for a Simple Responsive Banner Component, the following code example shows the expected response with the new ComponentTypeAttribute property called options:

  {
        "code": "SimpleResponsiveBannerComponent",
        "i18nKey": "type.simpleresponsivebannercomponent.name",
        "name": "Simple Responsive Banner Component",
        "attributes": [
        		{
                	"qualifier": "media",
                    "cmsStructureType": "Media",
                    "i18nKey": "type.simpleresponsivebannercomponent.media.name",
                    "localized": true,
					"options": [
						{
							"code": "widescreen",
							"label": "cms.media.format.widescreen"
						},
						{
							"code": "desktop",
							"label": "cms.media.format.desktop"
						},
						{
							"code": "tablet",
							"label": "cms.media.format.tablet"
						},
						{
							"code": "mobile",
							"label": "cms.media.format.mobile"
						}
					]
                }
        ]
    }

Exposing Your Additional Component Type Attribute Properties

Declaring a Value-Label Bean

You need a POJO bean that can contain a list of enumerated values and a translation key that can be mapped to a resource bundle to get a localized representation of the value. The following code excerpt shows how to declare this:

	<bean class="de.hybris.platform.cmswebservices.data.OptionData">
		<property name="value" type="String" />
		<property name="label" type="String" />
	</bean>

Extending the ComponentTypeAttributeData

To support the new ComponentTypeAttribute property, you must be able to set the property in the CMS Structure API attribute schema. To do this, you must add an OptionData property to the ComponentTypeAttributeData declaration in the beans.xml, as shown in the following code excerpt:

<bean class="de.hybris.platform.cmswebservices.data.ComponentTypeAttributeData">
    <property name="options" type="java.util.List&lt;de.hybris.platform.cmswebservices.data.OptionData&gt;" />
</bean>

Creating a Media Format Service

You must create a service that returns a collection of media formats to be returned by the CMS Structure API. The following code excerpt shows how to create a service for SimpleResponsiveBannerComponent ComponentTypes:

/**
 * Provide methods for retrieving media format information for a given CMS component model.
 */
public interface CMSMediaFormatService
{
	/**
	 * Retrieve a list of all media formats which are supported by the specified CMS component model
	 *
	 * @param classType
	 *           the CMS component model class
	 * @return the collection of media formats; never <tt>null</tt>
	 */
	Collection<MediaFormatModel> getMediaFormatsByComponentType(final Class<? extends AbstractCMSComponentModel> classType);
}

Creating a ComponentTypeAttribute Populator

To populate the new options property of the ComponentTypeAttributeData POJO, you need a populator that calls your service and populate the property. To create a new populator, implement the populator interface and add your logic as shown in the following example:

public class MediaFormatsComponentTypeAttributePopulator implements Populator<AttributeDescriptorModel, ComponentTypeAttributeData>
{
	private TypeService typeService;
	private CMSMediaFormatService cmsMediaFormatService;
 
	@Override
	public void populate(final AttributeDescriptorModel source, final ComponentTypeAttributeData target) throws ConversionException
	{
		final List<OptionData> options = new ArrayList<>();
		final Collection<MediaFormatModel> mediaFormats = cmsMediaFormatService.getMediaFormatsByComponentType(typeService.getModelClass(source.getEnclosingType()));


		for (final MediaFormatModel mediaFormat : mediaFormats)
		{
			final OptionData option = new OptionData();
			option.setValue(mediaFormat.getQualifier());
			option.setLabel("cms.media.format." + mediaFormat.getQualifier().toLowerCase());
			options.add(option);
		}
		target.setOptions(options);
	}
	
	// Getters and Setters
}

Registering the Property Populator

You must register the property populator. You need to specify that the populator is used to populate the options property of the media attribute of your component. The following code excerpt shows how to register the property populator for the SimpleResponsiveBannerComponent:

<!-- Register the populator bean -->
<bean id="mediaFormatsComponentTypeAttributePopulator" class="de.hybris.platform.cmswebservices.types.facade.populator.MediaFormatsComponentTypeAttributePopulator">
	<property name="typeService" ref="typeService" />
	<property name="cmsMediaFormatService" ref="cmsMediaFormatService" />
</bean>
 
<!-- Associate component type atribute to the new populator -->
<bean class="de.hybris.platform.cmswebservices.types.service.impl.DefaultComponentTypeAttributeStructure" p:typecode="SimpleResponsiveBannerComponent" p:qualifier="media">
	<property name="populators"><set><ref bean=mediaFormatsComponentTypeAttributePopulators" /></set></property>
</bean>

Restricting CMS Item Types and Attributes

By default, the CMS Structure API exposes all CMS Item Types and attributes. If you want to restrict CMS Item Types and attributes that are exposed, you must add the specified CMS Item Type or Type Attribute in the configuration file.

Restricting the CMS Item Type

By default, the CMS Structure API exposes all CMS Item Types. If you don't want the API to expose certain types, you must specify that CMS item type in the configuration file. The following example shows how to restrict the use of the simple banner component CMS item type:

 <bean id="customCmsStructureTypeBlacklistSet" parent="cmsStructureTypeBlacklistSet" class="org.springframework.beans.factory.config.SetFactoryBean">
    <property name="sourceSet">
        <list merge="true">
            <value>SimpleBannerComponent</value>
        </list>
    </property>
</bean>

Restricting the Type Attribute

By default, the CMS Structure API exposes all attributes for a CMS item type. If you don't want the API to expose an attribute, you must specify that attribute in the configuration file. The following example shows how to restrict the use of the content attribute of the CMS paragraph component CMS item type:

<bean depends-on="cmsStructureTypeBlacklistAttributeMap" parent="mapMergeDirective">
   <property name="key" value="CMSParagraphComponent" />
   <property name="value" value="content" />
</bean>

Modifying the Generic CMS Structure Type

By default, the CMS Structure API exposes all attributes for a CMS Item Type and assigns a predetermined CMS Structure Type to it. If you want to modify this behavior, you must add an entry to the Predicate Map attribute.

The Predicate Map attribute is an instance of the LinkedHashMap class. All Predicates (java.util.function.Predicate) are tested in the order in which they appear in the map. If you add an entry to the Predicate Map and the predicate holds true for that predicate, then you can modify the component type attribute structure as you want.

The following examples show you how to modify the generic CMS Structure Type by adding entries to the Predicate Map, where the de.hybris.platform.cmsfacades.types.service.predicate.RegexpStructureTypeAttributePredicate predicate is used.

Set RichText for Any java.lang.String Type

In the following example, the richTextComponentTypeAttributePopulator is added to the attribute populator's list. It assigns the RichText CMS structure type to all attributes of the String type.

<bean depends-on="cmsAttributePredicatePopulatorListMap" parent="mapMergeDirective">
   <property name="key">
	  <bean class="de.hybris.platform.cmsfacades.types.service.predicate.RegexpStructureTypeAttributePredicate" p:pattern=".*@AtomicType:java.lang.String"/>
   </property>
   <property name="value">
	  <list>
		<ref bean="richTextComponentTypeAttributePopulator" />
	  </list>
   </property>
</bean>

Set RichText for Any java.lang.String Type of a CMS Paragraph Component

In the following example, the richTextComponentTypeAttributePopulator is added to the attribute populator's list. It assigns the RichText CMS structure type to any attributes of the String type for a CMS paragraph component.

<bean depends-on="cmsAttributePredicatePopulatorListMap" parent="mapMergeDirective">
   <property name="key">
	  <bean class="de.hybris.platform.cmsfacades.types.service.predicate.RegexpStructureTypeAttributePredicate" p:pattern="CMSParagraphComponent@AtomicType:java.lang.String"/>
   </property>
   <property name="value">
	  <list>
		<ref bean="richTextComponentTypeAttributePopulator" />
	  </list>
   </property>
</bean>