Subversion Repositories eFlore/Applications.del

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2212 arthur 1
package org.tela_botanica.del.client.composants.moteurrecherche;
2
 
3
import java.util.Map;
4
 
5
import org.tela_botanica.del.client.cache.CacheClient;
6
import org.tela_botanica.del.client.composants.formulaires.autocompletion.AutoCompletionComboBoxPresenteur;
7
import org.tela_botanica.del.client.composants.formulaires.autocompletion.AutoCompletionComboBoxVue;
8
import org.tela_botanica.del.client.composants.presenteur.Presenteur;
9
import org.tela_botanica.del.client.composants.rss.RssPresenteur;
10
import org.tela_botanica.del.client.composants.rss.RssVue;
11
import org.tela_botanica.del.client.config.Config;
12
import org.tela_botanica.del.client.modeles.InformationsRecherche;
13
import org.tela_botanica.del.client.modeles.ModeRecherche;
14
import org.tela_botanica.del.client.services.ReferentielService;
15
import org.tela_botanica.del.client.services.rest.PaysService;
16
import org.tela_botanica.del.client.services.rest.PaysServiceConcret;
17
import org.tela_botanica.del.client.services.rest.async.PaysCallback;
18
import org.tela_botanica.del.client.utils.InfosNomPourAutocompletion;
19
import org.tela_botanica.del.client.utils.UtilitairesAutoCompletionService;
20
 
21
import com.google.gwt.event.dom.client.ClickEvent;
22
import com.google.gwt.event.dom.client.ClickHandler;
23
import com.google.gwt.event.dom.client.HasClickHandlers;
24
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
25
import com.google.gwt.event.dom.client.KeyCodes;
26
import com.google.gwt.event.dom.client.KeyPressEvent;
27
import com.google.gwt.event.dom.client.KeyPressHandler;
28
import com.google.gwt.http.client.Response;
29
import com.google.gwt.user.client.ui.HasWidgets;
30
import com.google.gwt.user.client.ui.IsWidget;
31
 
