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;
}
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;
}
Aggregations