Search in sources :

Example 1 with MarshallException

use of org.directwebremoting.extend.MarshallException in project jaffa-framework by jaffa-projects.

the class CurrencyConverter method convertOutbound.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, 
	 * org.directwebremoting.OutboundContext)
	 */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    Double out = 0.0;
    // Error out if an unsupported class is passed
    if (!(data instanceof Currency)) {
        log.warn("Unsupported input. Class=" + data.getClass() + ", data=" + data);
        throw new MarshallException(data.getClass());
    }
    String currency = data.toString();
    if (currency != null && currency != "") {
        String layout = CurrencyFieldMetaData.getCurrencyFormat();
        NumberFormat fmt;
        if (LocaleContext.getLocale() != null) {
            fmt = NumberFormat.getCurrencyInstance(LocaleContext.getLocale());
        } else {
            fmt = NumberFormat.getCurrencyInstance();
        }
        fmt.setCurrency(java.util.Currency.getInstance(Currency.USD));
        if (layout != null && layout.length() > 0 && fmt instanceof DecimalFormat) {
            ((DecimalFormat) fmt).applyPattern(layout);
        }
        try {
            out = Double.valueOf((fmt.parse(currency)).doubleValue());
            currency = out.toString();
        } catch (ParseException e) {
        }
    }
    return new SimpleOutboundVariable(currency, outctx, true);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) Currency(org.jaffa.datatypes.Currency) DecimalFormat(java.text.DecimalFormat) ParseException(java.text.ParseException) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) NumberFormat(java.text.NumberFormat)

Example 2 with MarshallException

use of org.directwebremoting.extend.MarshallException in project jaffa-framework by jaffa-projects.

the class FlexBeanConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to invoke the setter on the FlexBean for unknown properties.
     */
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        FlexBean bean;
        if (instanceType != null) {
            bean = (FlexBean) instanceType.newInstance();
        } else {
            bean = (FlexBean) paramType.newInstance();
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
        {
            String key = "dynaClass";
            String val = (String) tokens.remove(key);
            Property property = (Property) properties.get(key);
            if (val != null && property != null) {
                // property.getPropertyType();
                Class propType = FlexClass.class;
                String[] split = ParseUtil.splitInbound(val);
                String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
                String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
                InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
                TypeHintContext incc = createTypeHintContext(inctx, property);
                Object output = converterManager.convertInbound(propType, nested, inctx, incc);
                property.setValue(bean, output);
                // The input from the client may merely pass the name of the FlexClass
                // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexBean
                DynaClass flexClass = bean.getDynaClass();
                if (flexClass != null) {
                    flexClass = FlexClass.instance(flexClass.getName());
                    bean = FlexBean.instance((FlexClass) flexClass);
                }
            }
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            Property property = (Property) properties.get(key);
            if (property == null) {
                // CUSTOM CODE: Instead of logging a warning, assume that the inbound token
                // is valid, and create a descriptor for it, such that it invokes the setter
                // on the FlexBean
                Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                property = new FlexDescriptor(key, propType);
            }
            Class propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : DynaClass(org.apache.commons.beanutils.DynaClass) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) FlexBean(org.jaffa.flexfields.FlexBean) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) DynaClass(org.apache.commons.beanutils.DynaClass) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Example 3 with MarshallException

use of org.directwebremoting.extend.MarshallException in project jaffa-framework by jaffa-projects.

the class FlexCriteriaBeanConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to invoke the setter on the FlexCriteriaBean for unknown properties.
     */