32
public abstract class MoteurRecherchePresenteur extends Presenteur {
33
 
34
	public abstract interface Vue extends IsWidget {
35
		public void ajouterVue(HasWidgets composite);
36
 
37
		public HasClickHandlers getLienRechercheAvancee();
38
 
39
		public void basculerAffichageZoneCache();
40
 
41
		public HasClickHandlers getBoutonRechercheSimple();
42
 
43
		public HasClickHandlers getBoutonFermer();
44
 
45
		public HasClickHandlers getBoutonVider();
46
 
47
		public HasClickHandlers getBoutonRechercheAvancee();
48
 
49
		public HasKeyPressHandlers getChampSaisie();
50
 
51
		public HasClickHandlers getChampSaisieCliquable();
52
 
53
		public String getValeurRechercheSimple();
54
 
55
		public String getLabelRecherche();
56
 
57
		public String getContientMots();
58
 
59
		public boolean getPnInscritsSeulement();
60
 
61
		public String getDepartement();
62
 
63
		public HasWidgets getCommune();
64
 
65
		public HasWidgets getTaxon();
66
 
67
		public String getFamille();
68
 
69
		public String getGenre();
70
 
71
		public String getMotCle();
72
 
73
		public String getAuteur();
74
 
75
		public String getDate();
76
 
77
		public String getPays();
78
 
79
		public void setValeurRechercheSimple(String valeurRecherche);
80
 
81
		public void setContientMots(String mots);
82
 
83
		public void setPnInscritsSeulement(boolean valeur);
84
 
85
		public void setValeurDepartement(String dpt);
86
 
87
		public void chargerValeursRecherchePrecedente(InformationsRecherche informationsRecherche);
88
 
89
		public void focusSaisie();
90
 
91
		public void nettoyer();
92
 
93
		public HasKeyPressHandlers getChampsFamille();
94
 
95
		public HasKeyPressHandlers getChampsGenre();
96
 
97
		public HasKeyPressHandlers getChampsMotCle();
98
 
99
		public HasKeyPressHandlers getChampsMotCleDel();
100
 
101
		public HasKeyPressHandlers getChampsMotCleCel();
102
 
103
		public HasKeyPressHandlers getChampsAuteur();
104
 
105
		public HasKeyPressHandlers getChampsDate();
106
 
107
		public HasKeyPressHandlers getChampsDepartement();
108
 
109
		public HasKeyPressHandlers getChampsContientMots();
110
 
111
		public HasWidgets getZoneRss();
112
 
113
		public String getReferentiel();
114
 
115
		public void setReferentielLectureSeule(boolean lectureSeule);
116
 
117
		void remplirListeReferentiels(Map<String, String> listeReferentiels);
118
 
119
		public String getMotCleDel();
120
 
121
		public String getMotCleCel();
122
 
123
		public void cacherChampsTagsImage();
124
		public void cacherChampsTagsObs();
125
 
126
		void remplirListePays(Map<String, String> listePays);
127
 
128
	}
129
 
130
	private Vue vue;
131
	private Config config;
132
	private ModeRecherche modeRecherche;
133
	private RssPresenteur presenteurRss;
134
 
135
	public enum TypeMoteur {
136
		SIMPLE, AVANCEE
137
	};
138
 
139
	private TypeMoteur typeMoteur = TypeMoteur.SIMPLE;
140
 
141
	private AutoCompletionComboBoxPresenteur completionTaxonsPresenteur = null;
142
	private AutoCompletionComboBoxPresenteur completionCommunesPresenteur = null;
143
 
144
	public MoteurRecherchePresenteur(Vue vue, ModeRecherche mode) {
145
		initialiser(vue, mode, new Config());
146
	}
147
 
148
	public MoteurRecherchePresenteur(AutoCompletionComboBoxPresenteur presenteurAutoCompletionTaxon, AutoCompletionComboBoxPresenteur presenteurAutoCompletionCommunes, Vue vue, ModeRecherche mode, Config configuration) {
149
		completionTaxonsPresenteur = presenteurAutoCompletionTaxon;
150
		completionCommunesPresenteur = presenteurAutoCompletionCommunes;
151
		initialiser(vue, mode, configuration);
152
	}
153
 
154
	private void initialiser(Vue vue, ModeRecherche mode, Config configuration) {
155
 
156
		//TODO : fixer le flux avant de le mettre en place
157
		//this.presenteurRss = new RssPresenteur(new RssVue(), CacheClient.getInstance().getInformationsRechercheObservation(), "S'abonner au flux rss des résultats de cette recherche");
158
		//presenteurRss.go(vue.getZoneRss());
159
 
160
		this.vue = vue;
161
		setMode(mode);
162
		this.config = configuration;
163
		if (completionTaxonsPresenteur == null)
164
			this.creerCompletionTaxons();
165
		if (completionCommunesPresenteur == null)
166
			this.creerCompletionCommunes();
167
		gererEvenements();
168
 
169
		vue.remplirListeReferentiels(ReferentielService.getReferentiels());
170
		if(CacheClient.getInstance().getReferentielNonModifiable()) {
171
			vue.setReferentielLectureSeule(true);
172
		}
173
 
174
		if(CacheClient.getInstance().getListePays() != null) {
175
			vue.remplirListePays(CacheClient.getInstance().getListePays());
176
		} else {
177
			PaysService paysService = new PaysServiceConcret();
178
			paysService.getPays(new PaysCallback() {
179
				@Override
180
				public void surRetour(Map<String, String> listePays) {
181
					CacheClient.getInstance().setListePays(listePays);
182
					MoteurRecherchePresenteur.this.vue.remplirListePays(listePays);
183
				}
184
			});
185
		}
186
 
187
		if(estPourRechercheImages()) {
188
			vue.cacherChampsTagsObs();
189
		} else {
190
			vue.cacherChampsTagsImage();
191
		}
192
	}
193
 
194
	public ModeRecherche getMode() {
195
		return modeRecherche;
196
	}
197
 
198
	private void setMode(ModeRecherche mode) {
199
		modeRecherche = mode;
200
	}
201
 
202
	public TypeMoteur getTypeMoteur() {
203
		return this.typeMoteur;
204
	}
205
 
206
	public void setTypeMoteur(TypeMoteur typeMoteur) {
207
		this.typeMoteur = typeMoteur;
208
	}
209
 
210
	public boolean estPourRechercheImages() {
211
		return (modeRecherche == ModeRecherche.MODE_IMAGE);
212
	}
213
 
214
	public boolean estPourRechercheObservations() {
215
		return (modeRecherche == ModeRecherche.MODE_OBSERVATION);
216
	}
217
 
218
	private void creerCompletionTaxons() {
219
		String url = this.config.getServiceBaseUrl() + "nomstaxons";
220
		completionTaxonsPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
221
			protected String effectuerPreTraitementChaineRequete(String requete) {
222
				// si aucun référentiel selectionné, pas de requete
223
				if(vue.getReferentiel() != null && !vue.getReferentiel().equals("")) {
224
					requete = "?masque.nom=" + requete + "&masque.referentiel=" + vue.getReferentiel();
225
					return requete;
226
				} else {
227
					return null;
228
				}
229
				// A décommenter lors de l'utilisation des web services eflore
230
				// return
231
				// RetourAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceEflore(requete);
232
			}
233
 
234
			@Override
235
			protected InfosNomPourAutocompletion[] parserResultatRequete(Response response) {
236
				return UtilitairesAutoCompletionService.parserResultatRetourSimple(response);
237
				// A décommenter lors de l'utilisation des web services eflore
238
				// return RetourAutoCompletionService.parserRetourOss(response);
239
			}
240
		};
241
	}
