Example usage for com.mongodb BasicDBList BasicDBList

List of usage examples for com.mongodb BasicDBList BasicDBList

Introduction

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

Prototype

BasicDBList

Source Link

Usage

From source file:net.tbnr.gearz.arena.ArenaManager.java

License:Open Source License

/**
 * Converts an Arena object into a DB Object that can be reversed using the other method (which is not static)
 *
 * @param arena The arena to convert into a DBObject
 * @return The DBObject version of this Arena.
 *///  w w w  . ja  va 2 s .c o  m
public static DBObject objectFromArena(Arena arena) {
    BasicDBObjectBuilder objectBuilder = new BasicDBObjectBuilder(); //Start building the database object for this arena
    for (Field field : arena.getClass().getFields()) { //Get all the fields ...
        if (!field.isAnnotationPresent(ArenaField.class)) {
            continue; //... that we can access, and are annotated by ArenaField ...
        }
        ArenaField annotation = field.getAnnotation(ArenaField.class); //Get the Annotation from the field as an object
        ArenaIterator iterator; //Setup an object to put the iterator in
        try {
            iterator = (ArenaIterator) field.get(arena); //Try to getSetting the arena iterator
        } catch (IllegalAccessException e) {
            continue; //Didn't work :o
        }
        BasicDBList list = new BasicDBList(); //Pour our list into the DB List object
        while (iterator.hasNext()) {
            Object next = iterator.next();
            ArenaFieldSerializer.SerializationDelegate<?> serializerFor = ArenaFieldSerializer
                    .getSerializerFor(next.getClass());
            if (serializerFor == null)
                continue;
            list.add(serializerFor.getObjectFor(next)); //Add whatever "next" is now. Depending on code above, it could be a DBObject, or whatever the iterator has in store.
        }
        objectBuilder.append(annotation.key(), list); //Put that in the database
    }
    objectBuilder.append("name", arena.getName()); //Meta for a bit.
    objectBuilder.append("description", arena.getDescription());
    objectBuilder.append("worldId", arena.getWorldId());
    objectBuilder.append("last-updated", Calendar.getInstance().getTimeInMillis());
    objectBuilder.append("author", arena.getAuthors());
    if (arena.getId() != null) {
        objectBuilder.append("_id", new ObjectId(arena.getId())); //Good for replacing/updating
    }
    return objectBuilder.get(); //Finish the object off! :D
}

From source file:net.tbnr.gearz.game.GearzMetrics.java

License:Open Source License

public void done(Map<String, Object> data) {
    BasicDBObject object = new BasicDBObject();
    object.put("game", game.getGameMeta().key());
    object.put("game_start", gameStart);
    object.put("game_end", gameEnd);
    object.put("game_length", gameEnd - gameStart);
    object.put("arena", game.getArena().getId());
    BasicDBList players = new BasicDBList();
    for (GearzPlayer player : this.players) {
        players.add(player.getTPlayer().getPlayerDocument().get("_id"));
    }//  ww w .ja v  a2s.  c  o  m
    object.put("players", players);
    if (data != null) {
        for (Map.Entry<String, Object> stringObjectEntry : data.entrySet()) {
            object.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
        }
    }
    this.metricsCollection.save(object);
}

From source file:net.tbnr.gearz.GearzBungee.java

License:Open Source License

public Object[] getMotds() {
    Object motd = getBungeeConfig().get("motds");
    if (motd == null || !(motd instanceof BasicDBList)) {
        BasicDBList dbList = new BasicDBList();
        dbList.add("Another Gearz Server");
        getBungeeConfig().put("motds", dbList);
        return dbList.toArray();
    }/*from ww  w.j  av  a  2s . co  m*/
    return ((BasicDBList) motd).toArray();
}

From source file:net.tbnr.gearz.GearzBungee.java

License:Open Source License

public Object[] getAnnouncements() {
    Object announcements = getBungeeConfig().get("announcements");
    if (announcements == null || !(announcements instanceof BasicDBList)) {
        BasicDBList dbList = new BasicDBList();
        dbList.add("Another Gearz Server - Test Announcement");
        getBungeeConfig().put("announcements", dbList);
        return dbList.toArray();
    }/*from  w w w .  j  a va 2  s. c o  m*/
    return ((BasicDBList) announcements).toArray();
}

