Example usage for com.mongodb.client MongoCursor next

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

Introduction

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

Prototype

@Override
    TResult next();

Source Link

Usage

From source file:com.sitewhere.event.persistence.mongodb.MongoDeviceEventManagement.java

License:Open Source License

@Override
public ISearchResults<IDeviceEvent> listDeviceEvents(IDeviceAssignment assignment,
        IDateRangeSearchCriteria criteria) throws SiteWhereException {
    MongoCollection<Document> events = getMongoClient().getEventsCollection();
    Document query = new Document(MongoDeviceEvent.PROP_DEVICE_ASSIGNMENT_ID, assignment.getId());
    MongoPersistence.addDateSearchCriteria(query, MongoDeviceEvent.PROP_EVENT_DATE, criteria);
    Document sort = new Document(MongoDeviceEvent.PROP_EVENT_DATE, -1)
            .append(MongoDeviceEvent.PROP_RECEIVED_DATE, -1);

    int offset = Math.max(0, criteria.getPageNumber() - 1) * criteria.getPageSize();
    FindIterable<Document> found = events.find(query).skip(offset).limit(criteria.getPageSize()).sort(sort);
    MongoCursor<Document> cursor = found.iterator();

    List<IDeviceEvent> matches = new ArrayList<IDeviceEvent>();
    SearchResults<IDeviceEvent> results = new SearchResults<IDeviceEvent>(matches);
    try {/*from  ww w . j a va 2 s  . c  om*/
        results.setNumResults(events.count(query));
        while (cursor.hasNext()) {
            Document match = cursor.next();
            matches.add(MongoDeviceEventManagementPersistence.unmarshalEvent(match));
        }
    } finally {
        cursor.close();
    }
    return results;
}

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

License:Open Source License

@Override
public List<IUser> listUsers(IUserSearchCriteria criteria) throws SiteWhereException {
    try {// w w  w . j  a v  a2  s  . c o  m
        MongoCollection<Document> users = getMongoClient().getUsersCollection();
        Document dbCriteria = new Document();
        if (!criteria.isIncludeDeleted()) {
            MongoSiteWhereEntity.setDeleted(dbCriteria, false);
        }
        FindIterable<Document> found = users.find(dbCriteria)
                .sort(new BasicDBObject(MongoUser.PROP_USERNAME, 1));
        MongoCursor<Document> cursor = found.iterator();

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

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  ww.  j  a  va  2 s. co  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. j  av  a2 s  . com
    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  . j ava2  s.c om*/
        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  ww .  j  a  va 2  s  .  c  o  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);//from ww  w . j  a  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();
        Arena arena;//from  w w  w .  ja  va  2s  .c  om
        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  w w  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();/*  ww w  . ja va  2s .  c  o  m*/
    while (dbc.hasNext()) {
        ScoreboardDefinition definition = EntityBuilder.load(dbc.next(), ScoreboardDefinition.class);
        definitions.put(definition.getObjectId(), definition);
    }
}