Search in sources :

Example 31 with ConfigurationProvider

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

the class SchemaResource method getSchemaOverviewResult.

// ----- public static methods -----
public static Result getSchemaOverviewResult() throws FrameworkException {
    final List<GraphObjectMap> resultList = new LinkedList<>();
    final ConfigurationProvider config = StructrApp.getConfiguration();
    // extract types from ModuleService
    final Set<String> nodeEntityKeys = config.getNodeEntities().keySet();
    final Set<String> relEntityKeys = config.getRelationshipEntities().keySet();
    Set<String> entityKeys = new HashSet<>();
    entityKeys.addAll(nodeEntityKeys);
    entityKeys.addAll(relEntityKeys);
    for (String rawType : entityKeys) {
        // create & add schema information
        Class type = SchemaHelper.getEntityClassForRawType(rawType);
        GraphObjectMap schema = new GraphObjectMap();
        resultList.add(schema);
        if (type != null) {
            String url = "/".concat(rawType);
            final boolean isRel = AbstractRelationship.class.isAssignableFrom(type);
            schema.setProperty(urlProperty, url);
            schema.setProperty(typeProperty, type.getSimpleName());
            schema.setProperty(nameProperty, type.getSimpleName());
            schema.setProperty(classNameProperty, type.getName());
            schema.setProperty(extendsClassNameProperty, type.getSuperclass().getName());
            schema.setProperty(isRelProperty, isRel);
            schema.setProperty(flagsProperty, SecurityContext.getResourceFlags(rawType));
            if (!isRel) {
                final List<GraphObjectMap> relatedTo = new LinkedList<>();
                final List<GraphObjectMap> relatedFrom = new LinkedList<>();
                for (final PropertyKey key : config.getPropertySet(type, PropertyView.All)) {
                    if (key instanceof RelationProperty) {
                        final RelationProperty relationProperty = (RelationProperty) key;
                        final Relation relation = relationProperty.getRelation();
                        if (!relation.isHidden()) {
                            switch(relation.getDirectionForType(type)) {
                                case OUTGOING:
                                    relatedTo.add(relationPropertyToMap(config, relationProperty));
                                    break;
                                case INCOMING:
                                    relatedFrom.add(relationPropertyToMap(config, relationProperty));
                                    break;
                                case BOTH:
                                    relatedTo.add(relationPropertyToMap(config, relationProperty));
                                    relatedFrom.add(relationPropertyToMap(config, relationProperty));
                                    break;
                            }
                        }
                    }
                }
                if (!relatedTo.isEmpty()) {
                    schema.setProperty(relatedToProperty, relatedTo);
                }
                if (!relatedFrom.isEmpty()) {
                    schema.setProperty(relatedFromProperty, relatedFrom);
                }
            }
        }
    }
    return new Result(resultList, resultList.size(), false, false);
}
Also used : RelationProperty(org.structr.core.property.RelationProperty) ConfigurationProvider(org.structr.schema.ConfigurationProvider) LinkedList(java.util.LinkedList) Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult) Relation(org.structr.core.entity.Relation) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey) HashSet(java.util.HashSet)

Example 32 with ConfigurationProvider

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

the class FindRelationshipFunction 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).relationshipQuery().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.getRelationshipEntityClass(typeString);
            if (type != null) {
                query.andTypes(type);
            } else {
                logger.warn("Error in find_relationship(): type \"{}\" not found.", typeString);
                return ERROR_MESSAGE_FIND_RELATIONSHIP_TYPE_NOT_FOUND + typeString;
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in find_relationship(): no type specified. Parameters: {}", getParametersAsString(sources));
            return ERROR_MESSAGE_FIND_RELATIONSHIP_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());
            return query.getFirst();
        } 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_FIND_RELATIONSHIP);
            }
            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);
                    }
                    query.and(key, value);
                }
            }
        }
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
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 33 with ConfigurationProvider

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

