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;
}
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;
}
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);
}
}
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;
}
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();
}
Aggregations