Search in sources :

Example 11 with StringProperty

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

the class HttpDeleteFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 1)) {
        final String uri = sources[0].toString();
        String contentType = "application/json";
        // override default content type
        if (sources.length >= 3 && sources[2] != null) {
            contentType = sources[2].toString();
        }
        final Map<String, String> responseData = HttpHelper.delete(uri, null, null, ctx.getHeaders());
        final int statusCode = Integer.parseInt(responseData.get("status"));
        responseData.remove("status");
        final String responseBody = responseData.get("body");
        responseData.remove("body");
        final GraphObjectMap response = new GraphObjectMap();
        if ("application/json".equals(contentType)) {
            final FromJsonFunction fromJsonFunction = new FromJsonFunction();
            response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, caller, new Object[] { responseBody }));
        } else {
            response.setProperty(new StringProperty("body"), responseBody);
        }
        response.setProperty(new IntProperty("status"), statusCode);
        final GraphObjectMap map = new GraphObjectMap();
        for (final Map.Entry<String, String> entry : responseData.entrySet()) {
            map.put(new StringProperty(entry.getKey()), entry.getValue());
        }
        response.setProperty(new StringProperty("headers"), map);
        return response;
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 12 with StringProperty

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

the class HttpPostFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final String uri = sources[0].toString();
        final String body = sources[1].toString();
        String contentType = "application/json";
        String charset = "utf-8";
        // override default content type
        if (sources.length >= 3 && sources[2] != null) {
            contentType = sources[2].toString();
        }
        // override default content type
        if (sources.length >= 4 && sources[3] != null) {
            charset = sources[3].toString();
        }
        final Map<String, String> responseData = HttpHelper.post(uri, body, null, null, ctx.getHeaders(), charset);
        final int statusCode = Integer.parseInt(responseData.get("status"));
        responseData.remove("status");
        final String responseBody = responseData.get("body");
        responseData.remove("body");
        final GraphObjectMap response = new GraphObjectMap();
        if ("application/json".equals(contentType)) {
            final FromJsonFunction fromJsonFunction = new FromJsonFunction();
            response.setProperty(new StringProperty("body"), fromJsonFunction.apply(ctx, caller, new Object[] { responseBody }));
        } else {
            response.setProperty(new StringProperty("body"), responseBody);
        }
        response.setProperty(new IntProperty("status"), statusCode);
        final GraphObjectMap map = new GraphObjectMap();
        for (final Map.Entry<String, String> entry : responseData.entrySet()) {
            map.put(new StringProperty(entry.getKey()), entry.getValue());
        }
        response.setProperty(new StringProperty("headers"), map);
        return response;
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Example 13 with StringProperty

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

the class SchemaHelper method extractMethods.

public static void extractMethods(final AbstractSchemaNode entity, final Map<String, List<ActionEntry>> actions) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    for (final String rawActionName : getActions(propertyContainer)) {
        if (propertyContainer.hasProperty(rawActionName)) {
            final String value = propertyContainer.getProperty(rawActionName).toString();
            if (entity instanceof AbstractSchemaNode) {
                final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
                final App app = StructrApp.getInstance();
                final String methodName = rawActionName.substring(3);
                if (app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, schemaNode).and(AbstractNode.name, methodName).getFirst() == null) {
                    app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.name, methodName), new NodeAttribute<>(SchemaMethod.source, value));
                    schemaNode.removeProperty(new StringProperty(rawActionName));
                }
            }
        }
    }
    final List<SchemaMethod> schemaMethods = entity.getSchemaMethods();
    if (schemaMethods != null) {
        for (final SchemaMethod schemaMethod : schemaMethods) {
            final ActionEntry entry = schemaMethod.getActionEntry(entity);
            final String name = entry.getName();
            List<ActionEntry> actionList = actions.get(name);
            if (actionList == null) {
                actionList = new LinkedList<>();
                actions.put(name, actionList);
            }
            actionList.add(entry);
            Collections.sort(actionList);
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) ActionEntry(org.structr.schema.action.ActionEntry) SchemaMethod(org.structr.core.entity.SchemaMethod) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) StringProperty(org.structr.core.property.StringProperty)

Example 14 with StringProperty

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

the class SchemaHelper method extractViews.

