* @param model the model
* @param maxDepth the maxiumum number of sub models to process
* @return the javascript object
*/
public static JavaScriptObject getJsObject(ModelData model, int maxDepth) {
JsObject jsObj = new JsObject();
for (String key : model.getPropertyNames()) {
Object value = model.get(key);
if (value == null) continue;
if (maxDepth > 0) {
if (value instanceof Collection) {
JsArray jsArray = new JsArray();
Collection c = (Collection) value;
for (Object obj : c) {
if (obj instanceof ModelData) {
jsArray.add(getJsObject((ModelData) obj, maxDepth - 1));
}
}
jsObj.set(key, jsArray);
} else if (value instanceof Object[]) {
JsArray jsArray = new JsArray();
Object[] c = (Object[]) value;
for (Object obj : c) {
if (obj instanceof ModelData) {
jsArray.add(getJsObject((ModelData) obj, maxDepth - 1));
}
}
jsObj.set(key, jsArray);
} else if (value instanceof ModelData) {
jsObj.set(key, getJsObject((ModelData) value, maxDepth - 1));
} else {
jsObj.set(key, value);
}
} else {
jsObj.set(key, value);
}
}
return jsObj.getJsObject();
}