Search in sources :

Example 1 with FlexParam

use of org.jaffa.flexfields.FlexParam in project jaffa-framework by jaffa-projects.

the class FlexBeanConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to convert the flexParams array as root level properties on the javascript object.
     */
@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    FlexBean flexBean = (FlexBean) data;
    // Where we collect out converted children
    Map ovs = new TreeMap();
    // We need to do this before collecing the children to save recurrsion
    ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
    outctx.put(flexBean, ov);
    try {
        Map properties = getPropertyMapFromObject(flexBean, true, false);
        for (Iterator it = properties.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String name = (String) entry.getKey();
            Property property = (Property) entry.getValue();
            // CUSTOM CODE: Special handling for flexParams
            if ("flexParams".equals(name)) {
                FlexParam[] flexParams = flexBean.getFlexParams();
                if (flexParams != null) {
                    for (FlexParam flexParam : flexParams) {
                        // Instead of the formatted value returned by flexParam.getValue(),
                        // use the original value returned by the flexBean. This will ensure
                        // standard DWR handling for those value.
                        Object value = flexBean.get(flexParam.getName());
                        if (value != null) {
                            // Added check to exclude null fields
                            OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
                            ovs.put(flexParam.getName(), nested);
                        }
                    }
                }
            } else {
                Object value = property.getValue(flexBean);
                if (value != null) {
                    // Added check to exclude null fields
                    OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
                    ovs.put(name, nested);
                }
            }
        }
        // Add the className to the object
        if (flexBean != null) {
            String className = flexBean.getClass().getSimpleName();
            OutboundVariable var = getConverterManager().convertOutbound(className, outctx);
            ovs.put("className", var);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(flexBean.getClass(), ex);
    }
    ov.init(ovs, getJavascript());
    return ov;
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) TreeMap(java.util.TreeMap) FlexBean(org.jaffa.flexfields.FlexBean) MarshallException(org.directwebremoting.extend.MarshallException) FlexParam(org.jaffa.flexfields.FlexParam) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Aggregations

Iterator (java.util.Iterator)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)1 MarshallException (org.directwebremoting.extend.MarshallException)1 OutboundVariable (org.directwebremoting.extend.OutboundVariable)1 Property (org.directwebremoting.extend.Property)1 FlexBean (org.jaffa.flexfields.FlexBean)1 FlexParam (org.jaffa.flexfields.FlexParam)1