Search in sources :

Example 26 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class GetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final SecurityContext securityContext = ctx.getSecurityContext();
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        final String keyName = sources[1].toString();
        GraphObject dataObject = null;
        // handle GraphObject
        if (sources[0] instanceof GraphObject) {
            dataObject = (GraphObject) sources[0];
        }
        // handle first element of a list of graph objects
        if (sources[0] instanceof List) {
            final List list = (List) sources[0];
            final int size = list.size();
            if (size == 1) {
                final Object value = list.get(0);
                if (value != null) {
                    if (value instanceof GraphObject) {
                        dataObject = (GraphObject) list.get(0);
                    } else {
                        return "get(): first element of collection is of type " + value.getClass() + " which is not supported.";
                    }
                } else {
                    return "get(): first element of collection is null.";
                }
            }
        }
        // handle map separately
        if (sources[0] instanceof Map && !(sources[0] instanceof GraphObjectMap)) {
            final Map map = (Map) sources[0];
            return map.get(keyName);
        }
        // handle request object
        if (sources[0] instanceof HttpServletRequest) {
            final HttpServletRequest request = (HttpServletRequest) sources[0];
            return request.getParameter(keyName);
        }
        if (dataObject != null) {
            final PropertyKey key = StructrApp.key(dataObject.getClass(), keyName);
            if (key != null) {
                final PropertyConverter inputConverter = key.inputConverter(securityContext);
                Object value = dataObject.getProperty(key);
                if (inputConverter != null) {
                    return inputConverter.revert(value);
                }
                return dataObject.getProperty(key);
            }
            return "";
        } else {
            return ERROR_MESSAGE_GET_ENTITY;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) List(java.util.List) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 27 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class GetOrCreateFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final App app = StructrApp.getInstance(securityContext);
        final PropertyMap properties = new PropertyMap();
        // the type to query for
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            final String typeString = sources[0].toString();
            type = config.getNodeEntityClass(typeString);
            if (type == null) {
                logger.warn("Error in get_or_create(): type \"{}\" not found.", typeString);
                return ERROR_MESSAGE_TYPE_NOT_FOUND + typeString;
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in get_or_create(): no type specified. Parameters: {}", getParametersAsString(sources));
            return ERROR_MESSAGE_NO_TYPE_SPECIFIED;
        }
        // experimental: disable result count, prevents instantiation
        // of large collections just for counting all the objects..
        securityContext.ignoreResultCount(true);
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            properties.putAll(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
        } else {
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_GET_OR_CREATE);
            }
            for (int c = 1; c < parameter_count; c += 2) {
                if (sources[c] == null) {
                    throw new IllegalArgumentException();
                }
                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);
                    }
                    properties.put(key, value);
                }
            }
        }
        final GraphObject obj = app.nodeQuery(type).disableSorting().pageSize(1).and(properties).getFirst();
        if (obj != null) {
            // return existing object
            return obj;
        }
        // create new object
        return app.create(type, properties);
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) 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) PropertyKey(org.structr.core.property.PropertyKey)

Example 28 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class PrivilegedFindFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
    if (sources != null) {
        final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Query query = StructrApp.getInstance(securityContext).nodeQuery().sort(GraphObject.createdDate).order(false);
        // the type to query for
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            final String typeString = sources[0].toString();
            type = config.getNodeEntityClass(typeString);
            if (type != null) {
                query.andTypes(type);
            } else {
                logger.warn("Error in find_privileged(): type \"{}\" not found.", typeString);
                return "Error in find_privileged(): type " + typeString + " not found.";
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in find_privileged(): no type specified. Parameters: {}", getParametersAsString(sources));
            return "Error in find_privileged(): no type specified.";
        }
        // experimental: disable result count, prevents instantiation
        // of large collections just for counting all the objects..
        securityContext.ignoreResultCount(true);
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
        } else if (sources.length == 2) {
            if (sources[1] == null) {
                throw new IllegalArgumentException();
            }
            // special case: second parameter is a UUID
            final PropertyKey key = StructrApp.key(type, "id");
            query.and(key, sources[1].toString());
            final int resultCount = query.getResult().size();
            switch(resultCount) {
                case 1:
                    return query.getFirst();
                case 0:
                    return null;
                default:
                    throw new FrameworkException(400, "Multiple Objects found for id! [" + sources[1].toString() + "]");
            }
        } else {
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_PRIVILEGEDFIND);
            }
            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);
                    }
                    query.and(key, value);
                }
            }
        }
        return query.getAsList();
    }
    return "";
}
Also used : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) 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) PropertyKey(org.structr.core.property.PropertyKey)

Example 29 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class KeysFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        if (sources.length == 2 && sources[0] != null && sources[0] instanceof GraphObject && sources[1] != null) {
            final Set<String> keys = new LinkedHashSet<>();
            final GraphObject source = (GraphObject) sources[0];
            for (final PropertyKey key : source.getPropertyKeys(sources[1].toString())) {
                keys.add(key.jsonName());
            }
            return new LinkedList<>(keys);
        } else if (sources.length == 1 && sources[0] != null && sources[0] instanceof GraphObjectMap) {
            return new LinkedList<>(((GraphObjectMap) sources[0]).keySet());
        } else if (sources.length == 1 && sources[0] != null && sources[0] instanceof Map) {
            return new LinkedList<>(((Map) sources[0]).keySet());
        } else {
            return null;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Example 30 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class AccessControlTest method test07ResultCountWithPaging.

@Test
public void test07ResultCountWithPaging() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        final Class type = TestOne.class;
        final List<NodeInterface> nodes = createTestNodes(type, 10);
        int count = 0;
        try (final Tx tx = app.tx()) {
            // add names to make sorting work...
            for (final NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, "node0" + count++);
            }
            nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(9).setProperty(AbstractNode.visibleToPublicUsers, true);
            tx.success();
        }
        SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
        PropertyKey sortKey = AbstractNode.name;
        boolean sortDesc = false;
        int pageSize = 2;
        int page = 1;
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            assertEquals(2, result.size());
            assertEquals(4, (int) result.getRawResultCount());
            assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
            assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Aggregations

PropertyKey (org.structr.core.property.PropertyKey)177 FrameworkException (org.structr.common.error.FrameworkException)108 Test (org.junit.Test)69 NodeInterface (org.structr.core.graph.NodeInterface)62 Tx (org.structr.core.graph.Tx)61 GraphObject (org.structr.core.GraphObject)59 StructrTest (org.structr.common.StructrTest)39 PropertyMap (org.structr.core.property.PropertyMap)37 List (java.util.List)31 Result (org.structr.core.Result)28 ConfigurationProvider (org.structr.schema.ConfigurationProvider)27 SecurityContext (org.structr.common.SecurityContext)26 LinkedList (java.util.LinkedList)22 StringProperty (org.structr.core.property.StringProperty)22 ErrorToken (org.structr.common.error.ErrorToken)20 Map (java.util.Map)18 PropertyConverter (org.structr.core.converter.PropertyConverter)18 NodeAttribute (org.structr.core.graph.NodeAttribute)17 App (org.structr.core.app.App)16 StructrApp (org.structr.core.app.StructrApp)16