Subversion Repositories eFlore/Applications.del

Rev

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