use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class CreateFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (sources != null && sources.length > 0) {
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
PropertyMap propertyMap;
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
type = config.getNodeEntityClass(sources[0].toString());
}
if (type == null) {
throw new FrameworkException(422, "Unknown type '" + sources[0].toString() + "' in create() method!");
}
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
} else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
} else {
propertyMap = new PropertyMap();
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_CREATE_JS : ERROR_MESSAGE_CREATE));
}
for (int c = 1; c < parameter_count; c += 2) {
final PropertyKey key = StructrApp.key(type, sources[c].toString());
if (key != null) {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object value = sources[c + 1];
if (inputConverter != null) {
value = inputConverter.convert(value);
}
propertyMap.put(key, value);
}
}
}
return StructrApp.getInstance(securityContext).create(type, propertyMap);
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class PropertyInfoFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
return null;
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final String typeName = sources[0].toString();
final String keyName = sources[1].toString();
Class type = config.getNodeEntityClass(typeName);
if (type == null) {
type = config.getRelationshipEntityClass(typeName);
}
if (type != null) {
final PropertyKey key = StructrApp.key(type, keyName);
if (key != null) {
return SchemaHelper.getPropertyInfo(ctx.getSecurityContext(), key);
} else {
logger.warn("Error: Unknown property \"{}.{}\". Parameters: {}", new Object[] { typeName, keyName, getParametersAsString(sources) });
return "Unknown property " + typeName + "." + keyName;
}
} else {
logger.warn("Error: Unknown type \"{}\". Parameters: {}", new Object[] { typeName, getParametersAsString(sources) });
return "Unknown type " + typeName;
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class MergePropertiesFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasMinLengthAndAllElementsNotNull(sources, 2) && sources[0] instanceof GraphObject && sources[1] instanceof GraphObject) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Set<PropertyKey> mergeKeys = new LinkedHashSet<>();
final GraphObject source = (GraphObject) sources[0];
final GraphObject target = (GraphObject) sources[1];
final int paramCount = sources.length;
for (int i = 2; i < paramCount; i++) {
final String keyName = sources[i].toString();
final PropertyKey key = StructrApp.key(target.getClass(), keyName);
mergeKeys.add(key);
}
for (final PropertyKey key : mergeKeys) {
final Object sourceValue = source.getProperty(key);
if (sourceValue != null) {
target.setProperty(key, sourceValue);
}
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return "";
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class RenderContextTest method testNotionTransformedPropertyAccess.
@Test
public void testNotionTransformedPropertyAccess() {
NodeInterface project = null;
NodeInterface task1 = null;
NodeInterface task2 = null;
NodeInterface task3 = null;
try (final Tx tx = app.tx()) {
final SchemaNode projectNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Project"), new NodeAttribute(new StringProperty("_taskList"), "Notion(tasks, id, name)"), new NodeAttribute(new StringProperty("_taskNames"), "Notion(tasks, name)"));
final SchemaNode taskNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Task"));
// create schema relationship
final PropertyMap taskProperties = new PropertyMap();
taskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
taskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
taskProperties.put(SchemaRelationshipNode.relationshipType, "TASK");
taskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
taskProperties.put(SchemaRelationshipNode.targetMultiplicity, "*");
taskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
taskProperties.put(SchemaRelationshipNode.targetJsonName, "tasks");
app.create(SchemaRelationshipNode.class, taskProperties);
// create schema relationship
final PropertyMap currentTaskProperties = new PropertyMap();
currentTaskProperties.put(SchemaRelationshipNode.sourceNode, projectNode);
currentTaskProperties.put(SchemaRelationshipNode.targetNode, taskNode);
currentTaskProperties.put(SchemaRelationshipNode.relationshipType, "CURRENT");
currentTaskProperties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
currentTaskProperties.put(SchemaRelationshipNode.targetMultiplicity, "1");
currentTaskProperties.put(SchemaRelationshipNode.sourceJsonName, "project");
currentTaskProperties.put(SchemaRelationshipNode.targetJsonName, "currentTask");
app.create(SchemaRelationshipNode.class, currentTaskProperties);
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class projectClass = config.getNodeEntityClass("Project");
final Class taskClass = config.getNodeEntityClass("Task");
final PropertyKey currentTaskKey = StructrApp.key(projectClass, "currentTask");
final PropertyKey tasksKey = StructrApp.key(projectClass, "tasks");
// create parent/child relationship
try (final Tx tx = app.tx()) {
project = app.create(projectClass, new NodeAttribute(SchemaNode.name, "Project1"));
task1 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task1"));
task2 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task2"));
task3 = app.create(taskClass, new NodeAttribute(SchemaNode.name, "Task3"));
// add task to project
final List tasks = new LinkedList<>();
tasks.add(task1);
tasks.add(task2);
tasks.add(task3);
final PropertyMap projectProperties = new PropertyMap();
projectProperties.put(tasksKey, tasks);
projectProperties.put(currentTaskKey, task3);
project.setProperties(project.getSecurityContext(), projectProperties);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
final RenderContext renderContext = new RenderContext(securityContext);
renderContext.putDataObject("project", project);
renderContext.putDataObject("task", task1);
assertEquals("Invalid dot syntax result: ", "Project1", Scripting.replaceVariables(renderContext, project, "${project.name}"));
assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.tasks[0].name}"));
assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.tasks[1].name}"));
assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.tasks[2].name}"));
assertEquals("Invalid dot syntax result: ", "[Task1, Task2, Task3]", Scripting.replaceVariables(renderContext, project, "${project.taskNames}"));
assertEquals("Invalid dot syntax result: ", "Task1", Scripting.replaceVariables(renderContext, project, "${project.taskNames[0]}"));
assertEquals("Invalid dot syntax result: ", "Task2", Scripting.replaceVariables(renderContext, project, "${project.taskNames[1]}"));
assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.taskNames[2]}"));
assertEquals("Invalid dot syntax result: ", "Task3", Scripting.replaceVariables(renderContext, project, "${project.currentTask.name}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class PropertyTest method testFunctionPropertyIndexing.
// ----- function property tests -----
/**
* This test creates a new type "Test" and links it to
* the built-in type "Group". It then creates a function
* property that references the name of the related group
* and assumes that a test entity is found by its related
* group name.
*/
@Test
public void testFunctionPropertyIndexing() {
// schema setup
try (final Tx tx = app.tx()) {
final SchemaNode group = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testFunction"), "Function(this.group.name)"));
assertNotNull("Invalid schema setup result", group);
assertNotNull("Invalid schema setup result", test);
app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, test), new NodeAttribute<>(SchemaRelationshipNode.targetNode, group), new NodeAttribute<>(SchemaRelationshipNode.sourceMultiplicity, "*"), new NodeAttribute<>(SchemaRelationshipNode.targetMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "tests"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "group"), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "group"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// entity setup
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
// create test type without link to group!
app.create(testType);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// entity setup
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
final Class groupType = config.getNodeEntityClass("Group");
final GraphObject group = app.create(groupType, "testgroup");
final GraphObject test = app.nodeQuery(testType).getFirst();
// create Test with link to Group
test.setProperty(StructrApp.key(testType, "group"), group);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// test
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
final PropertyKey key = StructrApp.key(testType, "testFunction");
// fetch test node
final GraphObject testNode = app.nodeQuery(testType).getFirst();
final GraphObject result = app.nodeQuery(testType).and(key, "testgroup").getFirst();
// test indexing
assertEquals("Invalid FunctionProperty indexing result", testNode, result);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
Aggregations