Search in sources :

Example 36 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class CreateFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (sources != null && sources.length > 0) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        PropertyMap propertyMap;
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            type = config.getNodeEntityClass(sources[0].toString());
        }
        if (type == null) {
            throw new FrameworkException(422, "Unknown type '" + sources[0].toString() + "' in create() method!");
        }
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
        } else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
        } else {
            propertyMap = new PropertyMap();
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_CREATE_JS : ERROR_MESSAGE_CREATE));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = StructrApp.key(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    propertyMap.put(key, value);
                }
            }
        }
        return StructrApp.getInstance(securityContext).create(type, propertyMap);
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 37 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class PropertyInfoFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return null;
        }
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final String typeName = sources[0].toString();
        final String keyName = sources[1].toString();
        Class type = config.getNodeEntityClass(typeName);
        if (type == null) {
            type = config.getRelationshipEntityClass(typeName);
        }
        if (type != null) {
            final PropertyKey key = StructrApp.key(type, keyName);
            if (key != null) {
                return SchemaHelper.getPropertyInfo(ctx.getSecurityContext(), key);
            } else {
                logger.warn("Error: Unknown property \"{}.{}\". Parameters: {}", new Object[] { typeName, keyName, getParametersAsString(sources) });
                return "Unknown property " + typeName + "." + keyName;
            }
        } else {
            logger.warn("Error: Unknown type \"{}\". Parameters: {}", new Object[] { typeName, getParametersAsString(sources) });
            return "Unknown type " + typeName;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) PropertyKey(org.structr.core.property.PropertyKey)

Example 38 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class MergePropertiesFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2) && sources[0] instanceof GraphObject && sources[1] instanceof GraphObject) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Set<PropertyKey> mergeKeys = new LinkedHashSet<>();
        final GraphObject source = (GraphObject) sources[0];
        final GraphObject target = (GraphObject) sources[1];
        final int paramCount = sources.length;
        for (int i = 2; i < paramCount; i++) {
            final String keyName = sources[i].toString();
            final PropertyKey key = StructrApp.key(target.getClass(), keyName);
            mergeKeys.add(key);
        }
        for (final PropertyKey key : mergeKeys) {
            final Object sourceValue = source.getProperty(key);
            if (sourceValue != null) {
                target.setProperty(key, sourceValue);
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 39 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class RenderContextTest method testNotionTransformedPropertyAccess.

@Test
public void testNotionTransformedPropertyAccess() {
    NodeInterface project = null;
    NodeInterface task1 = null;
    NodeInterface task2 = null;
    NodeInterface task3 = null;
    try (final Tx tx = app.tx()) {
        final SchemaNode projectNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Project"), new NodeAttribute(new StringProperty("_taskList"), "Notion(tasks, id, name)"), new NodeAttribute(new StringProperty("_taskNames"), "Notion(tasks, name)"));
        final SchemaNode taskNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Task"));
        // create schema relationship
        final PropertyMap taskProperties = new PropertyMap();
        taskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
        taskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
        taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
        taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
        taskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        taskProperties.put(SchemaRelationshipNode.targetMultiplicity, "*");
        taskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
        taskProperties.put(SchemaRelationshipNode.targetJsonName, "tasks");
        app.create(SchemaRelationshipNode.class, taskProperties);
        // create schema relationship
        final PropertyMap currentTaskProperties = new PropertyMap();
        currentTaskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
        currentTaskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
        currentTaskProperties.put(SchemaRelationshipNode.relationshipType, "CURRENT");
        currentTaskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        currentTaskProperties.put(SchemaRelationshipNode.targetMultiplicity, "1");
        currentTaskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
        currentTaskProperties.put(SchemaRelationshipNode.targetJsonName, "currentTask");
        app.create(SchemaRelationshipNode.class, currentTaskProperties);
        // compile the stuff
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class projectClass = config.getNodeEntityClass("Project");
    final Class taskClass = config.getNodeEntityClass("Task");
    final PropertyKey currentTaskKey = StructrApp.key(projectClass, "currentTask");
    final PropertyKey tasksKey = StructrApp.key(projectClass, "tasks");
    // create parent/child relationship
    try (final Tx tx = app.tx()) {
        project = app.create(projectClass, new NodeAttribute(SchemaNode.name, "Project1"));
        task1 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task1"));
        task2 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task2"));
        task3 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task3"));
        // add task to project
        final List tasks = new LinkedList<>();
        tasks.add(task1);
        tasks.add(task2);
        tasks.add(task3);
        final PropertyMap projectProperties = new PropertyMap();
        projectProperties.put(tasksKey, tasks);
        projectProperties.put(currentTaskKey, task3);
        project.setProperties(project.getSecurityContext(), projectProperties);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // check property access in template expressions
    try (final Tx tx = app.tx()) {
        final RenderContext renderContext = new RenderContext(securityContext);
        renderContext.putDataObject("project", project);
        renderContext.putDataObject("task", task1);
        assertEquals("Invalid dot syntax result: ", "Project1", Scripting.replaceVariables(renderContext, project, "${project.name}"));
        assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.tasks[0].name}"));
        assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.tasks[1].name}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.tasks[2].name}"));
        assertEquals("Invalid dot syntax result: ", "[Task1, Task2, Task3]", Scripting.replaceVariables(renderContext, project, "${project.taskNames}"));
        assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.taskNames[0]}"));
        assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.taskNames[1]}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.taskNames[2]}"));
        assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.currentTask.name}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) List(java.util.List) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 40 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class PropertyTest method testFunctionPropertyIndexing.