From source file:net.tbnr.gearz.modules.AnnouncerModule.java

License:Open Source License

@TCommand(aliases = { "announcer" }, usage = "/announcer", senders = { TCommandSender.Player,
        TCommandSender.Console }, permission = "gearz.announcer", name = "announcer")
@SuppressWarnings("unused")
public TCommandStatus announcer(CommandSender sender, TCommandSender type, TCommand meta, String[] args) {
    if (args.length == 0) {
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-help"));
        return TCommandStatus.SUCCESSFUL;
    }//  w w  w. j a va 2 s. c o m
    Object[] list = GearzBungee.getInstance().getAnnouncements();
    if (args[0].equalsIgnoreCase("list")) {
        for (int x = 0; x < announcements.size(); x++) {
            sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-list", false, false,
                    new String[] { "<num>", x + "" },
                    new String[] { "<announcement>", announcements.get(x).getColoredText() }));
        }
        return TCommandStatus.SUCCESSFUL;
    }
    List<String> strings = new ArrayList<>();
    for (Object o : list) {
        if (o instanceof String)
            strings.add((String) o);
    }
    if (args[0].equalsIgnoreCase("add")) {
        if (args.length < 2) {
            sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-badargs"));
            return TCommandStatus.INVALID_ARGS;
        }
        String s = GearzBungee.getInstance().compile(args, 1, args.length);
        strings.add(s);
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-add"));
    } else if (args[0].equalsIgnoreCase("remove")) {
        Integer toRemove = Integer.parseInt(args[1]);
        if (toRemove < 1 || toRemove > list.length) {
            sender.sendMessage(GearzBungee.getInstance().getFormat("index-out-of-range", false));
            return TCommandStatus.SUCCESSFUL;
        }
        strings.remove(toRemove - 1);
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-remove", false, false,
                new String[] { "<num>", toRemove + "" }));
    } else if (args[0].equalsIgnoreCase("interval")) {
        if (args[1] != null) {
            int num;
            try {
                num = Integer.parseInt(args[1]);
            } catch (NumberFormatException e) {
                sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-notanum"));
                return TCommandStatus.INVALID_ARGS;
            }
            GearzBungee.getInstance().setInterval(num);
            this.interval_seconds = num;
            sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-interval-set", false, false,
                    new String[] { "<num>", num + "" }));
        }
    } else if (args[0].equalsIgnoreCase("help")) {
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-help"));
        return TCommandStatus.SUCCESSFUL;
    } else if (args[0].equalsIgnoreCase("restart") || args[0].equalsIgnoreCase("start")) {
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-restart"));
        reschedule();
        return TCommandStatus.SUCCESSFUL;
    } else if (args[0].equalsIgnoreCase("stop")) {
        sender.sendMessage(GearzBungee.getInstance().getFormat("announcer-stop"));
        cancel();
        return TCommandStatus.SUCCESSFUL;
    }
    BasicDBList basicDBList = new BasicDBList();
    basicDBList.addAll(strings);
    List<Announcement> finalAnnouncements = new ArrayList<>();
    for (String string : strings) {
        finalAnnouncements.add(new Announcement(string));
    }
    this.announcements = finalAnnouncements;
    GearzBungee.getInstance().setAnnouncements(basicDBList);
    return TCommandStatus.SUCCESSFUL;
}

From source file:net.tbnr.gearz.modules.MotdHandler.java

License:Open Source License

@TCommand(name = "setmotd", permission = "gearz.setmotd", usage = "/setmotd [list|remove|add] [message (required if applicable)]", senders = {
        TCommandSender.Player, TCommandSender.Console }, aliases = {})
