Search in sources :

Example 1 with FlexCriteriaBean

use of org.jaffa.flexfields.FlexCriteriaBean 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 2 with FlexCriteriaBean

use of org.jaffa.flexfields.FlexCriteriaBean 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 3 with FlexCriteriaBean

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

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 4 with FlexCriteriaBean

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

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Aggregations

Iterator (java.util.Iterator)4 FlexCriteriaBean (org.jaffa.flexfields.FlexCriteriaBean)4 FlexClass (org.jaffa.flexfields.FlexClass)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JSONObject (net.sf.json.JSONObject)2 JsonConfig (net.sf.json.JsonConfig)2 NewBeanInstanceStrategy (net.sf.json.util.NewBeanInstanceStrategy)2 MarshallException (org.directwebremoting.extend.MarshallException)2 Property (org.directwebremoting.extend.Property)2 DynaClass (org.apache.commons.beanutils.DynaClass)1 ObjectOutboundVariable (org.directwebremoting.dwrp.ObjectOutboundVariable)1 InboundVariable (org.directwebremoting.extend.InboundVariable)1 OutboundVariable (org.directwebremoting.extend.OutboundVariable)1 TypeHintContext (org.directwebremoting.extend.TypeHintContext)1 FlexCriteriaParam (org.jaffa.flexfields.FlexCriteriaParam)1