Subversion Repositories eFlore/Applications.del

Rev

Rev 1222 | Rev 1283 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1196 gduche 1
package org.tela_botanica.del.client.vues.pictoflora.moteur;
2
 
1203 gduche 3
import java.util.List;
4
 
1196 gduche 5
import javax.validation.Configuration;
6
 
7
import org.tela_botanica.del.client.cache.CacheClient;
8
import org.tela_botanica.del.client.composants.moteurrecherche.MoteurRecherchePresenteur;
9
import org.tela_botanica.del.client.composants.moteurrecherche.MoteurRechercheVue;
10
import org.tela_botanica.del.client.composants.partageurl.PartageUrlPresenteur;
11
import org.tela_botanica.del.client.composants.partageurl.PartageUrlVue;
12
import org.tela_botanica.del.client.config.Config;
13
import org.tela_botanica.del.client.modeles.ModeRecherche;
1203 gduche 14
import org.tela_botanica.del.client.modeles.Protocole;
1196 gduche 15
import org.tela_botanica.del.client.modeles.ProtocoleServiceResultat;
1203 gduche 16
import org.tela_botanica.del.client.navigation.evenement.BusEvenementiel;
17
import org.tela_botanica.del.client.navigation.evenement.changementprotocole.EvenementChangementProtocole;
1196 gduche 18
import org.tela_botanica.del.client.services.rest.ImageServiceConcret;
19
import org.tela_botanica.del.client.services.rest.ProtocoleService;
20
import org.tela_botanica.del.client.services.rest.ProtocoleServiceConcret;
21
import org.tela_botanica.del.client.services.rest.async.ProtocolesCallback;
1222 gduche 22
import org.tela_botanica.del.client.utils.URLUtils;
1196 gduche 23
import org.tela_botanica.del.client.vues.pictoflora.resultats.ResultatPictofloraPresenteur;
24
import org.tela_botanica.del.client.vues.pictoflora.resultats.ResultatPictofloraVue;
25
 
1222 gduche 26
import com.google.gwt.core.shared.GWT;
1203 gduche 27
import com.google.gwt.event.dom.client.ChangeEvent;
28
import com.google.gwt.event.dom.client.ChangeHandler;
1196 gduche 29
import com.google.gwt.event.dom.client.ClickEvent;
30
import com.google.gwt.event.dom.client.ClickHandler;
1203 gduche 31
import com.google.gwt.event.dom.client.HasChangeHandlers;
1196 gduche 32
import com.google.gwt.event.dom.client.HasClickHandlers;
33
import com.google.gwt.user.client.Window;
34
import com.google.gwt.user.client.ui.HasWidgets;
35
import com.google.gwt.user.client.ui.IsWidget;
1203 gduche 36
import com.google.gwt.user.client.ui.ListBox;
1196 gduche 37
import com.google.gwt.user.client.ui.Panel;
38
 
