Beispielcode
Im Beispiel ist CustomChangeOrder ein modellierter Untertyp von WTChangeOrder2. CustomChangeOrder wird geändert, um das Rich Text-Attribut longDescription einzuschließen. Außerdem werden das einfache Textattribut myAttribute und das entsprechende Rich Text-Attribut myLongAttribute im modellierten Untertyp hinzugefügt. Um die einfachen Textbeschreibungsattribute, wie description und myAttribute, in Rich Text-Beschreibungsattribute, wie longDescription und myLongAttribute, zu konvertieren, wird der Delegate CustomChangeOrderDelegate wie folgt erzeugt:
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;
}
}
Die Klasse CustomChangeOrderDelegate wird wie folgt registriert:
<service context="default" name="wt.change2.converter.richtext.ChangeRichTextConverterDelegate">
<Option serviceClass="com.myCompany.CustomChangeOrderDelegate"
requestor="wt.change2.ChangeItem"
selector="customChangeOrder"
cardinality="singleton"/>
</Service>
War dies hilfreich?