use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class AbstractHintProvider method getAllHints.
protected List<Hint> getAllHints(final GraphObject currentNode, final String currentToken, final String previousToken, final String thirdToken) {
final boolean isDeclaration = isJavascript() && "var".equals(previousToken);
final boolean isAssignment = isJavascript() && "=".equals(previousToken);
final boolean isDotNotationRequest = ".".equals(currentToken);
final ConfigurationProvider config = StructrApp.getConfiguration();
final Map<String, DataKey> dataKeys = new TreeMap<>();
final List<Hint> hints = new LinkedList<>();
final List<Hint> local = new LinkedList<>();
Class currentObjectType = null;
// data key etc. hints
if (currentNode != null) {
recursivelyFindDataKeys(currentNode, dataKeys);
}
switch(previousToken) {
case "current":
currentObjectType = AbstractNode.class;
break;
case "this":
currentObjectType = DOMNode.class;
break;
case "me":
currentObjectType = User.class;
break;
case "page":
currentObjectType = Page.class;
break;
case "link":
currentObjectType = File.class;
break;
case "template":
currentObjectType = Template.class;
break;
case "parent":
currentObjectType = DOMElement.class;
break;
default:
DataKey key = dataKeys.get(previousToken);
if (key != null) {
currentObjectType = key.identifyType(config);
} else if (StringUtils.isNotBlank(thirdToken)) {
key = dataKeys.get(thirdToken);
if (key != null) {
currentObjectType = key.identifyType(config);
if (currentObjectType != null) {
final PropertyKey nestedKey = StructrApp.key(currentObjectType, previousToken);
if (nestedKey != null) {
currentObjectType = nestedKey.relatedType();
}
}
}
}
break;
}
if (!keywords.contains(previousToken) && !isDotNotationRequest && !dataKeys.containsKey(previousToken)) {
if (!isAssignment) {
for (final Function<Object, Object> func : Functions.getFunctions()) {
hints.add(func);
}
}
Collections.sort(hints, comparator);
// non-function hints
local.add(createHint("current", "", "Current data object", !isJavascript() ? null : "get('current')"));
local.add(createHint("request", "", "Current request object", !isJavascript() ? null : "get('request')"));
local.add(createHint("this", "", "Current object", !isJavascript() ? null : "get('this')"));
local.add(createHint("element", "", "Current object", !isJavascript() ? null : "get('element')"));
local.add(createHint("page", "", "Current page", !isJavascript() ? null : "get('page')"));
local.add(createHint("link", "", "Current link", !isJavascript() ? null : "get('link')"));
local.add(createHint("template", "", "Closest template node", !isJavascript() ? null : "get('template')"));
local.add(createHint("parent", "", "Parent node", !isJavascript() ? null : "get('parent')"));
local.add(createHint("children", "", "Collection of child nodes", !isJavascript() ? null : "get('children')"));
local.add(createHint("host", "", "Client's host name", !isJavascript() ? null : "get('host')"));
local.add(createHint("port", "", "Client's port", !isJavascript() ? null : "get('port')"));
local.add(createHint("path_info", "", "URL path", !isJavascript() ? null : "get('path_info')"));
local.add(createHint("now", "", "Current date", !isJavascript() ? null : "get('now')"));
local.add(createHint("me", "", "Current user", !isJavascript() ? null : "get('me)"));
local.add(createHint("locale", "", "Current locale", !isJavascript() ? null : "get('locale')"));
}
// add local hints to the beginning of the list
Collections.sort(local, comparator);
hints.addAll(0, local);
// prepend data keys
if (currentObjectType == null && !dataKeys.containsKey(previousToken) && !isDotNotationRequest || isAssignment) {
for (final DataKey dataKey : dataKeys.values()) {
final String replacement = isJavascript() && !isDeclaration ? "get('" + dataKey.getDataKey() + "')" : null;
final Hint dataKeyHint = createHint(dataKey.getDataKey(), "", dataKey.getDescription(), replacement);
// disable replacement with "Structr.get(...)" when in Javascript declaration
dataKeyHint.allowNameModification(!isDeclaration);
hints.add(0, dataKeyHint);
}
}
// prepend property keys of current object type
collectHintsForType(hints, config, currentObjectType);
return hints;
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class StructrApp method key.
public static <T> PropertyKey<T> key(final Class type, final String name) {
final ConfigurationProvider config = StructrApp.getConfiguration();
PropertyKey<T> key = config.getPropertyKeyForJSONName(type, name, false);
if (key == null) {
// not found, next try: dynamic type
final Class dynamicType = config.getNodeEntityClass(type.getSimpleName());
if (dynamicType != null) {
key = config.getPropertyKeyForJSONName(dynamicType, name, false);
} else {
// next try: interface
final Class iface = config.getInterfaces().get(type.getSimpleName());
if (iface != null) {
key = config.getPropertyKeyForJSONName(iface, name, false);
}
}
}
if (key != null) {
return key;
}
logger.warn("Unknown property key {}.{}!", type.getSimpleName(), name);
Thread.dumpStack();
return null;
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class CsvImportTest method testCsvFileImportSingleQuotes.
@Test
public void testCsvFileImportSingleQuotes() {
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.ConfigurationProvider in project structr by structr.
the class CsvImportTest method testCsvFileImportNoQuotes.
@Test
public void testCsvFileImportNoQuotes() {
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.ConfigurationProvider in project structr by structr.
the class CsvImportTest method testCsvFileImportSingleQuotesLineBreakInCell.
@Test
public void testCsvFileImportSingleQuotesLineBreakInCell() {
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:\none';'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:\none", 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.");
}
}
Aggregations