Search in sources :

Example 1 with XWorkException

use of com.opensymphony.xwork2.XWorkException in project entando-core by entando.

the class ApsDateTypeConverter method convertFromString.

@Override
public Object convertFromString(Map context, String[] values, Class toType) {
    if (null == values)
        return null;
    if (values.length > 1) {
        throw new XWorkException("Multiple values");
    }
    Date result = null;
    String value = values[0];
    if (value instanceof String && value != null && ((String) value).length() > 0) {
        String sa = (String) value;
        DateFormat df = new SimpleDateFormat(ApsAdminSystemConstants.CALENDAR_DATE_PATTERN);
        try {
            // let's use strict parsing (XW-341)
            df.setLenient(false);
            result = df.parse(sa);
            if (!(Date.class == toType)) {
                try {
                    Constructor constructor = toType.getConstructor(new Class[] { long.class });
                    return constructor.newInstance(new Object[] { Long.valueOf(result.getTime()) });
                } catch (Exception e) {
                    throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
                }
            }
        } catch (ParseException e) {
            throw new XWorkException("Could not parse date", e);
        }
    }
    return result;
}
Also used : XWorkException(com.opensymphony.xwork2.XWorkException) Constructor(java.lang.reflect.Constructor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) XWorkException(com.opensymphony.xwork2.XWorkException) ParseException(java.text.ParseException)

Example 2 with XWorkException

use of com.opensymphony.xwork2.XWorkException in project entando-core by entando.

the class ApsTimestampTypeConverter method convertFromString.

@Override
public Object convertFromString(Map context, String[] values, Class toType) {
    if (null == values)
        return null;
    if (values.length > 1) {
        throw new XWorkException("Multiple values");
    }
    Date result = null;
    String value = values[0];
    if (value instanceof String && value != null && ((String) value).length() > 0) {
        String sa = (String) value;
        DateFormat df = new SimpleDateFormat(ApsAdminSystemConstants.CALENDAR_TIMESTAMP_PATTERN);
        try {
            // let's use strict parsing (XW-341)
            df.setLenient(false);
            result = df.parse(sa);
            if (!(Date.class == toType)) {
                try {
                    Constructor constructor = toType.getConstructor(new Class[] { long.class });
                    return constructor.newInstance(new Object[] { Long.valueOf(result.getTime()) });
                } catch (Exception e) {
                    throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
                }
            }
        } catch (ParseException e) {
            throw new XWorkException("Could not parse date", e);
        }
    }
    return result;
}
Also used : XWorkException(com.opensymphony.xwork2.XWorkException) Constructor(java.lang.reflect.Constructor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) XWorkException(com.opensymphony.xwork2.XWorkException) ParseException(java.text.ParseException)

Aggregations

XWorkException (com.opensymphony.xwork2.XWorkException)2 Constructor (java.lang.reflect.Constructor)2 DateFormat (java.text.DateFormat)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2