Package org.ametys.cms.data
Class ContentDataHelper
- java.lang.Object
-
- org.ametys.cms.data.ContentDataHelper
-
- All Implemented Interfaces:
Component
,Serviceable
public class ContentDataHelper extends Object implements Serviceable, Component
Helper for data of type 'content'
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ContentDataHelper.InvertRelationManager
Invert relation manager (to add or remove a relation on a content
-
Field Summary
Fields Modifier and Type Field Description private ContentTypeExtensionPoint
_cTypeExtensionPoint
private AmetysObjectResolver
_resolver
static String
ROLE
Avalon role
-
Constructor Summary
Constructors Constructor Description ContentDataHelper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
_addReferences(Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references, ContentAttributeDefinition definition, String dataPath, Set<ContentValue> previousContents, Set<ContentValue> newContents)
private void
_collectInvertRelation(Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, String name, ContentAttributeDefinition definition, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
private void
_collectReferencedContents(ViewItemContainer viewItemContainer, Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
private void
_collectRepeater(ModelViewItemGroup group, Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, String name, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
private Set<ContentValue>
_getContentRefs(Object value)
private static Pair<Boolean,Optional<ContentValue>>
_isInvertRelationBroken(Content source, ContentValue destinationValue, ContentAttributeDefinition definition)
Check if the invert relation between the given contents is brokenprivate static boolean
_removeSingleInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
static boolean
addInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
Adds the invert relation to the givenDataHolder
static Collection<ReferencedContents>
checkBrokenInvertRelations(Content content, boolean fix)
Find any broken invert relation in a content attributes.static Map<String,List<String>>
checkBrokenReferences(ModelAwareDataHolder dataHolder, boolean fix)
Find any broken reference in data holder's content attributes.Collection<ReferencedContents>
collectReferencedContents(ViewItemContainer viewItemContainer, ModifiableContent content, Map<String,Object> values)
Prepares a write operation in the givenModelAwareDataHolder
by traversing aView
with the values to be written.
The goal is to find content attributes and to extract added and removed values.static String
getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath)
Retrieves the content identifier of a content datastatic String
getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath, String defaultValue)
Retrieves the content identifier of a content datastatic String[]
getContentIdsArrayFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath)
Retrieves the content identifiers in an array from a multiple content datastatic List<String>
getContentIdsListFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath)
Retrieves the content identifiers in aList
from a multiple content datastatic Stream<String>
getContentIdsStreamFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath)
Retrieves aStream
of the content identifiers from a multiple content datastatic boolean
isMultipleContentDataEmpty(ModelAwareDataHolder dataHolder, String dataPath)
Retrieves the content identifiers in an array from a multiple content datastatic Map<String,ModifiableContent>
manageInvertRelations(String invertRelationPath, Map<ContentValue,Collection<String>> referencedContents, ContentDataHelper.InvertRelationManager invertRelationManager, Map<String,ModifiableContent> alreadyModifiedContents)
Manages the invert relations concerned by the given path, through the givenContentDataHelper.InvertRelationManager
static Map<String,ModifiableContent>
manageInvertRelations(String invertRelationPath, Set<ContentValue> referencedContents, String referencingContentId, ContentDataHelper.InvertRelationManager invertRelationManager, Map<String,ModifiableContent> alreadyModifiedContents)
Manages the invert relations concerned by the given path, through the givenContentDataHelper.InvertRelationManager
static boolean
removeInvertRelation(ModifiableContent referencedContent, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
Removes the invert relation to the givenDataHolder
void
service(ServiceManager manager)
-
-
-
Field Detail
-
_resolver
private AmetysObjectResolver _resolver
-
_cTypeExtensionPoint
private ContentTypeExtensionPoint _cTypeExtensionPoint
-
-
Constructor Detail
-
ContentDataHelper
public ContentDataHelper()
-
-
Method Detail
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
getContentIdFromContentData
public static String getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath, String defaultValue) throws BadItemTypeException
Retrieves the content identifier of a content data- Parameters:
dataHolder
- data holder that contains the content datadataPath
- path to the content datadefaultValue
- The default value to return- Returns:
- the content identifier
- Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
getContentIdFromContentData
public static String getContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException
Retrieves the content identifier of a content data- Parameters:
dataHolder
- data holder that contains the content datadataPath
- path to the content data- Returns:
- the content identifier, empty string if it is invalid
- Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
isMultipleContentDataEmpty
public static boolean isMultipleContentDataEmpty(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException
Retrieves the content identifiers in an array from a multiple content data- Parameters:
dataHolder
- data holder that contains the multiple content datadataPath
- path to the multiple content data- Returns:
- an array containing the content identifiers
- Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
getContentIdsListFromMultipleContentData
public static List<String> getContentIdsListFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException
Retrieves the content identifiers in aList
from a multiple content data- Parameters:
dataHolder
- data holder that contains the multiple content datadataPath
- path to the multiple content data- Returns:
- a
List
containing the content identifiers - Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
getContentIdsArrayFromMultipleContentData
public static String[] getContentIdsArrayFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException
Retrieves the content identifiers in an array from a multiple content data- Parameters:
dataHolder
- data holder that contains the multiple content datadataPath
- path to the multiple content data- Returns:
- an array containing the content identifiers
- Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
getContentIdsStreamFromMultipleContentData
public static Stream<String> getContentIdsStreamFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath) throws BadItemTypeException
Retrieves aStream
of the content identifiers from a multiple content data- Parameters:
dataHolder
- data holder that contains the multiple content datadataPath
- path to the multiple content data- Returns:
- a
Stream
of the content identifiers - Throws:
BadItemTypeException
- if the data at the given path is not a content data
-
collectReferencedContents
public Collection<ReferencedContents> collectReferencedContents(ViewItemContainer viewItemContainer, ModifiableContent content, Map<String,Object> values)
Prepares a write operation in the givenModelAwareDataHolder
by traversing aView
with the values to be written.
The goal is to find content attributes and to extract added and removed values.- Parameters:
viewItemContainer
- the view to walk throughcontent
- the sourceModifiableContent
values
- the new values- Returns:
- the contents to-be-added and to-be-removed
-
_getContentRefs
private Set<ContentValue> _getContentRefs(Object value)
-
_addReferences
private void _addReferences(Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references, ContentAttributeDefinition definition, String dataPath, Set<ContentValue> previousContents, Set<ContentValue> newContents)
-
_collectReferencedContents
private void _collectReferencedContents(ViewItemContainer viewItemContainer, Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
-
_collectInvertRelation
private void _collectInvertRelation(Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, String name, ContentAttributeDefinition definition, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
-
_collectRepeater
private void _collectRepeater(ModelViewItemGroup group, Optional<ModelAwareDataHolder> currentDataHolder, String dataPathPrefix, Optional<Map<String,Object>> values, boolean insideKeptDataHolder, String name, Map<ContentAttributeDefinition,Pair<Map<ContentValue,List<String>>,Map<ContentValue,List<String>>>> references)
-
checkBrokenReferences
public static Map<String,List<String>> checkBrokenReferences(ModelAwareDataHolder dataHolder, boolean fix)
Find any broken reference in data holder's content attributes. A reference is considered as broken if the referenced content does not exist- Parameters:
dataHolder
- theModelAwareDataHolder
to inspect.fix
- iftrue
, and the dataHolder is modifiable, broken references will be removed Warning: if modifications are made on the given data holder, the modifications won't be saved and the workflow won't change- Returns:
- all broken references, by attribute path
-
checkBrokenInvertRelations
public static Collection<ReferencedContents> checkBrokenInvertRelations(Content content, boolean fix)
Find any broken invert relation in a content attributes. The invert relation is considered as broken if the referenced content does not reference the first one by the attribute with the invert relation path This check does not take nonexistent referenced contents into account. First call thecheckBrokenReferences(ModelAwareDataHolder, boolean)
method- Parameters:
content
- the content to inspect.fix
- iftrue
, and the referenced contents are modifiable, broken invert relations will be fixed by adding the relation on the referenced content if the referenced content is referencing another content and the attribute is single (and not in a repeater), the third content will be modified too, to remove the relation to the second content. Warning: if modifications are made on some contents, the modifications won't be saved and the workflows won't change- Returns:
- All broken invert relations in a collection of
ReferencedContents
. CallReferencedContents.getAddedContents()
to get all contents referenced by the given content that does not reference it in return CallReferencedContents.getThirdPartyContents()
to get contents referenced by content in added contents that reference it in return
-
_isInvertRelationBroken
private static Pair<Boolean,Optional<ContentValue>> _isInvertRelationBroken(Content source, ContentValue destinationValue, ContentAttributeDefinition definition)
Check if the invert relation between the given contents is broken- Parameters:
source
- the source content of the invert relationdestinationValue
- the destination content of the invert relationdefinition
- the definition concerned by the checked invert relation- Returns:
- a
Pair
containing the result of the check (Boolean
part left) and an optional third part content, if the relation is broken and the destination content references another content in a single attribute that should be modified
-
manageInvertRelations
public static Map<String,ModifiableContent> manageInvertRelations(String invertRelationPath, Set<ContentValue> referencedContents, String referencingContentId, ContentDataHelper.InvertRelationManager invertRelationManager, Map<String,ModifiableContent> alreadyModifiedContents)
Manages the invert relations concerned by the given path, through the givenContentDataHelper.InvertRelationManager
- Parameters:
invertRelationPath
- the concerned invert relation pathreferencedContents
- aSet
containing the referenced contents to manage (add or remove the relation)referencingContentId
- the id of the content referencing the given contentsinvertRelationManager
- theContentDataHelper.InvertRelationManager
to use (to add or remove the invert relations)alreadyModifiedContents
- aMap
of contents (indexed by their identifiers) that have already been modified. This map will be used to initialize the returned map and to search for contents that have already been resolved (to resolve each content only once, even if there are several modifications on a same content)- Returns:
- a
Map
containing the contents (indexed by their identifiers) that have been modified by this call and the ones that have already been modified
-
manageInvertRelations
public static Map<String,ModifiableContent> manageInvertRelations(String invertRelationPath, Map<ContentValue,Collection<String>> referencedContents, ContentDataHelper.InvertRelationManager invertRelationManager, Map<String,ModifiableContent> alreadyModifiedContents)
Manages the invert relations concerned by the given path, through the givenContentDataHelper.InvertRelationManager
- Parameters:
invertRelationPath
- the concerned invert relation pathreferencedContents
- aMap
containing the referenced contents to manage (add or remove the relation) and the id of the content referencing this contentinvertRelationManager
- theContentDataHelper.InvertRelationManager
to use (to add or remove the invert relations)alreadyModifiedContents
- aMap
of contents (indexed by their identifiers) that have already been modified. This map will be used to initialize the returned map and to search for contents that have already been resolved (to resolve each content only once, even if there are several modifications on a same content)- Returns:
- a
Map
containing the contents (indexed by their identifiers) that have been modified by this call and the ones that have already been modified
-
addInvertRelation
public static boolean addInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
Adds the invert relation to the givenDataHolder
- Parameters:
dataHolder
- the data holder where to add the invert relationinvertRelationPath
- the path of the invert relationreferencingContentIds
- the id of the contents that reference the data holdercontext
- the value's context- Returns:
true
if the given data holder has been modified,false
otherwise
-
removeInvertRelation
public static boolean removeInvertRelation(ModifiableContent referencedContent, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
Removes the invert relation to the givenDataHolder
- Parameters:
referencedContent
- the content where to remove the invert relationinvertRelationPath
- the path of the invert relationreferencingContentIds
- the id of the contents that do not reference the given content anymorecontext
- the value's context- Returns:
true
if the referenced content has been modified,false
otherwise
-
_removeSingleInvertRelation
private static boolean _removeSingleInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
-
-