Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:com.sitewhere.user.persistence.mongodb.MongoUserManagement.java

License:Open Source License

@Override
public List<IGrantedAuthority> listGrantedAuthorities(IGrantedAuthoritySearchCriteria criteria)
        throws SiteWhereException {
    try {//from   w  w w .ja v  a 2  s . c  o m
        MongoCollection<Document> auths = getMongoClient().getAuthoritiesCollection();
        FindIterable<Document> found = auths.find()
                .sort(new BasicDBObject(MongoGrantedAuthority.PROP_AUTHORITY, 1));
        MongoCursor<Document> cursor = found.iterator();

        List<IGrantedAuthority> matches = new ArrayList<IGrantedAuthority>();
        try {
            while (cursor.hasNext()) {
                Document match = cursor.next();
                matches.add(MongoGrantedAuthority.fromDBObject(match));
            }
        } finally {
            cursor.close();
        }
        return matches;
    } catch (MongoTimeoutException e) {
        throw new SiteWhereException("Connection to MongoDB lost.", e);
    }
}

From source file:com.spleefleague.core.chat.ChatChannel.java

public static void init() {
    MongoCursor<Document> dbc = SpleefLeague.getInstance().getPluginDB().getCollection("ChatChannels").find()
            .iterator();/* w ww  .java 2  s . co m*/
    while (dbc.hasNext()) {
        ChatChannel channel = EntityBuilder.load(dbc.next(), ChatChannel.class);
        ChatChannel staticChannel = getField(channel.getName());
        if (staticChannel != null) {
            staticChannel.name = channel.name;
            staticChannel.displayName = channel.displayName;
            staticChannel.defaultChannel = channel.defaultChannel;
            staticChannel.visible = channel.visible;
            staticChannel.minRank = channel.minRank;
            channel = staticChannel;
        }
        channels.put(channel.getName(), channel);
        ChatManager.registerChannel(channel, true);
    }
    SpleefLeague.getInstance().log("Loaded " + channels.size() + " chat channels!");
}

From source file:com.spleefleague.core.command.commands.infractions.java

private void infractions(CommandSender cs, String[] args) {
    if (args.length >= 1) {
        UUID id;/*from   w w  w  .  ja va 2  s  .  com*/
        if ((id = DatabaseConnection.getUUID(args[0])) == null) {
            error(cs, "The player \"" + args[0] + "\" has not been on the server yet!");
            return;
        }
        DatabaseConnection.find(SpleefLeague.getInstance().getPluginDB().getCollection("Infractions"),
                new Document("uuid", id.toString()), (result) -> {
                    result.sort(new Document("time", -1));
                    Set<Document> dbc = new HashSet<>();
                    for (Document d : result) {
                        dbc.add(d);
                    }
                    if (dbc.isEmpty()) {
                        error(cs, "The player \"" + args[0] + "\" doesn't have any infractions yet!");
                        return;
                    }
                    int maxPages = (dbc.size() - 1) / 10 + 1;
                    int page = 1;
                    if (args.length != 1) {
                        int argsPage;
                        try {
                            argsPage = Integer.parseInt(args[1]);
                        } catch (NumberFormatException e) {
                            error(cs, "\"" + args[1] + "\" is not a number!");
                            return;
                        }
                        if (argsPage > 0 && maxPages >= argsPage) {
                            page = argsPage;
                        } else {
                            error(cs,
                                    argsPage + " is not a valid page." + (maxPages == 1
                                            ? " There is only one page!"
                                            : " Please choose a number between 1 and " + maxPages + "!"));
                            return;
                        }
                    }
                    cs.sendMessage(ChatColor.DARK_GRAY + "[========== " + ChatColor.GRAY + args[0]
                            + "'s infractions (" + ChatColor.RED + page + ChatColor.GRAY + "/" + ChatColor.RED
                            + maxPages + ChatColor.GRAY + ") " + ChatColor.DARK_GRAY + "==========]");
                    result.skip((page - 1) * 10);
                    MongoCursor<Document> mc = result.iterator();
                    for (int i = 0; i < 10 && mc.hasNext(); i++) {
                        Infraction inf = EntityBuilder.load(mc.next(), Infraction.class);
                        cs.sendMessage(ChatColor.RED + String.valueOf(page * 10 - 9 + i) + ". "
                                + ChatColor.DARK_GRAY + "| " + inf.getType().getColor() + inf.getType()
                                + ChatColor.DARK_GRAY + " | " + ChatColor.RED
                                + (inf.getPunisher().equals(
                                        UUID.fromString("00000000-0000-0000-0000-000000000000")) ? "CONSOLE"
                                                : DatabaseConnection.getUsername(inf.getPunisher()))
                                + ChatColor.DARK_GRAY + " | " + ChatColor.GRAY + inf.getMessage()
                                + ChatColor.DARK_GRAY + " | " + ChatColor.GRAY
                                + TimeUtil.dateToString(new Date(inf.getTime()), false) + " ago");
                    }
                });
    } else {
        sendUsage(cs);
    }
}

From source file:com.spleefleague.core.io.Settings.java

public static void loadSettings() {
    MongoCursor<Document> dbc = SpleefLeague.getInstance().getPluginDB().getCollection("Settings").find()
            .iterator();/*w  w w.j  a v  a 2s  .co m*/
    while (dbc.hasNext()) {
        Document dbo = dbc.next();
        String key = (String) dbo.get("key");
        settings.put(key, dbo);
    }
}

From source file:com.spleefleague.core.player.Rank.java

