Package org.ametys.core.model.type
Class AbstractElementType<T>
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.core.model.type.AbstractModelItemType
-
- org.ametys.core.model.type.AbstractElementType<T>
-
- Type Parameters:
T
- Type of the element value
- All Implemented Interfaces:
ElementType<T>
,ModelItemType
,LogEnabled
,PluginAware
,Component
,Serviceable
- Direct Known Subclasses:
AbstractBinaryElementType
,AbstractBooleanElementType
,AbstractContentElementType
,AbstractDateElementType
,AbstractDateTimeElementType
,AbstractDoubleElementType
,AbstractFileElementType
,AbstractGeocodeElementType
,AbstractLongElementType
,AbstractMultilingualStringElementType
,AbstractReferenceElementType
,AbstractRichTextElementType
,AbstractStringElementType
,AbstractUserElementType
public abstract class AbstractElementType<T> extends AbstractModelItemType implements ElementType<T>, Component, Serviceable
Abstract class for element types
-
-
Field Summary
Fields Modifier and Type Field Description protected JSONUtils
_jsonUtils
The JSON conversion utilities.private Class<T>
_type
private Class<T[]>
_typeArray
-
Constructor Summary
Constructors Constructor Description AbstractElementType()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Stream<Triple<DataChangeType,DataChangeTypeDetail,String>>
_compareMultipleValues(T[] value1, T[] value2)
Compares the given multiple values and retrieves the changes as a stream ofTriple
s.protected Stream<Triple<DataChangeType,DataChangeTypeDetail,String>>
_compareSingleValues(T value1, T value2)
Compares the given single values and retrieves the changes as a stream ofTriple
s.private boolean
_containsAll(T[] list1, T[] list2)
Check if list1 contains all elements of list2 [A, B, C], [A, A, B, C] will return false [A, A, B, C], [A, A, A, B, C] will return false [A, A, B, C], [A, B, C] will return true [A, B, C, D], [A, B, C] will return trueprotected AttributesImpl
_getValueAttributes(Object value, DataContext context, boolean isEdition)
Retrieves the attributes of the single value to SAXprotected boolean
_isValueFromXMLMultiple(Element element)
Checks if the value in the given node is marked as multipleprivate T[]
_removeAllOnce(T[] list1, T[] list2)
Remove once all elements from list2 in list1 [A, A, B, C], [A, B] will result [A, C]protected T
_singleValueFromXML(Element element, Optional<Object> additionalData)
Retrieves a single typed value from the given DOM nodeprotected Object
_singleValueToJSON(T value, DataContext context)
Convert the single value into a JSON objectprotected void
_singleValueToSAX(ContentHandler contentHandler, String tagName, T value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes)
Generates SAX events for the given single valueprotected boolean
_useJSONForEdition()
Determine if the JSON format has to be used in edition mode for this element typeprotected void
_valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, boolean isEdition)
Generates SAX events for the given valueprotected void
_valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes)
Generates SAX events for the given valueprotected void
_valueToSAXForEdition(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes)
Generates SAX events for the given value in edition mode.T
castValue(Object value)
Cast the given value to an object of the corresponding type.
It may be an untyped String value, or another Object, depending of what the type is actually compatible with.Stream<Triple<DataChangeType,DataChangeTypeDetail,String>>
compareValues(Object value1, Object value2)
Compares the given values and retrieves the changes as a stream ofTriple
s.T
convertValue(Object value)
Try to convert the given value.Class<T>
getManagedClass()
Get the class managed by the implementationClass<T[]>
getManagedClassArray()
Get the class representing an array of managed class by the implementationboolean
isCompatible(Object value)
Checks if the value is compatible with the element typeT
parseConfiguration(Configuration configuration)
Parses the given configuration to get the typed valuevoid
service(ServiceManager manager)
String
toString(T value)
Cast a typed value to a StringObject
valueFromXML(Element parent, String name, Optional<Object> additionalData)
Parses the given DOM node to get the typed valueObject
valueToJSONForClient(Object value, DataContext context)
Convert the value into a JSON object to use client sidevoid
valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context)
Generates SAX events for the given valuevoid
valueToSAXForEdition(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context)
Generates SAX events for the given value in edition mode.-
Methods inherited from class org.ametys.core.model.type.AbstractModelItemType
_getContextAttributes, getId, setPluginInfo
-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.ametys.runtime.model.type.ElementType
fromJSONForClient, isSimple
-
Methods inherited from interface org.ametys.runtime.model.type.ModelItemType
getId, valueToSAX
-
Methods inherited from interface org.ametys.runtime.plugin.component.PluginAware
setPluginInfo
-
-
-
-
Field Detail
-
_jsonUtils
protected JSONUtils _jsonUtils
The JSON conversion utilities.
-
_typeArray
private Class<T[]> _typeArray
-
-
Constructor Detail
-
AbstractElementType
public AbstractElementType()
-
-
Method Detail
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
toString
public String toString(T value)
Description copied from interface:ElementType
Cast a typed value to a String- Specified by:
toString
in interfaceElementType<T>
- Parameters:
value
- the value to cast- Returns:
- the String representation of the value
-
valueToJSONForClient
public Object valueToJSONForClient(Object value, DataContext context)
Description copied from interface:ElementType
Convert the value into a JSON object to use client side- Specified by:
valueToJSONForClient
in interfaceElementType<T>
- Parameters:
value
- the value to convertcontext
- The context of the data to convert- Returns:
- The value as JSON
-
_singleValueToJSON
protected Object _singleValueToJSON(T value, DataContext context)
Convert the single value into a JSON object- Parameters:
value
- the value to convertcontext
- The context of the data to convert- Returns:
- The value as JSON
-
parseConfiguration
public T parseConfiguration(Configuration configuration) throws ConfigurationException
Description copied from interface:ElementType
Parses the given configuration to get the typed value- Specified by:
parseConfiguration
in interfaceElementType<T>
- Parameters:
configuration
- the configuration to parse- Returns:
- The typed value in the configuration
- Throws:
ConfigurationException
- if an error occurs while parsing the configuration
-
valueFromXML
public Object valueFromXML(Element parent, String name, Optional<Object> additionalData) throws TransformerException, IOException
Description copied from interface:ElementType
Parses the given DOM node to get the typed value- Specified by:
valueFromXML
in interfaceElementType<T>
- Parameters:
parent
- the DOM element containing the valuename
- the name of the item to readadditionalData
- additional data needed to get the typed value- Returns:
- the value
- Throws:
TransformerException
- if an error occurs while parsing the DOM nodeIOException
- if an error occurs while parsing a value using the I/O API
-
_isValueFromXMLMultiple
protected boolean _isValueFromXMLMultiple(Element element) throws TransformerException
Checks if the value in the given node is marked as multiple- Parameters:
element
- the DOM element- Returns:
true
if the value is marked as multiple,false
otherwise- Throws:
TransformerException
- if an error occurs while parsing the DOM node
-
_singleValueFromXML
protected T _singleValueFromXML(Element element, Optional<Object> additionalData) throws TransformerException, IOException
Retrieves a single typed value from the given DOM node- Parameters:
element
- the DOM element containing the single valueadditionalData
- additional data needed to get the typed value- Returns:
- the value
- Throws:
TransformerException
- if an error occurs while parsing the DOM nodeIOException
- if an error occurs while parsing a value using the I/O API
-
valueToSAXForEdition
public void valueToSAXForEdition(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context) throws SAXException, IOException
Description copied from interface:ModelItemType
Generates SAX events for the given value in edition mode. This method exist for legacy purposes because some types have to be saxed as JSON- Specified by:
valueToSAXForEdition
in interfaceModelItemType
- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the value to SAXviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- 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 API
-
valueToSAX
public void valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context) throws SAXException, IOException
Description copied from interface:ModelItemType
Generates SAX events for the given value- Specified by:
valueToSAX
in interfaceModelItemType
- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the value to SAXviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- 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 API
-
_valueToSAX
protected void _valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, boolean isEdition) throws SAXException, IOException
Generates SAX events for the given value- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the value to SAXviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- 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 API
-
_valueToSAXForEdition
protected void _valueToSAXForEdition(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes) throws SAXException, IOException
Generates SAX events for the given value in edition mode. This default implementation generates SAX event using thevalueToJSONForClient(Object, DataContext)
method- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the value to SAXviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- The context of the data to SAXattributes
- the attributes for the SAX events to generate- Throws:
SAXException
- if an error occurs during the SAX events generationIOException
- if an error occurs while reading a value using the I/O API
-
_valueToSAX
protected void _valueToSAX(ContentHandler contentHandler, String tagName, Object value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes) throws SAXException, IOException
Generates SAX events for the given value- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the value to SAXviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- The context of the data to SAXattributes
- the attributes for the SAX events to generate- Throws:
SAXException
- if an error occurs during the SAX events generationIOException
- if an error occurs while reading a value using the I/O API
-
_singleValueToSAX
protected void _singleValueToSAX(ContentHandler contentHandler, String tagName, T value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes) throws SAXException, IOException
Generates SAX events for the given single value- Parameters:
contentHandler
- theContentHandler
that will receive the SAX eventstagName
- the tag name of the SAX event to generate.value
- the single value to SAXattributes
- the attributes for the SAX event to generateviewItem
- The optional view item corresponding item that is currently saxed. This view item gives context for the SAX event that will be generated here.context
- 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 API
-
_getValueAttributes
protected AttributesImpl _getValueAttributes(Object value, DataContext context, boolean isEdition)
Retrieves the attributes of the single value to SAX- Parameters:
value
- the single valuecontext
- The context of the data to SAXisEdition
-true
if SAX events are generated in edition mode,false
otherwise- Returns:
- the attributes
-
_useJSONForEdition
protected boolean _useJSONForEdition()
Determine if the JSON format has to be used in edition mode for this element type- Returns:
true
if the JSON format has to be used,false
otherwise
-
compareValues
public Stream<Triple<DataChangeType,DataChangeTypeDetail,String>> compareValues(Object value1, Object value2) throws IOException
Description copied from interface:ElementType
Compares the given values and retrieves the changes as a stream ofTriple
s. ATriple
contains:- the general type of the change (added, modified or removed) as a
DataChangeType
, - some details about this change if possible (after or before for a date, more or less for a number, ...) as a
DataChangeTypeDetail
- The data concerned by this change if not the element itself (or an empty String)
- Specified by:
compareValues
in interfaceElementType<T>
- Parameters:
value1
- the 1st valuevalue2
- the 2nd value- Returns:
- the changes between the two given values as a stream of
Triple
. Retrieves an empty stream if there is no change - Throws:
IOException
- if an error occurs while comparing values using the I/O API
- the general type of the change (added, modified or removed) as a
-
_compareMultipleValues
protected Stream<Triple<DataChangeType,DataChangeTypeDetail,String>> _compareMultipleValues(T[] value1, T[] value2) throws IOException
Compares the given multiple values and retrieves the changes as a stream ofTriple
s. TheTriple
s contains:- the general type of the change (added, modified or removed) as a
DataChangeType
, - some details about this change if possible (after or before for a date, more or less for a number, ...) as a
DataChangeTypeDetail
- The data concerned by this change if not the element itself (or an empty String)
- Parameters:
value1
- the 1st multiple valuevalue2
- the 2nd multiple value- Returns:
- the changes between the two given multiple values as a stream of
Triple
. Retrieves an empty stream if there is no change - Throws:
IOException
- if an error occurs while comparing values using the I/O API
- the general type of the change (added, modified or removed) as a
-
_compareSingleValues
protected Stream<Triple<DataChangeType,DataChangeTypeDetail,String>> _compareSingleValues(T value1, T value2) throws IOException
Compares the given single values and retrieves the changes as a stream ofTriple
s. TheTriple
contains:- the general type of the change (added, modified or removed) as a
DataChangeType
, - some details about this change if possible (after or before for a date, more or less for a number, ...) as a
DataChangeTypeDetail
- The data concerned by this change if not the element itself (or an empty String)
- Parameters:
value1
- the 1st single valuevalue2
- the 2nd single value- Returns:
- the changes between the two given single values as a stream of
Triple
. Retrieves an empty stream if there is no change - Throws:
IOException
- if an error occurs while comparing values using the I/O API
- the general type of the change (added, modified or removed) as a
-
_removeAllOnce
private T[] _removeAllOnce(T[] list1, T[] list2)
Remove once all elements from list2 in list1 [A, A, B, C], [A, B] will result [A, C]- Parameters:
list1
- origin listlist2
- list of objects to remove- Returns:
- a new list with removed objects
-
_containsAll
private boolean _containsAll(T[] list1, T[] list2)
Check if list1 contains all elements of list2 [A, B, C], [A, A, B, C] will return false [A, A, B, C], [A, A, A, B, C] will return false [A, A, B, C], [A, B, C] will return true [A, B, C, D], [A, B, C] will return true- Parameters:
list1
- complete listlist2
- elements to check- Returns:
- true if all elements in list2 are once in list1
-
isCompatible
public boolean isCompatible(Object value)
Description copied from interface:ElementType
Checks if the value is compatible with the element type- Specified by:
isCompatible
in interfaceElementType<T>
- Parameters:
value
- the value to check- Returns:
true
if the value matches the current type,false
otherwise
-
castValue
public T castValue(Object value) throws BadItemTypeException
Description copied from interface:ElementType
Cast the given value to an object of the corresponding type.
It may be an untyped String value, or another Object, depending of what the type is actually compatible with.- Specified by:
castValue
in interfaceElementType<T>
- Parameters:
value
- the value to cast- Returns:
- An object of the parameterized type representing the given value. Returns null if value cannot be cast
- Throws:
BadItemTypeException
- if the String value can't be cast to the type
-
convertValue
public T convertValue(Object value)
Try to convert the given value.- Parameters:
value
- the value to convert- Returns:
- the converted value or null if it's not possible.
-
getManagedClass
public Class<T> getManagedClass()
Description copied from interface:ElementType
Get the class managed by the implementation- Specified by:
getManagedClass
in interfaceElementType<T>
- Returns:
- The class managed (T.class)
-
getManagedClassArray
public Class<T[]> getManagedClassArray()
Description copied from interface:ElementType
Get the class representing an array of managed class by the implementation- Specified by:
getManagedClassArray
in interfaceElementType<T>
- Returns:
- The class managed (T[].class)
-
-