Java Code Examples for javax.jcr.query.QueryResult#getRows()

The following examples show how to use javax.jcr.query.QueryResult#getRows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProductsSuggestionOmniSearchHandlerTest.java    From commerce-cif-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuggestionQueryNoCifResults() throws Exception {

    // The mocked JCR result
    MockNode jcrNode = new MockNode("/var/commerce/products/jcrnode");
    jcrNode.setProperty("rep:suggest()", BROOKLYN_COAT);
    MockQueryResult jcrResults = new MockQueryResult(Collections.singletonList(jcrNode));
    Query jcrQuery = Mockito.mock(Query.class);
    Mockito.when(jcrQuery.execute()).thenReturn(jcrResults);

    Mockito.doReturn(jcrQuery).when(suggestionHandler).getSuperSuggestionQuery(resolver, "coats");
    Mockito.doReturn(resolver).when(suggestionHandler).getResourceResolver();

    Mockito.when(resolver.findResources(Mockito.any(), Mockito.any())).thenThrow(new RuntimeException());

    suggestionHandler.onEvent(null);
    suggestionHandler.activate(null);

    Query suggestionQuery = suggestionHandler.getSuggestionQuery(resolver, "coats");
    QueryResult queryResult = suggestionQuery.execute();
    RowIterator rows = queryResult.getRows();

    // No CIF result, only JCR result
    Assert.assertEquals(BROOKLYN_COAT, rows.nextRow().getValue("rep:suggest()").getString());
    Assert.assertFalse(rows.hasNext());
}
 
Example 2
Source File: ProductsSuggestionOmniSearchHandler.java    From commerce-cif-connector with Apache License 2.0 5 votes vote down vote up
@Override
public QueryResult execute() throws InvalidQueryException, RepositoryException {
    // Get JCR results
    QueryResult queryResult = query.execute();

    // Get CIF products
    Iterator<Resource> it = getVirtualResults(resolver, Collections.singletonMap("fulltext", searchTerm), 10, 0);
    if (it == null || !it.hasNext()) {
        return queryResult; // No CIF results
    }

    ValueFactory valueFactory = resolver.adaptTo(Session.class).getValueFactory();
    List<Row> rows = new ArrayList<>();
    while (it.hasNext()) {
        String title = it.next().getValueMap().get(JcrConstants.JCR_TITLE, String.class);
        Value value = valueFactory.createValue(title);
        rows.add(new SuggestionRow(value));
    }

    RowIterator suggestionIterator = queryResult.getRows();
    while (suggestionIterator.hasNext()) {
        rows.add(suggestionIterator.nextRow());
    }

    SuggestionQueryResult result = new SuggestionQueryResult(rows);
    return result;
}
 