public static void init() {
    Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
    Set<Team> teams = scoreboard.getTeams();
    teams.forEach(Team::unregister);/*  w w  w  .ja v a  2 s  .  c o  m*/
    MongoCursor<Document> dbc = SpleefLeague.getInstance().getPluginDB().getCollection("Ranks").find()
            .iterator();
    while (dbc.hasNext()) {
        Rank rank = EntityBuilder.load(dbc.next(), Rank.class);
        Rank staticRank = getField(rank.getName());
        if (staticRank != null) {
            staticRank.name = rank.name;
            staticRank.displayName = rank.displayName;
            staticRank.hasOp = rank.hasOp;
            staticRank.ladder = rank.ladder;
            staticRank.color = rank.color;
            staticRank.permissions = rank.permissions;
            staticRank.exclusivePermissions = rank.exclusivePermissions;
            rank = staticRank;
        }
        Team t = scoreboard.registerNewTeam(normalizeRankName(rank.getName()));
        t.setOption(Team.Option.COLLISION_RULE, Team.OptionStatus.NEVER);
        t.setDisplayName(t.getDisplayName());
        if (rank.getDisplayName().equalsIgnoreCase(Rank.DEFAULT.getDisplayName())) {
            t.setPrefix(rank.getColor().toString());
        } else {
            t.setPrefix(rank.getColor() + "[" + rank.getDisplayName() + "] ");
        }
        t.setSuffix(ChatColor.RESET.toString());
        rank.scoreboardTeam = t;
        ranks.put(rank.getName(), rank);
    }
    SpleefLeague.getInstance().log("Loaded " + ranks.size() + " ranks!");
}

From source file:com.spleefleague.superjump.game.Arena.java

public static void init() {
    arenas = new HashMap<>();
    MongoCursor<Document> dbc = SuperJump.getInstance().getPluginDB().getCollection("Arenas").find().iterator();
    while (dbc.hasNext()) {
        Document d = dbc.next();//from ww w. j av  a 2s  .c  om
        Arena arena;
        if (!d.containsKey("isRandom") || !d.getBoolean("isRandom")) {
            arena = EntityBuilder.load(d, Arena.class);
            if (arena.getSize() == 2) {
                arenas.put(arena.getName(), arena);
                SuperJump.getInstance().getBattleManager().registerArena(arena);
            }
        }
        //            else {
        //                arena = EntityBuilder.load(d, RandomArena.class);
        //            }
    }
    SuperJump.getInstance().log("Loaded " + arenas.size() + " arenas!");
}

From source file:com.spleefleague.superspleef.game.Arena.java

public static void init() {
    arenas = new HashMap<>();
    MongoCursor<Document> dbc = SuperSpleef.getInstance().getPluginDB().getCollection("Arenas")
            .find(new Document("spleefMode", "NORMAL")).iterator();
    while (dbc.hasNext()) {
        Arena arena = EntityBuilder.load(dbc.next(), Arena.class);
        arenas.put(arena.getName(), arena);
        SuperSpleef.getInstance().getBattleManager().registerArena(arena);
    }/*from ww  w.j ava  2  s  . co  m*/
    SuperSpleef.getInstance().log("Loaded " + arenas.size() + " arenas!");
}

From source file:com.spleefleague.superspleef.game.scoreboards.ScoreboardDefinition.java

public static void init() {
    definitions = new HashMap<>();
    MongoCursor<Document> dbc = SuperSpleef.getInstance().getPluginDB().getCollection("ScoreboardDefinitions")
            .find().iterator();//  w ww.  j  a va 2  s . c  o m
    while (dbc.hasNext()) {
        ScoreboardDefinition definition = EntityBuilder.load(dbc.next(), ScoreboardDefinition.class);
        definitions.put(definition.getObjectId(), definition);
    }
}

From source file:com.spleefleague.superspleef.game.TeamSpleefArena.java

public static void init() {
    arenas = new HashMap<>();
    MongoCursor<Document> dbc = SuperSpleef.getInstance().getPluginDB().getCollection("Arenas")
            .find(new Document("spleefMode", "TEAM")).iterator();
    while (dbc.hasNext()) {
        TeamSpleefArena arena = EntityBuilder.load(dbc.next(), TeamSpleefArena.class);
        arenas.put(arena.getName(), arena);
        SuperSpleef.getInstance().getBattleManager().registerArena(arena);
    }//from ww w .  j  a v a  2s  . co m
    SuperSpleef.getInstance().log("Loaded " + arenas.size() + " team spleef arenas!");
}

From source file:com.streamsets.pipeline.stage.processor.mongodb.MongoDBLookupLoader.java

License:Apache License

private Optional<List<Map<String, Field>>> lookupValuesForRecord(Document doc) throws StageException {
    List<Map<String, Field>> lookupItems = new ArrayList<>();
    if (LOG.isTraceEnabled()) {
        LOG.trace("Going to lookup with:" + doc.toJson());
    }//from   w  ww.ja v  a 2 s.c o m

    FindIterable<Document> it = mongoCollection.find(doc);
    if (it.first() != null) {
        MongoCursor<Document> ite = it.iterator();
        while (ite.hasNext()) {
            Document result = ite.next();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Found document:" + result.toJson());
            }
            try {
                Map<String, Field> fields = MongoDBUtil.createFieldFromDocument(result);
                lookupItems.add(fields);
            } catch (IOException io) {
                LOG.error(Errors.MONGODB_06.getMessage(), mongoCollection, result.toJson());
                throw new OnRecordErrorException(Errors.MONGODB_10, result.toJson());
            }
        }
    } else {
        // no lookup result.
        return Optional.empty();
    }
    return Optional.of(lookupItems);
}