Search in sources :

Example 1 with FormatDateTimeException

use of org.jaffa.datatypes.exceptions.FormatDateTimeException in project jaffa-framework by jaffa-projects.

the class DateTimeCriteriaField method parseAndAdd.

private static void parseAndAdd(String str, DateTimeFieldMetaData meta, List values) throws FormatDateTimeException {
    try {
        DateTime parsedValue = null;
        if (str != null && str.length() > 0) {
            if (meta != null)
                parsedValue = Parser.parseDateTime(str, meta.getLayout());
            else
                parsedValue = Parser.parseDateTime(str);
        }
        values.add(parsedValue);
    } catch (FormatDateTimeException e) {
        e.setField(meta != null ? meta.getLabelToken() : "");
        throw e;
    }
}
Also used : DateTime(org.jaffa.datatypes.DateTime) FormatDateTimeException(org.jaffa.datatypes.exceptions.FormatDateTimeException)

Example 2 with FormatDateTimeException

use of org.jaffa.datatypes.exceptions.FormatDateTimeException in project jaffa-framework by jaffa-projects.

the class JaffaDateConverter 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 {
    // Error out if an unsupported class is passed
    if (paramType != DateOnly.class && paramType != DateTime.class) {
        log.warn("Unsupported input. Class=" + paramType);
        throw new MarshallException(paramType);
    }
    // Extract Char Encoding from the Web Context
    String charEncoding = null;
    WebContext context = WebContextFactory.get();
    if (context != null) {
        HttpServletRequest req = context.getHttpServletRequest();
        if (req != null)
            charEncoding = req.getCharacterEncoding();
    }
    if (charEncoding == null)
        charEncoding = CHAR_ENCODER;
    String value = iv.getValue();
    // If the text is null then the whole bean is null
    if (value.trim().equals(ProtocolConstants.INBOUND_NULL))
        return null;
    Object output;
    try {
        // Handle a millisecond input
        long millis = 0;
        if (value.length() > 0)
            millis = Long.parseLong(value);
        // DWR returns null dates as '0', so we must return a null in this case
        if (millis == 0)
            return null;
        Boolean useServerTime = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(RULE_NAME_USE_SERVER_TIME));
        if (useServerTime != null && useServerTime) {
            if (log.isInfoEnabled())
                log.info("A String representation of a date should be posted by the client, since the application rule '" + RULE_NAME_USE_SERVER_TIME + "' is set");
        }
        output = paramType == DateOnly.class ? new DateOnly(millis) : new DateTime(millis);
    } catch (NumberFormatException e) {
        try {
            // Handle a String input
            if (log.isDebugEnabled())
                log.debug("Error in parsing '" + value + "' as milliseconds since 1970. Will attempt to parse it as a String representation of a date");
            output = paramType == DateOnly.class ? DateTime.toDateOnly(Parser.parseDateTime(URLDecoder.decode(value, charEncoding), FORMAT_DATE_TIME)) : Parser.parseDateTime(URLDecoder.decode(value, charEncoding), FORMAT_DATE_TIME);
        } catch (FormatDateTimeException e1) {
            if (log.isDebugEnabled())
                log.debug("Error in parsing Date '" + value + "' using the format '" + FORMAT_DATE_TIME + '\'', e1);
            throw new MarshallException(DateOnly.class, e1);
        } catch (UnsupportedEncodingException e1) {
            if (log.isDebugEnabled())
                log.debug("Error in encoding Date '" + value + "' using the format '" + charEncoding + '\'', e1);
            throw new MarshallException(DateOnly.class, e1);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Inbound '" + value + "' converted to '" + output + '\'');
    return output;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebContext(org.directwebremoting.WebContext) MarshallException(org.directwebremoting.extend.MarshallException) DateOnly(org.jaffa.datatypes.DateOnly) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DateTime(org.jaffa.datatypes.DateTime) FormatDateTimeException(org.jaffa.datatypes.exceptions.FormatDateTimeException)

Aggregations

DateTime (org.jaffa.datatypes.DateTime)2 FormatDateTimeException (org.jaffa.datatypes.exceptions.FormatDateTimeException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 WebContext (org.directwebremoting.WebContext)1 MarshallException (org.directwebremoting.extend.MarshallException)1 DateOnly (org.jaffa.datatypes.DateOnly)1