Search in sources :

Example 6 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SchemaJsonResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    final GraphObjectMap schema = new GraphObjectMap();
    int resultCount = 0;
    try {
        final JsonSchema jsonSchema = StructrSchema.createFromDatabase(StructrApp.getInstance());
        schema.setProperty(new StringProperty("schema"), jsonSchema.toString());
        resultCount = 1;
    } catch (URISyntaxException ex) {
        logger.error("Error while creating JsonSchema: " + ex.getMessage());
    }
    Result res = new Result(schema, true);
    res.setRawResultCount(resultCount);
    return res;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) JsonSchema(org.structr.schema.json.JsonSchema) StringProperty(org.structr.core.property.StringProperty) URISyntaxException(java.net.URISyntaxException) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 7 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SnapshotCommand method addSnapshot.

private void addSnapshot(final String fileName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    // isolate write output
    try (final Tx tx = app.tx()) {
        if (fileName != null) {
            final File snapshotFile = locateFile(fileName, false);
            try (final Reader reader = new FileReader(snapshotFile)) {
                final JsonSchema schema = StructrSchema.createFromSource(reader);
                StructrSchema.extendDatabaseSchema(app, schema);
            } catch (InvalidSchemaException iex) {
                throw new FrameworkException(422, iex.getMessage());
            }
        } else {
            throw new FrameworkException(422, "Please supply schema snapshot name to add to database.");
        }
        tx.success();
    } catch (IOException | URISyntaxException ioex) {
        logger.warn("", ioex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File)

Example 8 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SnapshotCommand method purgeLocalSchema.

private void purgeLocalSchema() throws FrameworkException {
    final App app = StructrApp.getInstance();
    // isolate write output
    try (final Tx tx = app.tx()) {
        try {
            final JsonSchema schema = StructrSchema.createEmptySchema();
            StructrSchema.replaceDatabaseSchema(app, schema);
        } catch (InvalidSchemaException iex) {
            throw new FrameworkException(422, iex.getMessage());
        }
        tx.success();
    } catch (URISyntaxException use) {
        logger.warn("", use);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) URISyntaxException(java.net.URISyntaxException)

Example 9 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SystemTest method testGrantFunctionPerformance.

@Test
public void testGrantFunctionPerformance() {
    // test setup, create a supernode with 10000 relationships
    try (final Tx tx = app.tx()) {
        // create test group
        app.create(Group.class, "group");
        JsonSchema schema = StructrSchema.createFromDatabase(app);
        schema.addType("GrantTest").addMethod("onCreation", "grant(first(find('Group')), this, 'read')", "");
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable fex) {
        fail("Unexpected exception");
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("GrantTest");
    try (final Tx tx = app.tx()) {
        final long t0 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            app.create(type, "test" + i);
        }
        final long t1 = System.currentTimeMillis();
        System.out.println((t1 - t0) + " ms");
        tx.success();
    } catch (Throwable fex) {
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema) Test(org.junit.Test)

Example 10 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SchemaTest method test02SimpleSymmetricReferences.

@Test
public void test02SimpleSymmetricReferences() {
    // we need to wait for the schema service to be initialized here.. :(
    try {
        Thread.sleep(1000);
    } catch (Throwable t) {
    }
    try {
        final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
        final JsonObjectType project = sourceSchema.addType("Project");
        final JsonObjectType task = sourceSchema.addType("Task");
        // create relation
        final JsonReferenceType rel = project.relate(task, "has", Cardinality.OneToMany, "project", "tasks");
        rel.setName("ProjectTasks");
        final String schema = sourceSchema.toString();
        System.out.println(schema);
        // test map paths
        final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
        mapPathValue(map, "definitions.Project.type", "object");
        mapPathValue(map, "definitions.Project.properties.tasks.$link", "#/definitions/ProjectTasks");
        mapPathValue(map, "definitions.Project.properties.tasks.items.$ref", "#/definitions/Task");
        mapPathValue(map, "definitions.Project.properties.tasks.type", "array");
        mapPathValue(map, "definitions.ProjectTasks.$source", "#/definitions/Project");
        mapPathValue(map, "definitions.ProjectTasks.$target", "#/definitions/Task");
        mapPathValue(map, "definitions.ProjectTasks.cardinality", "OneToMany");
        mapPathValue(map, "definitions.ProjectTasks.rel", "has");
        mapPathValue(map, "definitions.ProjectTasks.sourceName", "project");
        mapPathValue(map, "definitions.ProjectTasks.targetName", "tasks");
        mapPathValue(map, "definitions.ProjectTasks.type", "object");
        mapPathValue(map, "definitions.Task.type", "object");
        mapPathValue(map, "definitions.Task.properties.project.$link", "#/definitions/ProjectTasks");
        mapPathValue(map, "definitions.Task.properties.project.$ref", "#/definitions/Project");
        mapPathValue(map, "definitions.Task.properties.project.type", "object");
        // test
        compareSchemaRoundtrip(sourceSchema);
    } catch (FrameworkException | InvalidSchemaException | URISyntaxException ex) {
        logger.warn("", ex);
        fail("Unexpected exception.");
    }
}
Also used : JsonReferenceType(org.structr.schema.json.JsonReferenceType) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) URISyntaxException(java.net.URISyntaxException) JsonObjectType(org.structr.schema.json.JsonObjectType) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

JsonSchema (org.structr.schema.json.JsonSchema)36 Tx (org.structr.core.graph.Tx)24 Test (org.junit.Test)22 FrameworkException (org.structr.common.error.FrameworkException)19 URISyntaxException (java.net.URISyntaxException)13 JsonType (org.structr.schema.json.JsonType)13 GsonBuilder (com.google.gson.GsonBuilder)9 JsonObjectType (org.structr.schema.json.JsonObjectType)9 StructrTest (org.structr.common.StructrTest)8 InvalidSchemaException (org.structr.schema.json.InvalidSchemaException)8 Gson (com.google.gson.Gson)6 App (org.structr.core.app.App)6 StructrApp (org.structr.core.app.StructrApp)6 NodeInterface (org.structr.core.graph.NodeInterface)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 File (org.structr.web.entity.File)6 IOException (java.io.IOException)5 LinkedHashMap (java.util.LinkedHashMap)5 File (java.io.File)4 JsonReferenceType (org.structr.schema.json.JsonReferenceType)4