Exemple de code
Dans l'exemple indiqué, CustomChangeOrder est un sous-type modélisé de WTChangeOrder2. CustomChangeOrder est modifié pour inclure un attribut de texte enrichi longDescription. Par ailleurs, un attribut de texte simple myAttribute et l'attribut de texte enrichi correspondant myLongAttribute sont ajoutés dans le sous-type modélisé. Pour convertir les attributs de description de texte simple, comme description et myAttribute, en attributs de description de texte enrichi, par exemple longDescription et myLongAttribute, le délégué CustomChangeOrderDelegate est créé comme suit :
package com.myCompany;
public class CustomChangeOrderDelegate extends ChangeItemRichTextConverterDelegate
{
private static final Logger classLogger = LogR.getLogger(ChangeSimpleToRichTextUtility.class.getName());
/**
* This method returns a concrete class, that is a model class, which implements wt.change2.ChangeItem or wt.maturity.PromotionNotice.
*
* @return class for which the delegate is implemented.
*/
@Override
public Class getTargetClass()
{
return CustomChangeOrder.class;
}
/**
* This method returns an unmodifiable map. Each entry in the map contains key as the source attribute, which is the
* simple text, and value as the target attribute, which is the rich text. The map should not be blank.
*
* @return collection of source and destination attributes for conversion
*/
@Override
public Map<String, String> getAttributesForConversion()
{
Map<String, String> srcAndDestAttributesMap = new HashMap<>(2);
srcAndDestAttributesMap.put(CustomChangeOrder.DESCRIPTION, CustomChangeOrder.LONG_DESCRIPTION);
srcAndDestAttributesMap.put(CustomChangeOrder.myAttribute, CustomChangeOrder.LONG_myAttribute);
return Collections.unmodifiableMap(srcAndDestAttributesMap);
}
/**
* This method copies or moves the attributes. The method prevents conversion
* if source attribute value is blank. If cleanSource flag is true, source attribute is set to null.
*
* @param persistable
* @param cleanSource
* @throws WTPropertyVetoException
* @return true if conversion was successful or false if conversion was not performed or was unsuccessful.
*/
@Override
public boolean convert(Persistable persistable, boolean cleanSource) throws WTPropertyVetoException {
boolean isConversionSucessful = false, areSourceAttributesNull = true;
if (persistable instanceof CustomChangeOrder) {
CustomChangeOrder changeNotice = (CustomChangeOrder) persistable;
String description = changeNotice.getDescription();
if (StringUtils.isNotBlank(description)) {
changeNotice.setLongDescription(getRichText(description));
if (cleanSource) {
changeNotice.setDescription(null);
}
isConversionSucessful = true;
areSourceAttributesNull = false;
}
String myAttribute = changeNotice.getMyAttribute();
if (StringUtils.isNotBlank(myAttribute)) {
changeNotice.setLongMyAttribute(getRichText(myAttribute));
if (cleanSource) {
changeNotice.setMyAttribute(null);
}
isConversionSucessful = true;
areSourceAttributesNull = false;
}
if (areSourceAttributesNull && classLogger.isDebugEnabled()) {
classLogger.debug("The source attributes, description and myAttribute, are blank for customChangeOrder:"
+ changeNotice.getNumber() + " " + changeNotice.getName()
+ ". Simple to rich text conversion was not performed for this object.");
}
}
else if (classLogger.isDebugEnabled()) {
classLogger
.debug("The persistable class is not an instance of CustomChangeOrder. The persistable class is:"
+ persistable.getClass().getName()
+ ". Simple to rich text conversion was not performed for this object.");
}
return isConversionSucessful;
}
}
La classe CustomChangeOrderDelegate est enregistrée comme suit :
<service context="default" name="wt.change2.converter.richtext.ChangeRichTextConverterDelegate">
<Option serviceClass="com.myCompany.CustomChangeOrderDelegate"
requestor="wt.change2.ChangeItem"
selector="customChangeOrder"
cardinality="singleton"/>
</Service>