Search in sources :

Example 1 with Property

use of org.directwebremoting.extend.Property 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 2 with Property

use of org.directwebremoting.extend.Property 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 3 with Property

use of org.directwebremoting.extend.Property 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 4 with Property

use of org.directwebremoting.extend.Property 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)

Example 5 with Property

use of org.directwebremoting.extend.Property in project ma-core-public by infiniteautomation.

the class BasicObjectConverter method convertInbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertInbound(java.lang.Class, org.directwebremoting.InboundVariable, org.directwebremoting.InboundContext)
     */
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 {
        Object bean;
        if (instanceType != null) {
            bean = instanceType.newInstance();
        } else {
            bean = paramType.newInstance();
        }
        // is referenced later nested down in the conversion process.
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        Map properties = getPropertyMapFromObject(bean, false, true);
        // Loop through the properties passed in
        Map tokens = extractInboundTokens(paramType, value);
        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) {
                log.warn("Missing java bean property to match javascript property: " + key + ". For causes see debug level logs:");
                log.debug("- The javascript may be refer to a property that does not exist");
                log.debug("- You may be missing the correct setter: set" + Character.toTitleCase(key.charAt(0)) + key.substring(1) + "()");
                log.debug("- The property may be excluded using include or exclude rules.");
                StringBuffer all = new StringBuffer();
                for (Iterator pit = properties.keySet().iterator(); pit.hasNext(); ) {
                    all.append(pit.next());
                    if (pit.hasNext()) {
                        all.append(',');
                    }
                }
                log.debug("Fields exist for (" + all + ").");
                continue;
            }
            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 : TypeHintContext(org.directwebremoting.extend.TypeHintContext) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property)

Aggregations

Property (org.directwebremoting.extend.Property)10 Map (java.util.Map)9 MarshallException (org.directwebremoting.extend.MarshallException)9 Iterator (java.util.Iterator)8 TreeMap (java.util.TreeMap)8 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)5 OutboundVariable (org.directwebremoting.extend.OutboundVariable)5 InboundVariable (org.directwebremoting.extend.InboundVariable)4 TypeHintContext (org.directwebremoting.extend.TypeHintContext)4 HashMap (java.util.HashMap)3 Entry (java.util.Map.Entry)2 DynaClass (org.apache.commons.beanutils.DynaClass)2 FlexBean (org.jaffa.flexfields.FlexBean)2 FlexClass (org.jaffa.flexfields.FlexClass)2 FlexCriteriaBean (org.jaffa.flexfields.FlexCriteriaBean)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Converter (org.directwebremoting.extend.Converter)1