use of org.structr.schema.json.JsonType in project structr by structr.
the class StructrTypeDefinition method createDatabaseSchema.
AbstractSchemaNode createDatabaseSchema(final App app) throws FrameworkException {
final Map<String, SchemaProperty> schemaProperties = new TreeMap<>();
final Map<String, SchemaMethod> schemaMethods = new TreeMap<>();
final PropertyMap createProperties = new PropertyMap();
final PropertyMap nodeProperties = new PropertyMap();
// properties that always need to be set
createProperties.put(SchemaNode.isInterface, isInterface);
createProperties.put(SchemaNode.isAbstract, isAbstract);
createProperties.put(SchemaNode.category, category);
createProperties.put(SchemaNode.isBuiltinType, SchemaService.DynamicSchemaRootURI.equals(root.getId()));
final T schemaNode = createSchemaNode(app, createProperties);
for (final StructrPropertyDefinition property : properties) {
final SchemaProperty schemaProperty = property.createDatabaseSchema(app, schemaNode);
if (schemaProperty != null) {
schemaProperties.put(schemaProperty.getName(), schemaProperty);
}
}
// create views and associate the properties
for (final Entry<String, Set<String>> view : views.entrySet()) {
final List<SchemaProperty> viewProperties = new LinkedList<>();
final List<String> nonGraphProperties = new LinkedList<>();
for (final String propertyName : view.getValue()) {
final SchemaProperty property = schemaProperties.get(propertyName);
if (property != null) {
viewProperties.add(property);
} else {
nonGraphProperties.add(propertyName);
}
}
SchemaView viewNode = app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(SchemaView.name, view.getKey()).getFirst();
if (viewNode == null) {
viewNode = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.name, view.getKey()));
}
final PropertyMap updateProperties = new PropertyMap();
updateProperties.put(SchemaView.schemaProperties, viewProperties);
updateProperties.put(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ", "));
// update properties of existing or new schema view node
viewNode.setProperties(SecurityContext.getSuperUserInstance(), updateProperties);
}
for (final StructrMethodDefinition method : methods) {
final SchemaMethod schemaMethod = method.createDatabaseSchema(app, schemaNode);
if (schemaMethod != null) {
schemaMethods.put(schemaMethod.getName(), schemaMethod);
}
}
// extends
if (baseTypeReference != null) {
final Object def = root.resolveURI(baseTypeReference);
if (def != null && def instanceof JsonType) {
final JsonType jsonType = (JsonType) def;
final String superclassName = "org.structr.dynamic." + jsonType.getName();
nodeProperties.put(SchemaNode.extendsClass, superclassName);
} else {
final Class superclass = StructrApp.resolveSchemaId(baseTypeReference);
if (superclass != null) {
if (superclass.isInterface()) {
nodeProperties.put(SchemaNode.implementsInterfaces, superclass.getName());
} else {
nodeProperties.put(SchemaNode.extendsClass, superclass.getName());
}
} else if ("https://structr.org/v1.1/definitions/FileBase".equals(baseTypeReference.toString())) {
// FileBase doesn't exist any more, but we need to support it for some time..
nodeProperties.put(SchemaNode.implementsInterfaces, "org.structr.web.entity.File");
} else if (!StructrApp.getSchemaBaseURI().relativize(baseTypeReference).isAbsolute()) {
// resolve internal type referenced in special URI
final URI base = StructrApp.getSchemaBaseURI().resolve("definitions/");
final URI type = base.relativize(baseTypeReference);
final String typeName = type.getPath();
final String parameters = type.getQuery();
final Class internal = StructrApp.getConfiguration().getNodeEntityClass(typeName);
if (internal != null) {
nodeProperties.put(SchemaNode.extendsClass, getParameterizedType(internal, parameters));
}
}
}
}
// implements
if (!implementedInterfaces.isEmpty()) {
final Set<String> interfaces = new LinkedHashSet<>();
for (final URI implementedInterface : implementedInterfaces) {
final Object def = root.resolveURI(implementedInterface);
if (def != null && def instanceof JsonType) {
final JsonType jsonType = (JsonType) def;
final String superclassName = "org.structr.dynamic." + jsonType.getName();
if (jsonType.isInterface()) {
interfaces.add(superclassName);
} else {
nodeProperties.put(SchemaNode.extendsClass, superclassName);
}
} else {
final Class superclass = StructrApp.resolveSchemaId(implementedInterface);
if (superclass != null) {
interfaces.add(superclass.getName());
} else {
logger.warn("Unable to resolve built-in type {} against Structr schema", implementedInterface);
StructrApp.resolveSchemaId(implementedInterface);
}
}
}
nodeProperties.put(SchemaNode.implementsInterfaces, StringUtils.join(interfaces, ", "));
}
schemaNode.setProperties(SecurityContext.getSuperUserInstance(), nodeProperties);
return schemaNode;
}
use of org.structr.schema.json.JsonType 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.JsonType in project structr by structr.
the class AdvancedSchemaTest method testViewProperties.
@Test
public void testViewProperties() {
try (final Tx tx = app.tx()) {
final JsonSchema schema = StructrSchema.createFromDatabase(app);
final JsonType type = schema.addType("Test");
type.addStringProperty("test", PropertyView.Public);
type.addViewProperty(PropertyView.Public, "test");
StructrSchema.extendDatabaseSchema(app, schema);
tx.success();
} catch (Throwable t) {
t.printStackTrace();
}
}
use of org.structr.schema.json.JsonType in project structr by structr.
the class CsvImportTest method testCsvFileImportDoubleQuotes.
@Test
public void testCsvFileImportDoubleQuotes() {
String newFileId = null;
// test setup
try (final Tx tx = app.tx()) {
final String csvData = "\"id\";\"type\";\"name\";\"Test header with whitespace\";\"ümläüt header\"\n" + "\"0\";\"One\";\"name: one\";\"11\";\"22\"\n" + "\"1\";\"Two\";\"name: two\";\"22\";\"33\"\n" + "\"2\";\"Three\";\"name: three\";\"33\";\"44\"";
final byte[] fileData = csvData.getBytes("utf-8");
final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
// extract UUID for later use
newFileId = file.getUuid();
// create new type
final JsonSchema schema = StructrSchema.createEmptySchema();
final JsonType newType = schema.addType("Item");
newType.addStringProperty("name");
newType.addIntegerProperty("originId").isIndexed();
newType.addStringProperty("typeName");
newType.addIntegerProperty("test1");
newType.addIntegerProperty("test2");
StructrSchema.extendDatabaseSchema(app, schema);
// create test user
app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
tx.success();
} catch (Throwable t) {
t.printStackTrace();
fail("Unexpected exception.");
}
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final Map<String, Object> params = new LinkedHashMap<>();
final Map<String, Object> mappings = new LinkedHashMap<>();
// import parameters
params.put("targetType", "Item");
params.put("quoteChar", "\"");
params.put("delimiter", ";");
params.put("mappings", mappings);
// property mapping
mappings.put("originId", "id");
mappings.put("typeName", "type");
mappings.put("name", "name");
mappings.put("test1", "Test header with whitespace");
mappings.put("test2", "ümläüt header");
RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
// wait for result (import is async.)
try {
Thread.sleep(2000);
} catch (Throwable t) {
}
// check imported data for correct import
try (final Tx tx = app.tx()) {
final ConfigurationProvider conf = StructrApp.getConfiguration();
final Class type = conf.getNodeEntityClass("Item");
final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
final NodeInterface one = items.get(0);
final NodeInterface two = items.get(1);
final NodeInterface three = items.get(2);
assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.json.JsonType in project structr by structr.
the class XmlImportTest method testXmlFileImport.
@Test
public void testXmlFileImport() {
final String htmlPropertyData = "<!DOCTYPE html><html><head><title>bad idea</title></head><body><h1>ORLY?</h1></body></html>";
String newFileId = null;
// test setup
try (final Tx tx = app.tx()) {
final String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "<items>\n" + " <item id=\"0\" type=\"One\" name=\"name: one\">\n" + " <test1><![CDATA[11]]></test1>\n" + " <test2>22</test2>\n" + " </item>\n" + " <item id=\"1\" type=\"Two\" name=\"name: two\" >\n" + " <test1>22</test1>\n" + " <test2>33</test2>\n" + " </item>\n" + " <item id=\"2\" type=\"Three\" name=\"name: three\" >\n" + " <test1>33</test1>\n" + " <test2>44</test2>\n" + " <test3><![CDATA[" + htmlPropertyData + "]]></test3>\n" + " </item>\n" + "</items>\n";
final byte[] fileData = xmlData.getBytes("utf-8");
final File file = FileHelper.createFile(securityContext, fileData, "application/xml", File.class, "test.xml");
// extract UUID for later use
newFileId = file.getUuid();
// create new type
final JsonSchema schema = StructrSchema.createEmptySchema();
final JsonType newType = schema.addType("Item");
newType.addStringProperty("name");
newType.addIntegerProperty("originId").isIndexed();
newType.addStringProperty("typeName");
newType.addIntegerProperty("test1");
newType.addIntegerProperty("test2");
newType.addStringProperty("test3");
StructrSchema.extendDatabaseSchema(app, schema);
// create test user
app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
tx.success();
} catch (Throwable t) {
t.printStackTrace();
fail("Unexpected exception.");
}
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final Map<String, Object> params = new LinkedHashMap<>();
final Map<String, Object> itemConfig = new LinkedHashMap<>();
final Map<String, Object> itemProperties = new LinkedHashMap<>();
final Map<String, Object> test1Config = new LinkedHashMap<>();
final Map<String, Object> test2Config = new LinkedHashMap<>();
final Map<String, Object> test3Config = new LinkedHashMap<>();
params.put("/items/item", itemConfig);
params.put("/items/item/test1", test1Config);
params.put("/items/item/test2", test2Config);
params.put("/items/item/test3", test3Config);
// import parameters
itemConfig.put("action", "createNode");
itemConfig.put("isRoot", true);
itemConfig.put("type", "Item");
itemConfig.put("properties", itemProperties);
// property mapping
itemProperties.put("id", "originId");
itemProperties.put("type", "typeName");
itemProperties.put("name", "name");
test1Config.put("action", "setProperty");
test1Config.put("propertyName", "test1");
test2Config.put("action", "setProperty");
test2Config.put("propertyName", "test2");
test3Config.put("action", "setProperty");
test3Config.put("propertyName", "test3");
RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doXMLImport");
// wait for result (import is async.)
try {
Thread.sleep(300);
} catch (Throwable t) {
}
// check imported data for correct import
try (final Tx tx = app.tx()) {
final ConfigurationProvider conf = StructrApp.getConfiguration();
final Class type = conf.getNodeEntityClass("Item");
final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
assertEquals("Invalid XML import result, expected 3 items to be created from XML import. ", 3, items.size());
final NodeInterface one = items.get(0);
final NodeInterface two = items.get(1);
final NodeInterface three = items.get(2);
assertEquals("Invalid XML mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid XML mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid XML mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid XML mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid XML mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid XML mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid XML mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid XML mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid XML mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid XML mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid XML mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid XML mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid XML mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid XML mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid XML mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid XML mapping result", null, one.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
assertEquals("Invalid XML mapping result", null, two.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
assertEquals("Invalid XML mapping result", htmlPropertyData, three.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
Aggregations