Search in sources :

Example 36 with Result

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

the class MeResource method doGet.

@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
    Principal user = securityContext.getUser(true);
    if (user != null) {
        List<GraphObject> resultList = new LinkedList<>();
        resultList.add(user);
        return new Result(resultList, null, isCollectionResource(), isPrimitiveArray());
    } else {
        throw new NotAllowedException("No user");
    }
}
Also used : NotAllowedException(org.structr.rest.exception.NotAllowedException) GraphObject(org.structr.core.GraphObject) Principal(org.structr.core.entity.Principal) LinkedList(java.util.LinkedList) RestMethodResult(org.structr.rest.RestMethodResult) Result(org.structr.core.Result)

Example 37 with Result

use of org.structr.core.Result 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 38 with Result

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

the class StaticRelationshipResource method doGet.

// ~--- methods --------------------------------------------------------
@Override
public Result doGet(final PropertyKey sortKey, final boolean sortDescending, final int pageSize, final int page) throws FrameworkException {
    // ok, source node exists, fetch it
    final GraphObject sourceEntity = typedIdResource.getEntity();
    if (sourceEntity != null) {
        // first try: look through existing relations
        if (propertyKey == null) {
            if (sourceEntity instanceof NodeInterface) {
                if (!typeResource.isNode) {
                    final NodeInterface source = (NodeInterface) sourceEntity;
                    final Node sourceNode = source.getNode();
                    final Class relationshipType = typeResource.entityClass;
                    final Relation relation = AbstractNode.getRelationshipForType(relationshipType);
                    final Class destNodeType = relation.getOtherType(typedIdResource.getEntityClass());
                    final Set partialResult = new LinkedHashSet<>(typeResource.doGet(sortKey, sortDescending, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults());
                    // filter list according to end node type
                    final Set<GraphObject> set = Iterables.toSet(Iterables.filter(new OtherNodeTypeRelationFilter(securityContext, sourceNode, destNodeType), source.getRelationships(relationshipType)));
                    // intersect partial result with result list
                    set.retainAll(partialResult);
                    final List<GraphObject> finalResult = new LinkedList<>(set);
                    // sort after merge
                    applyDefaultSorting(finalResult, sortKey, sortDescending);
                    // return result
                    return new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
                } else {
                    // what here?
                    throw new NotFoundException("Cannot access relationship collection " + typeResource.getRawType());
                }
            }
        } else {
            Query query = typeResource.query;
            if (query == null) {
                query = StructrApp.getInstance(securityContext).nodeQuery();
            }
            // use search context from type resource
            typeResource.collectSearchAttributes(query);
            final Predicate<GraphObject> predicate = query.toPredicate();
            final Object value = sourceEntity.getProperty(propertyKey, predicate);
            if (value != null) {
                if (value instanceof Iterable) {
                    final Set<Object> propertyResults = new LinkedHashSet<>();
                    Iterator<Object> iter = ((Iterable<Object>) value).iterator();
                    boolean iterableContainsGraphObject = false;
                    while (iter.hasNext()) {
                        Object obj = iter.next();
                        propertyResults.add(obj);
                        if (obj != null && !iterableContainsGraphObject) {
                            if (obj instanceof GraphObject) {
                                iterableContainsGraphObject = true;
                            }
                        }
                    }
                    int rawResultCount = propertyResults.size();
                    if (rawResultCount > 0 && !iterableContainsGraphObject) {
                        GraphObjectMap gObject = new GraphObjectMap();
                        gObject.setProperty(new ArrayProperty(this.typeResource.rawType, Object.class), propertyResults.toArray());
                        Result r = new Result(gObject, true);
                        r.setRawResultCount(rawResultCount);
                        return r;
                    }
                    final List<GraphObject> finalResult = new LinkedList<>();
                    propertyResults.forEach(v -> finalResult.add((GraphObject) v));
                    applyDefaultSorting(finalResult, sortKey, sortDescending);
                    // return result
                    Result r = new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
                    r.setRawResultCount(rawResultCount);
                    return r;
                } else if (value instanceof GraphObject) {
                    return new Result((GraphObject) value, isPrimitiveArray());
                } else {
                    GraphObjectMap gObject = new GraphObjectMap();
                    PropertyKey key;
                    String keyName = this.typeResource.rawType;
                    int resultCount = 1;
                    // FIXME: Dynamically resolve all property types and their result count
                    if (value instanceof String) {
                        key = new StringProperty(keyName);
                    } else if (value instanceof Integer) {
                        key = new IntProperty(keyName);
                    } else if (value instanceof Long) {
                        key = new LongProperty(keyName);
                    } else if (value instanceof Double) {
                        key = new DoubleProperty(keyName);
                    } else if (value instanceof Boolean) {
                        key = new BooleanProperty(keyName);
                    } else if (value instanceof Date) {
                        key = new DateProperty(keyName);
                    } else if (value instanceof String[]) {
                        key = new ArrayProperty(keyName, String.class);
                        resultCount = ((String[]) value).length;
                    } else {
                        key = new GenericProperty(keyName);
                    }
                    gObject.setProperty(key, value);
                    Result r = new Result(gObject, true);
                    r.setRawResultCount(resultCount);
                    return r;
                }
            }
            // check propertyKey to return the right variant of empty result
            if (!(propertyKey instanceof StartNode || propertyKey instanceof EndNode)) {
                return new Result(Collections.EMPTY_LIST, 1, false, true);
            }
        }
    }
    return new Result(Collections.EMPTY_LIST, 0, false, true);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Query(org.structr.core.app.Query) DateProperty(org.structr.core.property.DateProperty) StartNode(org.structr.core.property.StartNode) EndNode(org.structr.core.property.EndNode) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) NotFoundException(org.structr.rest.exception.NotFoundException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult) Relation(org.structr.core.entity.Relation) NodeInterface(org.structr.core.graph.NodeInterface) StartNode(org.structr.core.property.StartNode) ArrayProperty(org.structr.core.property.ArrayProperty) BooleanProperty(org.structr.core.property.BooleanProperty) OtherNodeTypeRelationFilter(org.structr.core.entity.OtherNodeTypeRelationFilter) LinkedList(java.util.LinkedList) Date(java.util.Date) IntProperty(org.structr.core.property.IntProperty) EndNode(org.structr.core.property.EndNode) GraphObjectMap(org.structr.core.GraphObjectMap) LongProperty(org.structr.core.property.LongProperty) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) DoubleProperty(org.structr.core.property.DoubleProperty) PropertyKey(org.structr.core.property.PropertyKey)