public static void extractViews(final Schema entity, final Map<String, Set<String>> views, final Set<String> relPropertyNames, final ErrorBuffer errorBuffer) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    final ConfigurationProvider config = StructrApp.getConfiguration();
    Class superClass = config.getNodeEntityClass(entity.getSuperclassName());
    if (superClass == null) {
        superClass = config.getRelationshipEntityClass(entity.getSuperclassName());
    }
    if (superClass == null) {
        superClass = AbstractNode.class;
    }
    for (final String rawViewName : getViews(propertyContainer)) {
        if (!rawViewName.startsWith("___") && propertyContainer.hasProperty(rawViewName)) {
            final String value = propertyContainer.getProperty(rawViewName).toString();
            final String[] parts = value.split("[,\\s]+");
            final String viewName = rawViewName.substring(2);
            if (entity instanceof AbstractSchemaNode) {
                final List<String> nonGraphProperties = new LinkedList<>();
                final List<SchemaProperty> properties = new LinkedList<>();
                final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
                final App app = StructrApp.getInstance();
                if (app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(AbstractNode.name, viewName).getFirst() == null) {
                    // add parts to view, overrides defaults (because of clear() above)
                    for (int i = 0; i < parts.length; i++) {
                        String propertyName = parts[i].trim();
                        while (propertyName.startsWith("_")) {
                            propertyName = propertyName.substring(1);
                        }
                        // append this as a workaround to include remote properties
                        if (propertyName.endsWith("Property")) {
                            propertyName = propertyName.substring(0, propertyName.length() - "Property".length());
                        }
                        final SchemaProperty propertyNode = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).andName(propertyName).getFirst();
                        if (propertyNode != null) {
                            properties.add(propertyNode);
                        } else {
                            nonGraphProperties.add(propertyName);
                        }
                    }
                    app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.schemaProperties, properties), new NodeAttribute<>(SchemaView.name, viewName), new NodeAttribute<>(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ",")));
                    schemaNode.removeProperty(new StringProperty(rawViewName));
                }
            }
        }
    }
    final List<SchemaView> schemaViews = entity.getSchemaViews();
    if (schemaViews != null) {
        for (final SchemaView schemaView : schemaViews) {
            final String nonGraphProperties = schemaView.getProperty(SchemaView.nonGraphProperties);
            final String viewName = schemaView.getName();
            // clear view before filling it again
            Set<String> view = views.get(viewName);
            if (view == null) {
                view = new LinkedHashSet<>();
                views.put(viewName, view);
            }
            final List<SchemaProperty> schemaProperties = schemaView.getProperty(SchemaView.schemaProperties);
            for (final SchemaProperty property : schemaProperties) {
                if (property.getProperty(SchemaProperty.isBuiltinProperty) && !property.getProperty(SchemaProperty.isDynamic)) {
                    view.add(SchemaHelper.cleanPropertyName(property.getPropertyName()));
                } else {
                    view.add(SchemaHelper.cleanPropertyName(property.getPropertyName() + "Property"));
                }
            }
            // add properties that are not part of the graph
            if (StringUtils.isNotBlank(nonGraphProperties)) {
                for (final String propertyName : nonGraphProperties.split("[, ]+")) {
                    if (SchemaHelper.isDynamic(entity.getClassName(), propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName + "Property"));
                    } else if (relPropertyNames.contains(propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName) + "Property");
                    } else if (basePropertyNames.contains(propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName));
                    } else {
                        logger.warn("Unknown property {} in non-graph properties, ignoring.", propertyName);
                        SchemaHelper.isDynamic(entity.getClassName(), propertyName);
                    }
                }
            }
            final String order = schemaView.getProperty(SchemaView.sortOrder);
            if (order != null) {
                applySortOrder(view, order);
            }
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) SchemaProperty(org.structr.core.entity.SchemaProperty) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode)

Example 15 with StringProperty

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

the class Function method wrapStringInGraphObjectMap.

public static GraphObjectMap wrapStringInGraphObjectMap(final String str) {
    final GraphObjectMap stringWrapperObject = new GraphObjectMap();
    stringWrapperObject.put(new StringProperty("value"), str);
    return stringWrapperObject;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) StringProperty(org.structr.core.property.StringProperty)

Aggregations

StringProperty (org.structr.core.property.StringProperty)53 FrameworkException (org.structr.common.error.FrameworkException)28 Tx (org.structr.core.graph.Tx)28 Test (org.junit.Test)26 PropertyKey (org.structr.core.property.PropertyKey)22 GraphObject (org.structr.core.GraphObject)14 GraphObjectMap (org.structr.core.GraphObjectMap)14 StructrTest (org.structr.common.StructrTest)11 LinkedList (java.util.LinkedList)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SchemaNode (org.structr.core.entity.SchemaNode)9 List (java.util.List)8 AbstractNode (org.structr.core.entity.AbstractNode)7 NodeInterface (org.structr.core.graph.NodeInterface)7 StructrUiTest (org.structr.web.StructrUiTest)7 Map (java.util.Map)6 ErrorToken (org.structr.common.error.ErrorToken)6 IntProperty (org.structr.core.property.IntProperty)6 LinkedHashSet (java.util.LinkedHashSet)5 App (org.structr.core.app.App)5