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 interfaceContentDataHelper.InvertRelationManagerInvert relation manager (to add or remove a relation on a content
-
Field Summary
Fields Modifier and Type Field Description private ContentTypeExtensionPoint_cTypeExtensionPointprivate AmetysObjectResolver_resolverstatic StringROLEAvalon 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 booleanaddInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)Adds the invert relation to the givenDataHolderstatic 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 givenModelAwareDataHolderby traversing aViewwith the values to be written.
The goal is to find content attributes and to extract added and removed values.static StringgetContentIdFromContentData(ModelAwareDataHolder dataHolder, String dataPath)Retrieves the content identifier of a content datastatic StringgetContentIdFromContentData(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 aListfrom a multiple content datastatic Stream<String>getContentIdsStreamFromMultipleContentData(ModelAwareDataHolder dataHolder, String dataPath)Retrieves aStreamof the content identifiers from a multiple content datastatic booleanisMultipleContentDataEmpty(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.InvertRelationManagerstatic 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.InvertRelationManagerstatic booleanremoveInvertRelation(ModifiableContent referencedContent, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)Removes the invert relation to the givenDataHoldervoidservice(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:
servicein 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 aListfrom a multiple content data- Parameters:
dataHolder- data holder that contains the multiple content datadataPath- path to the multiple content data- Returns:
- a
Listcontaining 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 aStreamof 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
Streamof 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 givenModelAwareDataHolderby traversing aViewwith 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 sourceModifiableContentvalues- 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- theModelAwareDataHolderto 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
Paircontaining the result of the check (Booleanpart 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- aSetcontaining the referenced contents to manage (add or remove the relation)referencingContentId- the id of the content referencing the given contentsinvertRelationManager- theContentDataHelper.InvertRelationManagerto use (to add or remove the invert relations)alreadyModifiedContents- aMapof 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
Mapcontaining 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- aMapcontaining the referenced contents to manage (add or remove the relation) and the id of the content referencing this contentinvertRelationManager- theContentDataHelper.InvertRelationManagerto use (to add or remove the invert relations)alreadyModifiedContents- aMapof 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
Mapcontaining 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:
trueif the given data holder has been modified,falseotherwise
-
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:
trueif the referenced content has been modified,falseotherwise
-
_removeSingleInvertRelation
private static boolean _removeSingleInvertRelation(ModifiableModelAwareDataHolder dataHolder, String invertRelationPath, Collection<String> referencingContentIds, ValueContext context)
-
-