Example 3
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private <T> Spliterator<T> createResultSpliterator(QueryResult result, Function<Row, T> converter) throws MetadataRepositoryException {
    final RowIterator rowIterator;
    try {
        rowIterator = result.getRows();
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    return new Spliterator<T>() {
        @Override
        public boolean tryAdvance(Consumer<? super T> action) {
            while (rowIterator.hasNext()) {
                T item = converter.apply(rowIterator.nextRow());
                if (item != null) {
                    action.accept(item);
                    return true;
                }
            }
            return false;
        }

        @Override
        public Spliterator<T> trySplit() {
            return null;
        }

        @Override
        public long estimateSize() {
            return 0;
        }

        @Override
        public int characteristics() {
            return ORDERED + NONNULL;
        }
    };
}
 
Example 4
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
public List<ArtifactMetadata> runJcrQuery(final Session jcrSession, final String repositoryId, final String qParam,
                                          final Map<String, String> bindingParam, final boolean checkPath)
        throws MetadataRepositoryException {

    String q = qParam;
    List<ArtifactMetadata> artifacts;
    if (repositoryId != null && checkPath) {
        q += " AND ISDESCENDANTNODE(artifact,'/" + getRepositoryContentPath(repositoryId) + "')";
    }

    log.info("Running JCR Query: {}", q);

    try {
        QueryResult result = runNativeJcrQuery(jcrSession, q, bindingParam);
        artifacts = new ArrayList<>();
        RowIterator rows = result.getRows();
        while (rows.hasNext()) {
            Row row = rows.nextRow();
            Node node = row.getNode("artifact");
            artifacts.add(getArtifactFromNode(repositoryId, node));
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
    log.info("Artifacts found {}", artifacts.size());
    for (ArtifactMetadata meta : artifacts) {
        log.info("Artifact: " + meta.getVersion() + " " + meta.getFacetList());
    }
    return artifacts;
}
 
Example 5
Source File: ProductsSuggestionOmniSearchHandlerTest.java    From commerce-cif-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testSuggestionQuery() throws Exception {

    // The mocked JCR result
    MockNode jcrNode = new MockNode("/var/commerce/products/jcrnode");
    jcrNode.setProperty("rep:suggest()", BROOKLYN_COAT);
    MockQueryResult jcrResults = new MockQueryResult(Collections.singletonList(jcrNode));
    Query jcrQuery = Mockito.mock(Query.class);
    Mockito.when(jcrQuery.execute()).thenReturn(jcrResults);

    Mockito.doReturn(jcrQuery).when(suggestionHandler).getSuperSuggestionQuery(resolver, "coats");
    Mockito.doReturn(resolver).when(suggestionHandler).getResourceResolver();

    ValueFactory valueFactory = Mockito.mock(ValueFactory.class);
    Mockito.when(valueFactory.createValue(EL_GORDO_DOWN_JACKET)).thenReturn(new MockValue(EL_GORDO_DOWN_JACKET));
    Session session = Mockito.mock(Session.class);
    Mockito.when(resolver.adaptTo(Session.class)).thenReturn(session);
    Mockito.when(session.getValueFactory()).thenReturn(valueFactory);

    // The mocked CIF result
    MockResource cifProduct = new MockResource(resolver, "/var/commerce/products/graphql/cifresource", "commerce/components/product");
    cifProduct.addProperty(JcrConstants.JCR_TITLE, EL_GORDO_DOWN_JACKET); // The title is used for the suggestion
    Mockito.when(resolver.findResources(Mockito.any(), Mockito.any()))
        .thenReturn(Collections.singletonList((Resource) cifProduct).iterator());

    suggestionHandler.activate(null);
    suggestionHandler.onEvent(null);

    Query suggestionQuery = suggestionHandler.getSuggestionQuery(resolver, "coats");
    QueryResult queryResult = suggestionQuery.execute();
    RowIterator rows = queryResult.getRows();

    // The CIF result is first, then the JCR result
    Row row = rows.nextRow();
    Assert.assertEquals(EL_GORDO_DOWN_JACKET, row.getValue("rep:suggest()").getString());
    Assert.assertEquals(BROOKLYN_COAT, rows.nextRow().getValue("rep:suggest()").getString());

    // Not implemented
    Assert.assertNull(suggestionQuery.getLanguage());
    Assert.assertNull(suggestionQuery.getStatement());
    Assert.assertNull(suggestionQuery.getStoredQueryPath());
    Assert.assertNull(suggestionQuery.getBindVariableNames());
    Assert.assertNull(suggestionQuery.storeAsNode("whatever"));

    Assert.assertNull(queryResult.getColumnNames());
    Assert.assertNull(queryResult.getNodes());
    Assert.assertNull(queryResult.getSelectorNames());

    Assert.assertNull(row.getValues());
    Assert.assertNull(row.getPath());
    Assert.assertNull(row.getPath("whatever"));
    Assert.assertNull(row.getNode());
    Assert.assertNull(row.getNode("whatever"));
    Assert.assertEquals(0d, row.getScore(), 0);
    Assert.assertEquals(0d, row.getScore("whatever"), 0);

    suggestionHandler.deactivate(null);
}