use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testBulkDeleteWithoutBatching.
@Test
public void testBulkDeleteWithoutBatching() {
try (final Tx tx = app.tx()) {
createTestNodes(TestOne.class, 1000);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
Scripting.evaluate(ctx, null, "${delete(find('TestOne'))}", "test");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testGeoCoding.
@Test
public void testGeoCoding() {
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
final String locationId = Scripting.replaceVariables(ctx, null, "${create('Location')}");
final GeoCodingResult result = GeoHelper.geocode("", null, null, "Darmstadt", null, "");
if (result != null) {
// If geocoding itself fails, the test can not work => ignore
Double lat = result.getLatitude();
Double lon = result.getLongitude();
Scripting.replaceVariables(ctx, null, "${set(find('Location', '" + locationId + "'), geocode('Darmstadt', '', ''))}");
assertEquals("Latitude should be identical", lat.toString(), Scripting.replaceVariables(ctx, null, "${get(find('Location', '" + locationId + "'), 'latitude')}"));
assertEquals("Longitude should be identical", lon.toString(), Scripting.replaceVariables(ctx, null, "${get(find('Location', '" + locationId + "'), 'longitude')}"));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail(fex.getMessage());
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testNewlineAtEndOfScriptingCode.
@Test
public void testNewlineAtEndOfScriptingCode() {
final String script = "${{ return 'test'; }}\n";
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
assertEquals("Newline at end of JavaScript scripting should not prevent script evaluation.", "test", Scripting.evaluate(ctx, null, script, "test"));
assertEquals("Newline at end of JavaScript scripting should not prevent script evaluation.", "test\n", Scripting.replaceVariables(ctx, null, script));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class ScriptingTest method testNonPrimitiveReturnValue.
@Test
public void testNonPrimitiveReturnValue() {
try (final Tx tx = app.tx()) {
app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.name, "testReturnValueOfGlobalSchemaMethod"), new NodeAttribute<>(SchemaMethod.source, "{ return { name: 'test', value: 123, me: Structr.me }; }"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"))), new NodeAttribute<>(SchemaProperty.name, "returnTest"), new NodeAttribute<>(SchemaProperty.propertyType, "Function"), new NodeAttribute<>(SchemaProperty.readFunction, "{ return { name: 'test', value: 123, me: Structr.this }; }"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
final Map map = (Map) Scripting.evaluate(ctx, null, "${{return Structr.call('testReturnValueOfGlobalSchemaMethod')}}", "test");
final Object name = map.get("name");
final Object value = map.get("value");
final Object me = map.get("me");
assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
assertTrue("Invalid non-primitive scripting return value result, me should be of type SuperUser", me instanceof SuperUser);
tx.success();
} catch (UnlicensedException | FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
final NodeInterface obj = app.create(type, "test");
final Map map = (Map) obj.getProperty(StructrApp.key(type, "returnTest"));
final Object name = map.get("name");
final Object value = map.get("value");
final Object me = map.get("me");
assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
assertEquals("Invalid non-primitive scripting return value result, me should be the entity", obj, me);
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class VirtualType method transformInput.
public static void transformInput(final VirtualType thisType, final SecurityContext securityContext, final Class type, final Map<String, Object> propertySet) throws FrameworkException {
final ActionContext actionContext = new ActionContext(securityContext);
final List<VirtualProperty> props = VirtualType.sort(thisType.getVirtualProperties());
final Set<String> targetNames = VirtualType.extractTargetNames(props);
// remove all properties for which no VirtualProperty exists
final Iterator<String> it = propertySet.keySet().iterator();
while (it.hasNext()) {
final String propertyName = it.next();
if (!targetNames.contains(propertyName)) {
logger.debug("Removing property '{}' with value '{}' from propertyset because no matching virtual property was found", propertyName, propertySet.get(propertyName));
it.remove();
}
}
;
for (final VirtualProperty property : props) {
final Transformation transformation = property.getTransformation(type);
transformation.transformInput(actionContext, propertySet);
}
}
Aggregations