Search in sources :

Example 86 with RowIterator

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);
    }
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) RepositoryException(javax.jcr.RepositoryException) QueryResult(javax.jcr.query.QueryResult) QuerySyntaxException(org.apache.sling.api.resource.QuerySyntaxException) RowIterator(javax.jcr.query.RowIterator) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) Value(javax.jcr.Value) SlingException(org.apache.sling.api.SlingException) Row(javax.jcr.query.Row) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

RowIterator (javax.jcr.query.RowIterator)86 Node (javax.jcr.Node)48 QueryResult (javax.jcr.query.QueryResult)45 QueryManager (javax.jcr.query.QueryManager)27 Row (javax.jcr.query.Row)27 Query (javax.jcr.query.Query)25 Test (org.junit.Test)20 Session (javax.jcr.Session)17 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)15 RepositoryException (javax.jcr.RepositoryException)12 Value (javax.jcr.Value)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)8 ValueFactory (javax.jcr.ValueFactory)7 ArrayList (java.util.ArrayList)5 NodeIterator (javax.jcr.NodeIterator)5 NoSuchElementException (java.util.NoSuchElementException)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3