Subversion Repositories eFlore/Applications.del

Rev

Rev 2139 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1934 aurelien 1
package org.tela_botanica.del.client.cache;
2
 
3
import java.util.Arrays;
1995 aurelien 4
import java.util.HashMap;
1934 aurelien 5
import java.util.List;
1986 aurelien 6
import java.util.Map;
1934 aurelien 7
 
8
import org.tela_botanica.del.client.config.Config;
9
import org.tela_botanica.del.client.gestionhistorique.ConstantesNavigation;
10
import org.tela_botanica.del.client.modeles.Image;
11
import org.tela_botanica.del.client.modeles.InformationsRecherche;
12
import org.tela_botanica.del.client.modeles.ModeTri;
13
import org.tela_botanica.del.client.modeles.Observation;
14
import org.tela_botanica.del.client.modeles.Protocole;
15
import org.tela_botanica.del.client.modeles.Utilisateur;
16
import org.tela_botanica.del.client.utils.URLUtils;
2139 mathias 17
import org.tela_botanica.del.client.Del;
1934 aurelien 18
 
19
import com.google.gwt.core.client.GWT;
2054 aurelien 20
import com.google.gwt.dom.client.Document;
1934 aurelien 21
import com.google.gwt.user.client.Window;
22
import com.google.gwt.user.client.Window.Location;
23
 
