Search in sources :

Example 1 with ConfigurationProvider

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");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ActionContext(org.structr.schema.action.ActionContext) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) Collection(java.util.Collection) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with ConfigurationProvider

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");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) StringProperty(org.structr.core.property.StringProperty) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with ConfigurationProvider

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 "";
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 4 with ConfigurationProvider

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);
    }
}
Also used : Argument(graphql.language.Argument) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ObjectValue(graphql.language.ObjectValue) Value(graphql.language.Value) StringValue(graphql.language.StringValue) IntValue(graphql.language.IntValue) BooleanValue(graphql.language.BooleanValue) Occurrence(org.structr.api.search.Occurrence) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Example 5 with ConfigurationProvider

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurationProvider(org.structr.schema.ConfigurationProvider) LinkedList(java.util.LinkedList) MutableTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableTypeDefinition) MutablePolicyTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutablePolicyTypeDefinition) MutableRelationshipTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableRelationshipTypeDefinition) MutableFolderTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableFolderTypeDefinition) TypeDefinition(org.apache.chemistry.opencmis.commons.definitions.TypeDefinition) MutableDocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableDocumentTypeDefinition) MutableSecondaryTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableSecondaryTypeDefinition) MutableItemTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.MutableItemTypeDefinition)

Aggregations

ConfigurationProvider (org.structr.schema.ConfigurationProvider)50 PropertyKey (org.structr.core.property.PropertyKey)27 FrameworkException (org.structr.common.error.FrameworkException)25 GraphObject (org.structr.core.GraphObject)17 Tx (org.structr.core.graph.Tx)15 LinkedList (java.util.LinkedList)14 PropertyMap (org.structr.core.property.PropertyMap)14 NodeInterface (org.structr.core.graph.NodeInterface)12 Test (org.junit.Test)11 Map (java.util.Map)10 PropertyConverter (org.structr.core.converter.PropertyConverter)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SecurityContext (org.structr.common.SecurityContext)9 App (org.structr.core.app.App)9 StructrApp (org.structr.core.app.StructrApp)9 SchemaNode (org.structr.core.entity.SchemaNode)8 File (org.structr.web.entity.File)8 LinkedHashMap (java.util.LinkedHashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6