the class EnumInfoFunction method apply.

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
    try {
        if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
            final ConfigurationProvider config = StructrApp.getConfiguration();
            final String typeName = sources[0].toString();
            final String enumPropertyName = sources[1].toString();
            final boolean rawList = (sources.length == 3) ? Boolean.parseBoolean(sources[2].toString()) : false;
            final Class type = SchemaHelper.getEntityClassForRawType(typeName);
            if (type != null) {
                final PropertyKey key = StructrApp.key(type, enumPropertyName);
                if (key != null) {
                    if (key instanceof EnumProperty) {
                        final String formatString = SchemaHelper.getPropertyInfo(ctx.getSecurityContext(), key).get("format").toString();
                        final List<String> valueList = Arrays.asList(formatString.replace(" ", "").split(","));
                        if (rawList) {
                            return valueList;
                        } else {
                            final ArrayList<GraphObjectMap> resultList = new ArrayList();
                            for (final String value : valueList) {
                                final GraphObjectMap valueMap = new GraphObjectMap();
                                resultList.add(valueMap);
                                valueMap.put(new StringProperty("value"), value);
                            }
                            return resultList;
                        }
                    } else {
                        logger.warn("Error: Not an Enum property \"{}.{}\"", typeName, enumPropertyName);
                        return "Not an Enum property " + typeName + "." + enumPropertyName;
                    }
                } else {
                    logger.warn("Error: Unknown property \"{}.{}\"", typeName, enumPropertyName);
                    return "Unknown property " + typeName + "." + enumPropertyName;
                }
            } else {
                logger.warn("Error: Unknown type \"{}\"", typeName);
                return "Unknown type " + typeName;
            }
        } else {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : EnumProperty(org.structr.core.property.EnumProperty) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObjectMap(org.structr.core.GraphObjectMap) ArrayList(java.util.ArrayList) StringProperty(org.structr.core.property.StringProperty) PropertyKey(org.structr.core.property.PropertyKey)

Example 34 with ConfigurationProvider

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

the class ExtractFunction 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 == 1) {
            // no property key given, maybe we should extract a list of lists?
            if (sources[0] instanceof Collection) {
                final List extraction = new LinkedList();
                for (final Object obj : (Collection) sources[0]) {
                    if (obj instanceof Collection) {
                        extraction.addAll((Collection) obj);
                    }
                }
                return extraction;
            }
        }
        if (sources.length == 2) {
            if (sources[0] == null) {
                return null;
            }
            if (sources[0] instanceof Collection && sources[1] instanceof String) {
                final ConfigurationProvider config = StructrApp.getConfiguration();
                final List extraction = new LinkedList();
                final String keyName = (String) sources[1];
                for (final Object obj : (Collection) sources[0]) {
                    if (obj instanceof GraphObject) {
                        final PropertyKey key = StructrApp.key(obj.getClass(), keyName);
                        final Object value = ((GraphObject) obj).getProperty(key);
                        if (value != null) {
                            extraction.add(value);
                        }
                    }
                }
                return extraction;
            }
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return null;
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) Collection(java.util.Collection) List(java.util.List) LinkedList(java.util.LinkedList) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) PropertyKey(org.structr.core.property.PropertyKey)

Example 35 with ConfigurationProvider

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

the class FindFunction 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 Query query = app.nodeQuery().sort(GraphObject.createdDate).order(false);
        // paging applied by surrounding slice() function
        applyRange(securityContext, query);
        // 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(): type \"{}\" not found.", typeString);
                return ERROR_MESSAGE_FIND_TYPE_NOT_FOUND + typeString;
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in find(): no type specified. Parameters: {}", getParametersAsString(sources));
            return ERROR_MESSAGE_FIND_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());
            return query.getFirst();
        } 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_FIND);
            }
            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);
                    }
                    query.and(key, value);
                }
            }
        }
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } finally {
        resetRange();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) 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)

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