Search in sources :

Example 6 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class RenderContextTest method testScriptEvaluation.

@Test
public void testScriptEvaluation() {
    Settings.CypherDebugLogging.setValue(true);
    try (final Tx tx = app.tx()) {
        // create a Project type
        final SchemaNode projectNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Project"));
        // create a Task type with a string property "task"
        final SchemaNode taskNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task"), new NodeAttribute<>(new StringProperty("_task"), "String"));
        // create a schema relationship between them
        createTestNode(SchemaRelationshipNode.class, new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceNode"), projectNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetNode"), taskNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "relationshipType"), "has"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceMultiplicity"), "1"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetMultiplicity"), "*"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceJsonName"), "project"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetJsonName"), "tasks"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // obtain class objects to create instances of the above types
        final Class projectType = StructrApp.getConfiguration().getNodeEntityClass("Project");
        final Class taskType = StructrApp.getConfiguration().getNodeEntityClass("Task");
        final PropertyKey taskKey = StructrApp.key(taskType, "task");
        final PropertyKey tasksKey = StructrApp.key(projectType, "tasks");
        final List<NodeInterface> tasks = new LinkedList<>();
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 1"), new NodeAttribute<>(taskKey, "Task 1")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 2"), new NodeAttribute<>(taskKey, "Task 2")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 3"), new NodeAttribute<>(taskKey, "Task 3")));
        // create a project and a task
        final NodeInterface project = app.create(projectType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "project"), new NodeAttribute<>(tasksKey, tasks));
        // create an additional test task without a project
        final NodeInterface testTask = app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "test task"), new NodeAttribute<>(taskKey, "test task"));
        final RenderContext renderContext = new RenderContext(securityContext);
        renderContext.putDataObject("project", project);
        renderContext.putDataObject("task", testTask);
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${foo.page}"));
        assertEquals("Invalid scripting evaluation result", testTask.getUuid(), Scripting.replaceVariables(renderContext, null, "${task}"));
        assertEquals("Invalid scripting evaluation result", "test task", Scripting.replaceVariables(renderContext, null, "${task.task}"));
        assertEquals("Invalid scripting evaluation result", tasks.toString(), Scripting.replaceVariables(renderContext, null, "${project.tasks}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(0).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[0]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(1).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[1]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(2).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[2]}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3]}"));
        assertEquals("Invalid scripting evaluation result", "Task 1", Scripting.replaceVariables(renderContext, null, "${project.tasks[0].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 2", Scripting.replaceVariables(renderContext, null, "${project.tasks[1].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 3", Scripting.replaceVariables(renderContext, null, "${project.tasks[2].task}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3].task}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        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) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 7 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class SchemaMethodsTest method test05InheritedSchemaMethodOnBuildinType.

@Test
public void test05InheritedSchemaMethodOnBuildinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    User admin = null;
    try (final Tx tx = app.tx()) {
        admin = createAdminUser();
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Add schema method "testFileMethod" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
        final PropertyMap testFileMethodProperties = new PropertyMap();
        testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
        testFileMethodProperties.put(SchemaMethod.source, "()");
        testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
        SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post("/Image/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Example 8 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class SchemaMethodsTest method test06InheritedSchemaMethodOnEntityOfBuiltinType.

@Test
public void test06InheritedSchemaMethodOnEntityOfBuiltinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    User admin = null;
    try (final Tx tx = app.tx()) {
        admin = createAdminUser();
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Add schema method "testFileMethod" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
        final PropertyMap testFileMethodProperties = new PropertyMap();
        testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
        testFileMethodProperties.put(SchemaMethod.source, "()");
        testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
        SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    String id = createEntityAsAdmin("Image", "{'name': 'Test Image'}");
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post("/Image/" + id + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Example 9 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class SchemaMethodsTest method test01SchemaMethodOnBuiltinType.

@Test
public void test01SchemaMethodOnBuiltinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    try (final Tx tx = app.tx()) {
        // Add schema method "testFileMethod" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
        final PropertyMap testFileMethodProperties = new PropertyMap();
        testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
        testFileMethodProperties.put(SchemaMethod.source, "()");
        testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
        SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    User admin = null;
    try (final Tx tx = app.tx()) {
        admin = createAdminUser();
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post(builtinTypeName + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Example 10 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class CMISRepositoryService method getBaseTypeChildren.

private List<TypeDefinition> getBaseTypeChildren(final BaseTypeId baseTypeId, final Boolean includePropertyDefinitions) {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final List<TypeDefinition> result = new LinkedList<>();
    final App app = StructrApp.getInstance();
    // static definition of base type children, add new types here!
    switch(baseTypeId) {
        case CMIS_DOCUMENT:
            result.add(extendTypeDefinition(File.class, includePropertyDefinitions));
            break;
        case CMIS_FOLDER:
            result.add(extendTypeDefinition(Folder.class, includePropertyDefinitions));
            break;
        case CMIS_ITEM:
            try (final Tx tx = app.tx()) {
                for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).sort(AbstractNode.name).getAsList()) {
                    final Class type = config.getNodeEntityClass(schemaNode.getClassName());
                    if (type != null) {
                        final CMISInfo info = getCMISInfo(type);
                        if (info != null && baseTypeId.equals(info.getBaseTypeId())) {
                            final TypeDefinition extendedTypeDefinition = extendTypeDefinition(type, includePropertyDefinitions);
                            if (extendedTypeDefinition != null) {
                                result.add(extendedTypeDefinition);
                            }
                        }
                    }
                }
                tx.success();
            } catch (final FrameworkException fex) {
                logger.warn("", fex);
            }
            break;
    }
    return result;
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) SchemaNode(org.structr.core.entity.SchemaNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) CMISInfo(org.structr.cmis.CMISInfo) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) LinkedList(java.util.LinkedList) MutableTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableTypeDefinition) MutablePolicyTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutablePolicyTypeDefinition) MutableRelationshipTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableRelationshipTypeDefinition) MutableFolderTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableFolderTypeDefinition) TypeDefinition(org.apache.chemistry.opencmis.commons.definitions.TypeDefinition) MutableDocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableDocumentTypeDefinition) MutableSecondaryTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableSecondaryTypeDefinition) MutableItemTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableItemTypeDefinition)

Aggregations

SchemaNode (org.structr.core.entity.SchemaNode)54 Tx (org.structr.core.graph.Tx)44 FrameworkException (org.structr.common.error.FrameworkException)41 Test (org.junit.Test)34 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 NodeAttribute (org.structr.core.graph.NodeAttribute)14 LinkedList (java.util.LinkedList)13 PropertyMap (org.structr.core.property.PropertyMap)13 NodeInterface (org.structr.core.graph.NodeInterface)12 PropertyKey (org.structr.core.property.PropertyKey)11 StructrTest (org.structr.common.StructrTest)10 SchemaMethod (org.structr.core.entity.SchemaMethod)10 SchemaRelationshipNode (org.structr.core.entity.SchemaRelationshipNode)9 StringProperty (org.structr.core.property.StringProperty)9 List (java.util.List)8 ConfigurationProvider (org.structr.schema.ConfigurationProvider)8 SchemaProperty (org.structr.core.entity.SchemaProperty)7 LinkedHashSet (java.util.LinkedHashSet)6 FrontendTest (org.structr.web.basic.FrontendTest)6