| 163 | aurelien | 1 | package org.tela_botanica.del.client.services;
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | import java.util.ArrayList;
 | 
        
           | 1124 | gduche | 4 | import java.util.Iterator;
 | 
        
           | 163 | aurelien | 5 | import java.util.List;
 | 
        
           |  |  | 6 | import java.util.Map;
 | 
        
           | 227 | aurelien | 7 | import java.util.Set;
 | 
        
           | 163 | aurelien | 8 |   | 
        
           |  |  | 9 | import org.tela_botanica.del.client.modeles.MoyenneVote;
 | 
        
           |  |  | 10 | import org.tela_botanica.del.client.modeles.PropositionDetermination;
 | 
        
           |  |  | 11 | import org.tela_botanica.del.client.modeles.VoteDetermination;
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 |   | 
        
           | 1833 | aurelien | 14 |   | 
        
           | 163 | aurelien | 15 | public class CalculVoteDeterminationService {
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | 	public static List<MoyenneVote> calculerVoteDeterminationPlusPopulaire(List<PropositionDetermination> propositions) {
 | 
        
           |  |  | 18 | 		List<MoyenneVote> pairesVotes = new ArrayList<MoyenneVote>();
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 | 		for (PropositionDetermination proposition : propositions) {
 | 
        
           |  |  | 21 | 			pairesVotes.add(calculerVoteDetermination(proposition));
 | 
        
           |  |  | 22 | 		}
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | 		java.util.Collections.sort(pairesVotes);
 | 
        
           |  |  | 25 |   | 
        
           |  |  | 26 | 		return pairesVotes;
 | 
        
           |  |  | 27 | 	}
 | 
        
           |  |  | 28 |   | 
        
           | 1124 | gduche | 29 | 	public static int getNombreTotalVotants(PropositionDetermination proposition) {
 | 
        
           |  |  | 30 | 		int nbVotants = 0;
 | 
        
           |  |  | 31 | 		List<PropositionDetermination> votes = proposition.getObservation().getPropositionDeterminations();
 | 
        
           |  |  | 32 | 		Iterator<PropositionDetermination> itVotes =  votes.iterator();
 | 
        
           |  |  | 33 | 		while (itVotes.hasNext()) {
 | 
        
           |  |  | 34 | 			PropositionDetermination propositionCourante = itVotes.next();
 | 
        
           |  |  | 35 | 			nbVotants += propositionCourante.getVotesDeterminations().size();
 | 
        
           |  |  | 36 | 		}
 | 
        
           |  |  | 37 | 		return nbVotants;
 | 
        
           |  |  | 38 | 	}
 | 
        
           |  |  | 39 |   | 
        
           | 1139 | aurelien | 40 | 	public static MoyenneVote calculerVoteDetermination(PropositionDetermination propositionDetermination) {
 | 
        
           | 1124 | gduche | 41 | 		int nbVotants = getNombreTotalVotants(propositionDetermination);
 | 
        
           | 1139 | aurelien | 42 | 		return calculerProportionVote(propositionDetermination, nbVotants);
 | 
        
           |  |  | 43 | 	}
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | 	public static MoyenneVote calculerMoyenneVoteDetermination(PropositionDetermination propositionDetermination, int nbVotants) {
 | 
        
           |  |  | 46 | 		return calculerProportionVote(propositionDetermination, nbVotants);
 | 
        
           |  |  | 47 | 	}
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 | 	private static MoyenneVote calculerProportionVote(PropositionDetermination propositionDetermination, int nbVotants) {
 | 
        
           | 1767 | mathias | 50 | 		Map<String,VoteDetermination> votes = propositionDetermination.getVotesDeterminations();
 | 
        
           | 1098 | aurelien | 51 | 		double scoreVote = 0;
 | 
        
           |  |  | 52 |   | 
        
           | 1767 | mathias | 53 | 		if (votes.size() > 0 && nbVotants > 0) {
 | 
        
           | 227 | aurelien | 54 | 			Set<String> cles = votes.keySet();
 | 
        
           | 1098 | aurelien | 55 |   | 
        
           | 227 | aurelien | 56 | 			for (String cle : cles) {
 | 
        
           | 1098 | aurelien | 57 | 				String  idAuteur = cle;
 | 
        
           | 1833 | aurelien | 58 | 				boolean voteEstPositif = votes.get(cle).getVote() == 1;
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 				// un votant identifiant compte comme deux votants supplémentaires
 | 
        
           |  |  | 61 | 				// il a donc un vote 3 fois supérieur
 | 
        
           |  |  | 62 | 				int valeurVote = estUnAuteurIdentifie(idAuteur) ? 3 : 1;
 | 
        
           |  |  | 63 | 				scoreVote += voteEstPositif ? valeurVote : -valeurVote;
 | 
        
           | 163 | aurelien | 64 | 			}
 | 
        
           |  |  | 65 | 		}
 | 
        
           |  |  | 66 |   | 
        
           | 1098 | aurelien | 67 | 		return new MoyenneVote((new Double(scoreVote)).intValue(), propositionDetermination);
 | 
        
           | 163 | aurelien | 68 | 	}
 | 
        
           | 1098 | aurelien | 69 |   | 
        
           |  |  | 70 | 	private static boolean estUnAuteurIdentifie(String idAuteur) {
 | 
        
           |  |  | 71 | 		boolean estIdentifie = true;
 | 
        
           |  |  | 72 | 	    try {
 | 
        
           |  |  | 73 | 	        int intIdAuteur = Integer.parseInt(idAuteur);
 | 
        
           |  |  | 74 | 	    } catch (NumberFormatException nfe) {
 | 
        
           |  |  | 75 | 	    	estIdentifie = false;
 | 
        
           |  |  | 76 | 	    }
 | 
        
           |  |  | 77 | 	    return estIdentifie;
 | 
        
           |  |  | 78 | 	}
 | 
        
           | 163 | aurelien | 79 | }
 |