CMS Version Service

The CMS Version Service provides the functionality to version a CMS item model.

Overview

The CMS Version Service provides the functionality to version a CMS item model. Through a strategy of converters, the service can serialize a CMS item model into a JSON equivalent by inspecting the item's attributes. It can also deserialize JSON into a CMS item model. The CMS Version Service is used to create a version of any CMS item model.

The CMS Version Service is the main entry point for versioning. Using this service, you can create versions for an item, generate a CMS item model from a version, verify if the CMS item model is versionable, and make other operations

The service exposes the following methods in the cms-version-spring.xml file to handle versioning:

public interface CMSVersionService
{
	ItemModel createItemFromVersion(CMSVersionModel version);
	ItemModel getItemFromVersion(final CMSVersionModel version);
	Optional<CMSVersionModel> getVersionByUid(String uid);
	Optional<CMSVersionModel> getVersionByLabel(CMSItemModel itemModel, String label);
	String getTransactionId();
	CMSVersionModel createRevisionForItem(CMSItemModel itemModel);
	CMSVersionModel createVersionForItem(CMSItemModel itemModel, String label, String description);
	Optional<ItemModel> rollbackVersionForUid(String uid);
	boolean isVersionable(CMSItemModel itemModel);
	String generateVersionUid();
	void deleteVersionsForItem(final CMSItemModel itemModel);
	Optional<AbstractPageModel> findPageVersionedByTransactionId(final String transactionId); 
}

Items

The CMS Version Service uses the CMS version type defined in the cms2-items.xml file to store version information for any versionable CMS item model. The CMS version type defines the following fields:

  • description: The description of a version.
  • item catalog version: The catalog version of the CMS item being versioned.
  • item type code: The type code of the CMS item being versioned.
  • item uid: The unique identifier of the CMS item being versioned.
  • label: The label of a version.
  • payload: The serialized JSON payload representing the CMS item being versioned.
  • retain: Boolean that determines if a version needs to be retained while garbage collecting, defaults to false.
  • transaction id: Identifier that is used internally to identify all the versions created in a single transaction.
  • uid: The unique identifier of a version.

Class Diagram

The following class diagram depicts the interfaces and classes used to support the CMS Version Service:

Implementation

The CMS Version Service contains three major converters defined in the cms2-versions-spring.xml file. The converters are:

  • cmsVersionToDataConverter: Converts the CMS item model to String. The DefaultCMSVersionToDataConverter is the default implementation.
  • cmsVersionToItemModelPreviewConverter: Converts the CMSVersionModel back into a CMS item model for previewing. The DefaultCMSVersionToItemModelPreviewConverter is the default implementation.
  • cmsVersionToItemModelRollbackConverter: Converts the CMSVersionModel back intoa CMS item model for rolling back of the version. The DefaultCMSVersionToItemModelRollbackConverter is the default implementation

The flow of the DefaultCMSVersionToDataConverter is as follows:

  1. The generatePayloadForVersion function of the CMS Version Service accepts a CMS version model as a parameter.
  2. It extracts the CMS item model using the getCMSItemByVersion method and calls the convert method of the DefaultCMSVersionToDataConverter.

The following diagram shows how the DefaultCMSVersionToDataConverter is used within the CMS Version Service:

The flow of the DefaultCMSVersionToItemModelPreviewConverter is as follows:

  1. The createItemFromVersion function accepts the CMS version model as a parameter.

  2. It calls the convert method of the DefaultCMSVersionToItemModelPreviewConverter to extract the CMS item model for the version.

The following diagram depicts how the DefaultCMSVersionToItemModelPreviewConverter is used within theCMS Version Service:

The flow of the DefaultCMSVersionToItemModelRollbackConverter is as follows:

  1. The getItemFromVersion function accepts CMSVersionModel as a parameter.
  2. It calls the convert method of the DefaultCMSVersionToItemModelRollbackConverter to extract the CMS item model for the version.

The following diagram depicts how the DefaultCMSVersionToItemModelRollbackConverter is used within the CMS Version Service:

cmsVersionToDataConverter

The cmsVersionToDataConverter in the cms2-versions-spring.xml file provides the logic to convert any CMS item model into a JSON payload.

The cmsVersionToDataConverter does the following:

  1. Extracts all serializable attributes for the CMS item model.
  2. Converts all serializable attributes into an SLDDataContainer.AttributeValue.
  3. Converts all custom attributes into an SLDDataContainer.AttributeValue.
  4. Merges the results from the two previous steps into one collection.
  5. Serializes the result.

The following diagram depicts the flow of the cmsVersionToDataConverter:

The following diagram shows the flow to convert all attributes into an SLDDataContainer.AttributeValue:

The following diagram shows the flow to convert all custom attributes into an SLDDataContainer.AttributeValue:

