Java Code Examples for org.alfresco.repo.search.impl.querymodel.Constraint

The following examples show how to use org.alfresco.repo.search.impl.querymodel.Constraint. These examples are extracted from open source projects. 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
@Override
public void prepare(NamespaceService namespaceService, DictionaryService dictionaryService, QNameDAO qnameDAO, NodeDAO nodeDAO, TenantService tenantService, Set<String> selectors,
        Map<String, Argument> functionArgs, FunctionEvaluationContext functionContext, boolean supportBooleanFloatAndDouble)
{
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            if(constraint.getOccur() == Occur.OPTIONAL)
            {
                throw new QueryModelException("Disjunctions are not suported");
            }
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.prepare(namespaceService, dictionaryService, qnameDAO, nodeDAO, tenantService, selectors, functionArgs, functionContext, supportBooleanFloatAndDouble);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
}
 
Example 2
@Override
public void buildJoins(Map<QName, DBQueryBuilderJoinCommand> singleJoins, List<DBQueryBuilderJoinCommand> multiJoins)
{
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.buildJoins(singleJoins, multiJoins);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
    
}
 
Example 3
@Override
public void prepare(NamespaceService namespaceService, DictionaryService dictionaryService, QNameDAO qnameDAO, NodeDAO nodeDAO, TenantService tenantService, Set<String> selectors,
        Map<String, Argument> functionArgs, FunctionEvaluationContext functionContext, boolean supportBooleanFloatAndDouble)
{
    //throw new QueryModelException("Disjunctions are not suported");
    
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.prepare(namespaceService, dictionaryService, qnameDAO, nodeDAO, tenantService, selectors, functionArgs, functionContext, supportBooleanFloatAndDouble);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
}
 
Example 4
@Override
public void buildJoins(Map<QName, DBQueryBuilderJoinCommand> singleJoins, List<DBQueryBuilderJoinCommand> multiJoins)
{
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.buildJoins(singleJoins, multiJoins);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
    
}
 