24
public class CacheClient {
25
 
26
	private List<Protocole> listeProtocoles;
27
 
28
	private Observation observationCourante;
29
	private Image imageCourante;
30
	private String taxonPourRechercheEflore;
31
	private String idProtocoleEnAttente = null;
32
	private Protocole protocoleCourant;
33
	private String referentielCourant;
34
	private int numPageRechercheImage = 0;
35
	private int nbTotalImagesRecherchees = 0;
36
 
37
	private boolean referentielNonModifiable = false;
38
 
39
	private int pasPagination = 12;
40
	private int pageCouranteRechercheImage = 1;
41
	private int pageCouranteRechercheObservations = 1;
42
	private InformationsRecherche informationsRechercheImage;
43
	private InformationsRecherche informationsRechercheObservation;
44
	private Utilisateur utilisateur;
45
	private String pageCourante = "";
46
	private String statut;
47
	private String[] statutsPossibles = {
48
		ConstantesNavigation.PARAM_TYPE_TOUS,
49
		ConstantesNavigation.PARAM_TYPE_A_DETERMINER,
1982 aurelien 50
		ConstantesNavigation.PARAM_TYPE_A_CONFIRMER,
2050 aurelien 51
		ConstantesNavigation.PARAM_TYPE_VALIDEES,
52
		ConstantesNavigation.PARAM_TYPE_MONACTIVITE
1934 aurelien 53
	};
54
	private ModeTri modeTri = ModeTri.TRI_ASCENDANT;
55
 
2054 aurelien 56
	private static String titreOngletNavigateur = "";
57
 
1995 aurelien 58
	// Pour plus de simplicité on stocke une liste de pays où
59
	// la clé est le code iso et la valeur le pays
60
	// et une autre ou c'est l'inverse
1986 aurelien 61
	private Map<String, String> listePays;
1995 aurelien 62
	private Map<String, String> listePaysInversee;
1986 aurelien 63
 
1934 aurelien 64
	public void supprimerFiltreStatut() {
65
		this.statut = null;
66
	}
67
 
68
	public void setFiltreStatut(String statut) {
69
		this.statut = statut;
70
	}
71
 
72
	public String getFiltreStatut() {
73
		return this.statut;
74
	}
75
 
76
	public String getPageCourante() {
77
		return pageCourante;
78
	}
79
 
80
	public void setPageCourante(String pageCourante) {
81
		pageCourante = pageCourante.replaceAll("#", "");
82
		pageCourante = pageCourante.replaceAll("page_validation", ConstantesNavigation.PAGE_DETAIL_OBS);
83
		pageCourante = pageCourante.replaceAll("page_validation_pictoflora", ConstantesNavigation.PAGE_DETAIL_IMG);
84
		this.pageCourante = pageCourante;
85
	}
86
 
87
	private static CacheClient instance;
88
 
89
	private CacheClient() {
90
		instance = this;
91
	}
92
 
93
	public void initialiserAvecParametres() {
94
		String urlCourante = Window.Location.getHref();
95
		Config config = new Config();
96
		String urlAppliImg = config.getUrl("pictoflora");
97
 
98
		setPageCourante(Location.getHash());
99
 
100
		String rechercheLibre = Location.getParameter("masque");
101
		String famille = Location.getParameter("masque.famille");
102
		String taxon = Location.getParameter("masque.ns");
103
		String genre = Location.getParameter("masque.genre");
104
		String commune = Location.getParameter("masque.commune");
105
		String dept = Location.getParameter("masque.departement");
106
		String auteur = Location.getParameter("masque.auteur");
107
		String date = Location.getParameter("masque.date");
108
		String tag = Location.getParameter("masque.tag");
109
		String tagCel = Location.getParameter("masque.tag_cel");
110
		String tagDel = Location.getParameter("masque.tag_pictoflora");
111
		String referentiel = Location.getParameter("masque.referentiel");
112
		String protocole = Location.getParameter("protocole");
113
		String statutParam = Location.getParameter("masque.type");
1986 aurelien 114
		String paysParam = Location.getParameter("masque.pays");
2138 mathias 115
 
116
		String pnInscritsSeulement = Location.getParameter("masque.pninscritsseulement");
2139 mathias 117
		// filtrage par défaut
118
		boolean pnInscritsSeulementBool = true;
119
		if (pnInscritsSeulement != null) {
120
			// un jour on aura "0" et ça annulera le paramètre par défaut
121
			pnInscritsSeulementBool = pnInscritsSeulement.equals("1");
122
		}
2138 mathias 123
 
1934 aurelien 124
		String page = Location.getParameter("page");
125
		Integer pageInt = null;
126
		try {
127
			pageInt = Integer.parseInt(page);
128
		} catch (Exception e) {
129
			pageInt = null;
130
		}
131
 
132
		String pas = Location.getParameter("pas");
133
		Integer pasInt = null;
134
		if (pas != null) {
135
			try {
136
				pasInt = Integer.parseInt(pas);
137
			} catch (Exception e) {
138
				pasInt = null;
139
			}
140
		}
141
 
142
		InformationsRecherche rechercheParArguments = new InformationsRecherche();
143
		rechercheParArguments.setRechercheLibre(rechercheLibre);
144
		rechercheParArguments.setFamille(famille);
145
		rechercheParArguments.setTaxon(taxon);
146
		rechercheParArguments.setGenre(genre);
147
		rechercheParArguments.setCommune(commune);
148
		rechercheParArguments.setDepartement(dept);
149
		rechercheParArguments.setAuteur(auteur);
150
		rechercheParArguments.setDate(date);
151
		rechercheParArguments.setTag(tag);
1986 aurelien 152
		rechercheParArguments.setPays(paysParam);
2138 mathias 153
		rechercheParArguments.setPnInscritsSeulement(pnInscritsSeulementBool);
1934 aurelien 154
 
155
		if (protocole != null && !protocole.equals("")) {
156
			CacheClient.getInstance().setIdProtocoleEnAttente(protocole);
157
			rechercheParArguments.setIdProtocoleSelectionne(protocole);
158
		} else if (URLUtils.getURLSpecialParameterValue() != null) {
159
			CacheClient.getInstance().setIdProtocoleEnAttente(URLUtils.getURLSpecialParameterValue());
160
			rechercheParArguments.setIdProtocoleSelectionne(URLUtils.getURLSpecialParameterValue());
161
		}
162
 
163
		if (pasInt != null) {
164
			setPasPagination(pasInt);
165
		}
166
 
167
		if (urlCourante.contains(urlAppliImg)) {
168
			rechercheParArguments.setMotClefCel(tagCel);
169
			rechercheParArguments.setMotClefDel(tagDel);
170
			if (pageInt != null) {
171
				setPageCouranteRechercheImages(pageInt);
172
			}
173
		} else {
174
			if (pageInt != null) {
175
				setPageCouranteRechercheObservations(pageInt);
176
			}
177
		}
178
 
179
		definirOrdreTriCourantParUrl();
180
		String tri = Location.getParameter("tri");
181
		if (urlCourante.contains(urlAppliImg)) {
182
			if (tri != null) {
183
				// Pour PictoFlora
184
				if (tri.equals("moyenne-arithmetique")) {
185
					rechercheParArguments.setTriParMoyenneArithmetique(modeTri);
186
				} else if (tri.equals("points")) {
187
					rechercheParArguments.setTriParNbPoints(modeTri);
188
				} else if (tri.equals("tags")) {
189
					rechercheParArguments.setTriParNbTags(modeTri);
190
				} else {
191
					rechercheParArguments.setTriParDatePublication(modeTri);
192
				}
193
			} else {
194
				// Tri par défaut pour PictoFlora
195
				rechercheParArguments.setTriParDatePublication(ModeTri.TRI_DESCENDANT);
196
			}
197
		} else {
198
			// Pour IdentiPlante
199
			if (tri != null && tri.equals("date_observation")) {
200
				rechercheParArguments.setTriParDateObservation(modeTri);
201
			} else if(tri != null && tri.equals("nb_commentaires")) {
202
				rechercheParArguments.setTriParNbCommentaires(modeTri);
203
			} else {
204
				// Tri par défaut pour IdentiPlante
205
				rechercheParArguments.setTriParDatePublication(ModeTri.TRI_DESCENDANT);
206
			}
207
		}
208
 
209
		rechercheParArguments.setMotClef(tag);
210
 
211
		// si le référentiel est passé dans l'url alors il ne doit pas être modifiable
212
		if (referentiel != null && !referentiel.equals("")) {
213
			setReferentielCourant(referentiel);
214
			rechercheParArguments.setReferentiel(referentiel);
215
			referentielNonModifiable = true;
216
		}
217
 
218
		if (Location.getParameterMap().size() == 0) {
219
			// par défaut l'application s'ouvre sur l'onglet à déterminer
220
			statut = ConstantesNavigation.PARAM_TYPE_DEFAUT;
221
		} else {
222
			if (Arrays.asList(statutsPossibles).contains(statutParam)) {
223
				statut = statutParam;
224
			} else {
225
				statut = ConstantesNavigation.PARAM_TYPE_DEFAUT;
226
			}
227
		}
228
 
229
		informationsRechercheImage = rechercheParArguments;
230
		informationsRechercheObservation = rechercheParArguments;
231
 
232
		mettreAjourUrlCourante();
233
	}
234
 
235
	private void definirOrdreTriCourantParUrl() {
236
		String ordre = Location.getParameter("ordre");
237
		if (ordre != null) {
238
			if (ordre.equals("asc")) {
239
				this.modeTri = ModeTri.TRI_ASCENDANT;
240
			} else if (ordre.equals("desc")) {
241
				this.modeTri = ModeTri.TRI_DESCENDANT;
242
			}
243
		}
244
	}
2138 mathias 245
 
1934 aurelien 246
	public void setIdProtocoleEnAttente(String idProtocole) {
247
		idProtocoleEnAttente = idProtocole;
248
	}
2138 mathias 249
 
1934 aurelien 250
	public String getIdProtocoleEnAttente() {
251
		return idProtocoleEnAttente;
252
	}
253
 
254
	public Utilisateur getUtilisateur() {
255
		if (this.utilisateur == null) {
256
			this.utilisateur = new Utilisateur(null, null);
257
		}
258
		return this.utilisateur;
259
	}
260
 
261
	public void setUtilisateur(Utilisateur utilisateur) {
262
		this.utilisateur = utilisateur;
263
	}
264
 
265
	public void setHome(String home) {
266
		setPageCourante(home);
267
	}
268
 
269
	public String getHome() {
270
		return "";
271
	}
272
 
273
	public Observation getObservationCourante() {
274
		return observationCourante;
275
	}
276
 
277
	public void setObservationCourante(Observation observationCourante) {
278
		this.observationCourante = observationCourante;
279
	}
280
 
281
	public static CacheClient getInstance() {
282
		if (instance == null) {
283
			instance = new CacheClient();
284
		}
285
		return instance;
286
	}
287
 
288
	public String getTaxonPourRechercheEflore() {
289
		return taxonPourRechercheEflore;
290
	}
291
 
292
	public void setTaxonPourRechercheEflore(String taxonPourRechercheEflore) {
293
		this.taxonPourRechercheEflore = taxonPourRechercheEflore;
294
	}
295
 
296
	public int getNumPageRechercheImage() {
297
		return numPageRechercheImage;
298
	}
299
 
300
	// Pour la recherche :
301
	public int getPasPagination() {
302
		return pasPagination;
303
	}
304
 
305
	public void setPasPagination(int pasPagination) {
306
		this.pasPagination = pasPagination;
307
	}
308
 
309
	public void setPageCouranteRechercheImages(int pageCouranteRecherche) {
310
		this.pageCouranteRechercheImage = pageCouranteRecherche;
311
	}
312
 
313
	public int getPageCouranteRechercheImage() {
314
		return this.pageCouranteRechercheImage;
315
	}
316
 
317
	public void setNbTotalImagesRecherchees(int nbTotalImagesRecherchees) {
318
		this.nbTotalImagesRecherchees = nbTotalImagesRecherchees;
319
	}
320
 
321
	public int getNbTotalImagesRecherchees() {
322
		return this.nbTotalImagesRecherchees;
323
	}
324
 
325
	public Image getImageCourante() {
326
		return imageCourante;
327
	}
328
 
329
	public void setImageCourante(Image imageCourante) {
330
		this.imageCourante = imageCourante;
331
	}
332
 
333
	public void setListeProtocoles(List<Protocole> listeProtocole) {
334
		this.listeProtocoles = listeProtocole;
335
	}
336
 
337
	public List<Protocole> getListeProtocoles() {
338
		return this.listeProtocoles;
339
	}
340
 
341
	public Protocole getProtocoleCourant() {
342
		return protocoleCourant;
343
	}
344
 
345
	public void setProtocoleCourant(Protocole protocoleCourant) {
346
		this.protocoleCourant = protocoleCourant;
347
	}
348
 
349
	public InformationsRecherche getInformationsRechercheImage() {
350
		if (informationsRechercheImage == null) {
351
			informationsRechercheImage = new InformationsRecherche();
352
		}
353
		return informationsRechercheImage;
354
	}
355
 
356
	public InformationsRecherche getInformationsRechercheObservation() {
357
		if (informationsRechercheObservation == null) {
358
			informationsRechercheObservation = new InformationsRecherche();
359
		}
360
		return informationsRechercheObservation;
361
	}
362
 
363
	public void setInformationsRechercheImage(InformationsRecherche informationsRechercheImage) {
364
		this.informationsRechercheImage = informationsRechercheImage;
365
	}
366
 
367
	public void setInformationsRechercheObservation(InformationsRecherche informationsRechercheObservation) {
368
		this.informationsRechercheObservation = informationsRechercheObservation;
369
	}
370
 
371
	public int getPageCouranteRechercheObservations() {
372
		return pageCouranteRechercheObservations;
373
	}
374
 
375
	public void setPageCouranteRechercheObservations(int pageCouranteRechercheObservations) {
376
		this.pageCouranteRechercheObservations = pageCouranteRechercheObservations;
377
	}
378
 
379
	public void setReferentielCourant(String referentielCourant) {
380
		this.referentielCourant = referentielCourant;
381
	}
382
 
383
	public String getReferentielCourant() {
384
		return referentielCourant;
385
	}
386
 
387
	public void setReferentielNonModifiable(boolean referentielNonModifiable) {
388
		this.referentielNonModifiable = referentielNonModifiable;
389
	}
390
 
391
	public boolean getReferentielNonModifiable() {
392
		return referentielNonModifiable;
393
	}
394
 
395
	public String getUrlPageSignalerMauvaiseId(String id) {
396
		Config config = new Config();
397
		String urlSignalerMauvaiseId = config.getUrl("identiplante");
398
		if (!GWT.isScript()) {
399
			urlSignalerMauvaiseId += "?gwt.codesvr="+Location.getParameter("gwt.codesvr")+"";
400
		}
401
		return urlSignalerMauvaiseId+"#"+ConstantesNavigation.PAGE_DETAIL_OBS+"~"+id;
402
 
403
	}
404
 
1986 aurelien 405
	public void setListePays(Map<String, String> listePays) {
406
		this.listePays = listePays;
1995 aurelien 407
		listePaysInversee = new HashMap<String, String>();
408
		for(Map.Entry<String, String> entry : listePays.entrySet()){
409
			listePaysInversee.put(entry.getValue(), entry.getKey());
410
		}
1986 aurelien 411
	}
412
 
413
	public Map<String, String> getListePays() {
414
		return listePays;
415
	}
416
 
1995 aurelien 417
	public Map<String, String> getListePaysInversee() {
418
		return listePaysInversee;
419
	}
420
 
1934 aurelien 421
	public String genererUrlCourante() {
422
		String urlCourante = Window.Location.getHref();
423
		Config config = new Config();
424
		String urlAppliObs = config.getUrl("identiplante");
425
		String urlAppliImg = config.getUrl("pictoflora");
426
		String url = urlCourante;
427
		String arguments = "";
428
 
429
		if (urlCourante.contains(urlAppliImg)) {
430
			InformationsRecherche infoRecherche;
431
			infoRecherche = getInformationsRechercheImage();
432
 
433
			if (CacheClient.getInstance().getProtocoleCourant() != null) {
434
				infoRecherche.setIdProtocoleSelectionne("" + CacheClient.getInstance().getProtocoleCourant().getId());
435
			}
436
 
437
			arguments = infoRecherche.versChaineRequete();
438
			arguments += (arguments.isEmpty()) ? "" : "&";
439
			arguments += "page="+getPageCouranteRechercheImage()+"&pas="+getPasPagination();
440
 
441
			url = config.getUrl("pictoflora");
442
		} else if (urlCourante.contains(urlAppliObs)) {
443
			InformationsRecherche infoRecherche;
444
			infoRecherche = getInformationsRechercheObservation();
445
 
446
			arguments = "masque.type="+statut;
447
			arguments += "&page="+getPageCouranteRechercheObservations()+"&pas="+getPasPagination();
448
			arguments += "&"+infoRecherche.versChaineRequete();
449
 
450
			url = config.getUrl("identiplante");
451
		}
452
 
453
		if (!GWT.isScript()) {
454
			arguments += "&gwt.codesvr="+Location.getParameter("gwt.codesvr")+"";
455
		}
456
		arguments = (arguments.isEmpty()) ? "" : "?" + arguments;
457
		String signet = getPageCourante().equals("") ? "" : "#" + getPageCourante();
458
		url += arguments + signet;
459
 
460
		// remplacements batards pour corriger l'url
461
		// TODO: factoriser toute la fonction
462
		url = url.replaceAll("&#", "#");
463
		url = url.replaceAll("&&", "&");
464
		url = url.replaceAll("\\?&", "?");
465
		if (url.endsWith("&")) {
466
			url = url.substring(0, url.length()-1);
467
		}
468
 
469
		return url;
470
	}
471
 
472
	public void mettreAjourUrlCourante() {
473
		mettreAJourUrlCouranteSansRecharger(genererUrlCourante());
474
	}
475
 
476
	private static native void mettreAJourUrlCouranteSansRecharger(String nouvelleUrl) /*-{
477
		// javascript double negative trick
478
		// pour plus d'info http://stackoverflow.com/questions/4686583/can-someone-explain-this-double-negative-trick
479
		if (!!($wnd.history && $wnd.history.pushState)) {
480
			var currentState = $wnd.history.state;
481
			if (currentState != nouvelleUrl) {
482
				$wnd.history.pushState(nouvelleUrl, "", nouvelleUrl);
483
			}
484
		}
485
	}-*/;
2054 aurelien 486
 
487
	public static void remplacerTitreOngletNavigateur(String titre) {
488
	    if (Document.get() != null) {
489
	    	titreOngletNavigateur = Document.get().getTitle();
490
	        Document.get().setTitle(titre);
491
	    }
492
	}
493
 
494
	public static void ajouterAuTitreOngletNavigateur(String titreSupplementaire) {
495
	    if (Document.get() != null) {
496
	    	titreOngletNavigateur = titreOngletNavigateur.isEmpty() ? Document.get().getTitle() : titreOngletNavigateur;
497
	        Document.get().setTitle(titreOngletNavigateur+titreSupplementaire);
498
	    }
499
	}
500
 
501
	public static void reinitialiserTitreOngletNavigateur() {
502
	    if (Document.get() != null) {
503
	    	titreOngletNavigateur = titreOngletNavigateur.isEmpty() ? Document.get().getTitle() : titreOngletNavigateur;
504
	        Document.get().setTitle(titreOngletNavigateur);
505
	    }
506
	}
1934 aurelien 507
}