The following sequence provides information about what each method in the converter does:

  1. The getConvertedAttributeValues function converts all available and serializable attributes of the CMS item model. It relies on the cmsVersionAttributeDescriptorHelper to find all serializable attributes.

  2. The getAttributeValueMapperForItem is called for every attribute and returns a function that accepts the AttributeDescriptorModel and returns an optional object that represents a converted value wrapped in an SLDDataContainer.AttributeValue.

    1. The getAttributeValueMapperForItem uses one of four suppliers to get a converted value from an attribute:

      • collectionGetter: Returns a supplier that converts a collection.
      • localizedCollectionGetter: Returns a supplier that converts a localized collection value.
      • localizedGetter: Returns a supplier that converts a simple localized value.
      • simpleGetter: Returns a supplier that converts a simple value such as String, Date, or Integer.
    2. Each supplier converts the value or a collection of values using the cmsVersionAttributeToDataStrategyConverter and returns the result wrapped in an SLDDataContainer.AttributeValue.

  3. The getCustomConvertedAttributeValues converts all custom attributes.

    The custom attribute is an attribute that cannot be extracted using the cmsVersionAttributeDescriptorHelper service. For example, it's not possible to extract content slots for a page or content slots for a template using attributes available in a page item model. However, you still need to save such information to a payload, therefore you must use custom converters. The customAttributeStrategyConverter is used to extract a list of all converters applicable to the current CMS item model. It is possible that the converter provider returns more than one custom converter for a current CMS item model.

  4. The cmsCollectionHelper in the cms2-spring.xml file is used to merge the two collections from the previous steps.

  5. The final result is serialized using the defaultPayloadSerializer, which is part of the audit module.

cmsVersionAttributeToDataStrategyConverter

The cmsVersionAttributeToDataStrategyConverter in the cms2-version-converters-spring.xml file is an attribute to the data strategy converter that is used to convert an attribute value into a value that is suitable for the JSON payload.

The cmsVersionAttributeToDataStrategyConverter uses four default model-to-data converters, which are stored in the cms2-version-converters-spring.xml file. The default converters are as follows:

Model to Data converters

Description

de.hybris.platform.cms2.version.converter.attribute.impl.CMSItemToDataConverter

It converts a CMS item model into its primary key if it's not versionable. It creates a version for the CMS item and the primary key of the new version is returned. It can be used as an attribute-to-data converter primarily for CMS item model data types.

de.hybris.platform.cms2.version.converter.attribute.impl.DefaultAttributeToDataConverter

It does not convert the input parameter, it just returns it as it is. It can be used as an attribute-to-data converter, primarily for primitive data types.

de.hybris.platform.cms2.version.converter.attribute.impl.ItemToDataConverter

It converts any item model into primary key. It can be used as an attribute-to-data converter primarily for item model data types.

de.hybris.platform.cms2.version.converter.attribute.impl.MediaContainerToDataConverter

It converts an item model of type MediaContainerModel. It creates a new version for every media container and then returns its primary key.

The cmsVersionAttributeToDataStrategyConverter uses four default model-to-data predicates, which are stored in the cms2-version-predicates-spring.xml. The predicates are as follows:

Predicate Class

Description

de.hybris.platform.cms2.version.predicate.AtomicTypePredicate

Identifies if the attribute is an AtomicType.

de.hybris.platform.cms2.version.predicate.CMSItemTypePredicate

Identifies if the attribute is a CMS item model.

de.hybris.platform.cms2.version.predicate.EnumTypePredicate

Identifies if the attribute is an HybrisEnumType.

de.hybris.platform.cms2.version.predicate.NonCMSItemTypePredicate

Identifies if the attribute is an item model and not a CMS item model.

cmsVersionToItemModelPreviewConverter

The cmsVersionToItemModelPreviewConverter located in the cms2-versions-spring.xml file provides the logic to convert any CMS version model into its corresponding CMS item model so that it can be previewed.

The cmsVersionToItemModelPreviewConverter does the following:

  1. Retrieves the generated item from the cache. If an item is found, it returns the item, if not, it moves to step 2.
  2. Creates an empty CMS item model based on the item type of the versioned item provided.
  3. Deserializes the payload of the CMS version model provided.
  4. Adds the generated item to the cache using the CmsVersionSessionContextProvider.
  5. Populates the newly created item using the CMSVersionToItemModelPopulator.
  6. Returns the populated itemModel

The flow is shown in the following diagram:

cmsVersionToItemModelRollbackConverter

The cmsVersionToItemModelRollbackConverter in the cms2-versions-spring.xml file provides the logic to convert any CMS version model into its corresponding CMS item model so that it can be rolled back to replace the current item.

