Example usage for com.mongodb QueryBuilder QueryBuilder

List of usage examples for com.mongodb QueryBuilder QueryBuilder

Introduction

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

Prototype

public QueryBuilder() 

Source Link

Document

Creates a builder with an empty query

Usage

From source file:org.keycloak.models.mongo.keycloak.adapters.MongoUserSessionPersisterProvider.java

License:Apache License

private void onUserRemoved(RealmModel realm, String userId) {
    DBObject query = new QueryBuilder().and("userId").is(userId).get();
    getMongoStore().removeEntities(MongoOnlineUserSessionEntity.class, query, false, invocationContext);
    getMongoStore().removeEntities(MongoOfflineUserSessionEntity.class, query, false, invocationContext);
}

From source file:org.keycloak.models.mongo.keycloak.adapters.MongoUserSessionPersisterProvider.java

License:Apache License

@Override
public void clearDetachedUserSessions() {
    DBObject query = new QueryBuilder().and("clientSessions").is(Collections.emptyList()).get();
    getMongoStore().removeEntities(MongoOnlineUserSessionEntity.class, query, false, invocationContext);
    getMongoStore().removeEntities(MongoOfflineUserSessionEntity.class, query, false, invocationContext);
}

From source file:org.keycloak.models.mongo.keycloak.adapters.MongoUserSessionPersisterProvider.java

License:Apache License

@Override
public int getUserSessionsCount(boolean offline) {
    DBObject query = new QueryBuilder().get();

    Class<? extends MongoUserSessionEntity> clazz = offline ? MongoOfflineUserSessionEntity.class
            : MongoOnlineUserSessionEntity.class;
    return getMongoStore().countEntities(clazz, query, invocationContext);
}

From source file:org.keycloak.models.mongo.keycloak.adapters.MongoUserSessionPersisterProvider.java

License:Apache License

@Override
public void updateAllTimestamps(int time) {
    // 1) Update timestamp of clientSessions

    DBObject timestampSubquery = new QueryBuilder().and("timestamp").notEquals(time).get();

    DBObject query = new QueryBuilder().and("clientSessions").elemMatch(timestampSubquery).get();

    DBObject update = new QueryBuilder().and("$set").is(new BasicDBObject("clientSessions.$.timestamp", time))
            .get();/*from   ww  w  .  j  a va 2s . co  m*/

    // Not sure how to do in single query :/
    int countModified = 1;
    while (countModified > 0) {
        countModified = getMongoStore().updateEntities(MongoOfflineUserSessionEntity.class, query, update,
                invocationContext);
    }

    countModified = 1;
    while (countModified > 0) {
        countModified = getMongoStore().updateEntities(MongoOnlineUserSessionEntity.class, query, update,
                invocationContext);
    }

    // 2) update lastSessionRefresh of userSessions
    query = new QueryBuilder().get();

    update = new QueryBuilder().and("$set").is(new BasicDBObject("lastSessionRefresh", time)).get();

    getMongoStore().updateEntities(MongoOfflineUserSessionEntity.class, query, update, invocationContext);
    getMongoStore().updateEntities(MongoOnlineUserSessionEntity.class, query, update, invocationContext);
}

From source file:org.keycloak.models.mongo.keycloak.adapters.MongoUserSessionPersisterProvider.java

License:Apache License

@Override
public List<UserSessionModel> loadUserSessions(int firstResult, int maxResults, boolean offline) {
    DBObject query = new QueryBuilder().get();
    DBObject sort = new BasicDBObject("id", 1);

    Class<? extends MongoUserSessionEntity> clazz = offline ? MongoOfflineUserSessionEntity.class
            : MongoOnlineUserSessionEntity.class;

    List<? extends MongoUserSessionEntity> entities = getMongoStore().loadEntities(clazz, query, sort,
            firstResult, maxResults, invocationContext);

    List<UserSessionModel> results = new LinkedList<>();
    for (MongoUserSessionEntity entity : entities) {
        RealmModel realm = session.realms().getRealm(entity.getRealmId());
        UserModel user = session.users().getUserById(entity.getUserId(), realm);

        // Case when user was deleted in the meantime
        if (user == null) {
            onUserRemoved(realm, entity.getUserId());
            return loadUserSessions(firstResult, maxResults, offline);
        }/* w  ww . ja  v a2 s.co  m*/

        PersistentUserSessionAdapter userSession = toAdapter(realm, user, entity);
        results.add(userSession);
    }
    return results;
}

From source file:org.keycloak.storage.mongo.entity.FederatedUser.java

License:Apache License

@Override
public void afterRemove(MongoStoreInvocationContext context) {
    // Remove all consents of this user
    DBObject query = new QueryBuilder().and("userId").is(getId()).get();

    context.getMongoStore().removeEntities(MongoUserConsentEntity.class, query, true, context);
}

From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java

License:Apache License

@Override
public List<String> getStoredUsers(RealmModel realm, int first, int max) {
    QueryBuilder queryBuilder = new QueryBuilder().and("realmId").is(realm.getId());

    DBObject query = queryBuilder.get();
    List<FederatedUser> users = getMongoStore().loadEntities(FederatedUser.class, query, null, first, max,
            invocationContext);//from  w  w  w  .  j  av  a  2s  .co m
    List<String> ids = new LinkedList<>();
    for (FederatedUser user : users)
        ids.add(user.getId());
    return ids;
}

From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java

License:Apache License

@Override
public void preRemove(RealmModel realm) {
    DBObject query = new QueryBuilder().and("realmId").is(realm.getId()).get();
    getMongoStore().removeEntities(FederatedUser.class, query, true, invocationContext);
}

From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java

License:Apache License

@Override
public void preRemove(RealmModel realm, GroupModel group) {
    DBObject query = new QueryBuilder().and("groupIds").is(group.getId()).get();

    DBObject pull = new BasicDBObject("$pull", query);
    getMongoStore().updateEntities(FederatedUser.class, query, pull, invocationContext);

}

From source file:org.keycloak.storage.mongo.MongoUserFederatedStorageProvider.java

License:Apache License

@Override
public void preRemove(RealmModel realm, RoleModel role) {
    DBObject query = new QueryBuilder().and("roleIds").is(role.getId()).get();

    DBObject pull = new BasicDBObject("$pull", query);
    getMongoStore().updateEntities(FederatedUser.class, query, pull, invocationContext);

}