Search in sources :

Example 36 with SchemaNode

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

the class StructrSchema method replaceDatabaseSchema.

/**
 * Replaces the current Structr schema with the given new schema. This
 * method is the reverse of createFromDatabase above.
 *
 * @param app
 * @param newSchema the new schema to replace the current Structr schema
 *
 * @throws FrameworkException
 * @throws URISyntaxException
 */
public static void replaceDatabaseSchema(final App app, final JsonSchema newSchema) throws FrameworkException, URISyntaxException {
    Services.getInstance().setOverridingSchemaTypesAllowed(true);
    try (final Tx tx = app.tx()) {
        for (final SchemaRelationshipNode schemaRelationship : app.nodeQuery(SchemaRelationshipNode.class).getAsList()) {
            app.delete(schemaRelationship);
        }
        for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
            app.delete(schemaNode);
        }
        for (final SchemaMethod schemaMethod : app.nodeQuery(SchemaMethod.class).getAsList()) {
            app.delete(schemaMethod);
        }
        for (final SchemaMethodParameter schemaMethodParameter : app.nodeQuery(SchemaMethodParameter.class).getAsList()) {
            app.delete(schemaMethodParameter);
        }
        for (final SchemaProperty schemaProperty : app.nodeQuery(SchemaProperty.class).getAsList()) {
            app.delete(schemaProperty);
        }
        for (final SchemaView schemaView : app.nodeQuery(SchemaView.class).getAsList()) {
            app.delete(schemaView);
        }
        newSchema.createDatabaseSchema(app, JsonSchema.ImportMode.replace);
        tx.success();
    }
}
Also used : SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) SchemaMethodParameter(org.structr.core.entity.SchemaMethodParameter)

Example 37 with SchemaNode

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

the class PropertyTest method testNotionProperty.

/**
 * This test creates a new type "Test" with a Notion property that references a type (User)
 * which is only present in the ui module.
 */
@Test
public void testNotionProperty() {
    // schema setup
    try (final Tx tx = app.tx()) {
        final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.name, "ownerPrincipalEmail"), new NodeAttribute<>(SchemaProperty.propertyType, "Notion"), new NodeAttribute<>(SchemaProperty.format, "owner, User.eMail"), new NodeAttribute<>(SchemaProperty.schemaNode, test));
        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) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 38 with SchemaNode

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

the class UiScriptingTest method testSpecialHeaders.

@Test
public void testSpecialHeaders() {
    String uuid = null;
    // schema setup
    try (final Tx tx = app.tx()) {
        // create list of 100 folders
        final List<Folder> folders = new LinkedList<>();
        for (int i = 0; i < 100; i++) {
            folders.add(createTestNode(Folder.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Folder" + i)));
        }
        // create parent folder
        final Folder parent = createTestNode(Folder.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Parent"), new NodeAttribute<>(StructrApp.key(Folder.class, "folders"), folders));
        uuid = parent.getUuid();
        // create function property that returns folder children
        final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName("Folder").getFirst();
        schemaNode.setProperty(new StringProperty("_testFunction"), "Function(this.folders)");
        // create admin user
        createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    RestAssured.basePath = "/structr/rest";
    RestAssured.given().contentType("application/json; charset=UTF-8").accept("application/json; properties=id,type,name,folders,testFunction").header("Range", "folders=0-10;testFunction=0-10").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", "admin", "X-Password", "admin").expect().statusCode(200).body("result.folders", Matchers.hasSize(10)).body("result.testFunction", Matchers.hasSize(10)).when().get("/Folder/" + uuid + "/ui");
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) SchemaNode(org.structr.core.entity.SchemaNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) Folder(org.structr.web.entity.Folder) LinkedList(java.util.LinkedList) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 39 with SchemaNode

use of org.structr.core.entity.SchemaNode 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 SchemaNode

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

the class SchemaMethodsTest method test03SchemaMethodOnEntityOfBuiltinType.

@Test
public void test03SchemaMethodOnEntityOfBuiltinType() {
    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);
    }
    File testFile = null;
    try (final Tx tx = app.tx()) {
        // Create File instance
        testFile = app.create(File.class, "Test File");
        testFile.setProperty(File.owner, admin);
        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 + "/" + testFile.getUuid() + "/" + 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) File(org.structr.web.entity.File) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

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