use of org.structr.schema.action.Function in project structr by structr.
the class StructrScriptable method get.
@Override
public Object get(final String name, Scriptable start) {
if ("get".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
if (parameters.length == 1 && parameters[0] != null) {
try {
return wrap(context, thisObject, null, actionContext.evaluate(entity, parameters[0].toString(), null, null, 0));
} catch (FrameworkException ex) {
exception = ex;
}
} else if (parameters.length > 1) {
// execute builtin get function
final Function<Object, Object> function = Functions.get("get");
try {
final Object[] unwrappedParameters = new Object[parameters.length];
int i = 0;
// unwrap JS objects
for (final Object param : parameters) {
unwrappedParameters[i++] = unwrap(param);
}
return wrap(context, scope, null, function.apply(actionContext, entity, unwrappedParameters));
} catch (FrameworkException fex) {
exception = fex;
}
return null;
}
return null;
}
}, null, 0, 0);
}
if ("clear".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
actionContext.clear();
return null;
}
}, null, 0, 0);
}
if ("this".equals(name)) {
return wrap(this.scriptingContext, start, null, entity);
}
if ("me".equals(name)) {
return wrap(this.scriptingContext, start, null, actionContext.getSecurityContext().getUser(false));
}
if ("vars".equals(name)) {
NativeObject nobj = new NativeObject();
for (Map.Entry<String, Object> entry : actionContext.getAllVariables().entrySet()) {
nobj.defineProperty(entry.getKey(), entry.getValue(), NativeObject.READONLY);
}
return nobj;
}
if ("include".equals(name) || "render".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
if (parameters.length > 0 && parameters[0] != null) {
try {
final Function func = Functions.get(name);
if (func != null) {
actionContext.print(func.apply(actionContext, entity, parameters));
}
return null;
} catch (FrameworkException ex) {
exception = ex;
}
}
return null;
}
}, null, 0, 0);
}
if ("includeJs".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
if (parameters.length == 1) {
final String fileName = parameters[0].toString();
final String source = actionContext.getJavascriptLibraryCode(fileName);
// use cached / compiled source code for JS libs
Scripting.compileOrGetCached(context, source, fileName, 1).exec(context, scope);
} else {
logger.warn("Incorrect usage of includeJs function. Takes exactly one parameter: The filename of the javascript file!");
}
return null;
}
}, null, 0, 0);
}
if ("batch".equals(name)) {
return new IdFunctionObject(new BatchFunctionCall(actionContext, this), null, 0, 0);
}
if ("cache".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
final CacheExpression cacheExpr = new CacheExpression();
Object retVal = null;
try {
for (int i = 0; i < parameters.length; i++) {
cacheExpr.add(new ConstantExpression(parameters[i]));
}
retVal = cacheExpr.evaluate(actionContext, entity);
} catch (FrameworkException ex) {
exception = ex;
}
return retVal;
}
}, null, 0, 0);
}
if ("slice".equals(name)) {
return new IdFunctionObject(new SliceFunctionCall(actionContext, entity, scriptingContext), null, 0, 0);
}
if ("doPrivileged".equals(name) || "do_privileged".equals(name)) {
return new IdFunctionObject(new IdFunctionCall() {
@Override
public Object execIdCall(final IdFunctionObject info, final Context context, final Scriptable scope, final Scriptable thisObject, final Object[] parameters) {
// backup security context
final SecurityContext securityContext = StructrScriptable.this.actionContext.getSecurityContext();
try {
// replace security context with super user context
actionContext.setSecurityContext(SecurityContext.getSuperUserInstance());
if (parameters != null && parameters.length == 1) {
final Object param = parameters[0];
if (param instanceof Script) {
final Script script = (Script) param;
return script.exec(context, scope);
} else {
// ...
}
} else {
// ...
}
return null;
} finally {
// restore saved security context
StructrScriptable.this.actionContext.setSecurityContext(securityContext);
}
}
}, null, 0, 0);
}
// execute builtin function?
final Function<Object, Object> function = Functions.get(CaseHelper.toUnderscore(name, false));
if (function != null) {
return new IdFunctionObject(new FunctionWrapper(function), null, 0, 0);
}
return null;
}
Aggregations