242
 
243
	private void creerCompletionCommunes() {
244
		String url = this.config.getServiceBaseUrl() + "communes";
245
		completionCommunesPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
246
			protected String effectuerPreTraitementChaineRequete(String requete) {
247
				requete = "?masque.nom=" + requete;
248
				return requete;
249
			}
250
 
251
			@Override
252
			protected void surSelectionSuggestion(String suggestion) {
253
				setValeur(suggestion);
254
				collecterInfosRecherche();
255
				setValeur(getInformationsRechercheEnCache().getCommune());
256
				vue.setValeurDepartement(getInformationsRechercheEnCache().getDepartement());
257
			}
258
 
259
			@Override
260
			protected InfosNomPourAutocompletion[] parserResultatRequete(Response response) {
261
				return UtilitairesAutoCompletionService.parserResultatRetourSimple(response);
262
			}
263
		};
264
	}
265
 
266
	@Override
267
	public void go(HasWidgets composite) {
268
		afficherRequeteEtLancerRecherche();
269
		completionTaxonsPresenteur.go(vue.getTaxon());
270
		completionCommunesPresenteur.go(vue.getCommune());
271
		vue.ajouterVue(composite);
272
		vue.focusSaisie();
273
	}
274
 
275
	@Override
276
	protected void gererEvenements() {
277
		vue.getLienRechercheAvancee().addClickHandler(new ClickHandler() {
278
			public void onClick(ClickEvent event) {
279
				setTypeMoteur(TypeMoteur.AVANCEE);
280
				vue.setContientMots(vue.getValeurRechercheSimple());
281
				vue.basculerAffichageZoneCache();
282
			}
283
		});
284
 
285
		vue.getBoutonRechercheSimple().addClickHandler(new ClickHandler() {
286
			public void onClick(ClickEvent event) {
287
				setTypeMoteur(TypeMoteur.SIMPLE);
288
				collecterInfosRecherche();
289
				afficherRequeteEtLancerRecherche();
290
			}
291
		});
292
 
293
		vue.getBoutonFermer().addClickHandler(new ClickHandler() {
294
			public void onClick(ClickEvent event) {
295
				vue.setValeurRechercheSimple(vue.getContientMots());
296
				vue.basculerAffichageZoneCache();
297
			}
298
		});
299
 
300
		vue.getBoutonVider().addClickHandler(new ClickHandler() {
301
			public void onClick(ClickEvent event) {
302
				nettoyerChamps();
303
				InformationsRecherche infosRecherche = new InformationsRecherche();
304
				if (estPourRechercheImages()) {
305
					CacheClient.getInstance().setInformationsRechercheImage(infosRecherche);
306
				} else if (estPourRechercheObservations()) {
307
					CacheClient.getInstance().setInformationsRechercheObservation(infosRecherche);
308
				}
309
			}
310
		});
311
 
312
		vue.getBoutonRechercheAvancee().addClickHandler(new ClickHandler() {
313
			public void onClick(ClickEvent event) {
314
				setTypeMoteur(TypeMoteur.AVANCEE);
315
				collecterInfosRecherche();
316
				vue.basculerAffichageZoneCache();
317
				afficherRequeteEtLancerRecherche();
318
			}
319
		});
320
 
321
		vue.getChampSaisie().addKeyPressHandler(new KeyPressHandler() {
322
			public void onKeyPress(KeyPressEvent event) {
323
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
324
					setTypeMoteur(TypeMoteur.SIMPLE);
325
					collecterInfosRecherche();
326
					afficherRequeteEtLancerRecherche();
327
				}
328
			}
329
		});
330
 
331
		vue.getChampSaisieCliquable().addClickHandler(new ClickHandler() {
332
			public void onClick(ClickEvent event) {
333
				if (vue.getValeurRechercheSimple().equals(vue.getLabelRecherche())) {
334
					vue.setValeurRechercheSimple("");
335
				}
336
			}
337
		});
338
 
339
		vue.getChampsAuteur().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
