@Override
  public void insertModel(Server model) {
    BasicDBObject dbServer = new BasicDBObject();
    dbServer.put("_id", model.getId());
    dbServer.put("created_at", model.getCreated_at());
    dbServer.put("updated_at", model.getUpdated_at());
    dbServer.put("network_id", model.getNetwork().getId().toString());
    dbServer.put("node_id", null);
    dbServer.put("server_type_id", model.getServerType().getId().toString());
    dbServer.put("port", 0);
    dbServer.put("container", "NULL");
    dbServer.put("players", new BasicDBObject());

    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", 0);

    getDatabase().insert("servers", dbServer);
  }
  @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));
  }
  @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;
  }