Search in sources :

Example 1 with ColumnImpl

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

the class QueryImpl method columnForName.

/**
 * Returns a column for the given property name and the default selector
 * name.
 *
 * @param propertyName the name of the property as well as the column.
 * @return a column.
 * @throws RepositoryException if an error occurs while creating the column.
 */
protected ColumnImpl columnForName(Name propertyName) throws RepositoryException {
    Workspace workspace = sessionContext.getSessionImpl().getWorkspace();
    QueryObjectModelFactory qomFactory = workspace.getQueryManager().getQOMFactory();
    String name = sessionContext.getJCRName(propertyName);
    return (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), name, name);
}
Also used : QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) Workspace(javax.jcr.Workspace)

Example 2 with ColumnImpl

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

the class QueryImpl method getColumns.

/**
 * Returns the columns for this query.
 *
 * @return array of columns.
 * @throws RepositoryException if an error occurs.
 */
protected ColumnImpl[] getColumns() throws RepositoryException {
    SessionImpl session = sessionContext.getSessionImpl();
    QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory();
    // get columns
    Map<Name, ColumnImpl> columns = new LinkedHashMap<Name, ColumnImpl>();
    for (Name name : root.getSelectProperties()) {
        String pn = sessionContext.getJCRName(name);
        ColumnImpl col = (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), pn, pn);
        columns.put(name, col);
    }
    if (columns.size() == 0) {
        // use node type constraint
        LocationStepQueryNode[] steps = root.getLocationNode().getPathSteps();
        final Name[] ntName = new Name[1];
        steps[steps.length - 1].acceptOperands(new DefaultQueryNodeVisitor() {

            public Object visit(AndQueryNode node, Object data) throws RepositoryException {
                return node.acceptOperands(this, data);
            }

            public Object visit(NodeTypeQueryNode node, Object data) {
                ntName[0] = node.getValue();
                return data;
            }
        }, null);
        if (ntName[0] == null) {
            ntName[0] = NameConstants.NT_BASE;
        }
        NodeTypeImpl nt = session.getNodeTypeManager().getNodeType(ntName[0]);
        PropertyDefinition[] propDefs = nt.getPropertyDefinitions();
        for (PropertyDefinition pd : propDefs) {
            QPropertyDefinition propDef = ((PropertyDefinitionImpl) pd).unwrap();
            if (!propDef.definesResidual() && !propDef.isMultiple()) {
                columns.put(propDef.getName(), columnForName(propDef.getName()));
            }
        }
    }
    // add jcr:path and jcr:score if not selected already
    if (!columns.containsKey(NameConstants.JCR_PATH)) {
        columns.put(NameConstants.JCR_PATH, columnForName(NameConstants.JCR_PATH));
    }
    if (!columns.containsKey(NameConstants.JCR_SCORE)) {
        columns.put(NameConstants.JCR_SCORE, columnForName(NameConstants.JCR_SCORE));
    }
    return columns.values().toArray(new ColumnImpl[columns.size()]);
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) LinkedHashMap(java.util.LinkedHashMap) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) SessionImpl(org.apache.jackrabbit.core.SessionImpl) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Aggregations

QueryObjectModelFactory (javax.jcr.query.qom.QueryObjectModelFactory)2 ColumnImpl (org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl)2 LinkedHashMap (java.util.LinkedHashMap)1 RepositoryException (javax.jcr.RepositoryException)1 Workspace (javax.jcr.Workspace)1 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)1 SessionImpl (org.apache.jackrabbit.core.SessionImpl)1 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)1 Name (org.apache.jackrabbit.spi.Name)1 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)1 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)1 AndQueryNode (org.apache.jackrabbit.spi.commons.query.AndQueryNode)1 DefaultQueryNodeVisitor (org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor)1 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)1 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)1