Search in sources :

Example 6 with Property

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

Example 7 with Property

use of org.directwebremoting.extend.Property in project Gemma by PavlidisLab.

the class DoublePointConverter method convertOutbound.

@Override
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    if (!(data instanceof DoublePoint))
        return super.convertOutbound(data, outctx);
    // Where we collect out converted children
    Map<String, OutboundVariable> ovs = new TreeMap<String, OutboundVariable>();
    // We need to do this before collecting the children to save recursion
    ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
    outctx.put(data, ov);
    try {
        Map<String, Property> properties = getPropertyMapFromObject(data, true, false);
        for (Iterator<Entry<String, Property>> it = properties.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, Property> entry = it.next();
            String name = entry.getKey();
            Property property = entry.getValue();
            Object value = property.getValue(data);
            OutboundVariable nested;
            if (value instanceof Double) {
                // Reduce precision to save bandwidth
                Double v = Double.parseDouble(String.format("%.3f", value));
                nested = getConverterManager().convertOutbound(v, outctx);
            } else {
                nested = getConverterManager().convertOutbound(value, outctx);
            }
            ovs.put(name, nested);
        }
    } 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) TreeMap(java.util.TreeMap) MarshallException(org.directwebremoting.extend.MarshallException) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) Entry(java.util.Map.Entry) MarshallException(org.directwebremoting.extend.MarshallException) DoublePoint(ubic.basecode.dataStructure.DoublePoint) Property(org.directwebremoting.extend.Property)

Example 8 with Property

use of org.directwebremoting.extend.Property in project Gemma by PavlidisLab.

the class CharacteristicConverter method convertInbound.

