Search in sources :

Example 66 with Result

use of org.structr.core.Result in project structr by structr.

the class ToJsonFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
    if (sources != null && sources.length >= 1 && sources.length <= 3) {
        try {
            final SecurityContext securityContext = ctx.getSecurityContext();
            final Value<String> view = new StaticValue<>("public");
            if (sources.length > 1) {
                view.set(securityContext, sources[1].toString());
            }
            int outputDepth = 3;
            if (sources.length > 2 && sources[2] instanceof Number) {
                outputDepth = ((Number) sources[2]).intValue();
            }
            final StreamingJsonWriter jsonStreamer = new StreamingJsonWriter(view, true, outputDepth);
            final StringWriter writer = new StringWriter();
            if (sources[0] instanceof GraphObject) {
                jsonStreamer.streamSingle(securityContext, writer, (GraphObject) sources[0]);
            } else if (sources[0] instanceof List) {
                final List list = (List) sources[0];
                jsonStreamer.stream(securityContext, writer, new Result(list, list.size(), true, false), null);
            } else if (sources[0] instanceof Map) {
                final GraphObjectMap map = new GraphObjectMap();
                this.recursivelyConvertMapToGraphObjectMap(map, (Map) sources[0], outputDepth);
                jsonStreamer.stream(securityContext, writer, new Result(map, false), null);
            }
            return writer.getBuffer().toString();
        } catch (Throwable t) {
            logException(caller, t, sources);
        }
        return "";
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return usage(ctx.isJavaScriptContext());
}
Also used : StaticValue(org.structr.core.StaticValue) GraphObject(org.structr.core.GraphObject) Result(org.structr.core.Result) StringWriter(java.io.StringWriter) StreamingJsonWriter(org.structr.rest.serialization.StreamingJsonWriter) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) List(java.util.List) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap)

Aggregations

Result (org.structr.core.Result)66 FrameworkException (org.structr.common.error.FrameworkException)45 Tx (org.structr.core.graph.Tx)36 Test (org.junit.Test)34 TestOne (org.structr.core.entity.TestOne)31 PropertyKey (org.structr.core.property.PropertyKey)28 PropertyMap (org.structr.core.property.PropertyMap)21 NodeInterface (org.structr.core.graph.NodeInterface)18 GraphObject (org.structr.core.GraphObject)14 AbstractNode (org.structr.core.entity.AbstractNode)12 RestMethodResult (org.structr.rest.RestMethodResult)12 Random (java.util.Random)11 SecurityContext (org.structr.common.SecurityContext)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 LinkedList (java.util.LinkedList)9 Query (org.structr.core.app.Query)9 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 List (java.util.List)7 Principal (org.structr.core.entity.Principal)7