public abstract class AbstractXmlConverter extends Object implements XmlConverter
Generated subclasses will call isRoleIncluded(String, Object)
and isAttributeIncluded(String, Object) to check if a role or
attribute should be included in the generated XML Document. This behaviour
can be used in two ways to add custom filters;
The obvious way is to override these methods, and take care of filtering in the subclass. However, there is support in the default implementation for adding filters, and here is a small example that shows how to utilize this.
Lets say we wish exclude a role named "Artist", and that we also wish to
exclude the "email" atributes of role "Customer" in an object selection named
"OrderOs". We then need to add the following code to the constructor of the
generated class OrderOsXmlConverter;
SimpleFilter filter = new SimpleFilter();
filter.addExcludedRoleName("Artist");
filter.addExcludedAttributeName("Customer.email");
addAttributeFilter(filter);
addRoleFilter(filter);
Have a look at AttributeFilter, RoleFilter and
SimpleFilter for a better understanding of
how this works.
The generated subclass will call
convertObjectToString(Object, OSConvertContext) for all included attributes
to convert them to a String. This method can be overridden to add custom
value convertion. However, there is support for adding custom value
converters for attributes in the default implementation. The following
example will show how to take advantage of this.
Lets say we wish to convert all attributes with name "specialPrice" from
an enumeration ({FullPrice, MediumPrice, LowPrice}) to the
discounted price in the OrderOs object selection. We then need to add the
following lines to the constructor;
ConverterRegistry cReg = new ConverterRegistry();
cReg.registerGlobalConverterForAttribute("specialPrice", new SimpleValueConverter {
public String convertToString(Object value, ConvertContext context) {
Record record = (Record) context.getDomainObject();
switch (record.getSpecialPrice().toValue()) {
case EPriceCategory.LowPrice:
return "" + (record.getPrice() * 0.50);
case EPriceCategory.MediumPrice:
return "" + (record.getPrice() * 0.75);
default:
return "" + record.getPrice();
}
}
});
setConverterRegistry(cReg);
The idea behind this approach is to gather all value convertions (in XML
context) for an application in as few places as possible. For instance, an
application needs only to create one instance of ConverterRegistry
. That registry can contain convertion logic for all object selection
used in the application.
Fields in the object selection of g9 type DATE, TIME or TIMESTAMP are treated differently. The value is given as a pre-formatted ISO-8601 String. The raw value is available through convertContext.getDomainObject().getXXX().
Have a look at @ValueConverter and ConverterRegistry to
get a better understanding of how to add custom value converters to XML
conversion.
| Modifier and Type | Field and Description |
|---|---|
protected ArrayList<AttributeFilter> |
attributeFilters
List of all attribute filters
|
protected ConverterRegistry |
converterRegistry
Registry of converters
|
protected ArrayList<RoleFilter> |
roleFilters
List of all role filters
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractXmlConverter()
Default constructor
|
| Modifier and Type | Method and Description |
|---|---|
protected AbstractXmlConverter |
addAttributeFilter(AttributeFilter filter)
Adds a filter that will be called for all attributes
|
protected AbstractXmlConverter |
addRoleFilter(RoleFilter filter)
Adds a filter that will be called for all roles
|
abstract Document |
convert(ObjectSelection os,
ClientContext ctx)
Generates a Document based on the supplied ObjectSelection, and any
previously added filters and converters.
|
protected String |
convertObjectToString(Object value,
OSConvertContext context)
Gets a converter from the converter registry, and applies it to the
value in the given context, and returns the result.
|
protected boolean |
isAttributeIncluded(String attributeName,
Object domainObject)
Returns false if an added filter dictates that the given attribute in
the given domain object should be removed.
|
protected boolean |
isRoleIncluded(String roleName,
Object domainObject)
Returns false if an added filter dictates that the given role of the
given domainObject should be excluded.
|
protected Node |
makeAttributeNode(Object attrValue,
Document doc,
OSConvertContext oSConvertContext)
Helper function that creates a document node from an atribute
|
protected void |
setConverterRegistry(ConverterRegistry converterRegistry)
Sets the converter registry to use to get ValueConverters for attributes.
|
protected ArrayList<AttributeFilter> attributeFilters
protected ArrayList<RoleFilter> roleFilters
protected ConverterRegistry converterRegistry
public abstract Document convert(ObjectSelection os, ClientContext ctx)
XmlConverterconvert in interface XmlConverteros - The ObjectSelection to convertctx - The client contextprotected AbstractXmlConverter addRoleFilter(RoleFilter filter)
filter - The RoleFilter to addprotected AbstractXmlConverter addAttributeFilter(AttributeFilter filter)
filter - The AttributeFilter to addprotected void setConverterRegistry(ConverterRegistry converterRegistry)
converterRegistry - The ConverterRegistry to useprotected boolean isRoleIncluded(String roleName, Object domainObject)
roleName - Name of the roledomainObject - The domain objectprotected boolean isAttributeIncluded(String attributeName, Object domainObject)
attributeName - Attribute namedomainObject - Domain objectprotected String convertObjectToString(Object value, OSConvertContext context)
value - The value Object to convertcontext - The OSConvertContextprotected Node makeAttributeNode(Object attrValue, Document doc, OSConvertContext oSConvertContext)
attrValue - Attribute valuedoc - The DOcument the attribute is to be included inoSConvertContext - Convert contextCopyright © 2006–2020 Esito AS. All rights reserved.