use of org.structr.schema.json.JsonReferenceProperty in project structr by structr.
the class SchemaTest method test03SchemaBuilder.
@Test
public void test03SchemaBuilder() {
// 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 String instanceId = app.getInstanceId();
final JsonObjectType task = sourceSchema.addType("Task");
final JsonProperty title = task.addStringProperty("title", "public", "ui").setRequired(true);
final JsonProperty desc = task.addStringProperty("description", "public", "ui").setRequired(true);
task.addDateProperty("description", "public", "ui").setDatePattern("dd.MM.yyyy").setRequired(true);
// test function property
task.addFunctionProperty("displayName", "public", "ui").setReadFunction("this.name");
task.addFunctionProperty("javascript", "public", "ui").setReadFunction("{ var x = 'test'; return x; }").setContentType("application/x-structr-javascript");
// a project
final JsonObjectType project = sourceSchema.addType("Project");
project.addStringProperty("name", "public", "ui").setRequired(true);
final JsonReferenceType projectTasks = project.relate(task, "HAS", Cardinality.OneToMany, "project", "tasks");
projectTasks.setCascadingCreate(Cascade.targetToSource);
project.getViewPropertyNames("public").add("tasks");
task.getViewPropertyNames("public").add("project");
// test enums
project.addEnumProperty("status", "ui").setEnums("active", "planned", "finished");
// a worker
final JsonObjectType worker = sourceSchema.addType("Worker");
final JsonReferenceType workerTasks = worker.relate(task, "HAS", Cardinality.OneToMany, "worker", "tasks");
workerTasks.setCascadingDelete(Cascade.sourceToTarget);
// reference Worker -> Task
final JsonReferenceProperty workerProperty = workerTasks.getSourceProperty();
final JsonReferenceProperty tasksProperty = workerTasks.getTargetProperty();
tasksProperty.setName("renamedTasks");
worker.addReferenceProperty("taskNames", tasksProperty, "public", "ui").setProperties("name");
worker.addReferenceProperty("taskInfos", tasksProperty, "public", "ui").setProperties("id", "name");
worker.addReferenceProperty("taskErrors", tasksProperty, "public", "ui").setProperties("id");
task.addReferenceProperty("workerName", workerProperty, "public", "ui").setProperties("name");
task.addReferenceProperty("workerNotion", workerProperty, "public", "ui").setProperties("id");
// test date properties..
project.addDateProperty("startDate", "public", "ui");
// methods
project.addMethod("onCreate", "set(this, 'name', 'wurst')", "comment for wurst");
// test URIs
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/#", sourceSchema.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task", task.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task/properties/title", title.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task/properties/description", desc.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Worker/properties/renamedTasks", tasksProperty.getId().toString());
compareSchemaRoundtrip(sourceSchema);
} catch (Exception ex) {
ex.printStackTrace();
logger.warn("", ex);
fail("Unexpected exception.");
}
}
Aggregations