Search in sources :

Example 1 with PropertyExpression

use of com.pogeyan.cmis.api.uri.expression.PropertyExpression in project copper-cms by PogeyanOSS.

the class MongoExpressionVisitor method visitOrder.

@Override
public Object visitOrder(OrderExpression orderExpression, Object filterResult, SortOrder sortOrder) {
    if (filterResult instanceof PropertyExpression) {
        PropertyExpression orderByProperty = (PropertyExpression) filterResult;
        String orderByLiteral = sortOrder == SortOrder.asc ? orderByProperty.getUriLiteral() : "-" + orderByProperty.getUriLiteral();
        // morphia maintains an internal sort representation
        return orderByLiteral;
    }
    return null;
}
Also used : PropertyExpression(com.pogeyan.cmis.api.uri.expression.PropertyExpression)

Example 2 with PropertyExpression

use of com.pogeyan.cmis.api.uri.expression.PropertyExpression in project copper-cms by PogeyanOSS.

the class MongoExpressionVisitor method visitBinary.

@Override
public Object visitBinary(BinaryExpression binaryExpression, BinaryOperator operator, Object leftSide, Object rightSide) {
    if (leftSide instanceof BinaryExpression) {
        // needs brackets to show the higher priority
        if (BinaryOperator.AND.equals(((BinaryExpression) binaryExpression).getOperator()) || BinaryOperator.OR.equals(((BinaryExpression) binaryExpression).getOperator())) {
        }
    } else if (leftSide instanceof PropertyExpression) {
        PropertyExpression leftOp = (PropertyExpression) leftSide;
        String rightSideValue = null;
        if (rightSide instanceof PropertyExpression) {
            PropertyExpression rightOp = (PropertyExpression) rightSide;
            rightSideValue = rightOp.getUriLiteral();
        } else if (rightSide instanceof LiteralExpression) {
            LiteralExpression rightOp = (LiteralExpression) rightSide;
            rightSideValue = rightOp.getUriLiteral();
        } else if (rightSide instanceof String) {
            rightSideValue = rightSide.toString();
        }
        switch(operator) {
            case EQ:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).equal(getStringObjectValue(rightSideValue));
            case NE:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).notEqual(getStringObjectValue(rightSideValue));
            case GE:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).greaterThanOrEq(getNumberObjectValue(rightSideValue));
            case GT:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).greaterThan(getNumberObjectValue(rightSideValue));
            case LE:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).lessThanOrEq(getNumberObjectValue(rightSideValue));
            case LT:
                return this.query.criteria(getQueryName(leftOp.getUriLiteral())).lessThan(getNumberObjectValue(rightSideValue));
            default:
                // Other operators are not supported for SQL Statements
                throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral());
        }
    } else {
        ArrayList<Criteria> operands = new ArrayList<>();
        if (leftSide.getClass() == CriteriaContainerImpl.class) {
            operands.add((Criteria) leftSide);
        }
        if (rightSide.getClass() == CriteriaContainerImpl.class) {
            operands.add((Criteria) rightSide);
        }
        switch(operator) {
            case OR:
                this.query.or(operands.toArray(new Criteria[operands.size()]));
                break;
            case AND:
                this.query.and(operands.toArray(new Criteria[operands.size()]));
                break;
            default:
                // Other operators are not supported for SQL Statements
                throw new UnsupportedOperationException("Unsupported operator: " + operator.toUriLiteral());
        }
    }
    // return the binary statement
    return this.query;
}
Also used : BinaryExpression(com.pogeyan.cmis.api.uri.expression.BinaryExpression) LiteralExpression(com.pogeyan.cmis.api.uri.expression.LiteralExpression) ArrayList(java.util.ArrayList) PropertyExpression(com.pogeyan.cmis.api.uri.expression.PropertyExpression) Criteria(org.mongodb.morphia.query.Criteria) CriteriaContainerImpl(org.mongodb.morphia.query.CriteriaContainerImpl)

Example 3 with PropertyExpression

use of com.pogeyan.cmis.api.uri.expression.PropertyExpression in project copper-cms by PogeyanOSS.

the class MongoExpressionVisitor method visitMethod.

@Override
public Object visitMethod(MethodExpression methodExpression, MethodOperator method, List<Object> parameters) {
    if (parameters.size() != 2) {
        throw new UnsupportedOperationException("Unsupported parameters length, it should be 2");
    }
    PropertyExpression fieldOperand = (PropertyExpression) parameters.get(0);
    String fieldValue = parameters.get(1).toString();
    fieldValue = fieldValue.replaceAll("\'", "");
    switch(method) {
        case STARTSWITH:
            Pattern sw_pattern = Pattern.compile("^" + fieldValue, Pattern.CASE_INSENSITIVE);
            return this.query.filter(getQueryName(fieldOperand.getUriLiteral()), sw_pattern);
        case ENDSWITH:
            Pattern ew_pattern = Pattern.compile(Pattern.quote(fieldValue) + "$", Pattern.CASE_INSENSITIVE);
            return this.query.filter(getQueryName(fieldOperand.getUriLiteral()), ew_pattern);
        case CONTAINS:
            Pattern iew_pattern = Pattern.compile(Pattern.quote(fieldValue), Pattern.CASE_INSENSITIVE);
            return this.query.filter(getQueryName(fieldOperand.getUriLiteral()), iew_pattern);
        default:
            // Other operators are not supported for SQL Statements
            throw new UnsupportedOperationException("Unsupported operator: " + method.toUriLiteral());
    }
}
Also used : Pattern(java.util.regex.Pattern) PropertyExpression(com.pogeyan.cmis.api.uri.expression.PropertyExpression)

Aggregations

PropertyExpression (com.pogeyan.cmis.api.uri.expression.PropertyExpression)3 BinaryExpression (com.pogeyan.cmis.api.uri.expression.BinaryExpression)1 LiteralExpression (com.pogeyan.cmis.api.uri.expression.LiteralExpression)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Criteria (org.mongodb.morphia.query.Criteria)1 CriteriaContainerImpl (org.mongodb.morphia.query.CriteriaContainerImpl)1