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