/**
  * Fill a group and save the result.
  *
  * @param aGroup a group to fill
  * @return the saved group with real participants
  */
 public GroupEntity fillGroupAndSave(final GroupEntity aGroup) {
   final GroupEntity result = fillGroup(aGroup);
   if (result != null && result.getParticipants() != null && !result.getParticipants().isEmpty()) {
     for (final EffectiveParticipantEntity aParticipant : result.getParticipants()) {
       effectiveParticipantDao.updateEffectiveParticipant(aParticipant, aParticipant.getId());
     }
   }
   return result;
 }
 /**
  * Fill a group and returns the group filled but not saved.
  *
  * @param aGroup a group to fill
  * @return the group with real participants
  */
 public GroupEntity fillGroup(final GroupEntity aGroup) {
   if (aGroup != null) {
     // First get the tournament
     final TournamentEntity theTournament = aGroup.getStage().getTournament();
     final boolean isTeam = theTournament.getIsTeamTournament();
     // Get players or teams list
     List<TeamEntity> teamsTournament = null;
     List<PlayerEntity> playersTournament = null;
     if (isTeam) {
       teamsTournament =
           teamService.getSortedListTeam(theTournament.getTeams(), SortType.RANKING_POINTS);
     } else {
       playersTournament =
           playerService.getSortedListPlayer(theTournament.getPlayers(), SortType.RANKING_POINTS);
     }
     // Loop over the participant of the group
     for (final EffectiveParticipantEntity aParticipant : aGroup.getParticipants()) {
       final GroupRankReference theReference = aParticipant.getInitialReference();
       final GroupEntity groupReference = theReference.getGroup();
       final StageEntity stageReference = theReference.getStage();
       final Integer rank = theReference.getRank();
       if (groupReference == null && stageReference == null) {
         // Get the real participant from tournament
         ParticipantEntity realParticipant = null;
         if (isTeam) {
           realParticipant = teamsTournament.get(rank - 1);
         } else {
           realParticipant = playersTournament.get(rank - 1);
         }
         aParticipant.setParticipant(realParticipant);
       } else if (groupReference == null && stageReference != null) {
         // Get from stage
         if (stageReference.getGroups() != null) {
           for (final GroupEntity groupReferenceStage : stageReference.getGroups()) {
             final List<EffectiveParticipantEntity> participants =
                 groupReferenceStage.getParticipants();
             for (final EffectiveParticipantEntity previousParticipant : participants) {
               final Integer finalRankStage =
                   previousParticipant.getFinalRankStage() != null
                       ? previousParticipant.getFinalRankStage()
                       : 0;
               if (finalRankStage == rank) {
                 aParticipant.setParticipant(previousParticipant.getParticipant());
                 break;
               }
             }
           }
         }
       } else {
         // Get from group
         final List<EffectiveParticipantEntity> participants = groupReference.getParticipants();
         for (final EffectiveParticipantEntity previousParticipant : participants) {
           final Integer finalRank =
               previousParticipant.getFinalRank() != null ? previousParticipant.getFinalRank() : 0;
           if (finalRank == rank) {
             aParticipant.setParticipant(previousParticipant.getParticipant());
             break;
           }
         }
       }
     }
   } else {
     final GroupEntity emptyGroup = new GroupEntity();
     emptyGroup.setParticipants(new ArrayList<EffectiveParticipantEntity>());
     return emptyGroup;
   }
   return aGroup;
 }