Search in sources :

Example 16 with ActionContext

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.");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 17 with ActionContext

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());
    }
}
Also used : GeoCodingResult(org.structr.common.geo.GeoCodingResult) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 18 with ActionContext

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.");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 19 with ActionContext

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.");
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) ActionContext(org.structr.schema.action.ActionContext) Map(java.util.Map) PropertyMap(org.structr.core.property.PropertyMap) SuperUser(org.structr.core.entity.SuperUser) NodeInterface(org.structr.core.graph.NodeInterface) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 20 with ActionContext

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);
    }
}
Also used : ActionContext(org.structr.schema.action.ActionContext)

Aggregations

ActionContext (org.structr.schema.action.ActionContext)44 FrameworkException (org.structr.common.error.FrameworkException)38 Tx (org.structr.core.graph.Tx)34 Test (org.junit.Test)33 StructrTest (org.structr.common.StructrTest)22 TestOne (org.structr.core.entity.TestOne)13 UnlicensedException (org.structr.common.error.UnlicensedException)11 GraphObject (org.structr.core.GraphObject)7 NodeInterface (org.structr.core.graph.NodeInterface)7 LinkedList (java.util.LinkedList)6 List (java.util.List)6 PropertyMap (org.structr.core.property.PropertyMap)6 Date (java.util.Date)5 SecurityContext (org.structr.common.SecurityContext)5 GraphObjectMap (org.structr.core.GraphObjectMap)4 Principal (org.structr.core.entity.Principal)4 SchemaNode (org.structr.core.entity.SchemaNode)4 PropertyKey (org.structr.core.property.PropertyKey)4 StructrUiTest (org.structr.web.StructrUiTest)4 SimpleDateFormat (java.text.SimpleDateFormat)3