Subversion Repositories eFlore/Applications.del

Rev

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