39
public class MoteurPictofloraPresenteur {
40
 
41
	public interface Vue extends IsWidget {
42
		public HasWidgets getZoneResultats();
43
 
44
		public HasWidgets getZoneRecherche();
45
 
46
		public void ajouterVue(HasWidgets composite);
47
 
1203 gduche 48
		public void setListeProtocoles(ListBox listeProtocoles);
49
 
50
		public HasChangeHandlers getListeProtocoles();
51
 
52
		public void ajouterProtocole(String NomProtocole, String idProtocole);
53
 
54
		public void selectionnerProtocole(int index);
55
 
1222 gduche 56
		public void selectionnerProtocoleParProtocole(Protocole protocole);
57
 
1203 gduche 58
		public String getNomProtocoleSelectionne();
59
 
60
		public int getIdProtocoleSelectionne();
61
 
62
		public void ajouterProtocoles(List<Protocole> protocoles);
63
 
64
		public void mettreAJourDescriptionProtocoleCourant(Protocole protocoleCourant);
65
 
1196 gduche 66
	}
67
 
68
	private Vue vue;
69
	private ProtocoleService protocoleService;
1203 gduche 70
	private List<Protocole> protocoles;
1196 gduche 71
 
72
	/**
73
	 * Constructeur
74
	 * */
75
	public MoteurPictofloraPresenteur(Vue vue, ProtocoleService protocoleService) {
1222 gduche 76
 
1196 gduche 77
		this.vue = vue;
78
		this.protocoleService = protocoleService;
1203 gduche 79
		chargerProtocoles();
1196 gduche 80
 
81
		// TODO: le code ci dessous sert à ne pas recharger les protocoles
82
		// à chaque fois, voir si on peut le factoriser quelque part
83
		if (CacheClient.getInstance().getProtocoleCourant() == null) {
84
			if (CacheClient.getInstance().getListeProtocoles() == null) {
85
				protocoleService.getProtocoles(new ProtocolesCallback() {
86
 
87
					@Override
88
					public void surRetour(ProtocoleServiceResultat protocolesRecus) {
89
						CacheClient.getInstance().setListeProtocoles(protocolesRecus.getProtocoles());
90
						if (protocolesRecus.getProtocoles().size() > 0) {
1226 gduche 91
 
92
							//Vérifier qu'on n'a pas demandé un protocole particulier en paramètre
93
							String parametre = URLUtils.getURLSpecialParameterValue();
94
							if (parametre != null) {
95
								for (Protocole protocoleCourant : protocoles) {
96
										if (parametre.equals(String.valueOf(protocoleCourant.getId()))) {
97
											CacheClient.getInstance().setProtocoleCourant(protocoleCourant);
98
										}
99
								}
100
							} else {
101
								CacheClient.getInstance().setProtocoleCourant(protocolesRecus.getProtocoles().get(0));
102
							}
1196 gduche 103
						} else {
104
							// TODO afficher message indiquant que la liste des
105
							// protocoles de votes n'est pas fourni
106
						}
107
					}
108
				});
109
			} else {
110
				CacheClient.getInstance().setProtocoleCourant(CacheClient.getInstance().getListeProtocoles().get(0));
111
			}
112
		}
113
		gererEvenements();
114
	}
115
 
116
	private void gererEvenements() {
1203 gduche 117
 
118
		vue.getListeProtocoles().addChangeHandler(new ChangeHandler() {
119
			@Override
120
			public void onChange(ChangeEvent event) {
121
				surChangementProtocole();
122
			}
123
		});
124
 
1196 gduche 125
	}
1203 gduche 126
 
127
	private void chargerProtocoles() {
128
		// test pour ne pas charger les protocoles déjà chargés
129
		// TODO: faire un systeme de cache gérés par les web service eux même
130
		if (CacheClient.getInstance().getListeProtocoles() == null) {
131
			protocoleService.getProtocoles(new ProtocolesCallback() {
1196 gduche 132
 
1203 gduche 133
				@Override
134
				public void surRetour(ProtocoleServiceResultat protocolesRecus) {
135
					protocoles = protocolesRecus.getProtocoles();
136
					remplirListeProtocole(protocoles);
1219 aurelien 137
					surChangementProtocole();
1203 gduche 138
				}
139
			});
140
		} else {
141
			protocoles = CacheClient.getInstance().getListeProtocoles();
142
			remplirListeProtocole(CacheClient.getInstance().getListeProtocoles());
143
		}
144
	}
145
 
146
	public void surChangementProtocole() {
147
		Protocole protocoleCourant = null;
148
		for (Protocole protocole : protocoles) {
149
			if (protocole.getId() == vue.getIdProtocoleSelectionne()) {
150
				protocoleCourant = protocole;
151
			}
152
		}
153
		vue.mettreAJourDescriptionProtocoleCourant(protocoleCourant);
154
		CacheClient.getInstance().setProtocoleCourant(protocoleCourant);
155
		EvenementChangementProtocole evenement = new EvenementChangementProtocole(protocoleCourant);
156
		BusEvenementiel.getInstance().fireEvent(evenement);
157
	}
158
 
159
	private void remplirListeProtocole(List<Protocole> protocoles) {
1222 gduche 160
		//si un paramètre est passé dans l'url, on sélectionne un protocole
161
		String parametre = URLUtils.getURLSpecialParameterValue();
162
		if (parametre != null) {
163
			for (Protocole protocoleCourant : protocoles) {
164
					if (parametre.equals(String.valueOf(protocoleCourant.getId()))) {
165
						CacheClient.getInstance().setProtocoleCourant(protocoleCourant);
166
					}
167
			}
168
		}
1203 gduche 169
		vue.ajouterProtocoles(protocoles);
170
		if (CacheClient.getInstance().getProtocoleCourant() == null) {
171
			vue.selectionnerProtocole(0);
172
			vue.mettreAJourDescriptionProtocoleCourant(protocoles.get(0));
173
		} else {
1222 gduche 174
			vue.selectionnerProtocoleParProtocole(CacheClient.getInstance().getProtocoleCourant());
1203 gduche 175
			vue.mettreAJourDescriptionProtocoleCourant(CacheClient.getInstance().getProtocoleCourant());
176
		}
177
	}
178
 
1196 gduche 179
	public void go(HasWidgets composite) {
180
		vue.ajouterVue(composite);
181
		chargerMoteurRechercheAvancee();
182
	}
183
 
184
	public void chargerMoteurRechercheAvancee() {
185
		MoteurRecherchePresenteur presenteurRecherche = new MoteurRecherchePresenteur(new MoteurRechercheVue(""), ModeRecherche.MODE_IMAGE) {
186
			public void lancerRecherche() {
187
				chercherImages();
188
			}
189
		};
190
		presenteurRecherche.go(vue.getZoneRecherche());
191
	}
192
 
193
	public void chercherImages() {
194
		vue.getZoneResultats().clear();
195
		new ResultatPictofloraPresenteur(new ImageServiceConcret(), new ProtocoleServiceConcret(), new ResultatPictofloraVue()).go(vue.getZoneResultats());
196
	}
197
 
198
	public HasWidgets getZoneResultats() {
199
		return vue.getZoneResultats();
200
	}
201
}