| 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) {
 | 
        
           | 1767 | mathias | 49 | 		Map<String,VoteDetermination> votes = propositionDetermination.getVotesDeterminations();
 | 
        
           | 1098 | aurelien | 50 | 		double scoreVote = 0;
 | 
        
           |  |  | 51 |   | 
        
           | 1767 | mathias | 52 | 		if (votes.size() > 0 && nbVotants > 0) {
 | 
        
           | 227 | aurelien | 53 | 			Set<String> cles = votes.keySet();
 | 
        
           | 1098 | aurelien | 54 |   | 
        
           | 227 | aurelien | 55 | 			for (String cle : cles) {
 | 
        
           | 1098 | aurelien | 56 | 				String  idAuteur = cle;
 | 
        
           | 1767 | mathias | 57 | 				boolean voteEstPositif = votes.get(cle).getVote() == 1 ? true : false;
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 | 				if (estUnAuteurIdentifie(idAuteur)) {
 | 
        
           | 1098 | aurelien | 60 | 					// un votant identifiant compte comme deux votants supplémentaires
 | 
        
           |  |  | 61 | 					// il a donc un vote 3 fois supérieur
 | 
        
           | 1767 | mathias | 62 | 					if (voteEstPositif) {
 | 
        
           |  |  | 63 | 						//scoreVote += 3;
 | 
        
           |  |  | 64 | 						scoreVote += 1;
 | 
        
           |  |  | 65 | 					} /*else {
 | 
        
           |  |  | 66 | 						scoreVote -= 3;
 | 
        
           |  |  | 67 | 					}*/
 | 
        
           | 1098 | aurelien | 68 | 				} else {
 | 
        
           | 1767 | mathias | 69 | 					if (voteEstPositif) {
 | 
        
           |  |  | 70 | 						scoreVote += 1;
 | 
        
           |  |  | 71 | 					} /*else {
 | 
        
           |  |  | 72 | 						scoreVote -= 1;
 | 
        
           |  |  | 73 | 					}*/
 | 
        
           | 1098 | aurelien | 74 | 				}
 | 
        
           | 163 | aurelien | 75 | 			}
 | 
        
           | 1767 | mathias | 76 | 			scoreVote = (scoreVote / nbVotants) * 100;
 | 
        
           | 163 | aurelien | 77 | 		}
 | 
        
           |  |  | 78 |   | 
        
           | 1098 | aurelien | 79 | 		return new MoyenneVote((new Double(scoreVote)).intValue(), propositionDetermination);
 | 
        
           | 163 | aurelien | 80 | 	}
 | 
        
           | 1098 | aurelien | 81 |   | 
        
           |  |  | 82 | 	private static boolean estUnAuteurIdentifie(String idAuteur) {
 | 
        
           |  |  | 83 | 		boolean estIdentifie = true;
 | 
        
           |  |  | 84 | 	    try {
 | 
        
           |  |  | 85 | 	        int intIdAuteur = Integer.parseInt(idAuteur);
 | 
        
           |  |  | 86 | 	    } catch (NumberFormatException nfe) {
 | 
        
           |  |  | 87 | 	    	estIdentifie = false;
 | 
        
           |  |  | 88 | 	    }
 | 
        
           |  |  | 89 | 	    return estIdentifie;
 | 
        
           |  |  | 90 | 	}
 | 
        
           | 163 | aurelien | 91 | }
 |