Example 5
/**
 * @param orNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildDisjunction(CommonTree orNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    List<Constraint> constraints = new ArrayList<Constraint>(orNode.getChildCount());
    for (int i = 0; i < orNode.getChildCount(); i++)
    {
        CommonTree andNode = (CommonTree) orNode.getChild(i);
        Constraint constraint = buildConjunction(andNode, factory, functionEvaluationContext, selectors, columnMap);
        constraints.add(constraint);
    }
    if (constraints.size() == 1)
    {
        return constraints.get(0);
    } else
    {
        return factory.createDisjunction(constraints);
    }
}
 
Example 6
/**
 * @param andNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildConjunction(CommonTree andNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    List<Constraint> constraints = new ArrayList<Constraint>(andNode.getChildCount());
    for (int i = 0; i < andNode.getChildCount(); i++)
    {
        CommonTree notNode = (CommonTree) andNode.getChild(i);
        Constraint constraint = buildNegation(notNode, factory, functionEvaluationContext, selectors, columnMap);
        constraints.add(constraint);
    }
    if (constraints.size() == 1 && constraints.get(0).getOccur() != Occur.EXCLUDE)
    {
        return constraints.get(0);
    } else
    {
        return factory.createConjunction(constraints);
    }
}
 
Example 7
/**
 * @param notNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildNegation(CommonTree notNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    if (notNode.getType() == CMISParser.NEGATION)
    {
        Constraint constraint = buildTest((CommonTree) notNode.getChild(0), factory, functionEvaluationContext,
                selectors, columnMap);
        constraint.setOccur(Occur.EXCLUDE);
        return constraint;
    } else
    {
        return buildTest(notNode, factory, functionEvaluationContext, selectors, columnMap);
    }
}
 
Example 8
static private Constraint buildFTSTest(CommonTree argNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext,
        Selector selector, Map<String, Column> columnMap, String defaultField)
{
    CommonTree testNode = argNode;
    switch (testNode.getType())
    {
    case CMIS_FTSParser.DISJUNCTION:
    case CMIS_FTSParser.CONJUNCTION:
        return buildFTSConnective(testNode, factory, functionEvaluationContext, selector, columnMap, defaultField);
    case CMIS_FTSParser.TERM:
        return buildTerm(testNode, factory, functionEvaluationContext, selector, columnMap);
    case CMIS_FTSParser.PHRASE:
        return buildPhrase(testNode, factory, functionEvaluationContext, selector, columnMap);
    default:
        throw new FTSQueryException("Unsupported FTS option " + testNode.getText());
    }
}
 
Example 9
public ResultSet executeQuery(SearchParameters searchParameters)
{
    String ftsExpression = searchParameters.getQuery();
    QueryModelFactory factory = queryEngine.getQueryModelFactory();
    AlfrescoFunctionEvaluationContext context = new AlfrescoFunctionEvaluationContext(
            namespaceService, dictionaryService,
            searchParameters.getNamespace());

    QueryOptions options = QueryOptions.create(searchParameters);

    FTSParser.Mode mode;

    if(options.getDefaultFTSConnective() == Connective.AND)
    {
        mode = FTSParser.Mode.DEFAULT_CONJUNCTION;
    }
    else
    {
        mode = FTSParser.Mode.DEFAULT_DISJUNCTION;
    }
        
    Constraint constraint = FTSQueryParser.buildFTS(ftsExpression, factory, context, null, null, mode, options.getDefaultFTSFieldConnective(),
            searchParameters.getQueryTemplates(), options.getDefaultFieldName(), FTSQueryParser.RerankPhase.SINGLE_PASS);
    org.alfresco.repo.search.impl.querymodel.Query query = factory.createQuery(null, null, constraint, buildOrderings(factory, searchParameters));

    QueryEngineResults results = queryEngine.executeQuery(query, options, context);
    ResultSet resultSet = results.getResults().values().iterator().next();
    return resultSet;
}
 
Example 10
@Override
public void buildPredicateCommands(List<DBQueryBuilderPredicatePartCommand> predicatePartCommands)
{
    DBQueryBuilderPredicatePartCommand open = new DBQueryBuilderPredicatePartCommand();
    open.setType(DBQueryBuilderPredicatePartCommandType.OPEN);
    predicatePartCommands.add(open);
    
    boolean requiresOr = false;
    
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            if(requiresOr)
            {
                DBQueryBuilderPredicatePartCommand and = new DBQueryBuilderPredicatePartCommand();
                and.setType(DBQueryBuilderPredicatePartCommandType.OR);
                predicatePartCommands.add(and);
            }
            else
            {
                requiresOr = true;
            }
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.buildPredicateCommands(predicatePartCommands);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
    
    DBQueryBuilderPredicatePartCommand close = new DBQueryBuilderPredicatePartCommand();
    close.setType(DBQueryBuilderPredicatePartCommandType.CLOSE);
    predicatePartCommands.add(close);
    
}
 
Example 11
static private void setBoost(Constraint constraint, CommonTree subNode)
{
    for (int i = 0, l = subNode.getChildCount(); i < l; i++)
    {
        CommonTree child = (CommonTree) subNode.getChild(i);
        if (child.getType() == FTSParser.BOOST)
        {
            String boostString = child.getChild(0).getText();
            float boost = Float.parseFloat(boostString);
            constraint.setBoost(boost);
            return;
        }
    }
}
 
Example 12
public BaseQuery(List<Column> columns, Source source, Constraint constraint, List<Ordering> orderings)
{
    this.columns = columns;
    this.source = source;
    this.constraint = constraint;
    this.orderings = orderings;
}
 
Example 13
public Q addComponent(Set<String> selectors, Map<String, Argument> functionArgs, LuceneQueryBuilderContext<Q, S, E> luceneContext, FunctionEvaluationContext functionContext)
        throws E
{
    LuceneQueryParserExpressionAdaptor<Q, E> expressionBuilder = luceneContext.getLuceneQueryParserAdaptor().getExpressionAdaptor();
    ArrayList<Pair<Constraint, Q>> queriestoDisjoin = new ArrayList<>();
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof LuceneQueryBuilderComponent)
        {
            @SuppressWarnings("unchecked")
            LuceneQueryBuilderComponent<Q, S, E> luceneQueryBuilderComponent = (LuceneQueryBuilderComponent<Q, S, E>) constraint;
            Q constraintQuery = luceneQueryBuilderComponent.addComponent(selectors, functionArgs, luceneContext, functionContext);
            queriestoDisjoin.add(new Pair<Constraint, Q>(constraint, constraintQuery));
            if (constraintQuery != null)
            {
                switch (constraint.getOccur())
                {
                case DEFAULT:
                case MANDATORY:
                case OPTIONAL:
                    expressionBuilder.addOptional(constraintQuery, constraint.getBoost());
                    break;
                case EXCLUDE:
                    LuceneQueryParserExpressionAdaptor<Q, E> subExpressionBuilder = luceneContext.getLuceneQueryParserAdaptor().getExpressionAdaptor();
                    subExpressionBuilder.addRequired(luceneContext.getLuceneQueryParserAdaptor().getMatchAllNodesQuery());
                    subExpressionBuilder.addExcluded(constraintQuery);
                    expressionBuilder.addOptional(subExpressionBuilder.getQuery(),  constraint.getBoost());
                    break;
                }
            }
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
    return expressionBuilder.getQuery();

}
 
Example 14
public BaseJoin(Source left, Source right, JoinType joinType, Constraint joinConstraint)
{
    this.left = left;
    this.right = right;
    this.joinType = joinType;
    this.joinConstraint = joinConstraint;
}
 
Example 15
/**
 * @param testNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildTest(CommonTree testNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    if (testNode.getType() == CMISParser.DISJUNCTION)
    {
        return buildDisjunction(testNode, factory, functionEvaluationContext, selectors, columnMap);
    } else
    {
        return buildPredicate(testNode, factory, functionEvaluationContext, selectors, columnMap);
    }
}
 
Example 16
@SuppressWarnings("unused")
static public Constraint buildFTS(String ftsExpression, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Selector selector,
        Map<String, Column> columnMap, String defaultField)
{
    // TODO: Decode sql escape for '' should do in CMIS layer

    // parse templates to trees ...

    CMIS_FTSParser parser = null;
    try
    {
        CharStream cs = new ANTLRStringStream(ftsExpression);
        CMIS_FTSLexer lexer = new CMIS_FTSLexer(cs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new CMIS_FTSParser(tokens);
        CommonTree ftsNode = (CommonTree) parser.cmisFtsQuery().getTree();
        return buildFTSConnective(ftsNode, factory, functionEvaluationContext, selector, columnMap, defaultField);
    }
    catch (RecognitionException e)
    {
        if (parser != null)
        {
            String[] tokenNames = parser.getTokenNames();
            String hdr = parser.getErrorHeader(e);
            String msg = parser.getErrorMessage(e, tokenNames);
            throw new FTSQueryException(hdr + "\n" + msg, e);
        }
        return null;
    }

}
 
Example 17
static private Constraint buildPhrase(CommonTree testNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Selector selector, Map<String, Column> columnMap)
{
    String functionName = FTSPhrase.NAME;
    Function function = factory.getFunction(functionName);
    Map<String, Argument> functionArguments = new LinkedHashMap<String, Argument>();
    LiteralArgument larg = factory.createLiteralArgument(FTSPhrase.ARG_PHRASE, DataTypeDefinition.TEXT, getText(testNode.getChild(0)));
    functionArguments.put(larg.getName(), larg);
    larg = factory.createLiteralArgument(FTSPhrase.ARG_TOKENISATION_MODE, DataTypeDefinition.ANY, AnalysisMode.DEFAULT);
    functionArguments.put(larg.getName(), larg);
    return factory.createFunctionalConstraint(function, functionArguments);
}
 
Example 18
static private Constraint buildTerm(CommonTree testNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext,
        Selector selector, Map<String, Column> columnMap)
{
    String functionName = FTSTerm.NAME;
    Function function = factory.getFunction(functionName);
    Map<String, Argument> functionArguments = new LinkedHashMap<String, Argument>();
    LiteralArgument larg = factory.createLiteralArgument(FTSTerm.ARG_TERM, DataTypeDefinition.TEXT, getText(testNode.getChild(0)));
    functionArguments.put(larg.getName(), larg);
    larg = factory.createLiteralArgument(FTSTerm.ARG_TOKENISATION_MODE, DataTypeDefinition.ANY, AnalysisMode.DEFAULT);
    functionArguments.put(larg.getName(), larg);
    return factory.createFunctionalConstraint(function, functionArguments);
}
 
Example 19
/**
 * @param source Source
 * @param constraint Constraint
 */
