Código de muestra
En el ejemplo dado, CustomChangeOrder es un subtipo modelado de WTChangeOrder2. CustomChangeOrder se modifica para incluir un atributo de texto enriquecido longDescription. Además, el atributo de texto simple myAttribute y el atributo de texto enriquecido correspondiente myLongAttribute se añaden en el subtipo modelado. Para convertir los atributos de descripción de texto simple, como description y myAttribute, a atributos de descripción de texto enriquecido, como longDescription y myLongAttribute, el delegado CustomChangeOrderDelegate se crea como se indica:
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 clase CustomChangeOrderDelegate se registra tal como se indica:
<service context="default" name="wt.change2.converter.richtext.ChangeRichTextConverterDelegate">
<Option serviceClass="com.myCompany.CustomChangeOrderDelegate"
requestor="wt.change2.ChangeItem"
selector="customChangeOrder"
cardinality="singleton"/>
</Service>
¿Fue esto útil?