Search in sources :

Example 1 with IRulesEngine

use of org.jaffa.rules.IRulesEngine in project jaffa-framework by jaffa-projects.

the class StringHelper method formatDescription.

/**
 * This method is invoked in cases where 'description' field is appended to a 'code' field.
 * This method formats the input dexcriptionField based on the passed layout.
 * The layout can be passed directly or can be determined from the parameters domainClassWithPackage and domainField, through the appropriate FieldMetaData object.
 * It then truncates the formatted String to the input limit. If the String is truncated, then the truncateIndicator will be appended.
 * No truncation will be performed if the limit <= 0
 * Finally the String will be packaged between the beginMarker and endMarker.
 * If the toHtml flag is true, then the result will made HTML safe.
 * @param field The description of the field.
 * @param layout The layout, if any to be used for formatting
 * @param domainClassWithPackage The domainClass to determine the FieldMetaData object, to get a handle on the layout.
 * @param domainField The domainField to determine the FieldMetaData object, to get a handle on the layout.
 * @param toHtml if true, then the output will be converted to HTML.
 * @param beginMarker The marker at the start of the output. Default is ' ('
 * @param endMarker The marker at the end of the output. Default is ')'
 * @param limit The limit for the formatted decription. Default is 25.
 * @param truncateIndicator The string to append tot he formatted description, if exceeds the limit and is truncated.
 * @return the formatted string packed between the markers. An empty String will be returned, in case the input field is null.
 */
public static String formatDescription(Object field, String layout, String domainClassWithPackage, String domainField, boolean toHtml, String beginMarker, String endMarker, int limit, String truncateIndicator) {
    String out = null;
    if (field == null)
        out = "";
    else {
        StringBuffer buf = new StringBuffer();
        String str = null;
        if (layout == null && domainClassWithPackage != null && domainField != null) {
            try {
                IRulesEngine rulesEngine = RulesEngineFactory.getRulesEngine();
                IPropertyRuleIntrospector propertyRuleIntrospector = null;
                if (rulesEngine != null)
                    propertyRuleIntrospector = rulesEngine.getPropertyRuleIntrospector(Class.forName(domainClassWithPackage), domainField);
                // Wrap the propertyRuleIntrospector with the FieldMetaData
                FieldMetaData meta = PersistentHelper.getFieldMetaData(domainClassWithPackage, domainField);
                propertyRuleIntrospector = new PropertyRuleIntrospectorUsingFieldMetaData(propertyRuleIntrospector, meta);
                str = propertyRuleIntrospector.format(field);
            } catch (Exception e) {
                String s = "Exception thrown while formatting the field " + domainField;
                log.error(s, e);
                throw new RuntimeException(s, e);
            }
        } else if (layout != null)
            str = Formatter.format(field, layout);
        else
            str = Formatter.format(field);
        // Build the Output
        if (str != null && str.length() > 0) {
            if (beginMarker != null)
                buf.append(beginMarker);
            // Truncate the formatted string, if needed, and append the truncateIndicator
            if (limit > 0 && str.length() > limit) {
                buf.append(str.substring(0, limit - 1));
                if (truncateIndicator != null)
                    buf.append(truncateIndicator);
            } else {
                buf.append(str);
            }
            if (endMarker != null)
                buf.append(endMarker);
        }
        out = buf.toString();
        if (toHtml)
            out = org.jaffa.util.StringHelper.convertToHTML(out);
    }
    return out;
}
Also used : IPropertyRuleIntrospector(org.jaffa.rules.IPropertyRuleIntrospector) FieldMetaData(org.jaffa.metadata.FieldMetaData) PropertyRuleIntrospectorUsingFieldMetaData(org.jaffa.metadata.PropertyRuleIntrospectorUsingFieldMetaData) PropertyRuleIntrospectorUsingFieldMetaData(org.jaffa.metadata.PropertyRuleIntrospectorUsingFieldMetaData) IRulesEngine(org.jaffa.rules.IRulesEngine)

Aggregations

FieldMetaData (org.jaffa.metadata.FieldMetaData)1 PropertyRuleIntrospectorUsingFieldMetaData (org.jaffa.metadata.PropertyRuleIntrospectorUsingFieldMetaData)1 IPropertyRuleIntrospector (org.jaffa.rules.IPropertyRuleIntrospector)1 IRulesEngine (org.jaffa.rules.IRulesEngine)1