Search in sources :

Example 1 with NewBeanInstanceStrategy

use of net.sf.json.util.NewBeanInstanceStrategy in project jaffa-framework by jaffa-projects.

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Example 2 with NewBeanInstanceStrategy

use of net.sf.json.util.NewBeanInstanceStrategy in project jaffa-framework by jaffa-projects.

the class ExcelExportService method createJsonConfig.

/**
 * Creates a JsonConfig with the rootClass set to the input. Adds
 * custom-support for handling FlexCriteriaBean.
 */
private static JsonConfig createJsonConfig(Class rootClass) {
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.setRootClass(rootClass);
    jsonConfig.setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {

        public Object newInstance(Class target, JSONObject source) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, InvocationTargetException {
            if (target == FlexCriteriaBean.class) {
                try {
                    // Determine the name of the associated dynaClass and
                    // use that to instantiate the FlexCriteriaBean
                    JSONObject dynaClassObject = source.getJSONObject("dynaClass");
                    String dynaClassName = dynaClassObject.getString("name");
                    FlexCriteriaBean bean = FlexCriteriaBean.instance(FlexClass.instance(dynaClassName));
                    // Add the criteria elements
                    source.remove("dynaClass");
                    for (Iterator i = source.keys(); i.hasNext(); ) {
                        String key = (String) i.next();
                        Class propType = bean.getDynaClass() != null && bean.getDynaClass().getDynaProperty(key) != null ? bean.getDynaClass().getDynaProperty(key).getType() : String.class;
                        propType = findCriteriaFieldClass(propType);
                        Object propValue = JSONObject.toBean(source.getJSONObject(key), propType);
                        bean.set(key, propValue);
                    }
                    source.clear();
                    return bean;
                } catch (Exception e) {
                    String s = "Exception thrown while instantiating FlexCriteriaBean from " + source;
                    log.error(s, e);
                    throw new InvocationTargetException(e, s);
                }
            }
            return target.newInstance();
        }
    });
    return jsonConfig;
}
Also used : JsonConfig(net.sf.json.JsonConfig) NewBeanInstanceStrategy(net.sf.json.util.NewBeanInstanceStrategy) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) JSONObject(net.sf.json.JSONObject) Iterator(java.util.Iterator) FlexClass(org.jaffa.flexfields.FlexClass) JSONObject(net.sf.json.JSONObject) FlexCriteriaBean(org.jaffa.flexfields.FlexCriteriaBean)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Iterator (java.util.Iterator)2 JSONObject (net.sf.json.JSONObject)2 JsonConfig (net.sf.json.JsonConfig)2 NewBeanInstanceStrategy (net.sf.json.util.NewBeanInstanceStrategy)2 FlexClass (org.jaffa.flexfields.FlexClass)2 FlexCriteriaBean (org.jaffa.flexfields.FlexCriteriaBean)2