@Override
public Object convertInbound(Class paramType, InboundVariable iv, InboundContext inctx) throws MarshallException {
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL)) {
        return null;
    }
    if (!value.startsWith(ProtocolConstants.INBOUND_MAP_START)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    if (!value.endsWith(ProtocolConstants.INBOUND_MAP_END)) {
        throw new MarshallException(paramType, Messages.getString("BeanConverter.FormatError", ProtocolConstants.INBOUND_MAP_START));
    }
    value = value.substring(1, value.length() - 1);
    try {
        FlexCriteriaBean bean;
        if (instanceType != null) {
            bean = (FlexCriteriaBean) instanceType.newInstance();
        } else {
            bean = (FlexCriteriaBean) paramType.newInstance();
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        // CUSTOM CODE: Determine the appropriate FlexClass, so that the properties are formatted to the correct layout
        {
            String key = "dynaClass";
            String val = (String) tokens.remove(key);
            Property property = (Property) properties.get(key);
            if (val != null && property != null) {
                // property.getPropertyType();
                Class propType = FlexClass.class;
                String[] split = ParseUtil.splitInbound(val);
                String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
                String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
                InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
                TypeHintContext incc = createTypeHintContext(inctx, property);
                Object output = converterManager.convertInbound(propType, nested, inctx, incc);
                property.setValue(bean, output);
                // The input from the client may merely pass the name of the FlexClass
                // Recreate the FlexClass to load all it's FlexProperties and then recreate the FlexCriteriaBean
                DynaClass flexClass = bean.getDynaClass();
                if (flexClass != null) {
                    flexClass = FlexClass.instance(flexClass.getName());
                    bean = FlexCriteriaBean.instance((FlexClass) flexClass);
                }
            }
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        for (Iterator it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            Property property = (Property) properties.get(key);
            if (property == null) {
                // CUSTOM CODE: Instead of logging a warning, assume that the inbound token
                // is valid, and create a descriptor for it, such that it invokes the setter
                // on the FlexCriteriaBean
                Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                propType = findCriteriaFieldClass(propType);
                property = new FlexDescriptor(key, propType);
            }
            Class propType = property.getPropertyType();
            String[] split = ParseUtil.splitInbound(val);
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            TypeHintContext incc = createTypeHintContext(inctx, property);
            Object output = converterManager.convertInbound(propType, nested, inctx, incc);
            property.setValue(bean, output);
        }
        return bean;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(paramType, ex);
    }
}
Also used : DynaClass(org.apache.commons.beanutils.DynaClass) TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) DynaClass(org.apache.commons.beanutils.DynaClass) Map(java.util.Map) TreeMap(java.util.TreeMap) Property(org.directwebremoting.extend.Property) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 4 with MarshallException

use of org.directwebremoting.extend.MarshallException in project jaffa-framework by jaffa-projects.

the class FlexCriteriaBeanConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     *
     * Copied from BasicObjectConverter
     *
     * Added custom code to convert the flexCriteriaParams array as root level properties on the javascript object.
     */
@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    FlexCriteriaBean flexCriteriaBean = (FlexCriteriaBean) 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(flexCriteriaBean, ov);
    try {
        Map properties = getPropertyMapFromObject(flexCriteriaBean, 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 flexCriteriaParams
            if ("flexCriteriaParams".equals(name)) {
                FlexCriteriaParam[] flexCriteriaParams = flexCriteriaBean.getFlexCriteriaParams();
                if (flexCriteriaParams != null) {
                    for (FlexCriteriaParam flexCriteriaParam : flexCriteriaParams) {
                        // Instead of the formatted value returned by flexCriteriaParam.getValue(),
                        // use the original value returned by the flexCriteriaBean. This will ensure
                        // standard DWR handling for those value.
                        Object value = flexCriteriaBean.get(flexCriteriaParam.getName());
                        if (value != null) {
                            // Added check to exclude null fields
                            OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
                            ovs.put(flexCriteriaParam.getName(), nested);
                        }
                    }
                }
            } else {
                Object value = property.getValue(flexCriteriaBean);
                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 (flexCriteriaBean != null) {
            String className = flexCriteriaBean.getClass().getSimpleName();
            OutboundVariable var = getConverterManager().convertOutbound(className, outctx);
            ovs.put("className", var);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(flexCriteriaBean.getClass(), ex);
    }
    ov.init(ovs, getJavascript());
    return ov;
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) TreeMap(java.util.TreeMap) MarshallException(org.directwebremoting.extend.MarshallException) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) FlexCriteriaParam(org.jaffa.flexfields.FlexCriteriaParam) Map(java.util.Map) TreeMap(java.util.TreeMap) Property(org.directwebremoting.extend.Property) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 5 with MarshallException

use of org.directwebremoting.extend.MarshallException in project jaffa-framework by jaffa-projects.

the class NotNullBeanConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     *
     * Copied from BasicObjectConverter
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    // 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(data, ov);
    try {
        Map properties = getPropertyMapFromObject(data, 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();
            Object value = property.getValue(data);
            if (value != null || (data instanceof GraphDataObject && ((GraphDataObject) data).hasChanged(name))) {
                // Added check to exclude null fields
                OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
                ovs.put(name, nested);
            }
        }
        // Add the className to the object
        if (data != null) {
            String className = data.getClass().getSimpleName();
            OutboundVariable var = getConverterManager().convertOutbound(className, outctx);
            ovs.put("className", var);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
    ov.init(ovs, getJavascript());
    return ov;
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) TreeMap(java.util.TreeMap) MarshallException(org.directwebremoting.extend.MarshallException) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Aggregations

MarshallException (org.directwebremoting.extend.MarshallException)43 Map (java.util.Map)16 OutboundVariable (org.directwebremoting.extend.OutboundVariable)12 Iterator (java.util.Iterator)10 HashMap (java.util.HashMap)9 TreeMap (java.util.TreeMap)9 Property (org.directwebremoting.extend.Property)9 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)8 InboundVariable (org.directwebremoting.extend.InboundVariable)8 TypeHintContext (org.directwebremoting.extend.TypeHintContext)7 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)5 IntrospectionException (java.beans.IntrospectionException)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 StringTokenizer (java.util.StringTokenizer)4 Converter (org.directwebremoting.extend.Converter)4 NamedConverter (org.directwebremoting.extend.NamedConverter)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 StringWriter (java.io.StringWriter)3