コード例 #1
0
  @Override
  public void saveModel(Server model) {
    BasicDBObject dbServer = new BasicDBObject();
    dbServer.put("updated_at", model.getUpdated_at());
    if (model.getNode() != null) {
      dbServer.put("node_id", model.getNode().getId().toString());
    }
    dbServer.put("port", model.getPort());
    dbServer.put("container", model.getContainerId());
    dbServer.put("players", new BasicDBObject(model.getPlayers()));

    BasicDBList metaDataList = new BasicDBList();
    for (Map.Entry<String, ServerMetaData> metaDataEntry : model.getMetaData().entrySet()) {
      DBObject dbMetaData = new BasicDBObject();
      dbMetaData.put("_id", metaDataEntry.getValue().getId());
      dbMetaData.put("created_at", metaDataEntry.getValue().getCreated_at());
      dbMetaData.put("updated_at", metaDataEntry.getValue().getUpdated_at());
      dbMetaData.put("key", metaDataEntry.getValue().getKey());
      dbMetaData.put("value", metaDataEntry.getValue().getValue());
      metaDataList.add(dbMetaData);
    }

    dbServer.put("metaData", metaDataList);

    dbServer.put("number", model.getNumber());
    getDatabase()
        .updateDocument(
            "servers",
            new BasicDBObject("_id", model.getId()),
            new BasicDBObject("$set", dbServer));
  }
コード例 #2
0
  @Override
  public Server getModel(ObjectId id) {
    DBObject dbServer = getDatabase().findOne("servers", new BasicDBObject("_id", id));

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

    Server server = new Server((ObjectId) dbServer.get("_id"), (Date) dbServer.get("created_at"));
    server.setUpdated_at((Date) dbServer.get("updated_at"));
    server.setNetwork(
        getDatabase()
            .getNetworkRepository()
            .getModel(new ObjectId((String) dbServer.get("network_id"))));
    String node = (String) dbServer.get("node_id");
    if (node != null) {
      server.setNode(getDatabase().getNodeRepository().getModel(new ObjectId(node)));
    }
    server.setServerType(
        getDatabase()
            .getServerTypeRepository()
            .getModel(new ObjectId((String) dbServer.get("server_type_id"))));
    server.setContainerId((String) dbServer.get("container"));
    server.setPort((int) dbServer.get("port"));

    BasicDBObject dbPlayers = (BasicDBObject) dbServer.get("players");
    for (String name : dbPlayers.keySet()) {
      server.getPlayers().put(name, (UUID) dbPlayers.get(name));
    }

    if (dbServer.containsField("metaData")) {
      BasicDBList metaDataList = (BasicDBList) dbServer.get("metaData");
      for (Object metaDataObj : metaDataList) {
        DBObject dbMetaData = (DBObject) metaDataObj;

        ServerMetaData metaData =
            new ServerMetaData(
                (ObjectId) dbMetaData.get("_id"), (Date) dbMetaData.get("created_at"));
        metaData.setUpdated_at((Date) dbMetaData.get("updated_at"));
        metaData.setKey((String) dbMetaData.get("key"));
        metaData.setValue((String) dbMetaData.get("value"));

        server.getMetaData().put(metaData.getKey(), metaData);
      }
    }

    server.setNumber((int) dbServer.get("number"));
    return server;
  }