private ClubRankingVO getClubRanking(Long seriesId) { final SeriesRankingVO seriesRanking = getSeriesRanking(seriesId); Map<Club, ClubResultVO> pointsPerClub = new HashMap<>(); for (SeriesRankingCategoryVO c : seriesRanking.getCategories()) { int points = c.getAthletes().size(); for (Athlete a : c.getAthletes()) { if (pointsPerClub.containsKey(a.getClub())) { ClubResultVO cr = pointsPerClub.get(a.getClub()); cr.setPoints(cr.getPoints() + points); } else { ClubResultVO cr = new ClubResultVO(); cr.setClub(a.getClub()); cr.setPoints(points); pointsPerClub.put(a.getClub(), cr); } points--; } } ClubRankingVO cr = new ClubRankingVO(); cr.setSeries(seriesRanking.getSeries()); cr.setClubs(new ArrayList(pointsPerClub.values())); Collections.sort(cr.getClubs()); return cr; }
public SeriesRankingVO getSeriesRanking(Long seriesId) { Series series = em.find(Series.class, seriesId); if (series == null) { return null; } TypedQuery<Athlete> q = em.createNamedQuery("Athlete.findBySeries", Athlete.class); q.setParameter("series_id", seriesId); List<Athlete> list = q.getResultList(); for (Athlete a : list) { TypedQuery<Result> tq = em.createNamedQuery("Result.findByAthleteAndSeries", Result.class); tq.setParameter("athleteId", a.getId()); tq.setParameter("seriesId", seriesId); List<Result> results = tq.getResultList(); a.setResults(results); } TypedQuery<Competition> qc = em.createNamedQuery("Competition.findAll", Competition.class); qc.setParameter("series_id", seriesId); List<Competition> cs = qc.getResultList(); series.setCompetitions(cs); SeriesRankingVO ranking = new SeriesRankingVO(); ranking.setSeries(series); Map<Category, List<Athlete>> map = new HashMap<>(); for (Athlete a : list) { List<Athlete> as = map.get(a.getCategory()); if (as == null) { as = new ArrayList<>(); } as.add(a); map.put(a.getCategory(), as); } for (Map.Entry<Category, List<Athlete>> entry : map.entrySet()) { SeriesRankingCategoryVO rc = new SeriesRankingCategoryVO(); Category c = entry.getKey(); rc.setCategory(c); rc.setAthletes(filterAndSort(series, entry.getValue())); ranking.getCategories().add(rc); } Collections.sort(ranking.getCategories()); return ranking; }