Example 39 with Result

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

the class LogResource method histogram.

private Result histogram(final LogState state) throws FrameworkException {
    // sort entries before creating the histogram
    state.sortEntries();
    final String dateFormat = state.aggregate();
    final long startTimestamp = state.beginTimestamp();
    final long endTimestamp = state.endTimestamp();
    final GraphObjectMap result = new GraphObjectMap();
    final long interval = findInterval(dateFormat);
    final long start = alignDateOnFormat(dateFormat, startTimestamp);
    final TreeMap<Long, Map<String, Object>> countMap = toHistogramCountMap(state);
    final Set<String> countProperties = getCountProperties(countMap);
    for (long current = start; current <= endTimestamp; current += interval) {
        final Map<Long, Map<String, Object>> counts = countMap.subMap(current, true, current + interval, false);
        final GraphObjectMap sum = new GraphObjectMap();
        // whether there are actual values or not)
        for (final String key : countProperties) {
            sum.put(new IntProperty(key), 0);
        }
        // evaluate counts
        for (final Map<String, Object> count : counts.values()) {
            for (final String key : countProperties) {
                final IntProperty prop = new IntProperty(key);
                Integer sumValue = sum.get(prop);
                if (sumValue == null) {
                    sumValue = 0;
                }
                Integer entryValue = (Integer) count.get(key);
                if (entryValue == null) {
                    entryValue = 0;
                }
                sum.put(prop, sumValue + entryValue);
            }
        }
        result.put(new GenericProperty(Long.toString(current)), sum);
    }
    return new Result(result, false);
}
Also used : Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult) IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) TreeMap(java.util.TreeMap)

Example 40 with Result

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

the class LogResource method aggregate.

private Result aggregate(final LogState state) throws FrameworkException {
    // sort entries before aggregation
    state.sortEntries();
    final long startTimestamp = state.beginTimestamp();
    final long endTimestamp = state.endTimestamp();
    final GraphObjectMap result = new GraphObjectMap();
    final long interval = findInterval(state.aggregate());
    final long start = alignDateOnFormat(state.aggregate(), startTimestamp);
    final TreeMap<Long, Map<String, Object>> countMap = toAggregatedCountMap(state);
    final Set<String> countProperties = getCountProperties(countMap);
    for (long current = start; current <= endTimestamp; current += interval) {
        final Map<Long, Map<String, Object>> counts = countMap.subMap(current, true, current + interval, false);
        final GraphObjectMap sum = new GraphObjectMap();
        // whether there are actual values or not)
        for (final String key : countProperties) {
            sum.put(new IntProperty(key), 0);
        }
        // evaluate counts
        for (final Map<String, Object> count : counts.values()) {
            for (final String key : countProperties) {
                final IntProperty prop = new IntProperty(key);
                Integer sumValue = sum.get(prop);
                if (sumValue == null) {
                    sumValue = 0;
                }
                Integer entryValue = (Integer) count.get(key);
                if (entryValue == null) {
                    entryValue = 0;
                }
                sum.put(prop, sumValue + entryValue);
            }
        }
        result.put(new GenericProperty(Long.toString(current)), sum);
    }
    return new Result(result, false);
}
Also used : Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult) IntProperty(org.structr.core.property.IntProperty) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) TreeMap(java.util.TreeMap)

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