public DBQuery(List<Column> columns, Source source, Constraint constraint, List<Ordering> orderings)
{
    super(columns, source, constraint, orderings);
}
 
Example 20
/**
 */
public DBConjunction(List<Constraint> constraints)
{
    super(constraints);
}
 
Example 21
@Override
public void buildPredicateCommands(List<DBQueryBuilderPredicatePartCommand> predicatePartCommands)
{
    DBQueryBuilderPredicatePartCommand open = new DBQueryBuilderPredicatePartCommand();
    open.setType(DBQueryBuilderPredicatePartCommandType.OPEN);
    predicatePartCommands.add(open);
    
    boolean requiresAnd = false;
    
    for (Constraint constraint : getConstraints())
    {
        if (constraint instanceof DBQueryBuilderComponent)
        {
            if(requiresAnd)
            {
                DBQueryBuilderPredicatePartCommand and = new DBQueryBuilderPredicatePartCommand();
                and.setType(DBQueryBuilderPredicatePartCommandType.AND);
                predicatePartCommands.add(and);
            }
            else
            {
                requiresAnd = true;
            }
            if(constraint.getOccur() == Occur.EXCLUDE)
            {
                DBQueryBuilderPredicatePartCommand not = new DBQueryBuilderPredicatePartCommand();
                not.setType(DBQueryBuilderPredicatePartCommandType.NOT);
                predicatePartCommands.add(not);
            }
            DBQueryBuilderComponent dbQueryBuilderComponent = (DBQueryBuilderComponent) constraint;
            dbQueryBuilderComponent.buildPredicateCommands(predicatePartCommands);
        }
        else
        {
            throw new UnsupportedOperationException();
        }
    }
    
    DBQueryBuilderPredicatePartCommand close = new DBQueryBuilderPredicatePartCommand();
    close.setType(DBQueryBuilderPredicatePartCommandType.CLOSE);
    predicatePartCommands.add(close);
    
}
 
Example 22
/**
 */
public DBDisjunction(List<Constraint> constraints)
{
    super(constraints);
}
 
Example 23
@Override
public Query createQuery(List<Column> columns, Source source, Constraint constraint, List<Ordering> orderings)
{
    return new DBQuery(columns, source, constraint, orderings);
}
 
Example 24
@Override
public Join createJoin(Source left, Source right, JoinType joinType, Constraint joinCondition)
{
    return new DBJoin(left, right, joinType, joinCondition);
}
 
Example 25
@Override
public Constraint createConjunction(List<Constraint> constraints)
{
    return new DBConjunction(constraints);
}
 
Example 26
@Override
public Constraint createDisjunction(List<Constraint> constraints)
{
    return new DBDisjunction(constraints);
}
 
Example 27
@Override
public Constraint createFunctionalConstraint(Function function, Map<String, Argument> functionArguments)
{
    return new DBFunctionalConstraint(function, functionArguments);
}
 
Example 28
public BaseConjunction(List<Constraint> constraints)
{
    this.constraints = constraints;
}
 
Example 29
public List<Constraint> getConstraints()
{
    return constraints;
}
 
Example 30
public Constraint getConstraint()
{
    return constraint;
}