Search in sources :

Example 21 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class ResetPasswordResource method doPost.

@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
    if (propertySet.containsKey("eMail")) {
        final String emailString = (String) propertySet.get("eMail");
        if (StringUtils.isEmpty(emailString)) {
            return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
        }
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final PropertyKey<String> confirmationKey = StructrApp.key(User.class, "confirmationKey");
        final PropertyKey<String> eMail = StructrApp.key(User.class, "eMail");
        final String localeString = (String) propertySet.get("locale");
        final String confKey = UUID.randomUUID().toString();
        final Principal user = StructrApp.getInstance().nodeQuery(User.class).and(eMail, emailString).getFirst();
        if (user != null) {
            // update confirmation key
            user.setProperties(SecurityContext.getSuperUserInstance(), new PropertyMap(confirmationKey, confKey));
            if (!sendResetPasswordLink(user, propertySet, localeString, confKey)) {
                // return 400 Bad request
                return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
            }
            // return 200 OK
            return new RestMethodResult(HttpServletResponse.SC_OK);
        } else {
            // so we're failing silently here
            return new RestMethodResult(HttpServletResponse.SC_OK);
        }
    } else {
        // return 400 Bad request
        return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) ConfigurationProvider(org.structr.schema.ConfigurationProvider) RestMethodResult(org.structr.rest.RestMethodResult) Principal(org.structr.core.entity.Principal)

Example 22 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class ListSchemaPropertiesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String view = (String) webSocketData.getNodeData().get("view");
    final String id = webSocketData.getId();
    final List<GraphObject> result = new LinkedList();
    if (view != null) {
        if (id != null) {
            AbstractNode schemaObject = getNode(id);
            if (schemaObject != null) {
                final ConfigurationProvider config = StructrApp.getConfiguration();
                String typeName = schemaObject.getProperty(AbstractNode.name);
                if (typeName == null && schemaObject instanceof SchemaRelationshipNode) {
                    typeName = ((SchemaRelationshipNode) schemaObject).getClassName();
                }
                Class type = config.getNodeEntityClass(typeName);
                if (type == null || GenericNode.class.equals(type)) {
                    type = config.getRelationshipEntityClass(typeName);
                }
                if (type != null) {
                    final Set<PropertyKey> allProperties = config.getPropertySet(type, PropertyView.All);
                    final Set<PropertyKey> viewProperties = config.getPropertySet(type, view);
                    final Set<PropertyKey> parentProperties = config.getPropertySet(type.getSuperclass(), view);
                    for (final PropertyKey key : allProperties) {
                        final String declaringClass = key.getDeclaringClass() != null ? key.getDeclaringClass().getSimpleName() : "GraphObject";
                        final String propertyName = key.jsonName();
                        final GraphObjectMap property = new GraphObjectMap();
                        final Class valueType = key.valueType();
                        String valueTypeName = "Unknown";
                        boolean _isDisabled = false;
                        if (valueType != null) {
                            valueTypeName = valueType.getSimpleName();
                        }
                        // (since it has to be configured there instead of locally)
                        if (parentProperties.contains(key)) {
                            _isDisabled = true;
                        }
                        property.put(AbstractNode.name, propertyName);
                        property.put(isSelected, viewProperties.contains(key));
                        property.put(isDisabled, _isDisabled);
                        property.put(SchemaProperty.propertyType, valueTypeName);
                        property.put(SchemaProperty.notNull, key.isNotNull());
                        property.put(SchemaProperty.unique, key.isUnique());
                        property.put(SchemaProperty.isDynamic, key.isDynamic());
                        property.put(SchemaProperty.declaringClass, declaringClass);
                        // store in result
                        result.add(property);
                    }
                } else {
                    getWebSocket().send(MessageBuilder.status().code(404).message("Type " + typeName + " not found.").build(), true);
                }
            } else {
                getWebSocket().send(MessageBuilder.status().code(404).message("Schema node with ID " + id + " not found.").build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(422).message("LIST_SCHEMA_PROPERTIES needs an ID.").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("LIST_SCHEMA_PROPERTIES needs a view name in nodeData.").build(), true);
    }
    webSocketData.setView(PropertyView.Ui);
    webSocketData.setResult(result);
    webSocketData.setRawResultCount(1);
    // send only over local connection
    getWebSocket().send(webSocketData, true);
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GenericNode(org.structr.core.entity.GenericNode) GraphObject(org.structr.core.GraphObject) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 23 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class PathResolvingComparator method resolve.

// ----- private methods -----
private Comparable resolve(final GraphObject obj, final String path) {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final String[] parts = path.split("[\\.]+");
    GraphObject current = obj;
    int pos = 0;
    for (final String part : parts) {
        final Class type = current.getEntityType();
        final PropertyKey key = config.getPropertyKeyForJSONName(type, part, false);
        if (key == null) {
            logger.warn("Unknown key {} while resolving path {} for sorting.", part, path);
            return null;
        }
        final Object value = current.getProperty(key);
        if (value != null) {
            // last part of path?
            if (++pos == parts.length) {
                if (value instanceof Comparable) {
                    return (Comparable) value;
                }
                logger.warn("Path evaluation result of component {} of type {} in {} cannot be used for sorting.", part, value.getClass().getSimpleName(), path);
                return null;
            }
            if (value instanceof GraphObject) {
                current = (GraphObject) value;
            } else {
                logger.warn("Path component {} of type {} in {} cannot be evaluated further.", part, value.getClass().getSimpleName(), path);
                return null;
            }
        }
    }
    return null;
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 24 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class SearchFunction 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 Query query = StructrApp.getInstance(securityContext).nodeQuery();
        applyRange(securityContext, query);
        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 search(): type {} not found.", typeString);
                return "Error in search(): type " + typeString + " not found.";
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in search(): no type specified. Parameters: {}", getParametersAsString(sources));
            return "Error in search(): 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) {
            final PropertyMap map = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
            for (final Map.Entry<PropertyKey, Object> entry : map.entrySet()) {
                query.and(entry.getKey(), entry.getValue(), false);
            }
        } 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_SEARCH);
            }
            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, false);
                }
            }
        }
        // return search results
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } finally {
        resetRange();
    }
}
Also used : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) PropertyMap(org.structr.core.property.PropertyMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) PropertyKey(org.structr.core.property.PropertyKey)

Example 25 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class SourcePattern method type.

private Class type(final String typeString) throws FrameworkException {
    Class type = null;
    final ConfigurationProvider config = StructrApp.getConfiguration();
    if (typeString != null) {
        type = config.getNodeEntityClass(typeString);
    }
    if (type == null) {
        throw new FrameworkException(422, "Unknown type '" + typeString + "'");
    }
    return type;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider)

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