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);
}
}
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);
}
}
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");
}
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);
}
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();
}
}
Aggregations