Search in sources :

Example 1 with QueryResult

use of org.structr.api.QueryResult in project structr by structr.

the class Factory method page.

protected Result page(final QueryResult<S> input, final int offset, final int pageSize) throws FrameworkException {
    final SecurityContext securityContext = factoryProfile.getSecurityContext();
    final boolean dontCheckCount = securityContext.ignoreResultCount();
    final List<T> nodes = new ArrayList<>();
    int overallCount = 0;
    int position = 0;
    int count = 0;
    try (final QueryResult<S> tmp = input) {
        for (final S item : tmp) {
            final T n = instantiate(item);
            if (n != null) {
                overallCount++;
                position++;
                if (disablePaging || (position > offset && position <= offset + pageSize)) {
                    nodes.add(n);
                    // stop if we got enough nodes
                    if (++count == pageSize && dontCheckCount && !disablePaging) {
                        break;
                    }
                }
            }
        }
    } catch (NetworkException nex) {
        throw new FrameworkException(503, nex.getMessage());
    }
    // The overall count may be inaccurate
    return new Result(nodes, overallCount, true, false);
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) ArrayList(java.util.ArrayList) NetworkException(org.structr.api.NetworkException) QueryResult(org.structr.api.QueryResult) Result(org.structr.core.Result)

Example 2 with QueryResult

use of org.structr.api.QueryResult in project structr by structr.

the class SearchCommand method doSearch.