The cmsVersionToItemModelRollbackConverter does the following:

  1. Retrieves the generated item from the cache, if found it returns the item, it not, it moves to step 2.
  2. Retrieves the existing item from the ItemService using the type code and mapped attributes.
  3. Deserializes the payload of the CMS version model provided.
  4. Adds the generated item to the cache using the CmsVersionSessionContextProvider.
  5. Populates the retrieved item using the CMSVersionToItemModelPopulator.
  6. Returns the populated itemModel.

The flow is shown in the following diagram:

CMSVersionToItemModelPopulator

The cmsVersionToItemModel populator is used to populate items for both preview and rollback operations.

The cmsVersionToItemModel does the following:

  1. Converts all simple attributes values from AuditPayload to attribute values of the CMS item model.
  2. Convert the localized attribute values from AuditPayload to attribute values of the CMS item model.
  3. Merges results from the previous two steps into one collection.
  4. Applies the collection onto the created CMS item model.
  5. Creates a map that contains default (empty) collection values for every collection attribute that is null and applies the empty attribute values map to the CMS item model.
  6. Converts the localized attribute values from AuditPayload into attribute values of the CMS item model and applies it to the CMS item model.

The following diagram shows the flow to convert simple attributes:

The following diagram shows the flow to convert localized attributes:

The following provides detailed information about what each method in the converter does:

  1. The convertSimpleAttributes uses the getAttributeValueMapperForTypedValue method to convert the attribute values from the AuditPayload into attribute values of the CMS item model.
  2. The getAttributeValueMapperForTypedValue method returns a function that is used to convert any non-localized values described by a TypedValue.
  3. The convertedLocalizedAttributeValues uses the getAttributeValueMapperForLocalizedTypedValue method to convert localized attribute values from the AuditPayload into attribute values of the CMS item model.
  4. The getAttributeValueMapperForLocalizedTypedValue method returns a function that converts any localized value described by a LocalizedTypedValue.
  5. The getAttributeRelatedData is called for each localized and non-localized attribute. It returns a function that is used to convert any value described by a ValueType. The function executes one of the suppliers provided depending on if the value passed is a collection or not.
  6. The convertCustomAttributesValues method converts custom attribute values that are represented by the CMSVersionCustomAttribute.

cmsVersionDataToAttributeStrategyConverter

The cmsVersionDataToAttributeStrategyConverter located in thecms2-version-converters-spring.xml file is a data-to-attribute strategy converter that is used to convert a data value into an attribute of the corresponding CMS item.

The cmsVersionDataToAttributeStrategyConverter uses the following four default converters:

Data to Model Converter

Description

de.hybris.platform.cms2.version.converter.attribute.impl.DateDataToModelConverter

Attribute converter for the java.util.Date. It converts the date to its proper instant object and formats the date from the ZoneOffset#UTC zone.

de.hybris.platform.cms2.version.converter.attribute.impl.DefaultDataToModelConverter

Implementation of the AttributeContentConverter that converts properties of the AtomicType to the corresponding object

de.hybris.platform.cms2.version.converter.attribute.impl.EnumDataToModelConverter

Implementation of the AttributeContentConverter that converts properties that are assignable from the HybrisEnumValue to a corresponding value.

de.hybris.platform.cms2.version.converter.attribute.impl.PKDataToModelConverter

Implementation of the converter that converts properties of type PK to the corresponding itemmodel.

The cmsVersionDataToAttributeStrategyConverter uses the following four default predicates:

Predicate Class

Description

de.hybris.platform.cms2.version.predicate.DataToModelAtomicTypePredicate

Identifies if the provided type is an AtomicType.

de.hybris.platform.cms2.version.predicate.DataToModelDatePredicate

Identifies if the provided type is assignable from the Date type.

de.hybris.platform.cms2.version.predicate.DataToModelEnumPredicate

Identifies if the provided type is assignable from HybrisEnumValue type.

de.hybris.platform.cms2.version.predicate.DataToModelPKPredicate

Identifies if the provided type is of PK type.

cmsVersionAttributeDescriptorHelper

The cmsVersionAttributeDescriptorHelper located in the cms2-versions-spring.xml file is a helper service that contains methods to extract serializable attributes. The default implementation of the cmsVersionAttributeDescriptorHelper returns all relations and all writable attributes except attributes that represent an item type and the primary key.

customAttributeStrategyConverter

The customAttributeStrategyConverter located in the cms2-version-converters-spring.xml file is a custom attribute strategy converter that contains a list of custom converters that can be used to save any attribute to the payload that cannot be extracted from the CMS item model directly.

The following code excerpt shows the CustomAttributeStrategyConverterProvider interface:

public interface CustomAttributeStrategyConverterProvider { List<CustomAttributeContentConverter> getConverters(ItemModel itemModel); }

The following code excerpt shows the CustomAttributeContentConverter interface:

public interface CustomAttributeContentConverter { String getQualifier(); Predicate<ItemModel> getConstrainedBy(); Object convertModelToData(ItemModel itemModel); void populateItemModel(ItemModel itemModel, String value); }