Search in sources :

Example 1 with JsonType

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

the class DeploymentTest method test36BuiltInTypesWithProperties.

@Test
public void test36BuiltInTypesWithProperties() {
    // setup schema
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        Assert.assertNotNull("StructrSchema must return a valid schema object", schema);
        final JsonType pageType = schema.getType("Page");
        final JsonType fileType = schema.getType("File");
        Assert.assertNotNull("Type Page must exist in every schema", pageType);
        Assert.assertNotNull("Type File must exist in every schema", fileType);
        pageType.addIntegerProperty("displayPosition");
        pageType.addStringProperty("icon");
        fileType.addIntegerProperty("test1");
        fileType.addStringProperty("test2");
        // install schema
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (FrameworkException | URISyntaxException fex) {
        fail("Unexpected exception.");
    }
    // setup
    try (final Tx tx = app.tx()) {
        final Page page = Page.createSimplePage(securityContext, "page1");
        page.setProperty(StructrApp.key(Page.class, "displayPosition"), 12);
        page.setProperty(StructrApp.key(Page.class, "icon"), "icon");
        final Folder folder = app.create(Folder.class, "files");
        folder.setProperty(StructrApp.key(Folder.class, "includeInFrontendExport"), true);
        // create test file with custom attributes
        app.create(File.class, new NodeAttribute<>(StructrApp.key(File.class, "name"), "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder), new NodeAttribute<>(StructrApp.key(File.class, "contentType"), "text/plain"), new NodeAttribute<>(StructrApp.key(File.class, "test1"), 123), new NodeAttribute<>(StructrApp.key(File.class, "test2"), "testString"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    compare(calculateHash(), true);
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) Page(org.structr.web.entity.dom.Page) URISyntaxException(java.net.URISyntaxException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with JsonType

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

the class StructrTypeDefinitions method addType.

public JsonObjectType addType(final String name) {
    final JsonType type = getType(name, false);
    if (type != null) {
        return (JsonObjectType) type;
    }
    final StructrNodeTypeDefinition def = new StructrNodeTypeDefinition(root, name);
    typeDefinitions.add(def);
    return def;
}
Also used : JsonType(org.structr.schema.json.JsonType) JsonObjectType(org.structr.schema.json.JsonObjectType)

Example 3 with JsonType

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

the class SchemaTest method test01Inheritance.

@Test
public void test01Inheritance() {
    // 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 JsonType contact = sourceSchema.addType("Contact").setExtends(sourceSchema.getType("Principal"));
        final JsonType customer = sourceSchema.addType("Customer").setExtends(contact);
        final String schema = sourceSchema.toString();
        final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
        mapPathValue(map, "definitions.Contact.type", "object");
        mapPathValue(map, "definitions.Contact.$extends.0", "#/definitions/Principal");
        mapPathValue(map, "definitions.Customer.type", "object");
        mapPathValue(map, "definitions.Customer.$extends.0", "#/definitions/Contact");
        // advanced: test schema roundtrip
        compareSchemaRoundtrip(sourceSchema);
    } catch (Exception t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) 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)

Example 4 with JsonType

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

the class SchemaTest method test00SimpleProperties.

@Test
public void test00SimpleProperties() {
    try {
        final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
        // a customer
        final JsonType 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");
        final String schema = sourceSchema.toString();
        final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
        mapPathValue(map, "definitions.Customer.type", "object");
        mapPathValue(map, "definitions.Customer.required.0", "name");
        mapPathValue(map, "definitions.Customer.properties.booleanArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.booleanArray.items.type", "boolean");
        mapPathValue(map, "definitions.Customer.properties.city.unique", null);
        mapPathValue(map, "definitions.Customer.properties.count.type", "integer");
        mapPathValue(map, "definitions.Customer.properties.count.minimum", 1.0);
        mapPathValue(map, "definitions.Customer.properties.count.maximum", 10.0);
        mapPathValue(map, "definitions.Customer.properties.count.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.type", "number");
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.maximum", 102.0);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.minimum", 2.0);
        mapPathValue(map, "definitions.Customer.properties.number.type", "number");
        mapPathValue(map, "definitions.Customer.properties.number.minimum", 2.0);
        mapPathValue(map, "definitions.Customer.properties.number.maximum", 5.0);
        mapPathValue(map, "definitions.Customer.properties.number.exclusiveMinimum", true);
        mapPathValue(map, "definitions.Customer.properties.number.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.longArray.items.type", "long");
        mapPathValue(map, "definitions.Customer.properties.longArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.maximum", 101.0);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.minimum", 1.0);
        mapPathValue(map, "definitions.Customer.properties.loong.type", "long");
        mapPathValue(map, "definitions.Customer.properties.loong.minimum", 20.0);
        mapPathValue(map, "definitions.Customer.properties.loong.maximum", 50.0);
        mapPathValue(map, "definitions.Customer.properties.loong.exclusiveMinimum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.intArray.items.type", "integer");
        mapPathValue(map, "definitions.Customer.properties.intArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.maximum", 100.0);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.minimum", 0.0);
        mapPathValue(map, "definitions.Customer.properties.isCustomer.type", "boolean");
        mapPathValue(map, "definitions.Customer.properties.description.type", "string");
        mapPathValue(map, "definitions.Customer.properties.description.contentType", "text/plain");
        mapPathValue(map, "definitions.Customer.properties.description.format", "multi-line");
        mapPathValue(map, "definitions.Customer.properties.displayName.type", "function");
        mapPathValue(map, "definitions.Customer.properties.displayName.readFunction", "concat(this.name, '.', this.id)");
        mapPathValue(map, "definitions.Customer.properties.name.type", "string");
        mapPathValue(map, "definitions.Customer.properties.name.unique", true);
        mapPathValue(map, "definitions.Customer.properties.street.type", "string");
        mapPathValue(map, "definitions.Customer.properties.status.type", "string");
        mapPathValue(map, "definitions.Customer.properties.status.enum.0", "active");
        mapPathValue(map, "definitions.Customer.properties.status.enum.1", "none");
        mapPathValue(map, "definitions.Customer.properties.status.enum.2", "retired");
        mapPathValue(map, "definitions.Customer.properties.stringArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.stringArray.items.type", "string");
        mapPathValue(map, "definitions.Customer.views.public.0", "birthday");
        mapPathValue(map, "definitions.Customer.views.public.1", "booleanArray");
        mapPathValue(map, "definitions.Customer.views.public.2", "city");
        mapPathValue(map, "definitions.Customer.views.public.3", "count");
        mapPathValue(map, "definitions.Customer.views.public.4", "description");
        mapPathValue(map, "definitions.Customer.views.public.5", "displayName");
        mapPathValue(map, "definitions.Customer.views.public.6", "doubleArray");
        mapPathValue(map, "definitions.Customer.views.public.7", "intArray");
        mapPathValue(map, "definitions.Customer.views.public.8", "isCustomer");
        mapPathValue(map, "definitions.Customer.views.public.9", "longArray");
        mapPathValue(map, "definitions.Customer.views.public.10", "loong");
        mapPathValue(map, "definitions.Customer.views.public.11", "name");
        mapPathValue(map, "definitions.Customer.views.public.12", "number");
        mapPathValue(map, "definitions.Customer.views.public.13", "status");
        mapPathValue(map, "definitions.Customer.views.public.14", "street");
        mapPathValue(map, "definitions.Customer.views.ui.0", "birthday");
        mapPathValue(map, "definitions.Customer.views.ui.1", "booleanArray");
        mapPathValue(map, "definitions.Customer.views.ui.2", "city");
        mapPathValue(map, "definitions.Customer.views.ui.3", "count");
        mapPathValue(map, "definitions.Customer.views.ui.4", "description");
        mapPathValue(map, "definitions.Customer.views.ui.5", "displayName");
        mapPathValue(map, "definitions.Customer.views.ui.6", "doubleArray");
        mapPathValue(map, "definitions.Customer.views.ui.7", "intArray");
        mapPathValue(map, "definitions.Customer.views.ui.8", "isCustomer");
        mapPathValue(map, "definitions.Customer.views.ui.9", "longArray");
        mapPathValue(map, "definitions.Customer.views.ui.10", "loong");
        mapPathValue(map, "definitions.Customer.views.ui.11", "name");
        mapPathValue(map, "definitions.Customer.views.ui.12", "number");
        mapPathValue(map, "definitions.Customer.views.ui.13", "status");
        mapPathValue(map, "definitions.Customer.views.ui.14", "street");
        // advanced: test schema roundtrip
        compareSchemaRoundtrip(sourceSchema);
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 5 with JsonType

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

the class StructrGraphQLTest method createTestUserType.

protected Class createTestUserType() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonType type = schema.addType("TestUser");
        type.setExtends(URI.create("#/definitions/Principal"));
        type.overrideMethod("onCreate", true, "setProperty(name, \"test\" + System.currentTimeMillis());");
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    return StructrApp.getConfiguration().getNodeEntityClass("TestUser");
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema)

Aggregations

JsonType (org.structr.schema.json.JsonType)15 JsonSchema (org.structr.schema.json.JsonSchema)13 Tx (org.structr.core.graph.Tx)11 Test (org.junit.Test)9 FrameworkException (org.structr.common.error.FrameworkException)8 GsonBuilder (com.google.gson.GsonBuilder)7 NodeInterface (org.structr.core.graph.NodeInterface)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 File (org.structr.web.entity.File)6 Gson (com.google.gson.Gson)5 LinkedHashMap (java.util.LinkedHashMap)5 URISyntaxException (java.net.URISyntaxException)2 LinkedHashSet (java.util.LinkedHashSet)2 LinkedList (java.util.LinkedList)2 StructrTest (org.structr.common.StructrTest)2 NodeAttribute (org.structr.core.graph.NodeAttribute)2 JsonObjectType (org.structr.schema.json.JsonObjectType)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1