Subversion Repositories eFlore/Applications.del

Rev

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