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);
}
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;
}
}
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);
}
}
Aggregations