Search in sources :

Example 1 with ObjectMorpher

use of net.sf.ezmorph.ObjectMorpher in project jaffa-framework by jaffa-projects.

the class ExcelExportService method registerCustomDateMorpher.

/**
 * Registers a custom Morpher to handle, based on the input flag, either a DateTime or DateOnly class.
 */
private static void registerCustomDateMorpher(boolean dateTime) {
    final Class targetType = dateTime ? DateTime.class : DateOnly.class;
    Morpher targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
    if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
        synchronized (JSONUtils.getMorpherRegistry()) {
            targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
            if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
                // Create a custom Morpher
                targetMorpher = new ObjectMorpher() {

                    /**
                     * Returns the target Class (DateTime.class or DateOnly.class) for conversion.
                     */
                    public Class morphsTo() {
                        return targetType;
                    }

                    /**
                     * Returns true if the Morpher supports conversion from this Class. Only the String class is supported currently.
                     */
                    public boolean supports(Class clazz) {
                        return clazz == String.class;
                    }

                    /**
                     * Morphs the input object into an output object of the supported type.
                     */
                    public Object morph(Object value) {
                        try {
                            String layout = "yyyy-MM-dd'T'HH:mm:ss";
                            return targetType == DateTime.class ? Parser.parseDateTime((String) value, layout) : Parser.parseDateOnly((String) value, layout);
                        } catch (Exception e) {
                            if (log.isDebugEnabled())
                                log.debug("Error in converting '" + value + "' to " + (targetType == DateTime.class ? "DateTime" : "DateOnly"), e);
                            return value;
                        }
                    }
                };
                JSONUtils.getMorpherRegistry().registerMorpher(targetMorpher);
            }
        }
    }
}
Also used : Morpher(net.sf.ezmorph.Morpher) ObjectMorpher(net.sf.ezmorph.ObjectMorpher) IdentityObjectMorpher(net.sf.ezmorph.object.IdentityObjectMorpher) ObjectMorpher(net.sf.ezmorph.ObjectMorpher) IdentityObjectMorpher(net.sf.ezmorph.object.IdentityObjectMorpher) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DateTime(org.jaffa.datatypes.DateTime)

Example 2 with ObjectMorpher

use of net.sf.ezmorph.ObjectMorpher in project jaffa-framework by jaffa-projects.

the class ExcelExportService method registerCustomDateMorpher.

/**
 * Registers a custom Morpher to handle, based on the input flag, either a DateTime or DateOnly class.
 */
private static void registerCustomDateMorpher(boolean dateTime) {
    final Class targetType = dateTime ? DateTime.class : DateOnly.class;
    Morpher targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
    if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
        synchronized (JSONUtils.getMorpherRegistry()) {
            targetMorpher = JSONUtils.getMorpherRegistry().getMorpherFor(targetType);
            if (targetMorpher == null || targetMorpher == IdentityObjectMorpher.getInstance()) {
                // Create a custom Morpher
                targetMorpher = new ObjectMorpher() {

                    /**
                     * Returns the target Class (DateTime.class or DateOnly.class) for conversion.
                     */
                    public Class morphsTo() {
                        return targetType;
                    }

                    /**
                     * Returns true if the Morpher supports conversion from this Class. Only the String class is supported currently.
                     */
                    public boolean supports(Class clazz) {
                        return clazz == String.class;
                    }

                    /**
                     * Morphs the input object into an output object of the supported type.
                     */
                    public Object morph(Object value) {
                        try {
                            String layout = "yyyy-MM-dd'T'HH:mm:ss";
                            return targetType == DateTime.class ? Parser.parseDateTime((String) value, layout) : Parser.parseDateOnly((String) value, layout);
                        } catch (Exception e) {
                            if (log.isDebugEnabled())
                                log.debug("Error in converting '" + value + "' to " + (targetType == DateTime.class ? "DateTime" : "DateOnly"), e);
                            return value;
                        }
                    }
                };
                JSONUtils.getMorpherRegistry().registerMorpher(targetMorpher);
            }
        }
    }
}
Also used : Morpher(net.sf.ezmorph.Morpher) ObjectMorpher(net.sf.ezmorph.ObjectMorpher) IdentityObjectMorpher(net.sf.ezmorph.object.IdentityObjectMorpher) ObjectMorpher(net.sf.ezmorph.ObjectMorpher) IdentityObjectMorpher(net.sf.ezmorph.object.IdentityObjectMorpher) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) DateTime(org.jaffa.datatypes.DateTime)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Morpher (net.sf.ezmorph.Morpher)2 ObjectMorpher (net.sf.ezmorph.ObjectMorpher)2 IdentityObjectMorpher (net.sf.ezmorph.object.IdentityObjectMorpher)2 JSONObject (net.sf.json.JSONObject)2 DateTime (org.jaffa.datatypes.DateTime)2 FlexClass (org.jaffa.flexfields.FlexClass)2