Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

From source file:org.sipfoundry.sipxconfig.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAddUser() throws Exception {
    Group g = new Group();
    PermissionName.INTERNATIONAL_DIALING.setEnabled(g, false);
    PermissionName.LONG_DISTANCE_DIALING.setEnabled(g, false);
    PermissionName.TOLL_FREE_DIALING.setEnabled(g, false);
    PermissionName.LOCAL_DIALING.setEnabled(g, true);
    PermissionName.FREESWITH_VOICEMAIL.setEnabled(g, false);
    PermissionName.EXCHANGE_VOICEMAIL.setEnabled(g, true);

    m_testUser.addGroup(g);/*  w ww .j  a v  a 2  s  .  c o  m*/
    m_testUser.setUserName("goober");
    m_testUser.setUniqueId(1);
    getReplicationManager().replicateEntity(m_testUser, DataSet.PERMISSION);

    assertObjectWithIdPresent(getEntityCollection(), "User1");
    assertObjectListFieldCount(getEntityCollection(), "User1", MongoConstants.PERMISSIONS, 8);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User1").and(MongoConstants.PERMISSIONS).size(4).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.LOCAL_DIALING.getName()).is(PermissionName.VOICEMAIL.getName())
            .is(PermissionName.EXCHANGE_VOICEMAIL.getName()).is(PermissionName.MOBILE.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAuthCodePermissions() {
    InternalUser user = new InternalUser();
    user.setSipPassword("123");
    user.setPintoken("11");
    user.setPermissionManager(getPermissionManager());
    user.setPermission(PermissionName.NINEHUNDERED_DIALING, true);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, false);
    user.setPermission(PermissionName.LOCAL_DIALING, false);
    user.setPermission(PermissionName.LONG_DISTANCE_DIALING, false);
    user.setPermission(PermissionName.MOBILE, false);
    user.setPermission(PermissionName.TOLL_FREE_DIALING, false);

    AuthCode code = new AuthCode();
    code.setInternalUser(user);//w w w.  java2  s . co m
    getReplicationManager().replicateEntity(code, DataSet.PERMISSION);
    assertObjectWithIdPresent(getEntityCollection(), "AuthCode-1");
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.NINEHUNDERED_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());

    user.setPermission(PermissionName.NINEHUNDERED_DIALING, false);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, true);
    code.setInternalUser(user);

    getReplicationManager().replicateEntity(code, DataSet.PERMISSION);
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.INTERNATIONAL_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.UserStaticTestIntegration.java

License:Contributor Agreement License

public void testGenerate() throws Exception {
    getReplicationManager().replicateEntity(m_users.get(0), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(1), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(2), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(3), DataSet.USER_STATIC);
    assertCollectionCount(getEntityCollection(), 4);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User0").and(MongoConstants.STATIC + "." + MongoConstants.CONTACT)
            .is("sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectPresent(getEntityCollection(), qb.get());
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.CONTACT, "sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.TO_URI, "sip:" + USER_DATA[0][3] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User1",
            MongoConstants.STATIC + "." + MongoConstants.EVENT, "message-summary");
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User2",
            MongoConstants.STATIC + "." + MongoConstants.FROM_URI, "sip:IVR@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User3",
            MongoConstants.STATIC + "." + MongoConstants.CONTACT, "sip:test@mwi.com");
}

From source file:org.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

@Override
public void visit(Comparison obj) {

    // this for $cond in the select statement, and formatting of command for $cond vs $match is different
    if (this.processingDerivedColumn) {
        visitDerivedExpression(obj);//w  w w .  j av a 2 s  .c o  m
        return;
    }

    // this for the normal where clause
    ColumnDetail leftExprDetails = getExpressionAlias(obj.getLeftExpression());
    append(obj.getRightExpression());
    Object rightExpr = this.onGoingExpression.pop();
    if (this.expressionMap.get(rightExpr) != null) {
        rightExpr = this.expressionMap.get(rightExpr).getProjectedName();
    }

    QueryBuilder query = leftExprDetails.getQueryBuilder();
    buildComparisionQuery(obj, rightExpr, query);

    if (leftExprDetails.partOfProject || obj.getLeftExpression() instanceof ColumnReference) {
        this.onGoingExpression.push(query.get());
    } else {
        this.onGoingExpression
                .push(buildFunctionQuery(obj, (BasicDBObject) leftExprDetails.expression, rightExpr));
    }

    if (obj.getLeftExpression() instanceof ColumnReference) {
        ColumnReference column = (ColumnReference) obj.getLeftExpression();
        this.mongoDoc.updateReferenceColumnValue(column.getTable().getName(), column.getName(), rightExpr);
    }
}

From source file:org.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

@Override
public void visit(Like obj) {
    append(obj.getLeftExpression());/*from   w w w. jav  a 2  s  .  c o  m*/
    Object expr = this.onGoingExpression.pop();
    ColumnDetail detail = this.expressionMap.get(expr);
    QueryBuilder query = QueryBuilder.start();
    if (detail == null) {
        this.exceptions.add(new TranslatorException(MongoDBPlugin.Event.TEIID18033,
                MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18033)));
    } else {
        query = detail.getQueryBuilder();
        buildLikeQuery(obj, query);
        this.onGoingExpression.push(query.get());
    }
}

From source file:org.teiid.translator.mongodb.MongoDBUpdateExecution.java

License:Open Source License

