use of javax.jcr.query.QueryManager in project jackrabbit by apache.
the class QueryResultTest method testIteratorNext.
public void testIteratorNext() throws RepositoryException {
QueryManager qm = superuser.getWorkspace().getQueryManager();
for (int i = 0; i < 10; i++) {
String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
int size = 0;
for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
it.nextNode();
size++;
}
assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, size);
// remove node for the next iteration
testRootNode.getNode("node" + i).remove();
testRootNode.save();
}
}
use of javax.jcr.query.QueryManager in project jackrabbit by apache.
the class GQL method executeJcrQuery.
private RowIterator executeJcrQuery(String jcrQuery, String jcrQueryLanguage) {
try {
QueryManager qm = session.getWorkspace().getQueryManager();
RowIterator nodes = qm.createQuery(jcrQuery, jcrQueryLanguage).execute().getRows();
if (filter != null) {
nodes = new FilteredRowIterator(nodes);
}
if (offset > 0) {
try {
nodes.skip(offset);
} catch (NoSuchElementException e) {
return RowIteratorAdapter.EMPTY;
}
}
if (numResults == Integer.MAX_VALUE) {
return new RowIterAdapter(nodes, nodes.getSize());
}
List<Row> resultRows = new ArrayList<Row>();
while (numResults-- > 0 && nodes.hasNext()) {
resultRows.add(nodes.nextRow());
}
return new RowIterAdapter(resultRows, resultRows.size());
} catch (RepositoryException e) {
// in case of error return empty result
return RowIteratorAdapter.EMPTY;
}
}
use of javax.jcr.query.QueryManager in project jackrabbit by apache.
the class SearchResourceImpl method getQueryGrammerSet.
//-------------------------------------------< SearchResource interface >---
/**
* @see SearchResource#getQueryGrammerSet()
*/
public QueryGrammerSet getQueryGrammerSet() {
QueryGrammerSet qgs = new QueryGrammerSet();
try {
QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
String[] langs = qMgr.getSupportedQueryLanguages();
for (String lang : langs) {
// Note: Existing clients already assume that the
// query languages returned in the DASL header are
// not prefixed with any namespace, so we probably
// shouldn't use an explicit namespace here.
qgs.addQueryLanguage(lang, Namespace.EMPTY_NAMESPACE);
}
} catch (RepositoryException e) {
log.debug(e.getMessage());
}
return qgs;
}
use of javax.jcr.query.QueryManager in project jackrabbit by apache.
the class SearchResourceImpl method getQuery.
/**
* Create a query from the information present in the <code>sInfo</code>
* object.<br>The following JCR specific logic is applied:
* <ul>
* <li>If the requested resource represents a node with nodetype nt:query, the
* request body is ignored and the query defined with the node is executed
* instead.</li>
* <li>If the requested resource does not represent an existing item, the
* specified query is persisted by calling {@link Query#storeAsNode(String)}.</li>
* </ul>
* @param sInfo defining the query to be executed
* @return <code>Query</code> object.
* @throws javax.jcr.query.InvalidQueryException if the query defined by <code>sInfo</code> is invalid
* @throws RepositoryException the query manager cannot be accessed or if
* another error occurs.
* @throws DavException if <code>sInfo</code> is <code>null</code> and
* the underlying repository item is not an nt:query node or if an error
* occurs when calling {@link Query#storeAsNode(String)}/
*/
private Query getQuery(SearchInfo sInfo) throws InvalidQueryException, RepositoryException, DavException {
Session session = getRepositorySession();
NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
Node rootNode = session.getRootNode();
QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
// test if query is defined by requested repository node
String itemPath = locator.getRepositoryPath();
if (itemPath != null && !rootNode.getPath().equals(itemPath)) {
String qNodeRelPath = itemPath.substring(1);
if (rootNode.hasNode(qNodeRelPath)) {
Node qNode = rootNode.getNode(qNodeRelPath);
if (qNode.isNodeType(JcrConstants.NT_QUERY)) {
return qMgr.getQuery(qNode);
}
}
}
Query q;
if (sInfo != null) {
// apply namespace mappings to session
Map<String, String> namespaces = sInfo.getNamespaces();
try {
for (Map.Entry<String, String> entry : namespaces.entrySet()) {
String prefix = entry.getKey();
String uri = entry.getValue();
session.setNamespacePrefix(prefix, uri);
}
q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
if (SearchInfo.NRESULTS_UNDEFINED != sInfo.getNumberResults()) {
q.setLimit(sInfo.getNumberResults());
}
if (SearchInfo.OFFSET_UNDEFINED != sInfo.getOffset()) {
q.setOffset(sInfo.getOffset());
}
} finally {
// reset namespace mappings
for (String uri : namespaces.values()) {
try {
session.setNamespacePrefix(nsReg.getPrefix(uri), uri);
} catch (RepositoryException e) {
log.warn("Unable to reset mapping of namespace: " + uri);
}
}
}
} else {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, locator.getResourcePath() + " is not a nt:query node -> searchRequest body required.");
}
/* test if resource path does not exist -> thus indicating that
the query must be made persistent by calling Query.save(String) */
if (itemPath != null && !getRepositorySession().itemExists(itemPath)) {
try {
q.storeAsNode(itemPath);
} catch (RepositoryException e) {
// ItemExistsException should never occur.
throw new JcrDavException(e);
}
}
return q;
}
use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.
the class LuceneIndexDescendantSuggestionTest method getSuggestions.
private Set<String> getSuggestions(String query) throws Exception {
QueryManager queryManager = session.getWorkspace().getQueryManager();
QueryResult result = queryManager.createQuery(query, Query.JCR_SQL2).execute();
RowIterator rows = result.getRows();
Set<String> suggestions = newHashSet();
while (rows.hasNext()) {
suggestions.add(rows.nextRow().getValue("suggestion").getString());
}
return suggestions;
}
Aggregations