use of javax.jcr.query.RowIterator in project sling by apache.
the class BasicQueryLanguageProvider method queryResources.
@Override
public Iterator<ValueMap> queryResources(final ResolveContext<JcrProviderState> ctx, final String query, final String language) {
final String queryLanguage = ArrayUtils.contains(getSupportedLanguages(ctx), language) ? language : DEFAULT_QUERY_LANGUAGE;
try {
final QueryResult result = JcrResourceUtil.query(ctx.getProviderState().getSession(), query, queryLanguage);
final String[] colNames = result.getColumnNames();
final RowIterator rows = result.getRows();
return new Iterator<ValueMap>() {
private ValueMap next;
{
next = seek();
}
@Override
public boolean hasNext() {
return next != null;
}
;
@Override
public ValueMap next() {
if (next == null) {
throw new NoSuchElementException();
}
final ValueMap result = next;
next = seek();
return result;
}
private ValueMap seek() {
ValueMap result = null;
while (result == null && rows.hasNext()) {
try {
final Row jcrRow = rows.nextRow();
final String resourcePath = jcrRow.getPath();
if (resourcePath != null && providerContext.getExcludedPaths().matches(resourcePath) == null) {
final Map<String, Object> row = new HashMap<String, Object>();
boolean didPath = false;
boolean didScore = false;
final Value[] values = jcrRow.getValues();
for (int i = 0; i < values.length; i++) {
Value v = values[i];
if (v != null) {
String colName = colNames[i];
row.put(colName, JcrResourceUtil.toJavaObject(values[i]));
if (colName.equals(QUERY_COLUMN_PATH)) {
didPath = true;
row.put(colName, JcrResourceUtil.toJavaObject(values[i]).toString());
}
if (colName.equals(QUERY_COLUMN_SCORE)) {
didScore = true;
}
}
}
if (!didPath) {
row.put(QUERY_COLUMN_PATH, jcrRow.getPath());
}
if (!didScore) {
row.put(QUERY_COLUMN_SCORE, jcrRow.getScore());
}
result = new ValueMapDecorator(row);
}
} catch (final RepositoryException re) {
logger.error("queryResources$next: Problem accessing row values", re);
}
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (final javax.jcr.query.InvalidQueryException iqe) {
throw new QuerySyntaxException(iqe.getMessage(), query, language, iqe);
} catch (final RepositoryException re) {
throw new SlingException(re.getMessage(), re);
}
}
Aggregations