public void undraftAllLeaguePlayersInTeam(Long teamid, String uname) {

    List<LeaguePlayer> leagueplayers = this.getLeaguePlayersByTeam(teamid, uname);

    Ref<LeagueTeam> r = null;

    for (LeaguePlayer lp : leagueplayers) {
      lp.setLeague_team(r);
    }

    this.save(leagueplayers, uname);
  }
  /**
   * Description: Undrafts league player, only requires league_id and player_projected_id from
   * container. Unknown player requires unknown_player_name.
   *
   * @param container
   * @param uname
   */
  public void undraftLeaguePlayer(LeaguePlayerInputDraftContainer container, String uname) {

    Key<League> leaguekey = Key.create(League.class, container.getLeague_id());
    List<LeaguePlayer> leagueplayers = new ArrayList<LeaguePlayer>();

    if (container.isUnknownplayer()) {
      // Key<LeagueTeam> teamkey = Key.create(LeagueTeam.class, container.getLeague_team_id());

      // Check to see if LeaguePlayer already exists
      leagueplayers =
          ofy()
              .load()
              .type(LeaguePlayer.class)
              .filter("league", leaguekey)
              .filter("unknown_player_name", container.getUnknown_player_name())
              .list();

      if (leagueplayers.size() > 0) {
        System.out.println("undraftLeaguePlayer: Found LeaguePlayer, deleting...");
        this.delete(leagueplayers.get(0).getId());
      } else System.out.println("undraftLeaguePlayer: Could not find league player to delete.");

    } else {

      Key<PlayerProjected> playerprojectedkey =
          Key.create(PlayerProjected.class, container.getPlayer_projected_id());

      // Check to see if LeaguePlayer already exists
      leagueplayers =
          ofy()
              .load()
              .type(LeaguePlayer.class)
              .filter("league", leaguekey)
              .filter("player_projected", playerprojectedkey)
              .list();

      LeaguePlayer lp = leagueplayers.get(0);

      Ref<LeagueTeam> r = null;

      lp.setLeague_team(r);
      lp.setTeam_roster_position(null);
      lp.setTeam_player_salary(0);

      this.save(lp, uname);
    }
  }
  /**
   * Description: Updates note for league player, only requires league_id, player_projected_id and
   * team_player_note from container.
   *
   * @param container
   * @param uname
   * @return Long, id of saved LeaguePlayer
   */
  public Long updateLeaguePlayerNote(LeaguePlayerInputNoteContainer container, String uname) {

    Key<League> leaguekey = Key.create(League.class, container.getLeague_id());
    Key<PlayerProjected> playerprojectedkey =
        Key.create(PlayerProjected.class, container.getPlayer_projected_id());

    // Check to see if LeaguePlayer already exists
    List<LeaguePlayer> leagueplayers =
        ofy()
            .load()
            .type(LeaguePlayer.class)
            .filter("league", leaguekey)
            .filter("player_projected", playerprojectedkey)
            .list();

    LeaguePlayer lp = new LeaguePlayer();

    // If exists, update existing LeaguePlayer note
    // Otherwise, update new LeaguePlayer with note
    if (leagueplayers.size() > 0) {
      lp = leagueplayers.get(0);
    } else {
      lp.setLeagueRef(Ref.create(leaguekey));
      lp.setPlayer_projected(Ref.create(playerprojectedkey));
    }

    lp.setTeam_player_note(container.getTeam_player_note());

    System.out.println("Saving player note for: " + lp.getPlayer_projected().getFull_name());
    System.out.println("Player note: " + lp.getTeam_player_note());

    return this.save(lp, uname);
  }
  /**
   * Description: Updates custom position eligibility for league player, only requires league_id,
   * player_projected_id and custom_position_eligibility from container.
   *
   * @param container
   * @param uname
   * @return Long, id of saved LeaguePlayer
   */
  public Long updateLeaguePlayerCustomPosition(
      LeaguePlayerInputCustPosContainer container, String uname) {

    Key<League> leaguekey = Key.create(League.class, container.getLeague_id());
    Key<PlayerProjected> playerprojectedkey =
        Key.create(PlayerProjected.class, container.getPlayer_projected_id());

    // Check to see if LeaguePlayer already exists
    List<LeaguePlayer> leagueplayers =
        ofy()
            .load()
            .type(LeaguePlayer.class)
            .filter("league", leaguekey)
            .filter("player_projected", playerprojectedkey)
            .list();

    LeaguePlayer lp = new LeaguePlayer();

    // If exists, update existing LeaguePlayer custom position
    // Otherwise, update new LeaguePlayer with custom position
    if (leagueplayers.size() > 0) {
      lp = leagueplayers.get(0);
    } else {
      lp.setLeagueRef(Ref.create(leaguekey));
      lp.setPlayer_projected(Ref.create(playerprojectedkey));
    }

    lp.setCustom_position(container.getCustom_position_eligibility());
    lp.setCustom_position_flag(true);

    System.out.println(
        "Saving player custom position for: " + lp.getPlayer_projected().getFull_name());
    System.out.println("Custom position: " + lp.getCustom_position());

    return this.save(lp, uname);
  }
  /**
   * Description: Removes custom position eligibility for league player, only requires league_id,
   * player_projected_id and custom_position_eligibility from container. Returns -1 if player not
   * found.
   *
   * @param container
   * @param uname
   * @return Long, id of saved LeaguePlayer
   */
  public Long removeLeaguePlayerCustomPosition(
      LeaguePlayerInputCustPosContainer container, String uname) {

    Key<League> leaguekey = Key.create(League.class, container.getLeague_id());
    Key<PlayerProjected> playerprojectedkey =
        Key.create(PlayerProjected.class, container.getPlayer_projected_id());

    // Check to see if LeaguePlayer already exists
    List<LeaguePlayer> leagueplayers =
        ofy()
            .load()
            .type(LeaguePlayer.class)
            .filter("league", leaguekey)
            .filter("player_projected", playerprojectedkey)
            .list();

    LeaguePlayer lp = new LeaguePlayer();

    // If exists, remove existing LeaguePlayer custom position
    // Otherwise, return -1
    if (leagueplayers.size() > 0) {
      lp = leagueplayers.get(0);
      lp.setCustom_position("");
      lp.setCustom_position_flag(false);

      System.out.println(
          "Removing player custom position for: " + lp.getPlayer_projected().getFull_name());
      System.out.println("Cusotm position flag: " + lp.isCustom_position_flag());
    } else {
      System.out.println(
          "Could NOT remove custom position for: " + lp.getPlayer_projected().getFull_name());
      System.out.println("Could not find player.");
      return Long.valueOf(-1);
    }

    return this.save(lp, uname);
  }
  public long draftLeaguePlayer(LeaguePlayerInputDraftContainer container, String uname) {

    System.out.println("draftLeaguePlayer: BEGIN");

    Key<LeagueTeam> teamkey = Key.create(LeagueTeam.class, container.getLeague_team_id());
    Key<League> leaguekey = Key.create(League.class, container.getLeague_id());

    List<LeaguePlayer> leagueplayers = new ArrayList<LeaguePlayer>();

    LeaguePlayer lp = new LeaguePlayer();

    System.out.println(
        "draftLeaguePlayer: unknown_player_name: " + container.getUnknown_player_name());
    System.out.println("draftLeaguePlayer: unknownplayer: " + container.isUnknownplayer());

    if (container.isUnknownplayer()) {

      // Check to see if LeaguePlayer already exists
      leagueplayers =
          ofy()
              .load()
              .type(LeaguePlayer.class)
              .filter("league", leaguekey)
              .filter("unknown_player_name", container.getUnknown_player_name())
              .list();

      // If exists, update existing LeaguePlayer
      // Otherwise, update new LeaguePlayer with draft information
      if (leagueplayers.size() > 0) {
        System.out.println(
            "draftLeaguePlayer: found existing unknown LeaguePlayer: "
                + container.getUnknown_player_name());
        lp = leagueplayers.get(0);
      } else {
        System.out.println(
            "draftLeaguePlayer: Did NOT find existing unknown LeaguePlayer: "
                + container.getUnknown_player_name());
        lp.setLeagueRef(Ref.create(leaguekey));
      }

      lp.setUnknownplayer(true);
      lp.setUnknown_player_name(container.getUnknown_player_name());
      lp.setUnknown_player_pitcher_hitter(container.getUnknown_player_pitcher_hitter());
      System.out.println(
          "draftLeaguePlayer: Set unknown player name: " + lp.getUnknown_player_name());

      lp.setLeague_team(Ref.create(teamkey));
      lp.setTeam_player_salary(container.getTeam_player_salary());
      lp.setTeam_roster_position(container.getTeam_roster_position());

      System.out.println("draftLeaguePlayer: Right before save 1: " + lp.getUnknown_player_name());

      return this.save(lp, uname);

    } else {
      System.out.println("draftLeaguePlayer: IN ELSE STATEMENT");
      Key<PlayerProjected> playerprojectedkey =
          Key.create(PlayerProjected.class, container.getPlayer_projected_id());

      // Check to see if LeaguePlayer already exists
      leagueplayers =
          ofy()
              .load()
              .type(LeaguePlayer.class)
              .filter("league", leaguekey)
              .filter("player_projected", playerprojectedkey)
              .list();

      // If exists, update existing LeaguePlayer
      // Otherwise, update new LeaguePlayer with draft information
      if (leagueplayers.size() > 0) {
        lp = leagueplayers.get(0);
      } else {
        lp.setLeagueRef(Ref.create(leaguekey));
        lp.setPlayer_projected(Ref.create(playerprojectedkey));
      }
    }

    lp.setLeague_team(Ref.create(teamkey));
    lp.setTeam_player_salary(container.getTeam_player_salary());
    lp.setTeam_roster_position(container.getTeam_roster_position());

    System.out.println("draftLeaguePlayer: Right before save 2: " + lp.getUnknown_player_name());

    return this.save(lp, uname);
  }