Search in sources :

Example 1 with InvalidSchemaException

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

the class SchemaJsonResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if (propertySet != null && propertySet.containsKey("schema")) {
        final App app = StructrApp.getInstance(securityContext);
        String schemaJson = (String) propertySet.get("schema");
        try {
            StructrSchema.replaceDatabaseSchema(app, StructrSchema.createFromSource(schemaJson));
        } catch (URISyntaxException | InvalidSchemaException ex) {
            logger.error("Error while importing JsonSchema: " + ex.getMessage());
        }
        return new RestMethodResult(200, "Schema import started");
    }
    return new RestMethodResult(400, "Invalid request body. Specify schema json string as 'schema' in request body.");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) URISyntaxException(java.net.URISyntaxException) RestMethodResult(org.structr.rest.RestMethodResult)

Example 2 with InvalidSchemaException

use of org.structr.schema.json.InvalidSchemaException 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 3 with InvalidSchemaException

use of org.structr.schema.json.InvalidSchemaException 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 4 with InvalidSchemaException

use of org.structr.schema.json.InvalidSchemaException 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)

Example 5 with InvalidSchemaException

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

the class SchemaJsonImporter method importSchemaJson.

public static void importSchemaJson(final String source) throws FrameworkException {
    // nothing to do
    if (StringUtils.isBlank(source)) {
        return;
    }
    final App app = StructrApp.getInstance();
    // isolate write output
    try (final Tx tx = app.tx()) {
        final JsonSchema schema;
        try {
            schema = StructrSchema.createFromSource(source);
        } catch (InvalidSchemaException | URISyntaxException ex) {
            throw new FrameworkException(422, ex.getMessage());
        }
        try {
            StructrSchema.extendDatabaseSchema(app, schema);
        } catch (URISyntaxException ex) {
            throw new FrameworkException(422, ex.getMessage());
        }
        tx.success();
    }
}
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)

Aggregations

URISyntaxException (java.net.URISyntaxException)7 InvalidSchemaException (org.structr.schema.json.InvalidSchemaException)7 FrameworkException (org.structr.common.error.FrameworkException)6 JsonSchema (org.structr.schema.json.JsonSchema)6 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 Tx (org.structr.core.graph.Tx)4 File (java.io.File)3 IOException (java.io.IOException)3 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 Test (org.junit.Test)2 JsonObjectType (org.structr.schema.json.JsonObjectType)2 JsonReferenceType (org.structr.schema.json.JsonReferenceType)2 GsonBuilder (com.google.gson.GsonBuilder)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1 StructrTest (org.structr.common.StructrTest)1 RestMethodResult (org.structr.rest.RestMethodResult)1 SnapshotCommand (org.structr.rest.maintenance.SnapshotCommand)1