| 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);
|
| 1092 |
gduche |
58 |
|
|
|
59 |
public void afficherVoteModifie();
|
| 629 |
aurelien |
60 |
}
|
| 939 |
benjamin |
61 |
|
| 629 |
aurelien |
62 |
private Vue vue;
|
| 423 |
gduche |
63 |
private Protocole protocole;
|
| 629 |
aurelien |
64 |
private Image image;
|
| 939 |
benjamin |
65 |
|
| 629 |
aurelien |
66 |
private int valeurVoteDefaut = -1;
|
|
|
67 |
private int valeurVoteUtilisateur = -1;
|
|
|
68 |
private int valeurVoteTotal = 0;
|
| 1085 |
gduche |
69 |
|
| 939 |
benjamin |
70 |
|
|
|
71 |
// TODO: on devrait passer un conteneur qui permet d'accéder à ces services
|
| 959 |
benjamin |
72 |
private VoteProtocoleService voteProtocoleService;
|
| 939 |
benjamin |
73 |
|
| 959 |
benjamin |
74 |
public MoyenneVotePresenteur(Image image, Protocole protocole, Vue vue, VoteProtocoleService voteProtocoleService) {
|
| 629 |
aurelien |
75 |
this.vue = vue;
|
| 423 |
gduche |
76 |
this.protocole = protocole;
|
| 629 |
aurelien |
77 |
this.image = image;
|
| 959 |
benjamin |
78 |
this.voteProtocoleService = voteProtocoleService;
|
| 1086 |
gduche |
79 |
|
|
|
80 |
String nomProtocole = protocole.getNom();
|
|
|
81 |
nomProtocole = nomProtocole.substring(0, 1).toUpperCase()+ nomProtocole.substring(1).toLowerCase();
|
|
|
82 |
vue.getZoneProtocole().setText(nomProtocole);
|
| 9 |
benjamin |
83 |
}
|
|
|
84 |
|
| 629 |
aurelien |
85 |
public void go(HasWidgets composite) {
|
|
|
86 |
vue.ajouterAuParent(composite);
|
| 423 |
gduche |
87 |
gererEvenements();
|
| 629 |
aurelien |
88 |
rafraichirVue();
|
| 9 |
benjamin |
89 |
}
|
| 939 |
benjamin |
90 |
|
| 423 |
gduche |
91 |
public void gererEvenements() {
|
|
|
92 |
vue.getVotes().addClickHandler(new ClickHandler() {
|
|
|
93 |
@Override
|
|
|
94 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
95 |
valeurVoteUtilisateur = vue.getValeurVote();
|
| 423 |
gduche |
96 |
vue.afficherBoutonVoter();
|
|
|
97 |
vue.afficherBoutonAnnuler();
|
|
|
98 |
vue.masquerNbVotes();
|
|
|
99 |
}
|
|
|
100 |
});
|
| 939 |
benjamin |
101 |
|
| 423 |
gduche |
102 |
vue.getBoutonAnnuler().addClickHandler(new ClickHandler() {
|
|
|
103 |
@Override
|
|
|
104 |
public void onClick(ClickEvent event) {
|
| 629 |
aurelien |
105 |
valeurVoteUtilisateur = valeurVoteDefaut;
|
| 423 |
gduche |
106 |
vue.masquerBoutonVoter();
|
|
|
107 |
vue.masquerBoutonAnnuler();
|
|
|
108 |
vue.afficherNbVotes();
|
|
|
109 |
vue.reinitialiserVotes();
|
|
|
110 |
}
|
|
|
111 |
});
|
| 939 |
benjamin |
112 |
|
| 423 |
gduche |
113 |
vue.getBoutonVoter().addClickHandler(new ClickHandler() {
|
|
|
114 |
@Override
|
|
|
115 |
public void onClick(ClickEvent event) {
|
|
|
116 |
enregistrerVote();
|
|
|
117 |
vue.masquerBoutonVoter();
|
|
|
118 |
vue.masquerBoutonAnnuler();
|
|
|
119 |
vue.afficherNbVotes();
|
|
|
120 |
vue.reinitialiserVotes();
|
|
|
121 |
}
|
|
|
122 |
});
|
|
|
123 |
}
|
| 939 |
benjamin |
124 |
|
| 629 |
aurelien |
125 |
public void setValeurVoteUtilisateur(int valeurVoteUtilisateur) {
|
|
|
126 |
this.valeurVoteUtilisateur = valeurVoteUtilisateur;
|
|
|
127 |
}
|
| 939 |
benjamin |
128 |
|
| 423 |
gduche |
129 |
public void enregistrerVote() {
|
| 939 |
benjamin |
130 |
final VoteProtocole voteProtocole = new VoteProtocole();
|
| 423 |
gduche |
131 |
voteProtocole.setProtocole(this.protocole);
|
| 629 |
aurelien |
132 |
voteProtocole.setVote(valeurVoteUtilisateur);
|
| 823 |
aurelien |
133 |
String idContributeur = CacheClient.getInstance().getUtilisateur().getId();
|
|
|
134 |
voteProtocole.setContributeur(idContributeur);
|
|
|
135 |
// TODO: Mettre un message de chargement pendant l'envoi du vote ?
|
|
|
136 |
// C'est très rapide mais bon
|
|
|
137 |
VoteProtocoleCallback vpc = new VoteProtocoleCallback() {
|
| 939 |
benjamin |
138 |
|
| 823 |
aurelien |
139 |
@Override
|
| 939 |
benjamin |
140 |
public void surRetour(Void objetRetour) {
|
|
|
141 |
// TODO: voir si l'on affiche un message en cas de succès ?
|
| 823 |
aurelien |
142 |
image.ajouterVoteProtocole(voteProtocole);
|
|
|
143 |
rafraichirVue();
|
|
|
144 |
}
|
| 939 |
benjamin |
145 |
|
| 823 |
aurelien |
146 |
};
|
| 939 |
benjamin |
147 |
|
|
|
148 |
if (image.utilisateurAVotePourProtocole(this.protocole.getId() + "", idContributeur)) {
|
| 823 |
aurelien |
149 |
voteProtocoleService.modifierVote(image.getIdImage(), voteProtocole, vpc);
|
| 1092 |
gduche |
150 |
vue.afficherVoteModifie();
|
| 823 |
aurelien |
151 |
} else {
|
|
|
152 |
voteProtocoleService.ajouterVote(image.getIdImage(), voteProtocole, vpc);
|
| 1092 |
gduche |
153 |
vue.afficherVotePrisEnCompte();
|
| 823 |
aurelien |
154 |
}
|
| 629 |
aurelien |
155 |
}
|
| 939 |
benjamin |
156 |
|
| 629 |
aurelien |
157 |
public int getValeurVoteTotal() {
|
|
|
158 |
return valeurVoteTotal;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public IsWidget getVue() {
|
|
|
162 |
return vue;
|
|
|
163 |
}
|
| 939 |
benjamin |
164 |
|
| 629 |
aurelien |
165 |
private void rafraichirVue() {
|
|
|
166 |
valeurVoteTotal = calculerMoyenneVotes();
|
| 1088 |
gduche |
167 |
VoteProtocole voteProtocole = image.getVotesProtocoles(protocole.getId()).get(CacheClient.getInstance().getUtilisateur().getId());
|
|
|
168 |
int voteUtilisateur = 0;
|
|
|
169 |
if (voteProtocole != null) {
|
|
|
170 |
voteUtilisateur = voteProtocole.getVote();
|
|
|
171 |
}
|
|
|
172 |
|
| 1085 |
gduche |
173 |
VoteFleurPresenteur presenteurFleur = new VoteFleurPresenteur(new VoteFleurVue());
|
|
|
174 |
presenteurFleur.setNote(valeurVoteTotal);
|
| 1088 |
gduche |
175 |
vue.setNoteGenerale(valeurVoteTotal);
|
| 1085 |
gduche |
176 |
presenteurFleur.go(vue.getZoneFleur());
|
| 1088 |
gduche |
177 |
vue.rafraichir(voteUtilisateur, image.getVotesProtocoles(protocole.getId()).size());
|
| 629 |
aurelien |
178 |
}
|
| 939 |
benjamin |
179 |
|
| 629 |
aurelien |
180 |
public int calculerMoyenneVotes() {
|
|
|
181 |
double valeurVote = 0;
|
|
|
182 |
int nbVote = 0;
|
|
|
183 |
for (Iterator<String> iterator = image.getVotesProtocoles(protocole.getId()).keySet().iterator(); iterator.hasNext();) {
|
|
|
184 |
VoteProtocole imageCelValidationData = image.getVotesProtocoles(protocole.getId()).get(iterator.next());
|
| 939 |
benjamin |
185 |
valeurVote += (double) imageCelValidationData.getVote() / 5;
|
| 629 |
aurelien |
186 |
nbVote++;
|
|
|
187 |
}
|
| 939 |
benjamin |
188 |
|
| 629 |
aurelien |
189 |
if (nbVote > 0) {
|
|
|
190 |
valeurVote /= nbVote;
|
|
|
191 |
valeurVote *= 5;
|
|
|
192 |
}
|
|
|
193 |
|
| 939 |
benjamin |
194 |
return (int) Math.round(valeurVote);
|
| 423 |
gduche |
195 |
}
|
| 9 |
benjamin |
196 |
}
|