use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class RenderContextTest method testVariableReplacementInDynamicTypes.
@Test
public void testVariableReplacementInDynamicTypes() {
SchemaNode itemNode = null;
NodeInterface parent = null;
NodeInterface child1 = null;
NodeInterface child2 = null;
try (final Tx tx = app.tx()) {
itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
final PropertyMap properties = new PropertyMap();
properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
properties.put(SchemaRelationshipNode.targetJsonName, "children");
app.create(SchemaRelationshipNode.class, properties);
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
// create parent/child relationship
try (final Tx tx = app.tx()) {
parent = app.create(itemClass);
child1 = app.create(itemClass);
child2 = app.create(itemClass);
final List<NodeInterface> children = new LinkedList<>();
children.add(child1);
children.add(child2);
parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// verify that parent has two children
try (final Tx tx = app.tx()) {
// verify that parentItem can be accessed....
final Object value = parent.getProperty(childrenProperty);
assertTrue(value instanceof Collection);
final Collection coll = (Collection) value;
assertEquals(2, coll.size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class RenderContextTest method testFunctionEvaluationInDynamicTypes.
@Test
public void testFunctionEvaluationInDynamicTypes() {
NodeInterface item = null;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testMethodCalled"), "Boolean"), new NodeAttribute(new StringProperty("___testMethod"), "set(this, 'testMethodCalled', true)"));
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
// create parent/child relationship
try (final Tx tx = app.tx()) {
item = app.create(itemClass, new NodeAttribute(SchemaNode.name, "Item"));
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("item", item);
assertEquals("Invalid combined array dot syntax result: ", "Item", Scripting.replaceVariables(renderContext, item, "${find('Item')[0].name}"));
Scripting.replaceVariables(renderContext, item, "${item.testMethod()}");
assertEquals("Invalid method evaluation result: ", "true", Scripting.replaceVariables(renderContext, item, "${item.testMethodCalled}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class SetFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
Class type = null;
PropertyMap propertyMap = null;
if (sources[0] instanceof GraphObject) {
final GraphObject source = (GraphObject) sources[0];
type = source.getEntityType();
}
if (type == null) {
throw new FrameworkException(422, "Can't get type of object '" + sources[0].toString() + "' in set() method!");
}
final GraphObject sourceObject = (GraphObject) sources[0];
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 if (sources.length == 2 && sources[1] instanceof String) {
final Gson gson = new GsonBuilder().create();
final Map<String, Object> values = deserialize(gson, sources[1].toString());
if (values != null) {
propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, values);
}
} else if (sources.length > 2) {
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_SET_JS : ERROR_MESSAGE_SET));
}
for (int c = 1; c < parameter_count; c += 2) {
final PropertyKey key = config.getPropertyKeyForJSONName(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);
}
}
} else {
throw new FrameworkException(422, "Invalid use of builtin method set, usage: set(entity, params..)");
}
if (propertyMap != null) {
sourceObject.setProperties(securityContext, propertyMap);
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return "";
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class QueryConfig method handleFieldArguments.
public void handleFieldArguments(final SecurityContext securityContext, final Class type, final Field parentField, final Field field) throws FrameworkException {
final ConfigurationProvider config = StructrApp.getConfiguration();
final PropertyKey parentKey = config.getPropertyKeyForJSONName(type, parentField.getName());
final PropertyKey key = config.getPropertyKeyForJSONName(type, field.getName(), false);
final List<SearchTuple> searchTuples = new LinkedList<>();
Occurrence occurrence = Occurrence.REQUIRED;
// parse arguments
for (final Argument argument : field.getArguments()) {
final String name = argument.getName();
final Value value = argument.getValue();
switch(name) {
case "_equals":
searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), true));
break;
case "_contains":
searchTuples.add(new SearchTuple(castValue(securityContext, key.relatedType(), key, value), false));
break;
case "_conj":
occurrence = getOccurrence(value);
break;
default:
break;
}
}
// only add field if a value was set
for (final SearchTuple tuple : searchTuples) {
addAttribute(parentKey, key.getSearchAttribute(securityContext, occurrence, tuple.value, tuple.exact, null), occurrence);
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class CMISRepositoryService method getTypeChildren.
private List<TypeDefinition> getTypeChildren(final String typeId, final Boolean includePropertyDefinitions) {
final Set<String> subtypes = new LinkedHashSet<>(SearchCommand.getAllSubtypesAsStringSet(typeId));
final ConfigurationProvider config = StructrApp.getConfiguration();
final List<TypeDefinition> result = new LinkedList<>();
// subtypes set from Structr contains initial type as well..
subtypes.remove(typeId);
for (final String subtype : subtypes) {
final Class subclass = config.getNodeEntityClass(subtype);
if (subclass != null) {
final TypeDefinition extendedTypeDefinition = extendTypeDefinition(subclass, includePropertyDefinitions);
if (extendedTypeDefinition != null) {
result.add(extendedTypeDefinition);
}
}
}
return result;
}
Aggregations