private Result<T> doSearch() throws FrameworkException {
    if (page == 0 || pageSize <= 0) {
        return Result.EMPTY_RESULT;
    }
    final Factory<S, T> factory = getFactory(securityContext, includeDeletedAndHidden, publicOnly, pageSize, page);
    boolean hasGraphSources = false;
    boolean hasSpatialSource = false;
    if (securityContext.getUser(false) == null && !isRelationshipSearch()) {
        rootGroup.add(new PropertySearchAttribute(GraphObject.visibleToPublicUsers, true, Occurrence.REQUIRED, true));
    } else if (securityContext.getUser(false) == null && isRelationshipSearch()) {
        rootGroup.add(new RelationshipVisibilitySearchAttribute());
    }
    // special handling of deleted and hidden flags
    if (!includeDeletedAndHidden && !isRelationshipSearch()) {
        rootGroup.add(new PropertySearchAttribute(NodeInterface.hidden, true, Occurrence.FORBIDDEN, true));
        rootGroup.add(new PropertySearchAttribute(NodeInterface.deleted, true, Occurrence.FORBIDDEN, true));
    }
    // At this point, all search attributes are ready
    final List<SourceSearchAttribute> sources = new ArrayList<>();
    boolean hasEmptySearchFields = false;
    boolean hasRelationshipVisibilitySearch = false;
    Result intermediateResult = null;
    // (some query types seem to allow no MUST occurs)
    for (final Iterator<SearchAttribute> it = rootGroup.getSearchAttributes().iterator(); it.hasNext(); ) {
        final SearchAttribute attr = it.next();
        if (attr instanceof SearchAttributeGroup) {
            // fixme: this needs to be done recursively, but how?
            for (final Iterator<SearchAttribute> groupIterator = ((SearchAttributeGroup) attr).getSearchAttributes().iterator(); groupIterator.hasNext(); ) {
                final SearchAttribute item = groupIterator.next();
                if (item instanceof SourceSearchAttribute) {
                    sources.add((SourceSearchAttribute) item);
                    // remove attribute from filter list
                    groupIterator.remove();
                    hasGraphSources = true;
                }
                if (item instanceof EmptySearchAttribute) {
                    hasEmptySearchFields = true;
                }
            }
        }
        // check for distance search and initialize
        if (attr instanceof DistanceSearchAttribute) {
            final DistanceSearchAttribute distanceSearch = (DistanceSearchAttribute) attr;
            if (distanceSearch.needsGeocding()) {
                final GeoCodingResult coords = GeoHelper.geocode(distanceSearch);
                if (coords != null) {
                    distanceSearch.setCoords(coords.toArray());
                }
            }
            hasSpatialSource = true;
        }
        // store source attributes for later use
        if (attr instanceof SourceSearchAttribute) {
            sources.add((SourceSearchAttribute) attr);
            hasGraphSources = true;
        }
        if (attr instanceof EmptySearchAttribute) {
            hasEmptySearchFields = true;
        }
        if (attr instanceof RelationshipVisibilitySearchAttribute) {
            hasRelationshipVisibilitySearch = true;
        }
    }
    // use filters to filter sources otherwise
    if (!hasSpatialSource && !sources.isEmpty()) {
        intermediateResult = new Result(new ArrayList<>(), null, false, false);
    } else {
        // apply sorting
        if (sortKey != null && !doNotSort) {
            rootGroup.setSortKey(sortKey);
            rootGroup.sortDescending(sortDescending);
        }
        final Index<S> index = getIndex();
        if (index != null) {
            // paging needs to be done AFTER instantiating all nodes
            if (hasEmptySearchFields || comparator != null) {
                factory.disablePaging();
            }
            // do query
            final QueryResult hits = index.query(getQueryContext(), rootGroup);
            intermediateResult = factory.instantiate(hits);
            if (comparator != null) {
                final List<T> rawResult = intermediateResult.getResults();
                Collections.sort(rawResult, comparator);
                return new Result(PagingHelper.subList(rawResult, pageSize, page), rawResult.size(), true, false);
            }
        }
    }
    if (intermediateResult != null && (hasEmptySearchFields || hasGraphSources || hasSpatialSource || hasRelationshipVisibilitySearch)) {
        // sorted result set
        final Set<GraphObject> intermediateResultSet = new LinkedHashSet<>(intermediateResult.getResults());
        final List<GraphObject> finalResult = new ArrayList<>();
        int resultCount = 0;
        if (hasGraphSources) {
            // merge sources according to their occur flag
            final Set<GraphObject> mergedSources = mergeSources(sources);
            if (hasSpatialSource) {
                // CHM 2014-02-24: preserve sorting of intermediate result, might be sorted by distance which we cannot reproduce easily
                intermediateResultSet.retainAll(mergedSources);
            } else {
                intermediateResultSet.addAll(mergedSources);
            }
        }
        // Filter intermediate result
        for (final GraphObject obj : intermediateResultSet) {
            boolean addToResult = true;
            // check all attributes before adding a node
            for (SearchAttribute attr : rootGroup.getSearchAttributes()) {
                // check all search attributes
                addToResult &= attr.includeInResult(obj);
            }
            if (addToResult) {
                finalResult.add(obj);
                resultCount++;
            }
        }
        // sort list
        Collections.sort(finalResult, new GraphObjectComparator(sortKey, sortDescending));
        // return paged final result
        return new Result(PagingHelper.subList(finalResult, pageSize, page), resultCount, true, false);
    } else {
        // no filtering
        return intermediateResult;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) GraphObject(org.structr.core.GraphObject) GeoCodingResult(org.structr.common.geo.GeoCodingResult) Result(org.structr.core.Result) QueryResult(org.structr.api.QueryResult) GeoCodingResult(org.structr.common.geo.GeoCodingResult) QueryResult(org.structr.api.QueryResult) GraphObjectComparator(org.structr.common.GraphObjectComparator)

Example 3 with QueryResult

use of org.structr.api.QueryResult in project structr by structr.

the class SessionTransaction method getStrings.

public QueryResult<String> getStrings(final String statement, final Map<String, Object> map) {
    final long t0 = System.currentTimeMillis();
    try {
        final StatementResult result = tx.run(statement, map);
        final Record record = result.next();
        final Value value = record.get(0);
        return new QueryResult<String>() {

            @Override
            public void close() {
                result.consume();
            }

            @Override
            public Iterator<String> iterator() {
                return value.asList(Values.ofString()).iterator();
            }
        };
    } catch (TransientException tex) {
        closed = true;
        throw new RetryException(tex);
    } catch (NoSuchRecordException nex) {
        throw new NotFoundException(nex);
    } catch (ServiceUnavailableException ex) {
        throw new NetworkException(ex.getMessage(), ex);
    } finally {
        logQuery(statement, map, t0);
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) QueryResult(org.structr.api.QueryResult) TransientException(org.neo4j.driver.v1.exceptions.TransientException) Value(org.neo4j.driver.v1.Value) NotFoundException(org.structr.api.NotFoundException) Record(org.neo4j.driver.v1.Record) ServiceUnavailableException(org.neo4j.driver.v1.exceptions.ServiceUnavailableException) RetryException(org.structr.api.RetryException) NetworkException(org.structr.api.NetworkException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException)

Aggregations

QueryResult (org.structr.api.QueryResult)3 ArrayList (java.util.ArrayList)2 NetworkException (org.structr.api.NetworkException)2 Result (org.structr.core.Result)2 LinkedHashSet (java.util.LinkedHashSet)1 Record (org.neo4j.driver.v1.Record)1 StatementResult (org.neo4j.driver.v1.StatementResult)1 Value (org.neo4j.driver.v1.Value)1 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)1 ServiceUnavailableException (org.neo4j.driver.v1.exceptions.ServiceUnavailableException)1 TransientException (org.neo4j.driver.v1.exceptions.TransientException)1 NotFoundException (org.structr.api.NotFoundException)1 RetryException (org.structr.api.RetryException)1 GraphObjectComparator (org.structr.common.GraphObjectComparator)1 SecurityContext (org.structr.common.SecurityContext)1 FrameworkException (org.structr.common.error.FrameworkException)1 GeoCodingResult (org.structr.common.geo.GeoCodingResult)1 GraphObject (org.structr.core.GraphObject)1