Class DefaultCMSItemConverter

  • All Implemented Interfaces:
    CMSItemConverter

    public class DefaultCMSItemConverter
    extends java.lang.Object
    implements CMSItemConverter
    The CMSItemConverter is the first layer of converters applied to convert a given CMSItemModel into Map<String, Object>. The reason why this class accepts any kind of ItemModel is that this same converter will be reused recursively when attributes are part of (AttributeDescriptorModel.getPartOf()) the enclosing item model, hence denoting a composition. This converter is also handling localized attributes and collections.
    • Constructor Detail

      • DefaultCMSItemConverter

        public DefaultCMSItemConverter()
    • Method Detail

      • convert

        public ItemModel convert​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Description copied from interface: CMSItemConverter
        Converts a deserializable Map to an ItemModel.
        Specified by:
        convert in interface CMSItemConverter
        Parameters:
        map - the Map to convert
        Returns:
        the Map converted to an ItemModel
      • convertAndValidate

        protected ItemModel convertAndValidate​(java.util.Map<java.lang.String,​java.lang.Object> map,
                                               ComposedTypeModel composedType)
        Converts and validates a deserializable Map to an ItemModel.
        Parameters:
        map - the Map to convert
        composedType - the type to which the Map will be converted to.
        Returns:
        the Map converted to an ItemModel
      • convertAndValidateAttribute

        protected void convertAndValidateAttribute​(java.util.Map<java.lang.String,​java.lang.Object> map,
                                                   ItemModel itemModel,
                                                   AttributeDescriptorModel attribute)
        Converts and validates a primitive data or deserializable Map to an attribute value (primitive or ItemModel).
        Parameters:
        map - the Map containing the attribute data to convert
        itemModel - the model which attribute value will be updated
        attribute - the attribute descriptor providing information about the attribute to be updated
      • initializeCloneComponentAttributeContext

        protected boolean initializeCloneComponentAttributeContext​(java.lang.String qualifier,
                                                                   java.lang.Object targetValue)
        Check that the value for the given qualifier was modified during the cloning process and stores the source component attribute value in the clone component context in the session.
        Parameters:
        qualifier - the attribute field name
        targetValue - the value to be applied to the component model
        Returns:
        TRUE when the attribute value was modified during the component cloning flow;
        FALSE when the given qualifier is UID or when the attribute value was not modified
      • getCmsItemValidator

        protected CMSItemValidator<ItemModel> getCmsItemValidator​(java.util.Map<java.lang.String,​java.lang.Object> map)
      • getItemModelFromRepresentation

        protected ItemModel getItemModelFromRepresentation​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Get Item Model from request payload
        Parameters:
        map - the map representing the Item Model
        Returns:
        the ItemModel related to this Map representation
      • isCloneComponentFlow

        protected boolean isCloneComponentFlow​(java.util.Map<java.lang.String,​java.lang.Object> map)
        Determine if a map that represents the item model is a component clone
        Parameters:
        map - the map representing the Item Model
        Returns:
        a boolean that determines if the item being created is a component clone
      • getCloneModelFromRepresentation

        protected ItemModel getCloneModelFromRepresentation​(java.lang.String sourceComponentUuid)
        Create a Clone Item Model and saves it to the CloneComponentContextProvider or get a Clone Item Model from the CloneComponentContextProvider
        Parameters:
        sourceComponentUuid - the UUID representing the source Item Model to clone from
        Returns:
        the ItemModel cloned from the provided sourceComponentUuid
      • transformCollection

        protected java.util.Collection<java.lang.Object> transformCollection​(AttributeDescriptorModel attribute,
                                                                             java.util.Collection<java.lang.Object> collection,
                                                                             java.util.function.Function<java.lang.Object,​java.lang.Object> transform)
        Convenience method to apply a transformation to all elements of a Function
        Parameters:
        collection - the Collection the elements of which we need to transform
        transform - the transformation Function that will be applied on each element of the collection
        Returns:
        a new collection
      • transformLocalizedValue

        protected java.util.Map<java.lang.String,​java.lang.Object> transformLocalizedValue​(AttributeDescriptorModel attribute,
                                                                                                 java.util.Map<java.lang.String,​java.lang.Object> itemMap,
                                                                                                 ItemModel itemModel,
                                                                                                 java.util.function.Function<java.lang.Object,​java.lang.Object> transform)
        Convenience method to apply a transformation to values of a Map
        Parameters:
        itemMap - the Map the values of which we need to transform
        itemModel - the model containing the original attribute value. If this is a new model, the attribute value will be empty.
        transform - the transformation Function that will be applied on each values of the itemMap
        Returns:
        a new itemMap
      • leafOrDeeperConvertToRepresentation

        protected java.util.function.Function<java.lang.Object,​java.lang.Object> leafOrDeeperConvertToRepresentation​(AttributeDescriptorModel attribute)
        Given an AttributeDescriptorModel, will return a Function that will do the following:
        if the attribute is a partOf of the owning type, it will return a conversion of it by means of convert(ItemModel)
        if the attribute is not a partOf of the owning type, it will return a conversion of it by means of the appropriate model-to-data converter returned by the attributeStrategyConverter.
        Parameters:
        attribute - the AttributeDescriptorModel describing the metadata of the property of a class
        Returns:
        a conversion of type Object
      • leafOrDeeperConvertToModel

        protected java.util.function.Function<java.lang.Object,​java.lang.Object> leafOrDeeperConvertToModel​(AttributeDescriptorModel attribute)
        Given an AttributeDescriptorModel, will return a Function that will do the following:
        if the attribute is a partOf of the owning type, it will return a conversion of it by means of DefaultCMSItemConverter#convert(Map)
        if the attribute is not a partOf of the owning type, it will return a conversion of it by means of the appropriate data-to-model converter returned by the attributeStrategyConverter. if no converter is found, AttributeContentConverter.Value.NOT_TO_BE_SET is returned to instruct no to try to persist this property
        Parameters:
        attribute - the AttributeDescriptorModel describing the metadata of the property of a class
        Returns:
        a conversion of type Object
        Throws:
        java.lang.IllegalAccessException
        java.lang.InstantiationException
      • isDynamicAttribute

        protected boolean isDynamicAttribute​(AttributeDescriptorModel attribute)
        Returns true if the attribute is a dynamic attribute. Note that this method doesn't consider RelationDescriptorModels as dynamic.
        Parameters:
        attribute - The attribute to evaluate.
        Returns:
        true if the attribute is dynamic, false otherwise.
      • getAttributes

        protected java.util.List<AttributeDescriptorModel> getAttributes​(ComposedTypeModel composedType)
        Returns the list of all attributes defined for a given composed type, including the inherited ones.
        Parameters:
        composedType - the composedType the attributes belong to
        Returns:
        a list of all attributes declared, including inherited attributes.
      • getAttributeValues

        protected java.util.function.Function<ItemModel,​java.util.Map<AttributeDescriptorModel,​java.lang.Object>> getAttributeValues()
        Function to get a Map of attribute descriptors and its Object value for a given ItemModel
        Returns:
        a function that when executed returns a map with attributes and their respective values.
      • convertAttributeValueToRepresentation

        protected java.util.function.Function<AttributeDescriptorModel,​java.util.Optional<java.lang.Object>> convertAttributeValueToRepresentation​(ItemModel source)
        Function that returns the serializable representation value (String, Map or Collection) of all AttributeDescriptorModel of a given ItemModel source. This methods recursively handles the following cases and their possible combinations:
        • localized field
        • collections
        • partOf
        Parameters:
        source - the ItemModel owning all the AttributeDescriptorModel
        Returns:
        returns the serializable representations
      • convertRepresentationToAttributeValue

        protected java.util.function.Function<AttributeDescriptorModel,​java.util.Optional<java.lang.Object>> convertRepresentationToAttributeValue​(java.lang.Object source,
                                                                                                                                                         ItemModel itemModel)
        Function that constructs the persistable representation of all AttributeDescriptorModel of a given serializable source (String, Map or Collection). This methods recursively handles the following cases and their possible combinations:
        • localized field
        • collections
        • partOf
        Parameters:
        source - serializable source (String, Map or Collection) described by a AttributeDescriptorModel
        itemModel - the model containing the original attribute value. If this is a new model, the attribute value will be empty.
        Returns:
        returns the persistable representations
      • getAttributeValue

        protected java.util.function.Function<AttributeDescriptorModel,​java.util.Optional<java.lang.Object>> getAttributeValue​(java.util.function.Supplier<java.lang.Object> localizedCollectionGetter,
                                                                                                                                     java.util.function.Supplier<java.lang.Object> localizedGetter,
                                                                                                                                     java.util.function.Supplier<java.lang.Object> collectionGetter,
                                                                                                                                     java.util.function.Supplier<java.lang.Object> simpleGetter)
        Returns a Function aimed at converting any value described by a AttributeDescriptorModel. This function will execute one of the provided suppliers depending on whether the attribute is localized and/or a collection
        Parameters:
        localizedCollectionGetter - a Supplier invoked if the attribute is both localized and a collection
        localizedGetter - a Supplier invoked if the attribute is localized and not a collection
        collectionGetter - a Supplier invoked if the attribute is a collection and not localized
        simpleGetter - a Supplier invoked if the attribute is neither localized nor an attribute
        Returns:
        a Function to convert a value
      • validate

        protected void validate​(java.lang.Object value,
                                AttributeDescriptorModel attribute,
                                AttributeContentValidator validator)
        Validates and throws an exception if there are validation errors
        Parameters:
        value - the value being validated
        attribute - the attribute descriptor model
        validator - the validator to be used
      • collectValidationErrors

        protected void collectValidationErrors​(ValidationException e,
                                               java.util.Optional<java.lang.String> language,
                                               java.util.Optional<java.lang.Integer> position)
        Collects the errors in the validation exception and adds to the global validation context.
        Parameters:
        e - the exception
        language - optional; the validated language
        position - optional; the position in which the object value in the collection
      • modelHasAssignedUUID

        protected boolean modelHasAssignedUUID​(java.util.Map<java.lang.String,​java.lang.Object> valueMap)
      • principalCanUpdateAttribute

        protected boolean principalCanUpdateAttribute​(java.lang.String typeCode,
                                                      java.lang.String qualifier)
        This method determines whether the current principal can update the attribute identified by the qualifier in the enclosing type. A user can update an attribute when both read and change attribute permissions are granted.
        Parameters:
        typeCode - the item model type code
        qualifier - the attribute qualifier
        Returns:
        TRUE when the user has read and change permissions on the attribute; otherwise FALSE
      • principalCanWriteAttributeLanguage

        protected boolean principalCanWriteAttributeLanguage​(java.lang.String qualifier,
                                                             java.lang.String languageIsoCode,
                                                             java.util.Set<java.lang.String> writeableLanguages)
        This method is used to check if the current principal can write a language of a localized attribute.
        Parameters:
        qualifier - - the qualifier of the attribute whose language wants to be changed.
        languageIsoCode - - the ISO code of the language that wants to be updated.
        writeableLanguages - - the list of languages the current principal can write. Each element represents the ISO code of a language
        Returns:
        a boolean flag. True, if the user can write the language for the localized element; False, otherwise.
      • convertRepresentationToAttributeValue

        @Deprecated
        protected java.util.function.Function<AttributeDescriptorModel,​java.util.Optional<java.lang.Object>> convertRepresentationToAttributeValue​(java.lang.Object source)
        Deprecated.
        Function that constructs the persistable representation of all AttributeDescriptorModel of a given serializable source (String, Map or Collection). This methods recursively handles the following cases and their possible combinations:
        • localized field
        • collections
        • partOf
        Parameters:
        source - serializable source (String, Map or Collection) described by a AttributeDescriptorModel
        Returns:
        returns the persistable representations
      • transformLocalizedValue

        @Deprecated
        protected java.util.Map<java.lang.String,​java.lang.Object> transformLocalizedValue​(AttributeDescriptorModel attribute,
                                                                                                 java.util.Map<java.lang.String,​java.lang.Object> itemMap,
                                                                                                 java.util.function.Function<java.lang.Object,​java.lang.Object> transform)
        Convenience method to apply a transformation to values of a Map
        Parameters:
        itemMap - the Map the values of which we need to transform
        transform - the transformation Function that will be applied on each values of the itemMap
        Returns:
        a new itemMap
      • getTypeService

        protected TypeService getTypeService()
      • setTypeService

        public void setTypeService​(TypeService typeService)
      • getModelService

        protected ModelService getModelService()
      • setModelService

        public void setModelService​(ModelService modelService)
      • setLocalizedPopulator

        public void setLocalizedPopulator​(LocalizedPopulator localizedPopulator)
      • setUniqueItemIdentifierService

        public void setUniqueItemIdentifierService​(UniqueItemIdentifierService uniqueItemIdentifierService)
      • setCmsAdminItemService

        public void setCmsAdminItemService​(CMSAdminItemService cmsAdminItemService)
      • setBaseAttributeContentValidator

        public void setBaseAttributeContentValidator​(AttributeContentValidator baseAttributeContentValidator)
      • setExtendedAttributeContentValidator

        public void setExtendedAttributeContentValidator​(AttributeContentValidator extendedAttributeContentValidator)
      • setValidationErrorsProvider

        public void setValidationErrorsProvider​(ValidationErrorsProvider validationErrorsProvider)
      • setValidatableService

        public void setValidatableService​(ValidatableService validatableService)
      • setComponentCloningStrategy

        public void setComponentCloningStrategy​(ComponentCloningStrategy componentCloningStrategy)
      • setCloneComponentContextProvider

        public void setCloneComponentContextProvider​(CloneComponentContextProvider cloneComponentContextProvider)
      • getCustomPopulators

        protected java.util.List<Populator<ItemModel,​java.util.Map<java.lang.String,​java.lang.Object>>> getCustomPopulators()
      • setCustomPopulators

        public void setCustomPopulators​(java.util.List<Populator<ItemModel,​java.util.Map<java.lang.String,​java.lang.Object>>> customPopulators)
      • setPermissionCRUDService

        public void setPermissionCRUDService​(PermissionCRUDService permissionCRUDService)
      • setCmsUserService

        public void setCmsUserService​(CMSUserService cmsUserService)
      • setCmsPermissionChecker

        public void setCmsPermissionChecker​(CMSPermissionChecker cmsPermissionChecker)
      • getIsCollectionPredicate

        protected java.util.function.Predicate<AttributeDescriptorModel> getIsCollectionPredicate()
      • setIsCollectionPredicate

        public void setIsCollectionPredicate​(java.util.function.Predicate<AttributeDescriptorModel> isCollectionPredicate)
      • getOriginalConvertedItemProvider

        protected CMSItemContextProvider<java.util.Map<java.lang.String,​java.lang.Object>> getOriginalConvertedItemProvider()
      • setOriginalConvertedItemProvider

        public void setOriginalConvertedItemProvider​(CMSItemContextProvider<java.util.Map<java.lang.String,​java.lang.Object>> originalConvertedItemProvider)