Search in sources :

Example 6 with InvalidSchemaException

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

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

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