Class DataHolderHelper
- java.lang.Object
-
- org.ametys.plugins.repository.data.holder.impl.DataHolderHelper
-
- All Implemented Interfaces:
Disposable
,Component
,Serviceable
public final class DataHolderHelper extends Object implements Component, Serviceable, Disposable
Helper for implementations of data holder
-
-
Field Summary
Fields Modifier and Type Field Description private static ExternalizableDataProviderExtensionPoint
_externalizableDataProviderEP
private static JSONUtils
_jsonUtils
static Pattern
REPEATER_ENTRY_PATTERN
Pattern for repeater entry : entryName[i]static String
ROLE
The Avalon role
-
Constructor Summary
Constructors Constructor Description DataHolderHelper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <T> Map<String,T>
_findItemsByType(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, String type, String prefix)
private static boolean
_hasExternalizableValue(ModelAwareDataHolder dataHolder, String dataName, DataContext context)
private static boolean
_hasValue(ModelAwareDataHolder dataHolder, String dataName, DataContext context)
private static void
_saxExternalizableValuesAsJson(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, String dataName, RepositoryElementType type, DataContext context)
static void
commentsToSAX(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, ViewItemAccessor viewItemAccessor, String dataPath)
Generates SAX events for the data comments in the given viewstatic void
copyTo(DataHolder source, ModifiableDataHolder destination)
Copies the sourceDataHolder
to the givenModifiableDataHolder
destination.static void
copyTo(DataHolder source, ModifiableDataHolder destination, String name)
Copy the data name of the sourceDataHolder
to the givenModifiableDataHolder
destination.static ValueContext
createValueContextFromSynchronizationContext(ModelAwareDataHolder dataHolder, String dataPath, SynchronizationContext synchronizationContext)
Creates a value context from the givenSynchronizationContext
static Map<String,Object>
dataToMap(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, DataContext context)
Returns all data of the given DataHolder as a typed-values Map.static void
dataToSAX(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, ViewItemAccessor viewItemAccessor, DataContext context, boolean isEdition)
Generates SAX events for the data in the given view in the currentDataHolder
static void
dataToSAX(ModelLessDataHolder dataHolder, ContentHandler contentHandler, String relativeDataPath, DataContext context)
Generates SAX events for data at the given relative path in thisDataHolder
static void
dataToSAX(ModelLessDataHolder dataHolder, ContentHandler contentHandler, DataContext context)
Generates SAX events for data contained in thisDataHolder
void
dispose()
static <T> Map<String,T>
findItemsByType(ModelAwareDataHolder dataHolder, String type)
Find all data of the given type on aModelAwareDataHolder
, including in composite or repeater entries.static <T> Map<String,T>
findItemsByType(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, String type)
Find all data of the given type on aModelAwareDataHolder
, including in composite or repeater entries.static ExternalizableDataProviderExtensionPoint
getExternalizableDataProviderExtensionPoint()
Retrieve theExternalizableDataProviderExtensionPoint
static RepeaterEntry
getRepeaterEntry(ModelAwareDataHolder dataHolder, String repeaterName, int entryPosition)
Retrieves the repeater entry at the given positionstatic Pair<String,Integer>
getRepeaterNameAndEntryPosition(String pathSegment)
Retrieves the pair of repeater name and entry position of the given path segment Returnnull
if the given path does not represent a repeater entrystatic <T> T
getValue(ModelAwareDataHolder dataHolder, String dataPath, ValueContext context)
Retrieves the value of the data at the given pathstatic Object
getValueToValidate(Object value)
Returns the real value to validate, unwrapping it if the given Object is aSynchronizableValue
.static boolean
hasRepeaterEntry(ModelAwareDataHolder dataHolder, String repeaterName, int entryPosition)
Checks if there is a repeater entry at the given positionstatic boolean
hasValue(ModelAwareDataHolder dataHolder, String dataPath, ValueContext context)
Checks if there is a value for the data at the given pathstatic boolean
isMultiple(ModelItem item)
Check if the givenModelItem
is multivalued, either being itself mutlivalued or part of a repeaterstatic boolean
isRepeaterEntryPath(String path)
Test if the path is a repeater entry path (for example entries[1])static void
removeValue(ModifiableModelAwareDataHolder dataHolder, String dataPath, ValueContext context)
Removes the stored value of the data at the given pathvoid
service(ServiceManager manager)
static void
setValue(ModifiableModelAwareDataHolder dataHolder, String dataPath, Object value, ValueContext context)
Sets the value of the data at the given path
-
-
-
Field Detail
-
REPEATER_ENTRY_PATTERN
public static final Pattern REPEATER_ENTRY_PATTERN
Pattern for repeater entry : entryName[i]
-
_jsonUtils
private static JSONUtils _jsonUtils
-
_externalizableDataProviderEP
private static ExternalizableDataProviderExtensionPoint _externalizableDataProviderEP
-
-
Constructor Detail
-
DataHolderHelper
public DataHolderHelper()
-
-
Method Detail
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
dispose
public void dispose()
- Specified by:
dispose
in interfaceDisposable
-
hasRepeaterEntry
public static boolean hasRepeaterEntry(ModelAwareDataHolder dataHolder, String repeaterName, int entryPosition) throws BadItemTypeException
Checks if there is a repeater entry at the given position- Parameters:
dataHolder
- data holder that contains the repeater entry. The data holder must be the direct parent of the repeaterrepeaterName
- the name of the repeaterentryPosition
- the position of the entry- Returns:
true
if there is an entry at the given position,false
otherwise- Throws:
BadItemTypeException
- if the value stored in the repository for the given repeater name is not a repeater
-
getRepeaterEntry
public static RepeaterEntry getRepeaterEntry(ModelAwareDataHolder dataHolder, String repeaterName, int entryPosition) throws BadItemTypeException
Retrieves the repeater entry at the given position- Parameters:
dataHolder
- data holder that contains the repeater entry. The data holder must be the direct parent of the repeaterrepeaterName
- the name of the repeaterentryPosition
- the position of the entry- Returns:
- the repeater entry
- Throws:
BadItemTypeException
- if the value stored in the repository for the given repeater name is not a repeater
-
isRepeaterEntryPath
public static boolean isRepeaterEntryPath(String path)
Test if the path is a repeater entry path (for example entries[1])- Parameters:
path
- the path representing the repeater entry- Returns:
- true if the pathSegment is a repeater entry path
-
getRepeaterNameAndEntryPosition
public static Pair<String,Integer> getRepeaterNameAndEntryPosition(String pathSegment)
Retrieves the pair of repeater name and entry position of the given path segment Returnnull
if the given path does not represent a repeater entry- Parameters:
pathSegment
- the path segment representing the repeater entry- Returns:
- the pair of repeater name and entry position
-
hasValue
public static boolean hasValue(ModelAwareDataHolder dataHolder, String dataPath, ValueContext context) throws IllegalArgumentException, BadDataPathCardinalityException
Checks if there is a value for the data at the given path- Parameters:
dataHolder
- the data holderdataPath
- path of the datacontext
- the context of the value to check- Returns:
true
if the data at the given path is defined by the model, if there is a value for the data (even empty) and if the type of this value matches the type of the definition.false
otherwise- Throws:
IllegalArgumentException
- if the given data path is null or emptyBadDataPathCardinalityException
- if the definition of a part of the data path is multiple. Only the last part can be multiple
-
getValue
public static <T> T getValue(ModelAwareDataHolder dataHolder, String dataPath, ValueContext context) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
Retrieves the value of the data at the given path- Type Parameters:
T
- type of the value to retrieve- Parameters:
dataHolder
- the data holderdataPath
- path of the datacontext
- the context of the value to retrieve- Returns:
- the value of the data or
null
if not exists or is empty. The object returned may be of a generic class defined by the storage (if the model is unknown). For example, an url may be returned as a String. - Throws:
IllegalArgumentException
- if the given data path is null or emptyUndefinedItemPathException
- if the given data path is not defined by the modelBadItemTypeException
- if the type defined by the model doesn't match the type of the stored valueBadDataPathCardinalityException
- if the definition of a part of the data path is multiple. Only the last part can be multiple
-
setValue
public static void setValue(ModifiableModelAwareDataHolder dataHolder, String dataPath, Object value, ValueContext context) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, BadDataPathCardinalityException
Sets the value of the data at the given path- Parameters:
dataHolder
- the data holderdataPath
- path of the datavalue
- the value to set. Givenull
to empty the value.context
- context of the data to set- Throws:
IllegalArgumentException
- if the given data path is null or emptyUndefinedItemPathException
- if the given data path is not defined by the modelBadItemTypeException
- if the type defined by the model doesn't match the given value to setBadDataPathCardinalityException
- if the definition of a part of the data path is multiple. Only the last part can be multiple
-
removeValue
public static void removeValue(ModifiableModelAwareDataHolder dataHolder, String dataPath, ValueContext context) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException, UnknownDataException, BadDataPathCardinalityException
Removes the stored value of the data at the given path- Parameters:
dataHolder
- the data holderdataPath
- path of the datacontext
- context of the data to remove- Throws:
IllegalArgumentException
- if the given data path is null or emptyUnknownDataException
- if the value at the given data path does not existBadItemTypeException
- if the value of the parent of the given path is not an item containerUndefinedItemPathException
- if the given data path is not defined by the modelBadDataPathCardinalityException
- if the definition of a part of the data path is multiple. Only the last part can be multiple
-
createValueContextFromSynchronizationContext
public static ValueContext createValueContextFromSynchronizationContext(ModelAwareDataHolder dataHolder, String dataPath, SynchronizationContext synchronizationContext)
Creates a value context from the givenSynchronizationContext
- Parameters:
dataHolder
- the data holderdataPath
- the path of the value needing contextsynchronizationContext
- theSynchronizationContext
- Returns:
- the created
ValueContext
-
copyTo
public static void copyTo(DataHolder source, ModifiableDataHolder destination)
Copies the sourceDataHolder
to the givenModifiableDataHolder
destination.- Parameters:
source
- the sourceDataHolder
destination
- theModifiableDataHolder
destination
-
copyTo
public static void copyTo(DataHolder source, ModifiableDataHolder destination, String name)
Copy the data name of the sourceDataHolder
to the givenModifiableDataHolder
destination.- Parameters:
source
- the sourceDataHolder
destination
- theModifiableDataHolder
destinationname
- the name of the data
-
dataToSAX
public static void dataToSAX(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, ViewItemAccessor viewItemAccessor, DataContext context, boolean isEdition) throws SAXException, IOException, BadItemTypeException
Generates SAX events for the data in the given view in the currentDataHolder
- Parameters:
dataHolder
- theModelAwareDataHolder
to SAXcontentHandler
- theContentHandler
that will receive the SAX eventsviewItemAccessor
- theViewItemAccessor
referencing the items for which generate SAX eventscontext
- The context of the data to SAXisEdition
-true
if SAX events are generated in edition mode,false
otherwise- Throws:
SAXException
- if an error occurs during the SAX events generationIOException
- if an error occurs while reading a value using the I/O APIBadItemTypeException
- if the saxed value's type does not matches the stored data
-
_saxExternalizableValuesAsJson
private static void _saxExternalizableValuesAsJson(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, String dataName, RepositoryElementType type, DataContext context) throws SAXException
- Throws:
SAXException
-
commentsToSAX
public static void commentsToSAX(ModelAwareDataHolder dataHolder, ContentHandler contentHandler, ViewItemAccessor viewItemAccessor, String dataPath) throws SAXException
Generates SAX events for the data comments in the given view- Parameters:
dataHolder
- theModelAwareDataHolder
contentHandler
- theContentHandler
that will receive the SAX eventsviewItemAccessor
- theViewItemAccessor
referencing the items for which generate SAX eventsdataPath
- the data path corresponding of the current data holder. Can benull
or empty.- Throws:
SAXException
- if an error occurs during the SAX events generation
-
dataToSAX
public static void dataToSAX(ModelLessDataHolder dataHolder, ContentHandler contentHandler, DataContext context) throws SAXException, IOException, UnknownTypeException, NotUniqueTypeException
Generates SAX events for data contained in thisDataHolder
- Parameters:
dataHolder
- theModelLessDataHolder
to SAXcontentHandler
- theContentHandler
that will receive the SAX eventscontext
- The context of the data to SAX- Throws:
SAXException
- if an error occurs during the SAX events generationIOException
- if an error occurs while reading a value using the I/O APIUnknownTypeException
- if there is no compatible type with the saxed valueNotUniqueTypeException
- if there are many compatible types (there is no way to determine which type is the good one) with the saxed value
-
dataToSAX
public static void dataToSAX(ModelLessDataHolder dataHolder, ContentHandler contentHandler, String relativeDataPath, DataContext context) throws SAXException, IOException, UnknownTypeException, NotUniqueTypeException
Generates SAX events for data at the given relative path in thisDataHolder
- Parameters:
dataHolder
- theModelLessDataHolder
to SAXcontentHandler
- theContentHandler
that will receive the SAX eventsrelativeDataPath
- the path of the data to SAX, relative to the given data holdercontext
- The context of the data to SAX- Throws:
SAXException
- if an error occurs during the SAX events generationIOException
- if an error occurs while reading a value using the I/O APIUnknownTypeException
- if there is no compatible type with the saxed valueNotUniqueTypeException
- if there are many compatible types (there is no way to determine which type is the good one) with the saxed value
-
_hasValue
private static boolean _hasValue(ModelAwareDataHolder dataHolder, String dataName, DataContext context)
-
_hasExternalizableValue
private static boolean _hasExternalizableValue(ModelAwareDataHolder dataHolder, String dataName, DataContext context)
-
dataToMap
public static Map<String,Object> dataToMap(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, DataContext context)
Returns all data of the given DataHolder as a typed-values Map.- Parameters:
dataHolder
- the DataHolder to exportviewItemAccessor
- theViewItemAccessor
referencing the items to include in the resulting Mapcontext
- The context of the data- Returns:
- a Map containing all data.
-
findItemsByType
public static <T> Map<String,T> findItemsByType(ModelAwareDataHolder dataHolder, String type)
Find all data of the given type on aModelAwareDataHolder
, including in composite or repeater entries.- Type Parameters:
T
- the actual type of the requested data.- Parameters:
dataHolder
- the data holder.type
- the type identifier.- Returns:
- a Map with all data paths as keys and corresponding data as values.
-
findItemsByType
public static <T> Map<String,T> findItemsByType(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, String type)
Find all data of the given type on aModelAwareDataHolder
, including in composite or repeater entries.- Type Parameters:
T
- the actual type of the requested data.- Parameters:
dataHolder
- the data holder.viewItemAccessor
- the view items to restrict totype
- the type identifier.- Returns:
- a Map with all data paths as keys and corresponding data as values.
-
_findItemsByType
private static <T> Map<String,T> _findItemsByType(ModelAwareDataHolder dataHolder, ViewItemAccessor viewItemAccessor, String type, String prefix)
-
getExternalizableDataProviderExtensionPoint
public static ExternalizableDataProviderExtensionPoint getExternalizableDataProviderExtensionPoint()
Retrieve theExternalizableDataProviderExtensionPoint
- Returns:
- the
ExternalizableDataProviderExtensionPoint
-
getValueToValidate
public static Object getValueToValidate(Object value)
Returns the real value to validate, unwrapping it if the given Object is aSynchronizableValue
.- Parameters:
value
- the input value- Returns:
- the real value
-
isMultiple
public static boolean isMultiple(ModelItem item)
Check if the givenModelItem
is multivalued, either being itself mutlivalued or part of a repeater- Parameters:
item
- the model item to check- Returns:
- true if the model item is multivalued
-
-