Search in sources :

Example 1 with SimpleOutboundVariable

use of org.directwebremoting.dwrp.SimpleOutboundVariable 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 SimpleOutboundVariable

use of org.directwebremoting.dwrp.SimpleOutboundVariable in project ma-core-public by infiniteautomation.

the class XOMConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    try {
        // Using XSLT to convert to a stream. Setup the source
        if (!(data instanceof Node)) {
            throw new MarshallException(data.getClass());
        }
        Node node = (Node) data;
        String script = EnginePrivate.xmlStringToJavascriptDom(node.toXML());
        OutboundVariable ov = new SimpleOutboundVariable(script, outctx, false);
        outctx.put(data, ov);
        return ov;
    } catch (MarshallException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new MarshallException(data.getClass(), ex);
    }
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Node(nu.xom.Node) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException)

Example 3 with SimpleOutboundVariable

use of org.directwebremoting.dwrp.SimpleOutboundVariable in project ma-core-public by infiniteautomation.

the class BlabberConverterManager method convertOutbound.

/*
     * (non-Javadoc)
     * 
     * @see org.directwebremoting.ConverterManager#convertOutbound(java.lang.Object,
     * org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object object, OutboundContext outctx) throws MarshallException {
    if (object == null) {
        return new SimpleOutboundVariable("null", outctx, true);
    }
    // Check to see if we have done this one already
    OutboundVariable ov = outctx.get(object);
    if (ov != null) {
        // So the object as been converted already, we just need to refer to it.
        return ov.getReferenceVariable();
    }
    // So we will have to do the conversion
    Converter converter = getConverter(object);
    if (converter == null) {
        log.error(Messages.getString("DefaultConverterManager.MissingConverter", object.getClass().getName()));
        return new SimpleOutboundVariable("null", outctx, true);
    }
    return converter.convertOutbound(object, outctx);
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) NamedConverter(org.directwebremoting.extend.NamedConverter) Converter(org.directwebremoting.extend.Converter) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable)

Example 4 with SimpleOutboundVariable

use of org.directwebremoting.dwrp.SimpleOutboundVariable in project ma-core-public by infiniteautomation.

the class UnitBeanConverter method convertOutbound.

/* (non-Javadoc)
	 * @see org.directwebremoting.extend.Converter#convertOutbound(java.lang.Object, org.directwebremoting.extend.OutboundContext)
	 */
@Override
public OutboundVariable convertOutbound(Object paramObject, OutboundContext paramOutboundContext) throws MarshallException {
    // Convert from Unit to String
    // Check to see if we have done this one already
    OutboundVariable ov = paramOutboundContext.get(paramObject);
    if (ov != null) {
        // So the object as been converted already, we just need to refer to it.
        return ov.getReferenceVariable();
    }
    if (paramObject instanceof Unit<?>) {
        Unit<?> unit = (Unit<?>) paramObject;
        String unitString = UnitUtil.formatLocal(unit);
        if (unit == unit.ONE)
            unitString = "ONE";
        return new SimpleOutboundVariable("'" + unitString + "';", paramOutboundContext, false);
    } else {
        throw new MarshallException(paramObject.getClass());
    }
}
Also used : OutboundVariable(org.directwebremoting.extend.OutboundVariable) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) MarshallException(org.directwebremoting.extend.MarshallException) Unit(javax.measure.unit.Unit) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable)

Example 5 with SimpleOutboundVariable

use of org.directwebremoting.dwrp.SimpleOutboundVariable in project jaffa-framework by jaffa-projects.

the class JaffaDateConverter method convertOutbound.

/* (non-Javadoc)
     * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, org.directwebremoting.OutboundContext)
     */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    // Error out if an unsupported class is passed
    if (!(data instanceof DateOnly) && !(data instanceof DateTime)) {
        log.warn("Unsupported input. Class=" + data.getClass() + ", data=" + data);
        throw new MarshallException(data.getClass());
    }
    // Create a javascipt Date object.
    // When using server time, Pass the individual date elements. This will ensure that a similar date will be constructed in the client's timezone.
    // When not using server time, pass the millisecond since 1970 value. This will result in a date adjusted to the client's timezone.
    StringBuilder jsDateConstructor = new StringBuilder("new Date(");
    Boolean useServerTime = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(RULE_NAME_USE_SERVER_TIME));
    if (data instanceof DateOnly) {
        DateOnly d = (DateOnly) data;
        if (useServerTime != null && useServerTime)
            jsDateConstructor.append(d.year()).append(',').append(d.month()).append(',').append(d.day());
        else
            jsDateConstructor.append(d.timeInMillis());
    } else {
        DateTime d = (DateTime) data;
        if (useServerTime != null && useServerTime)
            jsDateConstructor.append(d.year()).append(',').append(d.month()).append(',').append(d.day()).append(',').append(d.hourOfDay()).append(',').append(d.minute()).append(',').append(d.second()).append(',').append(d.milli());
        else
            jsDateConstructor.append(d.timeInMillis());
    }
    String output = jsDateConstructor.append(')').toString();
    if (log.isDebugEnabled())
        log.debug("Outbound '" + data + "' converted to '" + output + '\'');
    return new SimpleOutboundVariable(output, outctx, true);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) DateOnly(org.jaffa.datatypes.DateOnly) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) DateTime(org.jaffa.datatypes.DateTime)

Aggregations

SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)10 MarshallException (org.directwebremoting.extend.MarshallException)8 OutboundVariable (org.directwebremoting.extend.OutboundVariable)6 StringWriter (java.io.StringWriter)3 URL (java.net.URL)1 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Unit (javax.measure.unit.Unit)1 Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Node (nu.xom.Node)1 Converter (org.directwebremoting.extend.Converter)1 NamedConverter (org.directwebremoting.extend.NamedConverter)1 Node (org.dom4j.Node)1 OutputFormat (org.dom4j.io.OutputFormat)1