| 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 |
|
| 629 |
aurelien |
5 |
import org.tela_botanica.del.client.cache.CacheClient;
|
| 1085 |
gduche |
6 |
import org.tela_botanica.del.client.composants.votefleur.VoteFleurPresenteur;
|
|
|
7 |
import org.tela_botanica.del.client.composants.votefleur.VoteFleurVue;
|
| 629 |
aurelien |
8 |
import org.tela_botanica.del.client.modeles.Image;
|
| 423 |
gduche |
9 |
import org.tela_botanica.del.client.modeles.Protocole;
|
| 148 |
benjamin |
10 |
import org.tela_botanica.del.client.modeles.VoteProtocole;
|
| 423 |
gduche |
11 |
import org.tela_botanica.del.client.services.rest.VoteProtocoleService;
|
| 823 |
aurelien |
12 |
import org.tela_botanica.del.client.services.rest.async.VoteProtocoleCallback;
|
| 939 |
benjamin |
13 |
|
| 423 |
gduche |
14 |
import com.google.gwt.event.dom.client.ClickEvent;
|
|
|
15 |
import com.google.gwt.event.dom.client.ClickHandler;
|
| 629 |
aurelien |
16 |
import com.google.gwt.event.dom.client.HasClickHandlers;
|
| 1085 |
gduche |
17 |
import com.google.gwt.user.client.Window;
|
| 629 |
aurelien |
18 |
import com.google.gwt.user.client.ui.HasText;
|
| 9 |
benjamin |
19 |
import com.google.gwt.user.client.ui.HasWidgets;
|
| 629 |
aurelien |
20 |
import com.google.gwt.user.client.ui.IsWidget;
|
| 9 |
benjamin |
21 |
|
| 14 |
benjamin |
22 |
public class MoyenneVotePresenteur {
|
| 9 |
benjamin |
23 |
|
| 629 |
aurelien |
24 |
public interface Vue extends IsWidget {
|
|
|
25 |
public HasClickHandlers getBoutonVoter();
|
| 939 |
benjamin |
26 |
|
| 629 |
aurelien |
27 |
public HasClickHandlers getBoutonAnnuler();
|
| 939 |
benjamin |
28 |
|
| 629 |
aurelien |
29 |
public HasClickHandlers getVotes();
|
| 939 |
benjamin |
30 |
|
| 629 |
aurelien |
31 |
public int getValeurVote();
|
| 939 |
benjamin |
32 |
|
| 629 |
aurelien |
33 |
public void afficherBoutonVoter();
|
| 939 |
benjamin |
34 |
|
| 629 |
aurelien |
35 |
public void afficherBoutonAnnuler();
|
| 939 |
benjamin |
36 |
|
| 629 |
aurelien |
37 |
public void masquerBoutonVoter();
|
| 939 |
benjamin |
38 |
|
| 629 |
aurelien |
39 |
public void masquerBoutonAnnuler();
|
| 939 |
benjamin |
40 |
|
| 629 |
aurelien |
41 |
public void afficherNbVotes();
|
| 939 |
benjamin |
42 |
|
|
|
43 |
public void masquerNbVotes();
|
|
|
44 |
|
| 629 |
aurelien |
45 |
public void reinitialiserVotes();
|
| 939 |
benjamin |
46 |
|
| 629 |
aurelien |
47 |
public void rafraichir(int moyenneVote, int nbVotes);
|
| 939 |
benjamin |
48 |
|
| 629 |
aurelien |
49 |
public void ajouterAuParent(HasWidgets composite);
|
| 1084 |
gduche |
50 |
|
|
|
51 |
public void afficherVotePrisEnCompte();
|
| 1085 |
gduche |
52 |
|
|
|
53 |
public HasWidgets getZoneFleur();
|
| 1086 |
gduche |
54 |
|
|
|
55 |
public HasText getZoneProtocole();
|
| 1088 |
gduche |
56 |
|
|
|
57 |
public void setNoteGenerale(int note);
|
| 629 |
aurelien |
58 |
}
|
| 939 |
benjamin |
59 |
|
| 629 |
aurelien |
60 |
private Vue vue;
|
| 423 |
gduche |
61 |
private Protocole protocole;
|
| 629 |
aurelien |
62 |
private Image image;
|
| 939 |
benjamin |
63 |
|
| 629 |
aurelien |
64 |
private int valeurVoteDefaut = -1;
|
|
|
65 |
private int valeurVoteUtilisateur = -1;
|
|
|
66 |
private int valeurVoteTotal = 0;
|
| 1085 |
gduche |
67 |
|
| 939 |
benjamin |
68 |
|
|
|
69 |
// TODO: on devrait passer un conteneur qui permet d'accéder à ces services
|
| 959 |
benjamin |
70 |
private VoteProtocoleService voteProtocoleService;
|
| 939 |
benjamin |
71 |
|
| 959 |
benjamin |
72 |
public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue, VoteProtocoleService voteProtocoleService) {
|
| 629 |
aurelien |
73 |
this.vue = vue;
|
| 423 |
gduche |
74 |
this.protocole = protocole;
|
| 629 |
aurelien |
75 |
this.image = image;
|
| 959 |
benjamin |
76 |
this.voteProtocoleService = voteProtocoleService;
|
| 1086 |
gduche |
77 |
|
|
|
78 |
String nomProtocole = protocole.getNom();
|
|
|
79 |
nomProtocole = nomProtocole.substring(0, 1).toUpperCase()+ nomProtocole.substring(1).toLowerCase();
|
|
|
80 |
vue.getZoneProtocole().setText(nomProtocole);
|
| 9 |
benjamin |
81 |
}
|
|
|
82 |
|
| 629 |
aurelien |
83 |
public void go(HasWidgets composite) {
|
|
|
84 |
vue.ajouterAuParent(composite);
|
| 423 |
gduche |
85 |
gererEvenements();
|
| 629 |
aurelien |
86 |
rafraichirVue();
|
| 9 |
benjamin |
87 |
}
|
| 939 |
benjamin |
88 |
|
| 423 |
gduche |
89 |
public void gererEvenements() {
|
|
|
90 |
vue.getVotes().addClickHandler(new ClickHandler() {
|
|
|
91 |
@Override
|
|
|
92 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
93 |
valeurVoteUtilisateur = vue.getValeurVote();
|
| 423 |
gduche |
94 |
vue.afficherBoutonVoter();
|
|
|
95 |
vue.afficherBoutonAnnuler();
|
|
|
96 |
vue.masquerNbVotes();
|
|
|
97 |
}
|
|
|
98 |
});
|
| 939 |
benjamin |
99 |
|
| 423 |
gduche |
100 |
vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
|
|
|
101 |
@Override
|
|
|
102 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
103 |
valeurVoteUtilisateur = valeurVoteDefaut;
|
| 423 |
gduche |
104 |
vue.masquerBoutonVoter();
|
|
|
105 |
vue.masquerBoutonAnnuler();
|
|
|
106 |
vue.afficherNbVotes();
|
|
|
107 |
vue.reinitialiserVotes();
|
|
|
108 |
}
|
|
|
109 |
});
|
| 939 |
benjamin |
110 |
|
| 423 |
gduche |
111 |
vue.getBoutonVoter().addClickHandler(new ClickHandler() {
|
|
|
112 |
@Override
|
|
|
113 |
public void onClick(ClickEvent event) {
|
|
|
114 |
enregistrerVote();
|
|
|
115 |
vue.masquerBoutonVoter();
|
|
|
116 |
vue.masquerBoutonAnnuler();
|
|
|
117 |
vue.afficherNbVotes();
|
|
|
118 |
vue.reinitialiserVotes();
|
| 1084 |
gduche |
119 |
vue.afficherVotePrisEnCompte();
|
| 423 |
gduche |
120 |
}
|
|
|
121 |
});
|
|
|
122 |
}
|
| 939 |
benjamin |
123 |
|
| 629 |
aurelien |
124 |
public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
|
|
|
125 |
this.valeurVoteUtilisateur = valeurVoteUtilisateur;
|
|
|
126 |
}
|
| 939 |
benjamin |
127 |
|
| 423 |
gduche |
128 |
public void enregistrerVote() {
|
| 939 |
benjamin |
129 |
final VoteProtocole voteProtocole = new VoteProtocole();
|
| 423 |
gduche |
130 |
voteProtocole.setProtocole(this.protocole);
|
| 629 |
aurelien |
131 |
voteProtocole.setVote(valeurVoteUtilisateur);
|
| 823 |
aurelien |
132 |
String idContributeur = CacheClient.getInstance().getUtilisateur().getId();
|
|
|
133 |
voteProtocole.setContributeur(idContributeur);
|
|
|
134 |
// TODO: Mettre un message de chargement pendant l'envoi du vote ?
|
|
|
135 |
// C'est très rapide mais bon
|
|
|
136 |
VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
|
| 939 |
benjamin |
137 |
|
| 823 |
aurelien |
138 |
@Override
|
| 939 |
benjamin |
139 |
public void surRetour(Void objetRetour) {
|
|
|
140 |
// TODO: voir si l'on affiche un message en cas de succès ?
|
| 823 |
aurelien |
141 |
image.ajouterVoteProtocole(voteProtocole);
|
|
|
142 |
rafraichirVue();
|
|
|
143 |
}
|
| 939 |
benjamin |
144 |
|
| 823 |
aurelien |
145 |
};
|
| 939 |
benjamin |
146 |
|
|
|
147 |
if (image.utilisateurAVotePourProtocole(this.protocole.getId() + "", idContributeur)) {
|
| 823 |
aurelien |
148 |
voteProtocoleService.modifierVote(image.getIdImage(), voteProtocole, vpc);
|
|
|
149 |
} else {
|
|
|
150 |
voteProtocoleService.ajouterVote(image.getIdImage(), voteProtocole, vpc);
|
|
|
151 |
}
|
| 629 |
aurelien |
152 |
}
|
| 939 |
benjamin |
153 |
|
| 629 |
aurelien |
154 |
public int getValeurVoteTotal() {
|
|
|
155 |
return valeurVoteTotal;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
public IsWidget getVue() {
|
|
|
159 |
return vue;
|
|
|
160 |
}
|
| 939 |
benjamin |
161 |
|
| 629 |
aurelien |
162 |
private void rafraichirVue() {
|
|
|
163 |
valeurVoteTotal = calculerMoyenneVotes();
|
| 1088 |
gduche |
164 |
VoteProtocole voteProtocole = image.getVotesProtocoles(protocole.getId()).get(CacheClient.getInstance().getUtilisateur().getId());
|
|
|
165 |
int voteUtilisateur = 0;
|
|
|
166 |
if (voteProtocole != null) {
|
|
|
167 |
voteUtilisateur = voteProtocole.getVote();
|
|
|
168 |
}
|
|
|
169 |
|
| 1085 |
gduche |
170 |
VoteFleurPresenteur presenteurFleur = new VoteFleurPresenteur(new VoteFleurVue());
|
|
|
171 |
presenteurFleur.setNote(valeurVoteTotal);
|
| 1088 |
gduche |
172 |
vue.setNoteGenerale(valeurVoteTotal);
|
| 1085 |
gduche |
173 |
presenteurFleur.go(vue.getZoneFleur());
|
| 1088 |
gduche |
174 |
vue.rafraichir(voteUtilisateur, image.getVotesProtocoles(protocole.getId()).size());
|
| 629 |
aurelien |
175 |
}
|
| 939 |
benjamin |
176 |
|
| 629 |
aurelien |
177 |
public int calculerMoyenneVotes() {
|
|
|
178 |
double valeurVote = 0;
|
|
|
179 |
int nbVote = 0;
|
|
|
180 |
for (Iterator<String> iterator = image.getVotesProtocoles(protocole.getId()).keySet().iterator(); iterator.hasNext();) {
|
|
|
181 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(iterator.next());
|
| 939 |
benjamin |
182 |
valeurVote += (double) imageCelValidationData.getVote() / 5;
|
| 629 |
aurelien |
183 |
nbVote++;
|
|
|
184 |
}
|
| 939 |
benjamin |
185 |
|
| 629 |
aurelien |
186 |
if (nbVote > 0) {
|
|
|
187 |
valeurVote /= nbVote;
|
|
|
188 |
valeurVote *= 5;
|
|
|
189 |
}
|
|
|
190 |
|
| 939 |
benjamin |
191 |
return (int) Math.round(valeurVote);
|
| 423 |
gduche |
192 |
}
|
| 9 |
benjamin |
193 |
}
|