| 638 |
aurelien |
1 |
package org.tela_botanica.del.client.composants.votes.moyennevotes;
|
| 9 |
benjamin |
2 |
|
| 629 |
aurelien |
3 |
import java.util.Iterator;
|
| 9 |
benjamin |
4 |
import java.util.List;
|
|
|
5 |
|
| 629 |
aurelien |
6 |
import org.tela_botanica.del.client.cache.CacheClient;
|
|
|
7 |
import org.tela_botanica.del.client.modeles.Image;
|
| 423 |
gduche |
8 |
import org.tela_botanica.del.client.modeles.Protocole;
|
| 629 |
aurelien |
9 |
import org.tela_botanica.del.client.modeles.Utilisateur;
|
| 148 |
benjamin |
10 |
import org.tela_botanica.del.client.modeles.VoteProtocole;
|
| 423 |
gduche |
11 |
import org.tela_botanica.del.client.services.rest.VoteProtocoleService;
|
|
|
12 |
import org.tela_botanica.del.client.utils.MockDatasource;
|
| 9 |
benjamin |
13 |
|
| 629 |
aurelien |
14 |
import com.google.gwt.event.dom.client.ChangeEvent;
|
|
|
15 |
import com.google.gwt.event.dom.client.ChangeHandler;
|
| 423 |
gduche |
16 |
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
17 |
import com.google.gwt.event.dom.client.ClickHandler;
|
| 629 |
aurelien |
18 |
import com.google.gwt.event.dom.client.HasChangeHandlers;
|
|
|
19 |
import com.google.gwt.event.dom.client.HasClickHandlers;
|
| 423 |
gduche |
20 |
import com.google.gwt.user.client.Window;
|
| 629 |
aurelien |
21 |
import com.google.gwt.user.client.ui.HasText;
|
| 9 |
benjamin |
22 |
import com.google.gwt.user.client.ui.HasWidgets;
|
| 629 |
aurelien |
23 |
import com.google.gwt.user.client.ui.IsWidget;
|
| 9 |
benjamin |
24 |
|
| 14 |
benjamin |
25 |
public class MoyenneVotePresenteur {
|
| 9 |
benjamin |
26 |
|
| 629 |
aurelien |
27 |
public interface Vue extends IsWidget {
|
|
|
28 |
public HasClickHandlers getBoutonVoter();
|
|
|
29 |
public HasClickHandlers getBoutonAnnuler();
|
|
|
30 |
public HasText getNbVotes();
|
|
|
31 |
public HasClickHandlers getVotes();
|
|
|
32 |
public int getValeurVote();
|
|
|
33 |
public void afficherBoutonVoter();
|
|
|
34 |
public void afficherBoutonAnnuler();
|
|
|
35 |
public void masquerBoutonVoter();
|
|
|
36 |
public void masquerBoutonAnnuler();
|
|
|
37 |
public void afficherNbVotes();
|
|
|
38 |
public void masquerNbVotes();
|
|
|
39 |
public void reinitialiserVotes();
|
|
|
40 |
public void rafraichir(int moyenneVote, int nbVotes);
|
|
|
41 |
public void ajouterAuParent(HasWidgets composite);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
private Vue vue;
|
| 423 |
gduche |
45 |
private Protocole protocole;
|
| 629 |
aurelien |
46 |
private Image image;
|
| 423 |
gduche |
47 |
|
| 629 |
aurelien |
48 |
private int valeurVoteDefaut = -1;
|
|
|
49 |
private int valeurVoteUtilisateur = -1;
|
|
|
50 |
private int valeurVoteTotal = 0;
|
|
|
51 |
|
|
|
52 |
public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue) {
|
|
|
53 |
this.vue = vue;
|
| 423 |
gduche |
54 |
this.protocole = protocole;
|
| 629 |
aurelien |
55 |
this.image = image;
|
| 9 |
benjamin |
56 |
}
|
|
|
57 |
|
| 629 |
aurelien |
58 |
public void go(HasWidgets composite) {
|
|
|
59 |
vue.ajouterAuParent(composite);
|
| 423 |
gduche |
60 |
gererEvenements();
|
| 629 |
aurelien |
61 |
rafraichirVue();
|
| 9 |
benjamin |
62 |
}
|
| 423 |
gduche |
63 |
|
|
|
64 |
public void gererEvenements() {
|
|
|
65 |
vue.getVotes().addClickHandler(new ClickHandler() {
|
|
|
66 |
@Override
|
|
|
67 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
68 |
valeurVoteUtilisateur = vue.getValeurVote();
|
| 423 |
gduche |
69 |
vue.afficherBoutonVoter();
|
|
|
70 |
vue.afficherBoutonAnnuler();
|
|
|
71 |
vue.masquerNbVotes();
|
|
|
72 |
}
|
|
|
73 |
});
|
|
|
74 |
|
|
|
75 |
vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
|
|
|
76 |
@Override
|
|
|
77 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
78 |
valeurVoteUtilisateur = valeurVoteDefaut;
|
| 423 |
gduche |
79 |
vue.masquerBoutonVoter();
|
|
|
80 |
vue.masquerBoutonAnnuler();
|
|
|
81 |
vue.afficherNbVotes();
|
|
|
82 |
vue.reinitialiserVotes();
|
|
|
83 |
}
|
|
|
84 |
});
|
|
|
85 |
|
|
|
86 |
vue.getBoutonVoter().addClickHandler(new ClickHandler() {
|
|
|
87 |
@Override
|
|
|
88 |
public void onClick(ClickEvent event) {
|
|
|
89 |
enregistrerVote();
|
|
|
90 |
vue.masquerBoutonVoter();
|
|
|
91 |
vue.masquerBoutonAnnuler();
|
|
|
92 |
vue.afficherNbVotes();
|
|
|
93 |
vue.reinitialiserVotes();
|
|
|
94 |
}
|
|
|
95 |
});
|
|
|
96 |
}
|
|
|
97 |
|
| 629 |
aurelien |
98 |
public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
|
|
|
99 |
this.valeurVoteUtilisateur = valeurVoteUtilisateur;
|
|
|
100 |
}
|
|
|
101 |
|
| 423 |
gduche |
102 |
public void enregistrerVote() {
|
|
|
103 |
VoteProtocole voteProtocole = new VoteProtocole();
|
|
|
104 |
voteProtocole.setProtocole(this.protocole);
|
| 629 |
aurelien |
105 |
voteProtocole.setVote(valeurVoteUtilisateur);
|
|
|
106 |
voteProtocole.setContributeur(CacheClient.getInstance().getUtilisateur().getId());
|
|
|
107 |
// voteProtocoleService.saveVote(idImage, voteProtocole);
|
|
|
108 |
// TODO: sauver vote asynchrone
|
|
|
109 |
image.ajouterVoteProtocole(voteProtocole);
|
|
|
110 |
rafraichirVue();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public int getValeurVoteTotal() {
|
|
|
114 |
return valeurVoteTotal;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public IsWidget getVue() {
|
|
|
118 |
return vue;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private void rafraichirVue() {
|
|
|
122 |
valeurVoteTotal = calculerMoyenneVotes();
|
|
|
123 |
vue.rafraichir(valeurVoteTotal, image.getVotesProtocoles(protocole.getId()).size());
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public int calculerMoyenneVotes() {
|
|
|
127 |
double valeurVote = 0;
|
|
|
128 |
int nbVote = 0;
|
|
|
129 |
for (Iterator<String> iterator = image.getVotesProtocoles(protocole.getId()).keySet().iterator(); iterator.hasNext();) {
|
|
|
130 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(iterator.next());
|
|
|
131 |
valeurVote += (double)imageCelValidationData.getVote()/5;
|
|
|
132 |
nbVote++;
|
|
|
133 |
}
|
| 423 |
gduche |
134 |
|
| 629 |
aurelien |
135 |
if (nbVote > 0) {
|
|
|
136 |
valeurVote /= nbVote;
|
|
|
137 |
valeurVote *= 5;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
return (int)Math.round(valeurVote);
|
| 423 |
gduche |
141 |
}
|
| 9 |
benjamin |
142 |
}
|