2211 |
arthur |
1 |
package org.tela_botanica.del.client.composants.votes.moyennevotes;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Collections;
|
|
|
5 |
import java.util.Comparator;
|
|
|
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.Iterator;
|
|
|
8 |
import java.util.List;
|
|
|
9 |
|
|
|
10 |
import org.tela_botanica.del.client.cache.CacheClient;
|
|
|
11 |
import org.tela_botanica.del.client.composants.votefleur.VoteFleurPresenteur;
|
|
|
12 |
import org.tela_botanica.del.client.composants.votefleur.VoteFleurVue;
|
|
|
13 |
import org.tela_botanica.del.client.modeles.Image;
|
|
|
14 |
import org.tela_botanica.del.client.modeles.Protocole;
|
|
|
15 |
import org.tela_botanica.del.client.modeles.VoteProtocole;
|
|
|
16 |
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
|
|
|
17 |
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.EvenementChangementEtatUtilisateur;
|
|
|
18 |
import org.tela_botanica.del.client.navigation.evenement.changementEtatUtilisateur.GestionnaireEvenementChangementEtatUtilisateur;
|
|
|
19 |
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.EvenementSuppressionCommentaire;
|
|
|
20 |
import org.tela_botanica.del.client.navigation.evenement.suppressioncommentaire.GestionnaireEvenementSuppressionCommentaire;
|
|
|
21 |
import org.tela_botanica.del.client.services.rest.VoteProtocoleService;
|
|
|
22 |
import org.tela_botanica.del.client.services.rest.async.AjoutVoteProtocoleCallback;
|
|
|
23 |
import org.tela_botanica.del.client.services.rest.async.VoteProtocoleCallback;
|
|
|
24 |
|
|
|
25 |
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
26 |
import com.google.gwt.event.dom.client.ClickHandler;
|
|
|
27 |
import com.google.gwt.event.dom.client.HasClickHandlers;
|
|
|
28 |
import com.google.gwt.event.dom.client.HasMouseMoveHandlers;
|
|
|
29 |
import com.google.gwt.event.dom.client.MouseMoveEvent;
|
|
|
30 |
import com.google.gwt.event.dom.client.MouseMoveHandler;
|
|
|
31 |
import com.google.gwt.user.client.Window;
|
|
|
32 |
import com.google.gwt.user.client.ui.HasText;
|
|
|
33 |
import com.google.gwt.user.client.ui.HasWidgets;
|
|
|
34 |
import com.google.gwt.user.client.ui.IsWidget;
|
|
|
35 |
import com.google.gwt.user.client.ui.Panel;
|
|
|
36 |
|
|
|
37 |
public class MoyenneVotePresenteur {
|
|
|
38 |
|
|
|
39 |
public interface Vue extends IsWidget {
|
|
|
40 |
public HasClickHandlers getBoutonAnnuler();
|
|
|
41 |
|
|
|
42 |
public HasClickHandlers getVotes();
|
|
|
43 |
|
|
|
44 |
public int getValeurVote();
|
|
|
45 |
|
|
|
46 |
public void afficherBoutonAnnuler();
|
|
|
47 |
|
|
|
48 |
public void masquerBoutonAnnuler();
|
|
|
49 |
|
|
|
50 |
public void afficherNbVotes();
|
|
|
51 |
|
|
|
52 |
public void masquerNbVotes();
|
|
|
53 |
|
|
|
54 |
//public void afficherNbPoints();
|
|
|
55 |
|
|
|
56 |
//public void masquerNbPoints();
|
|
|
57 |
|
|
|
58 |
public void reinitialiserVotes();
|
|
|
59 |
|
|
|
60 |
public void rafraichir(int moyenneVote, int nbVotes, int nombrePoints, double moyenneArithmetique, double mediane, HashMap<Integer,Integer> occurrencesParNote, double moyennePonderee);
|
|
|
61 |
|
|
|
62 |
public void ajouterAuParent(HasWidgets composite);
|
|
|
63 |
|
|
|
64 |
public void afficherVotePrisEnCompte();
|
|
|
65 |
|
|
|
66 |
public HasWidgets getZoneFleur();
|
|
|
67 |
|
|
|
68 |
public HasText getZoneProtocole();
|
|
|
69 |
|
|
|
70 |
public HasText getNbVotes();
|
|
|
71 |
|
|
|
72 |
//public HasText getNbPoints();
|
|
|
73 |
|
|
|
74 |
public HasClickHandlers getLienNbVotes();
|
|
|
75 |
|
|
|
76 |
public void setNoteGenerale(int note);
|
|
|
77 |
|
|
|
78 |
public void afficherVoteModifie();
|
|
|
79 |
|
|
|
80 |
public HasMouseMoveHandlers getZoneVoter();
|
|
|
81 |
public void masquerZoneVoter();
|
|
|
82 |
|
|
|
83 |
public void afficherMessageVoteIdentifie(boolean doitEtreIdentifiePourVoter);
|
|
|
84 |
|
|
|
85 |
public void setNoteGeneraleToolTip(double valeurVoteTotalPrecise);
|
|
|
86 |
|
|
|
87 |
public void desactiverInteractionVote();
|
|
|
88 |
|
|
|
89 |
public void activerInteractionVote();
|
|
|
90 |
|
|
|
91 |
public void afficherVoteSupprime();
|
|
|
92 |
|
|
|
93 |
public Panel getPanneauDetailVotes();
|
|
|
94 |
|
|
|
95 |
public void afficherPanneauDetailVotes();
|
|
|
96 |
|
|
|
97 |
public void masquerPanneauDetailVotes();
|
|
|
98 |
|
|
|
99 |
public HasClickHandlers getLienPanneauFermer();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private Vue vue;
|
|
|
103 |
private Protocole protocole;
|
|
|
104 |
private Image image;
|
|
|
105 |
|
|
|
106 |
private int valeurVoteDefaut = -1;
|
|
|
107 |
private int valeurVoteUtilisateur = -1;
|
|
|
108 |
private int valeurVoteTotal = 0;
|
|
|
109 |
private double moyenneArithmetique = 0;
|
|
|
110 |
private double mediane = 0;
|
|
|
111 |
private int nombrePoints = 0;
|
|
|
112 |
private double valeurVoteTotalPrecise = 0.0;
|
|
|
113 |
private HashMap<Integer,Integer> occurrencesParNote;
|
|
|
114 |
|
|
|
115 |
// TODO: on devrait passer un conteneur qui permet d'accéder à ces services
|
|
|
116 |
private VoteProtocoleService voteProtocoleService;
|
|
|
117 |
|
|
|
118 |
public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue,
|
|
|
119 |
VoteProtocoleService voteProtocoleService) {
|
|
|
120 |
this.vue = vue;
|
|
|
121 |
this.protocole = protocole;
|
|
|
122 |
this.image = image;
|
|
|
123 |
this.voteProtocoleService = voteProtocoleService;
|
|
|
124 |
|
|
|
125 |
String nomProtocole = protocole.getNom();
|
|
|
126 |
nomProtocole = nomProtocole.substring(0, 1).toUpperCase()
|
|
|
127 |
+ nomProtocole.substring(1).toLowerCase();
|
|
|
128 |
vue.getZoneProtocole().setText(nomProtocole);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public void go(HasWidgets composite) {
|
|
|
132 |
vue.ajouterAuParent(composite);
|
|
|
133 |
gererEvenements();
|
|
|
134 |
rafraichirVue();
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
public void gererEvenements() {
|
|
|
138 |
|
|
|
139 |
vue.afficherMessageVoteIdentifie(utilisateurPeutVoter());
|
|
|
140 |
vue.getZoneVoter().addMouseMoveHandler(new MouseMoveHandler() {
|
|
|
141 |
|
|
|
142 |
@Override
|
|
|
143 |
public void onMouseMove(MouseMoveEvent event) {
|
|
|
144 |
if(utilisateurPeutVoter()) {
|
|
|
145 |
vue.masquerZoneVoter();
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
});
|
|
|
149 |
|
|
|
150 |
vue.getVotes().addClickHandler(new ClickHandler() {
|
|
|
151 |
@Override
|
|
|
152 |
public void onClick(ClickEvent event) {
|
|
|
153 |
if(utilisateurPeutVoter()) {
|
|
|
154 |
valeurVoteUtilisateur = vue.getValeurVote();
|
|
|
155 |
enregistrerVote();
|
|
|
156 |
vue.afficherBoutonAnnuler();
|
|
|
157 |
} else {
|
|
|
158 |
Window.alert("Voter pour ce protocole nécéssite d'être identifié");
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
}
|
|
|
162 |
});
|
|
|
163 |
|
|
|
164 |
vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
|
|
|
165 |
@Override
|
|
|
166 |
public void onClick(ClickEvent event) {
|
|
|
167 |
valeurVoteUtilisateur = valeurVoteDefaut;
|
|
|
168 |
vue.masquerBoutonAnnuler();
|
|
|
169 |
supprimerVote();
|
|
|
170 |
vue.reinitialiserVotes();
|
|
|
171 |
}
|
|
|
172 |
});
|
|
|
173 |
|
|
|
174 |
// Affiche un chouette panneau qui montre le détail des votes
|
|
|
175 |
vue.getLienNbVotes().addClickHandler(new ClickHandler() {
|
|
|
176 |
@Override
|
|
|
177 |
public void onClick(ClickEvent event) {
|
|
|
178 |
if (vue.getPanneauDetailVotes().isVisible()) {
|
|
|
179 |
vue.masquerPanneauDetailVotes();
|
|
|
180 |
} else {
|
|
|
181 |
vue.afficherPanneauDetailVotes();
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
});
|
|
|
185 |
|
|
|
186 |
vue.getLienPanneauFermer().addClickHandler(new ClickHandler() {
|
|
|
187 |
@Override
|
|
|
188 |
public void onClick(ClickEvent event) {
|
|
|
189 |
vue.masquerPanneauDetailVotes();
|
|
|
190 |
}
|
|
|
191 |
});
|
|
|
192 |
|
|
|
193 |
BusEvenementiel.getInstance().addHandler(EvenementChangementEtatUtilisateur.TYPE, new GestionnaireEvenementChangementEtatUtilisateur() {
|
|
|
194 |
@Override
|
|
|
195 |
public void onModificationEtatUtilisateur(EvenementChangementEtatUtilisateur evenementChangementEtatUtilisateur) {
|
|
|
196 |
rafraichirVue();
|
|
|
197 |
}
|
|
|
198 |
});
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
public boolean utilisateurPeutVoter() {
|
|
|
202 |
return !protocole.doitEtreIdentifiePourVoter() || CacheClient.getInstance().getUtilisateur().estIdentifie();
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
|
|
|
206 |
this.valeurVoteUtilisateur = valeurVoteUtilisateur;
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
public void enregistrerVote() {
|
|
|
210 |
vue.desactiverInteractionVote();
|
|
|
211 |
final VoteProtocole voteProtocole = new VoteProtocole();
|
|
|
212 |
voteProtocole.setProtocole(this.protocole);
|
|
|
213 |
voteProtocole.setVote(valeurVoteUtilisateur);
|
|
|
214 |
String idContributeur = CacheClient.getInstance().getUtilisateur()
|
|
|
215 |
.getId();
|
|
|
216 |
voteProtocole.setContributeur(idContributeur);
|
|
|
217 |
|
|
|
218 |
//TODO: ceci devrait pouvoir être factorisé
|
|
|
219 |
if (image.utilisateurAVotePourProtocole(this.protocole.getId() + "",
|
|
|
220 |
idContributeur)) {
|
|
|
221 |
voteProtocole.setId(image.getVotesProtocoles(this.protocole.getId()+"").get(idContributeur).getId());
|
|
|
222 |
VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
|
|
|
223 |
@Override
|
|
|
224 |
public void surRetour(Void objetRetour) {
|
|
|
225 |
image.ajouterVoteProtocole(voteProtocole);
|
|
|
226 |
rafraichirVue();
|
|
|
227 |
vue.afficherVoteModifie();
|
|
|
228 |
vue.activerInteractionVote();
|
|
|
229 |
}
|
|
|
230 |
};
|
|
|
231 |
voteProtocoleService.modifierVote(image.getIdImage(),
|
|
|
232 |
voteProtocole, vpc);
|
|
|
233 |
} else {
|
|
|
234 |
AjoutVoteProtocoleCallback vpc = new AjoutVoteProtocoleCallback() {
|
|
|
235 |
@Override
|
|
|
236 |
public void surRetour(String idVote) {
|
|
|
237 |
voteProtocole.setId(idVote);
|
|
|
238 |
image.ajouterVoteProtocole(voteProtocole);
|
|
|
239 |
rafraichirVue();
|
|
|
240 |
vue.afficherVotePrisEnCompte();
|
|
|
241 |
vue.activerInteractionVote();
|
|
|
242 |
}
|
|
|
243 |
};
|
|
|
244 |
voteProtocoleService.ajouterVote(image.getIdImage(), voteProtocole,
|
|
|
245 |
vpc);
|
|
|
246 |
}
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
public void supprimerVote() {
|
|
|
250 |
vue.desactiverInteractionVote();
|
|
|
251 |
final String idContributeur = CacheClient.getInstance().getUtilisateur()
|
|
|
252 |
.getId();
|
|
|
253 |
final VoteProtocole voteProtocole = image.getVotesProtocoles(protocole.getId()).get(idContributeur);
|
|
|
254 |
VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
|
|
|
255 |
@Override
|
|
|
256 |
public void surRetour(Void objetRetour) {
|
|
|
257 |
// TODO: voir si l'on affiche un message en cas de succès ?
|
|
|
258 |
image.supprimerVoteProtocole(protocole.getId() + "", idContributeur);
|
|
|
259 |
rafraichirVue();
|
|
|
260 |
vue.afficherVoteSupprime();
|
|
|
261 |
vue.activerInteractionVote();
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
};
|
|
|
265 |
voteProtocoleService.supprimerVote(image.getIdImage(), voteProtocole, vpc);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
public int getValeurVoteTotal() {
|
|
|
269 |
return valeurVoteTotal;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
public IsWidget getVue() {
|
|
|
273 |
return vue;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
private void rafraichirVue() {
|
|
|
277 |
valeurVoteTotal = calculerMoyenneVotesArrondie();
|
|
|
278 |
moyenneArithmetique = calculerMoyenneVotes();
|
|
|
279 |
mediane = calculerMedianeVotes();
|
|
|
280 |
valeurVoteTotalPrecise = calculerMoyennePondereeVotes();
|
|
|
281 |
nombrePoints = calculerNombrePointsEchelleArbitraire();
|
|
|
282 |
VoteProtocole voteProtocole = image.getVotesProtocoles(
|
|
|
283 |
protocole.getId()).get(
|
|
|
284 |
CacheClient.getInstance().getUtilisateur().getId());
|
|
|
285 |
int voteUtilisateur = 0;
|
|
|
286 |
if (voteProtocole != null) {
|
|
|
287 |
voteUtilisateur = voteProtocole.getVote();
|
|
|
288 |
vue.masquerZoneVoter();
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
if(image.utilisateurAVotePourProtocole(protocole.getId()+"", CacheClient.getInstance().getUtilisateur().getId())) {
|
|
|
292 |
vue.afficherBoutonAnnuler();
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
vue.afficherMessageVoteIdentifie(utilisateurPeutVoter());
|
|
|
296 |
|
|
|
297 |
VoteFleurPresenteur presenteurFleur = new VoteFleurPresenteur(
|
|
|
298 |
new VoteFleurVue());
|
|
|
299 |
presenteurFleur.setNote(valeurVoteTotal);
|
|
|
300 |
vue.setNoteGenerale(valeurVoteTotal);
|
|
|
301 |
vue.setNoteGeneraleToolTip(valeurVoteTotalPrecise);
|
|
|
302 |
presenteurFleur.go(vue.getZoneFleur());
|
|
|
303 |
vue.rafraichir(voteUtilisateur, image.getVotesProtocoles(protocole.getId()).size(), nombrePoints,
|
|
|
304 |
moyenneArithmetique, mediane, occurrencesParNote, valeurVoteTotalPrecise);
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
public double calculerMedianeVotes() {
|
|
|
308 |
double med = 0;
|
|
|
309 |
// comparateur de votes (le bulldozer Java™ pour écraser une mouche)
|
|
|
310 |
class Comparatisateur3000 implements Comparator<VoteProtocole> {
|
|
|
311 |
@Override
|
|
|
312 |
public int compare(VoteProtocole v1, VoteProtocole v2) {
|
|
|
313 |
if (v1.getVote() > v2.getVote()) {
|
|
|
314 |
return 1;
|
|
|
315 |
} else if (v1.getVote() > v2.getVote()) {
|
|
|
316 |
return -1;
|
|
|
317 |
} else {
|
|
|
318 |
return 0;
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
// conversion en liste triable
|
|
|
323 |
List<VoteProtocole> liste = new ArrayList<VoteProtocole>(image.getVotesProtocoles(protocole.getId()).values());
|
|
|
324 |
Collections.sort(liste, new Comparatisateur3000());
|
|
|
325 |
int longueur = liste.size();
|
|
|
326 |
if (longueur > 0) {
|
|
|
327 |
if (longueur % 2 == 0) {
|
|
|
328 |
// moyenne des éléments centraux
|
|
|
329 |
med = (liste.get(longueur / 2 - 1).getVote() + liste.get(longueur / 2).getVote()) / 2;
|
|
|
330 |
} else {
|
|
|
331 |
// élément central
|
|
|
332 |
med = liste.get((int) (longueur / 2)).getVote();
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
return med;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
// calcule la moyenne arithmétique
|
|
|
339 |
public double calculerMoyenneVotes() {
|
|
|
340 |
double valeurVote = 0;
|
|
|
341 |
double nbVote = 0;
|
|
|
342 |
for (Iterator<String> iterator = image
|
|
|
343 |
.getVotesProtocoles(protocole.getId()).keySet().iterator(); iterator
|
|
|
344 |
.hasNext();) {
|
|
|
345 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(
|
|
|
346 |
protocole.getId()).get(iterator.next());
|
|
|
347 |
valeurVote += (double) imageCelValidationData.getVote() / 5;
|
|
|
348 |
nbVote++;
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
if (nbVote > 0) {
|
|
|
352 |
valeurVote /= nbVote;
|
|
|
353 |
valeurVote *= 5;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
return valeurVote;
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
// Calcule une moyenne, où chaque note est pondérée par son nombre d'occurrences au carré.
|
|
|
360 |
// voir http://www.tela-botanica.org/wikini/DevInformatiques/wakka.php?wiki=AppliDelCalculVotes
|
|
|
361 |
public double calculerMoyennePondereeVotes() {
|
|
|
362 |
|
|
|
363 |
double score = 0;
|
|
|
364 |
double diviseur = 0;
|
|
|
365 |
int nbOccurrences;
|
|
|
366 |
// mise à jour des occurrences par note, utile pour le panneau de détails
|
|
|
367 |
occurrencesParNote = new HashMap<Integer,Integer>();
|
|
|
368 |
|
|
|
369 |
// rangement par note => occurrences
|
|
|
370 |
for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
|
|
|
371 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
|
|
|
372 |
nbOccurrences = 1;
|
|
|
373 |
if (occurrencesParNote.containsKey(imageCelValidationData.getVote())) {
|
|
|
374 |
nbOccurrences = occurrencesParNote.get(imageCelValidationData.getVote()) + 1;
|
|
|
375 |
}
|
|
|
376 |
occurrencesParNote.put(imageCelValidationData.getVote(), nbOccurrences);
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
// calcul pondéré
|
|
|
380 |
for (Integer clef : occurrencesParNote.keySet()) {
|
|
|
381 |
score += clef * occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
|
|
|
382 |
diviseur += occurrencesParNote.get(clef) * occurrencesParNote.get(clef);
|
|
|
383 |
}
|
|
|
384 |
|
|
|
385 |
if (diviseur > 0) {
|
|
|
386 |
score /= diviseur;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
return score;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
// remplace chaque note par un nombre de points noté dans "echelle" afin de favoriser
|
|
|
393 |
// les note fortes (5 et 4), pour le défi photo notamment
|
|
|
394 |
public int calculerNombrePointsEchelleArbitraire() {
|
|
|
395 |
int points = 0;
|
|
|
396 |
//int[] echelle = {-1,0,1,4,20};
|
|
|
397 |
int[] echelle = {1,10,100,1000,10000};
|
|
|
398 |
|
|
|
399 |
for (String clef : image.getVotesProtocoles(protocole.getId()).keySet()) {
|
|
|
400 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(clef);
|
|
|
401 |
if (imageCelValidationData.getVote() > 0) {
|
|
|
402 |
points += echelle[imageCelValidationData.getVote() - 1];
|
|
|
403 |
}
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
return points;
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
public int calculerMoyenneVotesArrondie() {
|
|
|
410 |
//double valeurVote = calculerMoyenneVotes();
|
|
|
411 |
double valeurVote = calculerMoyennePondereeVotes();
|
|
|
412 |
return (int) Math.round(valeurVote);
|
|
|
413 |
}
|
|
|
414 |
}
|