@SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public void saveClan(Clan clan) {
    Pool<SQLSession> pool = Static.currentLink().getSQLPool();

    SQLSession sql = null;
    try {
      sql = pool.acquire();

      Statement st = sql.createStatement();
      ResultSet rs =
          st.executeQuery("SELECT * FROM clans WHERE owner='" + clan.getOwner() + "' LIMIT 1");
      if (!rs.next()) {
        st.executeUpdate("INSERT INTO clans (owner) VALUES ('" + clan.getOwner() + "')");
      }
      rs.close();

      StringBuilder query = new StringBuilder();
      query.append("UPDATE clans SET");
      query.append(" name='").append(clan.getName()).append("'");
      query.append(", enabled='").append(clan.isEnabled() ? 1 : 0).append("'");
      query.append(", enterReq='").append(clan.getEnterRequirement().intValue()).append("'");
      query.append(", talkReq='").append(clan.getTalkRequirement().intValue()).append("'");
      query.append(", kickReq='").append(clan.getKickRequirement().intValue()).append("'");
      query
          .append(", lootShareReq='")
          .append(clan.getLootShareRequirement().intValue())
          .append("'");

      List[] ranks = new List[9];
      for (Map.Entry<String, Rank> ranked : clan.getRanks().entrySet()) {
        List rankList = ranks[ranked.getValue().intValue() + 1];
        if (rankList == null) {
          rankList = ranks[ranked.getValue().intValue() + 1] = new ArrayList();
        }

        rankList.add(ranked.getKey());
      }
      List curList;

      query
          .append(", recruits='")
          .append(
              (curList = ranks[Rank.RECRUIT.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");
      query
          .append(", corporals='")
          .append(
              (curList = ranks[Rank.CORPORAL.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");
      query
          .append(", sergeants='")
          .append(
              (curList = ranks[Rank.SERGEANT.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");
      query
          .append(", lieutenants='")
          .append(
              (curList = ranks[Rank.LIEUTENANT.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");
      query
          .append(", captains='")
          .append(
              (curList = ranks[Rank.CAPTAIN.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");
      query
          .append(", generals='")
          .append(
              (curList = ranks[Rank.GENERAL.intValue() + 1]) != null
                  ? ArrayUtilities.toString(curList.toArray(new String[0]))
                  : "")
          .append("'");

      query.append(" WHERE owner='").append(clan.getOwner()).append("'");
      st.executeUpdate(query.toString());
      st.close();
    } catch (SQLException e) {
      logger.error("Error saving clan [owner=" + clan + "]", e);
    } finally {
      if (sql != null) {
        pool.release(sql);
      }
    }
  }
  private boolean loadClan(String owner, Clan clan, Statement st) throws SQLException {
    ResultSet rs = st.executeQuery("SELECT * FROM clans WHERE owner='" + owner + "' LIMIT 1");
    if (!rs.next()) {
      rs.close();
      return false;
    }

    clan.setName(rs.getString("name"));

    clan.setEnabled(rs.getBoolean("enabled"));

    clan.setEnterRequirement(Rank.forValue(rs.getInt("enterReq")));
    clan.setTalkRequirement(Rank.forValue(rs.getInt("talkReq")));
    clan.setKickRequirement(Rank.forValue(rs.getInt("kickReq")));
    clan.setLootShareRequirement(Rank.forValue(rs.getInt("lootShareReq")));

    String[] rankedMembers;

    rankedMembers = toArray(rs.getString("recruits"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.RECRUIT);
    }
    rankedMembers = toArray(rs.getString("corporals"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.CORPORAL);
    }
    rankedMembers = toArray(rs.getString("sergeants"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.SERGEANT);
    }
    rankedMembers = toArray(rs.getString("lieutenants"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.LIEUTENANT);
    }
    rankedMembers = toArray(rs.getString("captains"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.CAPTAIN);
    }
    rankedMembers = toArray(rs.getString("generals"), new String[0]);
    for (String member : rankedMembers) {
      clan.getRanks().put(member, Rank.GENERAL);
    }

    rs.close();

    return true;
  }