Search in sources :

Example 26 with JsonSchema

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

the class SnapshotCommand method createSnapshot.

// ----- private methods -----
private void createSnapshot(final String name, final List<String> types) throws FrameworkException {
    // we want to create a sorted, human-readble, diffable representation of the schema
    final App app = StructrApp.getInstance();
    // isolate write output
    try (final Tx tx = app.tx()) {
        final File snapshotFile = locateFile(name, true);
        try (final Writer writer = new FileWriter(snapshotFile)) {
            final JsonSchema schema = StructrSchema.createFromDatabase(app, types);
            writer.append(schema.toString());
            // useful newline
            writer.append("\n");
            writer.flush();
        }
        tx.success();
    } catch (IOException | URISyntaxException ioex) {
        logger.warn("", ioex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FileWriter(java.io.FileWriter) JsonSchema(org.structr.schema.json.JsonSchema) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 27 with JsonSchema

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

the class SnapshotCommand method restoreSnapshot.

private void restoreSnapshot(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.replaceDatabaseSchema(app, schema);
            } catch (InvalidSchemaException iex) {
                throw new FrameworkException(422, iex.getMessage());
            }
        } else {
            throw new FrameworkException(422, "Please supply schema snapshot name to restore from.");
        }
        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 28 with JsonSchema

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

the class UiTest method createTestImageType.

// ----- private methods -----
private Class createTestImageType() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonType type = schema.addType("TestImage");
        type.setExtends(URI.create("#/definitions/Image"));
        type.addCustomProperty("thumbnail", ThumbnailProperty.class.getName()).setFormat("200, 100, false");
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return StructrApp.getConfiguration().getNodeEntityClass("TestImage");
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema)

Example 29 with JsonSchema

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

the class SchemaSnapshotTest method testSnapshotRoundtripWithPrimitives.

@Test
public void testSnapshotRoundtripWithPrimitives() {
    File file = null;
    String source = null;
    String export = null;
    String imp = null;
    // 1. step: create schema with all possible (primitive) property types
    try {
        // create new instance id
        app.getInstanceId();
        final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
        // a customer
        final JsonObjectType customer = sourceSchema.addType("Customer");
        customer.addStringProperty("name", "public", "ui").setRequired(true).setUnique(true);
        customer.addStringProperty("street", "public", "ui");
        customer.addStringProperty("city", "public", "ui");
        customer.addDateProperty("birthday", "public", "ui");
        customer.addEnumProperty("status", "public", "ui").setEnums("active", "retired", "none").setDefaultValue("active");
        customer.addIntegerProperty("count", "public", "ui").setMinimum(1).setMaximum(10, true).setDefaultValue("5");
        customer.addNumberProperty("number", "public", "ui").setMinimum(2.0, true).setMaximum(5.0, true).setDefaultValue("3.0");
        customer.addLongProperty("loong", "public", "ui").setMinimum(20, true).setMaximum(50);
        customer.addBooleanProperty("isCustomer", "public", "ui");
        customer.addFunctionProperty("displayName", "public", "ui").setReadFunction("concat(this.name, '.', this.id)");
        customer.addStringProperty("description", "public", "ui").setContentType("text/plain").setFormat("multi-line");
        customer.addStringArrayProperty("stringArray", "public", "ui");
        customer.addIntegerArrayProperty("intArray", "public", "ui").setMinimum(0, true).setMaximum(100, true);
        customer.addLongArrayProperty("longArray", "public", "ui").setMinimum(1, true).setMaximum(101, true);
        customer.addDoubleArrayProperty("doubleArray", "public", "ui").setMinimum(2.0, true).setMaximum(102.0, true);
        customer.addBooleanArrayProperty("booleanArray", "public", "ui");
        // a project
        final JsonObjectType project = sourceSchema.addType("Project");
        final JsonReferenceType rel = customer.relate(project);
        rel.setCardinality(Relation.Cardinality.OneToMany);
        rel.setCascadingDelete(JsonSchema.Cascade.sourceToTarget);
        rel.setRelationship("hasProject");
        rel.setSourcePropertyName("customer");
        rel.setTargetPropertyName("projects");
        source = sourceSchema.toString();
        try (final FileWriter writer = new FileWriter(createTempFile(1))) {
            writer.append(source);
            writer.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        StructrSchema.replaceDatabaseSchema(app, sourceSchema);
    } catch (Throwable t) {
        fail("Unexpected exception");
    }
    // step 2: export schema
    try {
        app.command(SnapshotCommand.class).execute("export", "test");
    } catch (FrameworkException ex) {
        fail("Unexpected exception");
    }
    // step 3: read schema export file and compare to source
    try {
        file = new File(basePath + "/snapshots").listFiles()[0];
        export = IOUtils.toString(new FileInputStream(file)).trim();
        try (final FileWriter writer = new FileWriter(createTempFile(3))) {
            writer.append(export);
            writer.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        assertEquals("Invalid schema export result, ", source, export);
    } catch (IOException t) {
        t.printStackTrace();
        fail("Unexpected exception");
    }
    // step 4: clear schema
    try {
        StructrSchema.replaceDatabaseSchema(app, StructrSchema.createEmptySchema());
    } catch (FrameworkException | InvalidSchemaException | URISyntaxException ex) {
        fail("Unexpected exception");
    }
    // step 5: import schema from export file
    try {
        app.command(SnapshotCommand.class).execute("restore", file.getName());
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }
    // step 6: create string representation of imported schema
    try {
        final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
        imp = sourceSchema.toString();
    } catch (Throwable t) {
        fail("Unexpected exception");
    }
    try (final FileWriter writer = new FileWriter(createTempFile(2))) {
        writer.append(imp);
        writer.flush();
    } catch (IOException ioex) {
        ioex.printStackTrace();
    }
    // step 7: compare import result to initial source
    assertEquals("Invalid schema export result, ", source, imp);
}
Also used : JsonReferenceType(org.structr.schema.json.JsonReferenceType) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) FileWriter(java.io.FileWriter) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JsonObjectType(org.structr.schema.json.JsonObjectType) FileInputStream(java.io.FileInputStream) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) SnapshotCommand(org.structr.rest.maintenance.SnapshotCommand) File(java.io.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 30 with JsonSchema

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

the class AdvancedSchemaTest method testCustomSchemaMethod.

@Test
public void testCustomSchemaMethod() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        schema.getType("User").addMethod("simpleTest", "log('test')", "test");
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) 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