@SuppressWarnings("unused")
public TCommandStatus changeMotd(CommandSender sender, TCommandSender type, TCommand meta, String[] args) {
    if (args.length < 1)
        return TCommandStatus.HELP;
    String command = args[0];//www  .  j  ava  2s.  c  o m
    Object[] motds1 = GearzBungee.getInstance().getMotds();
    if (command.equalsIgnoreCase("list")) {
        sender.sendMessage(GearzBungee.getInstance().getFormat("header-motdlist", false));
        int index = 0;
        for (Object o : motds1) {
            index++;
            if (!(o instanceof String))
                continue;
            String s = (String) o;
            sender.sendMessage(GearzBungee.getInstance().getFormat("list-motdlist", false, true,
                    new String[] { "<index>", String.valueOf(index) }, new String[] { "<motd>", s }));
        }
        return TCommandStatus.SUCCESSFUL;
    }
    if (args.length < 2)
        return TCommandStatus.FEW_ARGS;
    List<String> strings = new ArrayList<>();
    for (Object o : motds1) {
        if (o instanceof String)
            strings.add((String) o);
    }
    if (command.equalsIgnoreCase("remove")) {
        Integer toRemove = Integer.parseInt(args[1]);
        if (toRemove < 1 || toRemove > motds1.length) {
            sender.sendMessage(GearzBungee.getInstance().getFormat("index-out-of-range", false));
            return TCommandStatus.SUCCESSFUL;
        }
        String s = strings.get(toRemove - 1);
        strings.remove(toRemove - 1);
        sender.sendMessage(
                GearzBungee.getInstance().getFormat("removed-motd", false, true, new String[] { "<motd>", s }));
    } else if (command.equalsIgnoreCase("add")) {
        StringBuilder build = new StringBuilder();
        int index = 1;
        while (index < args.length) {
            build.append(args[index]).append(" ");
            index++;
        }
        build.substring(0, build.length() - 1);
        String s = build.toString();
        strings.add(s);
        sender.sendMessage(
                GearzBungee.getInstance().getFormat("added-motd", false, true, new String[] { "<motd>", s }));
    } else {
        return TCommandStatus.INVALID_ARGS;
    }
    BasicDBList basicDBList = new BasicDBList();
    basicDBList.addAll(strings);
    GearzBungee.getInstance().setMotds(basicDBList);
    return TCommandStatus.SUCCESSFUL;
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Format {@link java.lang.Exception} for the database
 *
 * @param ex The {@link java.lang.Exception} to format.
 * @return List of database.//from   w ww.j  a v  a  2s . co m
 */
private static BasicDBList formatException(Exception ex) {
    BasicDBList stringList = new BasicDBList();
    for (StackTraceElement stackTraceElement : ex.getStackTrace()) {
        stringList.add(String.format("%s(%s:%d)", stackTraceElement.getMethodName(),
                stackTraceElement.getFileName(), stackTraceElement.getLineNumber()));
    }
    return stringList;
}

From source file:net.tbnr.util.player.TPlayer.java

License:Open Source License

private void addToList(String fieldName, String toAdd) {
    BasicDBList field = (BasicDBList) this.playerDocument.get(fieldName);
    if (field == null) {
        field = new BasicDBList();
    }/*from  ww w . j  a v a 2  s.  c o  m*/
    if (!field.contains(toAdd)) {
        field.add(toAdd);
    }
    this.playerDocument.put(fieldName, field);
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoQueries.java

License:Open Source License

private DBObject or(DBObject... subQueries) {
    BasicDBList or = new BasicDBList();
    for (DBObject dbObject : subQueries) {
        or.add(dbObject);//  w w  w.  j  a v a2 s  .  c om
    }

    return new BasicDBObject("$or", or);
}

From source file:nl.vu.psy.relic.persistence.mongo.MongoMapper.java

License:Open Source License

public static DBObject relicToDBObject(Relic r) {
    DBObject result = new BasicDBObject();
    DBObject relic = new BasicDBObject();
    relic.put("identifier", r.getIdentifier());
    relic.put("filename", r.getFileName());
    relic.put("description", r.getDescription());
    BasicDBList properties = new BasicDBList();
    for (Object s : r.getProperties().keySet()) {
        BasicDBObject prop = new BasicDBObject();
        String key = (String) s;
        prop.put(key, r.getProperty(key));
        properties.add(prop);//from  w  ww .ja  va2 s  .  co m
    }
    relic.put("properties", properties);
    result.put("relic", relic);
    return result;
}