Search in sources :

Example 41 with GraphObject

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

the class Resource method doDelete.

public RestMethodResult doDelete() throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    Iterable<GraphObject> results = null;
    // catch 204, DELETE must return 200 if resource is empty
    try (final Tx tx = app.tx(false, false, false)) {
        results = doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults();
        tx.success();
    } catch (final NoResultsException nre) {
        results = null;
    }
    if (results != null) {
        app.command(BulkDeleteCommand.class).bulkDelete(results.iterator());
    }
    return new RestMethodResult(HttpServletResponse.SC_OK);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NoResultsException(org.structr.rest.exception.NoResultsException) Tx(org.structr.core.graph.Tx) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult)

Example 42 with GraphObject

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

the class Resource method doPut.

public RestMethodResult doPut(final Map<String, Object> propertySet) throws FrameworkException {
    final Result<GraphObject> result = doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE);
    final List<GraphObject> results = result.getResults();
    if (results != null && !results.isEmpty()) {
        final Class type = results.get(0).getClass();
        // instruct deserialization strategies to set properties on related nodes
        securityContext.setAttribute("setNestedProperties", true);
        PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, type, propertySet);
        for (final GraphObject obj : results) {
            if (obj.isNode() && !obj.getSyncNode().isGranted(Permission.write, securityContext)) {
                throw new FrameworkException(403, "Modification not permitted.");
            }
            obj.setProperties(securityContext, properties);
        }
        return new RestMethodResult(HttpServletResponse.SC_OK);
    }
    throw new IllegalPathException(getResourceSignature() + " can only be applied to a non-empty resource");
}
Also used : IllegalPathException(org.structr.rest.exception.IllegalPathException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult)

Example 43 with GraphObject

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

the class ResultGSONAdapter method serialize.

@Override
public JsonElement serialize(Result src, Type typeOfSrc, JsonSerializationContext context) {
    long t0 = System.nanoTime();
    JsonObject result = new JsonObject();
    // result fields in alphabetical order
    List<? extends GraphObject> results = src.getResults();
    Integer page = src.getPage();
    Integer pageCount = src.getPageCount();
    Integer pageSize = src.getPageSize();
    String queryTime = src.getQueryTime();
    Integer resultCount = src.getRawResultCount();
    String searchString = src.getSearchString();
    String sortKey = src.getSortKey();
    String sortOrder = src.getSortOrder();
    GraphObject metaData = src.getMetaData();
    if (page != null) {
        result.add("page", new JsonPrimitive(page));
    }
    if (pageCount != null) {
        result.add("page_count", new JsonPrimitive(pageCount));
    }
    if (pageSize != null) {
        result.add("page_size", new JsonPrimitive(pageSize));
    }
    if (queryTime != null) {
        result.add("query_time", new JsonPrimitive(queryTime));
    }
    if (resultCount != null) {
        result.add("result_count", new JsonPrimitive(resultCount));
    }
    if (results != null) {
        if (results.isEmpty()) {
            final Object nonGraphObjectResult = src.getNonGraphObjectResult();
            if (nonGraphObjectResult != null) {
                result.add("result", graphObjectGsonAdapter.serializeObject(nonGraphObjectResult, System.currentTimeMillis()));
            } else {
                result.add("result", new JsonArray());
            }
        } else if (src.isPrimitiveArray()) {
            JsonArray resultArray = new JsonArray();
            for (GraphObject graphObject : results) {
                // FIXME: UUID key hard-coded, use variable in Result here!
                final Object value = graphObject.getProperty(GraphObject.id);
                if (value != null) {
                    resultArray.add(new JsonPrimitive(value.toString()));
                }
            }
            result.add("result", resultArray);
        } else {
            // FIXME: do we need this check, or does it cause trouble?
            if (results.size() > 1 && !src.isCollection()) {
                throw new IllegalStateException(src.getClass().getSimpleName() + " is not a collection resource, but result set has size " + results.size());
            }
            // keep track of serialization time
            long startTime = System.currentTimeMillis();
            if (src.isCollection()) {
                // serialize list of results
                JsonArray resultArray = new JsonArray();
                for (GraphObject graphObject : results) {
                    JsonElement element = graphObjectGsonAdapter.serialize(graphObject, startTime);
                    if (element != null) {
                        resultArray.add(element);
                    } else {
                        // stop serialization if timeout occurs
                        result.add("status", new JsonPrimitive("Serialization aborted due to timeout"));
                        src.setHasPartialContent(true);
                        break;
                    }
                }
                result.add("result", resultArray);
            } else {
                // use GraphObject adapter to serialize single result
                result.add("result", graphObjectGsonAdapter.serialize(results.get(0), startTime));
            }
        }
    }
    if (searchString != null) {
        result.add("search_string", new JsonPrimitive(searchString));
    }
    if (sortKey != null) {
        result.add("sort_key", new JsonPrimitive(sortKey));
    }
    if (sortOrder != null) {
        result.add("sort_order", new JsonPrimitive(sortOrder));
    }
    if (metaData != null) {
        JsonElement element = graphObjectGsonAdapter.serialize(metaData, System.currentTimeMillis());
        if (element != null) {
            result.add("meta_data", element);
        }
    }
    result.add("serialization_time", new JsonPrimitive(decimalFormat.format((System.nanoTime() - t0) / 1000000000.0)));
    return result;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject)

Example 44 with GraphObject

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

the class CypherQueryResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    // Admins only
    if (!securityContext.isSuperUser()) {
        throw new NotAllowedException("Use of the cypher endpoint is restricted to admin users");
    }
    try {
        Object queryObject = securityContext.getRequest().getParameter("query");
        if (queryObject != null) {
            String query = queryObject.toString();
            List<GraphObject> resultList = StructrApp.getInstance(securityContext).command(CypherQueryCommand.class).execute(query, Collections.EMPTY_MAP);
            return new Result(resultList, resultList.size(), true, false);
        }
    } catch (org.structr.api.NotFoundException nfe) {
        throw new NotFoundException("Entity not found for the given query");
    }
    return new Result(Collections.EMPTY_LIST, 0, false, false);
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) NotFoundException(org.structr.rest.exception.NotFoundException) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) CypherQueryCommand(org.structr.core.graph.CypherQueryCommand) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 45 with GraphObject

use of org.structr.core.GraphObject 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)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8