@SuppressWarnings("unchecked")
@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 {
        Map<String, String> tokens = extractInboundTokens(paramType, value);
        Object bean;
        // usually there is a "null" valueUri.
        if (tokens.containsKey("valueUri")) {
            String[] split = ParseUtil.splitInbound(tokens.get("valueUri"));
            String splitValue = split[LocalUtil.INBOUND_INDEX_VALUE];
            String splitType = split[LocalUtil.INBOUND_INDEX_TYPE];
            InboundVariable nested = new InboundVariable(iv.getLookup(), null, splitType, splitValue);
            if (StringUtils.isBlank(nested.getValue()) || "null".equals(nested.getValue())) {
                bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
            } else {
                bean = ubic.gemma.model.common.description.VocabCharacteristic.Factory.newInstance();
            }
        } else {
            bean = ubic.gemma.model.common.description.Characteristic.Factory.newInstance();
        }
        if (instanceType != null) {
            inctx.addConverted(iv, instanceType, bean);
        } else {
            inctx.addConverted(iv, paramType, bean);
        }
        Map<String, Property> properties = getPropertyMapFromObject(bean, false, true);
        for (Iterator<Entry<String, String>> it = tokens.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, String> entry = it.next();
            String key = entry.getKey();
            String val = entry.getValue();
            Property property = properties.get(key);
            if (property == null) {
                // 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);
            // Collection
            if ((key.equals("properties")) && (output instanceof ArrayList)) {
                ArrayList<Object> propertyList = (ArrayList<Object>) output;
                output = new HashSet<Object>(propertyList);
            }
            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) ArrayList(java.util.ArrayList) InboundVariable(org.directwebremoting.extend.InboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Entry(java.util.Map.Entry) MarshallException(org.directwebremoting.extend.MarshallException) Property(org.directwebremoting.extend.Property) Map(java.util.Map)

Example 9 with Property

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

the class BasicObjectConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
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);
            OutboundVariable nested = getConverterManager().convertOutbound(value, outctx);
            ovs.put(name, nested);
        }
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
    ov.init(ovs, getJavascript());
    return ov;
}
Also used : ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) OutboundVariable(org.directwebremoting.extend.OutboundVariable) ObjectOutboundVariable(org.directwebremoting.dwrp.ObjectOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Iterator(java.util.Iterator) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Property(org.directwebremoting.extend.Property) MarshallException(org.directwebremoting.extend.MarshallException)

Example 10 with Property

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

the class DefaultRemoter method generateInterfaceScript.

/* (non-Javadoc)
     * @see org.directwebremoting.Remoter#generateInterfaceScript(java.lang.String, java.lang.String)
     */
public String generateInterfaceScript(String scriptName, String path) throws SecurityException {
    String actualPath = path;
    if (overridePath != null) {
        actualPath = overridePath;
    }
    StringBuffer buffer = new StringBuffer();
    // Output the class definitions for the converted objects
    Collection converterMatches = converterManager.getConverterMatchStrings();
    Iterator it = converterMatches.iterator();
    while (it.hasNext()) {
        String match = (String) it.next();
        try {
            StringBuffer paramBuffer = new StringBuffer();
            Converter conv = converterManager.getConverterByMatchString(match);
            // We will only generate JavaScript classes for compound objects/beans
            if (conv instanceof NamedConverter) {
                NamedConverter boConv = (NamedConverter) conv;
                String jsClassName = boConv.getJavascript();
                // We need a configured JavaScript class name
                if (jsClassName != null && !jsClassName.equals("")) {
                    // Wildcard match strings are currently not supported
                    if (match.indexOf("*") == -1) {
                        paramBuffer.append('\n');
                        // output: if (typeof <class> != "function") { var <class> = function() {
                        paramBuffer.append("if (typeof " + jsClassName + " != \"function\") {\n");
                        paramBuffer.append("  function " + jsClassName + "() {\n");
                        // output: this.<property> = <init-value>;
                        Class mappedType;
                        try {
                            mappedType = LocalUtil.classForName(match);
                        } catch (ClassNotFoundException ex) {
                            throw new IllegalArgumentException(ex.getMessage());
                        }
                        Map properties = boConv.getPropertyMapFromClass(mappedType, true, true);
                        for (Iterator pit = properties.entrySet().iterator(); pit.hasNext(); ) {
                            Map.Entry entry = (Map.Entry) pit.next();
                            String name = (String) entry.getKey();
                            Property property = (Property) entry.getValue();
                            Class propType = property.getPropertyType();
                            // Property name
                            paramBuffer.append("    this." + name + " = ");
                            // Default property values
                            if (propType.isArray()) {
                                paramBuffer.append("[]");
                            } else if (propType == boolean.class) {
                                paramBuffer.append("false");
                            } else if (propType.isPrimitive()) {
                                paramBuffer.append("0");
                            } else {
                                paramBuffer.append("null");
                            }
                            paramBuffer.append(";\n");
                        }
                        paramBuffer.append("  }\n");
                        paramBuffer.append("}\n");
                    }
                }
            }
            buffer.append(paramBuffer.toString());
        } catch (Exception ex) {
            log.warn("Failed to create parameter declaration for " + match, ex);
            buffer.append("// Missing parameter declaration for " + match + ". See the server logs for details.");
        }
    }
    Creator creator = creatorManager.getCreator(scriptName);
    buffer.append('\n');
    String init = EnginePrivate.getEngineInitScript();
    buffer.append(init);
    buffer.append("if (" + scriptName + " == null) var " + scriptName + " = {};\n");
    buffer.append(scriptName + "._path = '" + actualPath + "';\n");
    Method[] methods = creator.getType().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String methodName = method.getName();
        // check if we can display it
        try {
            accessControl.assertIsDisplayable(creator, scriptName, method);
        } catch (SecurityException ex) {
            if (!allowImpossibleTests) {
                continue;
            }
        }
        // Is it on the list of banned names
        if (JavascriptUtil.isReservedWord(methodName)) {
            continue;
        }
        // Check to see if the creator is reloadable
        // If it is, then do not cache the generated Javascript
        String script;
        if (!creator.isCacheable()) {
            script = getMethodJS(scriptName, method);
        } else {
            String key = scriptName + "." + method.getName();
            // For optimal performance we might use the Memoizer pattern
            // JCiP#108 however performance isn't a big issue and we are
            // prepared to cope with getMethodJS() being run more than once.
            script = (String) methodCache.get(key);
            if (script == null) {
                script = getMethodJS(scriptName, method);
                methodCache.put(key, script);
            }
        }
        buffer.append(script);
    }
    return buffer.toString();
}
Also used : NamedConverter(org.directwebremoting.extend.NamedConverter) Creator(org.directwebremoting.extend.Creator) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Iterator(java.util.Iterator) Collection(java.util.Collection) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter) HashMap(java.util.HashMap) 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