// ----- function property tests -----
/**
 * This test creates a new type "Test" and links it to
 * the built-in type "Group". It then creates a function
 * property that references the name of the related group
 * and assumes that a test entity is found by its related
 * group name.
 */
@Test
public void testFunctionPropertyIndexing() {
    // schema setup
    try (final Tx tx = app.tx()) {
        final SchemaNode group = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
        final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testFunction"), "Function(this.group.name)"));
        assertNotNull("Invalid schema setup result", group);
        assertNotNull("Invalid schema setup result", test);
        app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, test), new NodeAttribute<>(SchemaRelationshipNode.targetNode, group), new NodeAttribute<>(SchemaRelationshipNode.sourceMultiplicity, "*"), new NodeAttribute<>(SchemaRelationshipNode.targetMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "tests"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "group"), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "group"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // entity setup
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Class testType = config.getNodeEntityClass("Test");
        // create test type without link to group!
        app.create(testType);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // entity setup
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Class testType = config.getNodeEntityClass("Test");
        final Class groupType = config.getNodeEntityClass("Group");
        final GraphObject group = app.create(groupType, "testgroup");
        final GraphObject test = app.nodeQuery(testType).getFirst();
        // create Test with link to Group
        test.setProperty(StructrApp.key(testType, "group"), group);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // test
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Class testType = config.getNodeEntityClass("Test");
        final PropertyKey key = StructrApp.key(testType, "testFunction");
        // fetch test node
        final GraphObject testNode = app.nodeQuery(testType).getFirst();
        final GraphObject result = app.nodeQuery(testType).and(key, "testgroup").getFirst();
        // test indexing
        assertEquals("Invalid FunctionProperty indexing result", testNode, result);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

ConfigurationProvider (org.structr.schema.ConfigurationProvider)50 PropertyKey (org.structr.core.property.PropertyKey)27 FrameworkException (org.structr.common.error.FrameworkException)25 GraphObject (org.structr.core.GraphObject)17 Tx (org.structr.core.graph.Tx)15 LinkedList (java.util.LinkedList)14 PropertyMap (org.structr.core.property.PropertyMap)14 NodeInterface (org.structr.core.graph.NodeInterface)12 Test (org.junit.Test)11 Map (java.util.Map)10 PropertyConverter (org.structr.core.converter.PropertyConverter)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SecurityContext (org.structr.common.SecurityContext)9 App (org.structr.core.app.App)9 StructrApp (org.structr.core.app.StructrApp)9 SchemaNode (org.structr.core.entity.SchemaNode)8 File (org.structr.web.entity.File)8 LinkedHashMap (java.util.LinkedHashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6