DBObject getInsertMatch(MongoDocument mongoDocument, Map<String, Object> values) throws TranslatorException {
    List<DBObject> matches = new ArrayList<DBObject>();
    HashMap<String, Object> matchValues = new HashMap<String, Object>();
    MongoDocument mergeDocument = mongoDocument.getMergeDocument();
    MongoDocument targetDocument = mongoDocument.getTargetDocument();

    if (mongoDocument.getMergeKey().getAssociation() == Association.ONE) {
        while (mergeDocument.isMerged()) {
            matches.add(QueryBuilder.start(mergeDocument.getDocumentName()).exists(true).get());
            if (mergeDocument.getMergeKey().getAssociation() == Association.ONE) {
                mergeDocument = mergeDocument.getMergeDocument();
            } else {
                break;
            }//w w w .j  a v  a  2  s . c o  m
        }
        if (mergeDocument.equals(targetDocument)) {
            matchValues.put("_id", values.get("_id")); //$NON-NLS-1$ //$NON-NLS-2$
        } else {
            matchValues.put(mergeDocument + "._id", values.get("_id")); //$NON-NLS-1$ //$NON-NLS-2$
        }
    } else {
        // this nested so add exists
        if (!mergeDocument.equals(targetDocument)) {
            matches.add(QueryBuilder.start(mergeDocument.getDocumentName()).exists(true).get());
        }

        // this value can go only one level up
        String columnName = mergeDocument
                .getColumnName(mongoDocument.getMergeKey().getReferenceColumns().get(0));
        Object value = values.get(mongoDocument.getMergeKey().getColumns().get(0));
        if (value instanceof MergeDetails) {
            value = ((MergeDetails) value).getValue();
        }

        if (mergeDocument.equals(targetDocument)) {
            matchValues.put(columnName, value);
        } else {
            while (mergeDocument.isMerged()) {
                if (mergeDocument.getMergeKey().getAssociation() == Association.ONE) {
                    mergeDocument = mergeDocument.getMergeDocument();
                } else {
                    break;
                }
            }
            matchValues.put(columnName, value);
        }
    }

    // build match
    BasicDBObject match = new BasicDBObject();
    for (String key : matchValues.keySet()) {
        match.append(key, matchValues.get(key));
    }
    if (!matches.isEmpty()) {
        matches.add(match);
        QueryBuilder qb = QueryBuilder.start().and(matches.toArray(new BasicDBObject[matches.size()]));
        match = (BasicDBObject) qb.get();
    }
    return match;
}

From source file:org.teiid.translator.mongodb.MongoDBUpdateVisitor.java

License:Open Source License

@Override
public void visit(Comparison obj) {
    if (!this.mongoDoc.isMerged()
            || this.mongoDoc.isMerged() && this.mongoDoc.getMergeAssociation() != Association.MANY) {
        super.visit(obj);
        return;//from  w ww.java  2s.  c om
    }

    try {
        // this for the normal where clause
        ColumnDetail leftExpr = getExpressionAlias(obj.getLeftExpression());

        append(obj.getRightExpression());

        Object rightExpr = this.onGoingExpression.pop();
        if (this.expressionMap.get(rightExpr) != null) {
            rightExpr = this.expressionMap.get(rightExpr).getProjectedName();
        }
        // build pull criteria for delete; the pull criteria only applies in merge scenario
        // and only columns in the embedded document.
        boolean buildPullQuery = (includeInPullCriteria(obj.getLeftExpression())
                && includeInPullCriteria(obj.getRightExpression()));

        if (!buildPullQuery) {
            QueryBuilder query = leftExpr.getQueryBuilder();
            buildComparisionQuery(obj, rightExpr, query);
            this.onGoingExpression.push(query.get());
        } else {
            QueryBuilder pullQuery = leftExpr.getPullQueryBuilder();
            buildComparisionQuery(obj, rightExpr, pullQuery);
            this.onGoingPullCriteria.push(pullQuery.get());
        }

        if (obj.getLeftExpression() instanceof ColumnReference) {
            ColumnReference column = (ColumnReference) obj.getLeftExpression();
            this.mongoDoc.updateReferenceColumnValue(column.getTable().getName(), column.getName(), rightExpr);
        }
    } catch (TranslatorException e) {
        this.exceptions.add(e);
    }
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

private DBObject createMongoQuery(final SearchCriteria searchCriteria) {

    QueryBuilder queryBuilder = null;//from   w  w w .j av a  2  s.  co m

    final List<SearchCriterion> and = searchCriteria.getAnd();
    if (and != null && !and.isEmpty()) {

        queryBuilder = new QueryBuilder();

        for (final SearchCriterion searchCriterion : and) {

            final String referenceSlot = searchCriterion.getReferenceSlot();
            queryBuilder.and(referenceSlot);
            addQueryCriterion(searchCriterion, queryBuilder);

        }

    }

    final List<SearchCriterion> or = searchCriteria.getOr();
    if (or != null && !or.isEmpty()) {

        final DBObject[] orQueryCriterionArray = new DBObject[or.size()];
        for (int i = 0; i < or.size(); i++) {
            final SearchCriterion searchCriterion = or.get(i);
            final String referenceSlot = searchCriterion.getReferenceSlot();
            final QueryBuilder orQueryCriterionBuilder = QueryBuilder.start(referenceSlot);
            addQueryCriterion(searchCriterion, orQueryCriterionBuilder);
            orQueryCriterionArray[i] = orQueryCriterionBuilder.get();
        }

        final QueryBuilder orQueryBuilder = new QueryBuilder();
        orQueryBuilder.or(orQueryCriterionArray);

        if (queryBuilder != null) {
            // AND the OR clause together with the AND query
            queryBuilder.and(orQueryBuilder.get());
        } else {
            queryBuilder = orQueryBuilder;
        }
    }

    if (queryBuilder == null) {
        return null;
    }

    final DBObject mongoQuery = queryBuilder.get();
    return mongoQuery;
}