Implement static HopObject.getById() function as described in this thread:
http://grazia.helma.org/pipermail/helma-user/2005-February/002777.html
This commit is contained in:
parent
0c3997ccc3
commit
91136fed4c
1 changed files with 31 additions and 0 deletions
|
@ -20,6 +20,7 @@ import helma.scripting.rhino.extensions.*;
|
|||
import helma.framework.core.*;
|
||||
import helma.objectmodel.*;
|
||||
import helma.objectmodel.db.DbMapping;
|
||||
import helma.objectmodel.db.DbKey;
|
||||
import helma.scripting.*;
|
||||
import helma.util.CacheMap;
|
||||
import helma.util.SystemMap;
|
||||
|
@ -275,6 +276,8 @@ public final class RhinoCore {
|
|||
ScriptRuntime.setFunctionProtoAndParent(global, fo);
|
||||
fo.setImmunePrototypeProperty(op);
|
||||
|
||||
// add static getById() function
|
||||
fo.defineProperty("getById", new GetById(name), GetById.ATTRIBUTES);
|
||||
op.setParentScope(fo);
|
||||
|
||||
ScriptableObject.defineProperty(global, name, fo, ScriptableObject.DONTENUM);
|
||||
|
@ -1071,5 +1074,33 @@ public final class RhinoCore {
|
|||
}
|
||||
}
|
||||
|
||||
class GetById extends BaseFunction {
|
||||
static final int ATTRIBUTES = DONTENUM | PERMANENT | READONLY;
|
||||
String typeName;
|
||||
|
||||
public GetById(String typeName) {
|
||||
this.typeName = typeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve any persistent HopObject by type name and id.
|
||||
*
|
||||
* @return the HopObject or null if it doesn't exist
|
||||
*/
|
||||
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
|
||||
DbMapping dbmap = app.getDbMapping(typeName);
|
||||
if (dbmap == null)
|
||||
return null;
|
||||
Object node = null;
|
||||
try {
|
||||
DbKey key = new DbKey(dbmap, Context.toString(args[0]));
|
||||
node = app.getNodeManager().getNode(key);
|
||||
} catch (Exception x) {
|
||||
return null;
|
||||
}
|
||||
return node == null ? null : Context.toObject(node, this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue