public void updatePrInList(Leader oldLeader, Leader newLeader) {
    boolean inList = false;
    PRInfo newPr = newLeader.getPullRequests().get(0);
    List<PRInfo> oldPRList = oldLeader.getPullRequests();
    Scoring scoring = oldLeader.getScoring();

    for (int i = 0; i < oldPRList.size(); i++) {
      PRInfo oldPr = oldPRList.get(i);
      if (oldPr.getId() == newPr.getId()) {
        if (scoring.getPositionToScore(newPr.getRole().getPosition())
            >= scoring.getPositionToScore(oldPr.getRole().getPosition())) {
          oldPr.setDate(newPr.getDate());
          oldPr.setRole(newPr.getRole());
        } else if (scoring.getPositionToScore(newPr.getRole().getPosition())
            < scoring.getPositionToScore(oldPr.getRole().getPosition())) {
          oldPr.setDate(newPr.getDate());
        } else if (newPr.getRole() == Role.NULL) {
          oldPRList.remove(i);
        }
        inList = true;
        break;
      }
    }

    if ((!inList || oldPRList.isEmpty()) && (newPr.getRole() != Role.NULL)) {
      oldPRList.add(newPr);
    }
  }
 public void updateLeaderInList(List<Leader> Leaders, Leader newLeader) {
   boolean inList = false;
   for (Leader oldLeader : Leaders) {
     if (oldLeader.getSlug().equals(newLeader.getSlug())) {
       updatePrInList(oldLeader, newLeader);
       inList = true;
       break;
     }
   }
   if ((!inList || Leaders.isEmpty())
       && newLeader.getPullRequests().get(0).getRole() != Role.NULL) {
     Leaders.add(newLeader);
   }
 }
  public JSONObject convertLeadersToJson(List<Leader> leaders) throws JSONException {
    JSONArray userList = new JSONArray();
    JSONObject finalObj = new JSONObject();

    for (Leader leader : leaders) {
      List<PRInfo> prs = leader.getPullRequests();
      JSONObject userInfo = new JSONObject();
      userInfo.put("slug", leader.getSlug());
      userInfo.put("displayName", leader.getDisplayName());
      JSONArray prArray = new JSONArray();
      for (PRInfo pr : prs) {
        JSONObject prInfo = new JSONObject();
        prInfo.put("id", pr.getId());
        prInfo.put("date", pr.getDate());
        prInfo.put("role", pr.getRole().toString());
        prArray.put(prInfo);
      }
      userInfo.put("pullRequests", prArray);
      userList.put(userInfo);
    }
    finalObj.put("users", userList);
    return finalObj;
  }
  public Map<String, Object> getLeadersFromDatabase(
      Scoring scoring, Repository repository, String history) throws JSONException {
    // Get string from database for repo
    String repoLeaderboardString = "";
    Object repoLeaderboard =
        pluginSettings.get(
            repository.getProject().getKey() + "." + repository.getSlug() + ".prInfo");
    if (repoLeaderboard != null) {
      repoLeaderboardString = repoLeaderboard.toString();
    }

    // convert json to leaders
    List<Leader> leaders = convertJsonToLeaders(scoring, repoLeaderboardString);
    if (history != null) {
      for (Leader leader : leaders) {
        leader.setScore(history);
      }
      sortLeaders(leaders);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("leaders", leaders);
    return map;
  }