Search in sources :

Example 1 with DefaultTraversingQOMTreeVisitor

use of org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor in project jackrabbit by apache.

the class QueryObjectModelImpl method init.

/**
 * Initializes a query instance from a query object model.
 *
 * @param sessionContext component context of the current session
 * @param handler  the query handler of the search index.
 * @param qomTree  the query object model tree.
 * @param language the original query syntax from where the JQOM was
 *                 created.
 * @param node     a nt:query node where the query was read from or
 *                 <code>null</code> if it is not a stored query.
 * @throws InvalidQueryException if the qom tree cannot be serialized
 *                               according to the given language.
 * @throws RepositoryException   if another error occurs
 */
public void init(SessionContext sessionContext, QueryHandler handler, QueryObjectModelTree qomTree, String language, Node node) throws InvalidQueryException, RepositoryException {
    checkNotInitialized();
    this.sessionContext = sessionContext;
    this.language = language;
    this.handler = handler;
    this.qomTree = qomTree;
    this.node = node;
    this.statement = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language).toString(this);
    try {
        qomTree.accept(new DefaultTraversingQOMTreeVisitor() {

            @Override
            public Object visit(BindVariableValueImpl node, Object data) {
                variables.put(node.getBindVariableName(), null);
                return data;
            }
        }, null);
    } catch (Exception ignore) {
    }
    this.lqf = new LuceneQueryFactory(sessionContext.getSessionImpl(), (SearchIndex) handler, variables);
    setInitialized();
}
Also used : DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) BindVariableValueImpl(org.apache.jackrabbit.spi.commons.query.qom.BindVariableValueImpl) SearchIndex(org.apache.jackrabbit.core.query.lucene.SearchIndex) LuceneQueryFactory(org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory) RepositoryException(javax.jcr.RepositoryException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 2 with DefaultTraversingQOMTreeVisitor

use of org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor in project jackrabbit by apache.

the class Ordering method fromQOM.

/**
 * Creates an ordering from a JCR QOM ordering.
 *
 * @param ordering   the JCR QOM ordering specification.
 * @param scs        the sort comparator source from the search index.
 * @param nsMappings the index internal namespace mappings.
 * @return an ordering.
 * @throws RepositoryException if an error occurs while translating the JCR
 *                             QOM ordering.
 */
public static Ordering fromQOM(final OrderingImpl ordering, final SharedFieldComparatorSource scs, final NamespaceMappings nsMappings) throws RepositoryException {
    final Name[] selectorName = new Name[1];
    QOMTreeVisitor visitor = new DefaultTraversingQOMTreeVisitor() {

        public Object visit(LengthImpl node, Object data) throws Exception {
            PropertyValueImpl propValue = (PropertyValueImpl) node.getPropertyValue();
            selectorName[0] = propValue.getSelectorQName();
            return new SortField(propValue.getPropertyQName().toString(), new LengthSortComparator(nsMappings), !ordering.isAscending());
        }

        public Object visit(LowerCaseImpl node, Object data) throws Exception {
            SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data);
            selectorName[0] = node.getSelectorQName();
            return new SortField(sf.getField(), new LowerCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending());
        }

        public Object visit(UpperCaseImpl node, Object data) throws Exception {
            SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data);
            selectorName[0] = node.getSelectorQName();
            return new SortField(sf.getField(), new UpperCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending());
        }

        public Object visit(FullTextSearchScoreImpl node, Object data) throws Exception {
            selectorName[0] = node.getSelectorQName();
            return new SortField(null, SortField.SCORE, !ordering.isAscending());
        }

        public Object visit(NodeLocalNameImpl node, Object data) throws Exception {
            selectorName[0] = node.getSelectorQName();
            return new SortField(FieldNames.LOCAL_NAME, SortField.STRING, !ordering.isAscending());
        }

        public Object visit(NodeNameImpl node, Object data) throws Exception {
            selectorName[0] = node.getSelectorQName();
            return new SortField(FieldNames.LABEL, SortField.STRING, !ordering.isAscending());
        }

        public Object visit(PropertyValueImpl node, Object data) throws Exception {
            selectorName[0] = node.getSelectorQName();
            return new SortField(node.getPropertyQName().toString(), scs, !ordering.isAscending());
        }

        public Object visit(OrderingImpl node, Object data) throws Exception {
            return ((DynamicOperandImpl) node.getOperand()).accept(this, data);
        }
    };
    try {
        SortField field = (SortField) ordering.accept(visitor, null);
        return new Ordering(selectorName[0], field);
    } catch (Exception e) {
        throw new RepositoryException(e);
    }
}
Also used : DynamicOperandImpl(org.apache.jackrabbit.spi.commons.query.qom.DynamicOperandImpl) DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) NodeLocalNameImpl(org.apache.jackrabbit.spi.commons.query.qom.NodeLocalNameImpl) OrderingImpl(org.apache.jackrabbit.spi.commons.query.qom.OrderingImpl) UpperCaseImpl(org.apache.jackrabbit.spi.commons.query.qom.UpperCaseImpl) FullTextSearchScoreImpl(org.apache.jackrabbit.spi.commons.query.qom.FullTextSearchScoreImpl) NodeNameImpl(org.apache.jackrabbit.spi.commons.query.qom.NodeNameImpl) SortField(org.apache.lucene.search.SortField) RepositoryException(javax.jcr.RepositoryException) QOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.QOMTreeVisitor) DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name) LowerCaseImpl(org.apache.jackrabbit.spi.commons.query.qom.LowerCaseImpl) LengthImpl(org.apache.jackrabbit.spi.commons.query.qom.LengthImpl) PropertyValueImpl(org.apache.jackrabbit.spi.commons.query.qom.PropertyValueImpl)

Aggregations

RepositoryException (javax.jcr.RepositoryException)2 DefaultTraversingQOMTreeVisitor (org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor)2 InvalidQueryException (javax.jcr.query.InvalidQueryException)1 LuceneQueryFactory (org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory)1 SearchIndex (org.apache.jackrabbit.core.query.lucene.SearchIndex)1 Name (org.apache.jackrabbit.spi.Name)1 BindVariableValueImpl (org.apache.jackrabbit.spi.commons.query.qom.BindVariableValueImpl)1 DynamicOperandImpl (org.apache.jackrabbit.spi.commons.query.qom.DynamicOperandImpl)1 FullTextSearchScoreImpl (org.apache.jackrabbit.spi.commons.query.qom.FullTextSearchScoreImpl)1 LengthImpl (org.apache.jackrabbit.spi.commons.query.qom.LengthImpl)1 LowerCaseImpl (org.apache.jackrabbit.spi.commons.query.qom.LowerCaseImpl)1 NodeLocalNameImpl (org.apache.jackrabbit.spi.commons.query.qom.NodeLocalNameImpl)1 NodeNameImpl (org.apache.jackrabbit.spi.commons.query.qom.NodeNameImpl)1 OrderingImpl (org.apache.jackrabbit.spi.commons.query.qom.OrderingImpl)1 PropertyValueImpl (org.apache.jackrabbit.spi.commons.query.qom.PropertyValueImpl)1 QOMTreeVisitor (org.apache.jackrabbit.spi.commons.query.qom.QOMTreeVisitor)1 UpperCaseImpl (org.apache.jackrabbit.spi.commons.query.qom.UpperCaseImpl)1 SortField (org.apache.lucene.search.SortField)1