340
		vue.getChampsDate().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
341
		vue.getChampsFamille().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
342
		vue.getChampsGenre().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
343
		vue.getChampsMotCle().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
344
		vue.getChampsMotCleCel().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
345
		vue.getChampsMotCleDel().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
346
		vue.getChampsDepartement().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
347
		vue.getChampsContientMots().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
348
 
349
	}
350
 
351
	public KeyPressHandler creerGestionnaireEvenementToucheEntree() {
352
		return new KeyPressHandler() {
353
			public void onKeyPress(KeyPressEvent event) {
354
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
355
					vue.basculerAffichageZoneCache();
356
					collecterInfosRecherche();
357
					afficherRequeteEtLancerRecherche();
358
				}
359
			}
360
		};
361
	}
362
 
363
	private void nettoyerChamps() {
364
		completionCommunesPresenteur.nettoyer();
365
		completionTaxonsPresenteur.nettoyer();
366
		vue.nettoyer();
367
	}
368
 
369
	public void collecterInfosRecherche() {
370
		InformationsRecherche informationRecherche = new InformationsRecherche();
371
		informationRecherche.setRechercheLibre(this.getRechercheLibre());
372
		if (this.typeMoteur == TypeMoteur.AVANCEE) {
373
			informationRecherche.setTaxon(completionTaxonsPresenteur.getValeur());
374
			informationRecherche.setDepartement(vue.getDepartement());
375
			informationRecherche.setCommune(completionCommunesPresenteur.getValeur());
376
			informationRecherche.setFamille(vue.getFamille());
377
			informationRecherche.setGenre(vue.getGenre());
378
			if(estPourRechercheObservations()) {
379
				informationRecherche.setMotClef(vue.getMotCle());
380
			} else {
381
				informationRecherche.setMotClefDel(vue.getMotCleDel());
382
				informationRecherche.setMotClefCel(vue.getMotCleCel());
383
			}
384
			informationRecherche.setTag(vue.getMotCle());
385
			informationRecherche.setAuteur(vue.getAuteur());
386
			informationRecherche.setDate(vue.getDate());
387
			informationRecherche.setReferentiel(vue.getReferentiel());
388
			informationRecherche.setPays(vue.getPays());
389
			informationRecherche.setPnInscritsSeulement(vue.getPnInscritsSeulement());
390
		}
391
 
392
		if(CacheClient.getInstance().getReferentielNonModifiable()) {
393
			informationRecherche.setReferentiel(CacheClient.getInstance().getReferentielCourant());
394
		}
395
 
396
		if (estPourRechercheImages()) {
397
			CacheClient.getInstance().setPageCouranteRechercheImages(1);
398
			CacheClient.getInstance().setInformationsRechercheImage(informationRecherche);
399
		} else if (estPourRechercheObservations()) {
400
			CacheClient.getInstance().setPageCouranteRechercheObservations(1);
401
			CacheClient.getInstance().setInformationsRechercheObservation(informationRecherche);
402
		}
403
	}
404
 
405
	private String getRechercheLibre() {
406
		String rechercheLibre = "";
407
		switch (this.typeMoteur) {
408
		case SIMPLE:
409
			rechercheLibre = vue.getValeurRechercheSimple();
410
			break;
411
		case AVANCEE:
412
			rechercheLibre = vue.getContientMots();
413
			break;
414
		default:
415
			// TODO : voir comment gérer les exceptions proprement
416
			// throw new
417
			// Exception("Le type de moteur de recherche indiqué n'est pas disponible");
418
		}
419
		return rechercheLibre;
420
	}
421
 
422
	private InformationsRecherche getInformationsRechercheEnCache() {
423
		if (estPourRechercheImages()) {
424
			return CacheClient.getInstance().getInformationsRechercheImage();
425
		} else if (estPourRechercheObservations()) {
426
			return CacheClient.getInstance().getInformationsRechercheObservation();
427
		}
428
		return null;
429
	}
430
 
431
	public void afficherRequeteEtLancerRecherche() {
432
		InformationsRecherche informationsRecherche = getInformationsRechercheEnCache();
433
		if (informationsRecherche != null) {
434
			completionTaxonsPresenteur.setValeur(informationsRecherche.getTaxon());
435
			completionCommunesPresenteur.setValeur(informationsRecherche.getCommune());
436
			vue.chargerValeursRecherchePrecedente(informationsRecherche);
437
		}
438
		lancerRecherche();
439
		//presenteurRss.genererLien(informationsRecherche);
440
	}
441
 
442
	public abstract void lancerRecherche();
443
}