Search in sources :

Example 1 with JsonReferenceProperty

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.");
    }
}
Also used : JsonReferenceType(org.structr.schema.json.JsonReferenceType) JsonProperty(org.structr.schema.json.JsonProperty) JsonSchema(org.structr.schema.json.JsonSchema) JsonReferenceProperty(org.structr.schema.json.JsonReferenceProperty) JsonObjectType(org.structr.schema.json.JsonObjectType) URISyntaxException(java.net.URISyntaxException) FrameworkException(org.structr.common.error.FrameworkException) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

URISyntaxException (java.net.URISyntaxException)1 Test (org.junit.Test)1 StructrTest (org.structr.common.StructrTest)1 FrameworkException (org.structr.common.error.FrameworkException)1 InvalidSchemaException (org.structr.schema.json.InvalidSchemaException)1 JsonObjectType (org.structr.schema.json.JsonObjectType)1 JsonProperty (org.structr.schema.json.JsonProperty)1 JsonReferenceProperty (org.structr.schema.json.JsonReferenceProperty)1 JsonReferenceType (org.structr.schema.json.JsonReferenceType)1 JsonSchema (org.structr.schema.json.JsonSchema)1