Subversion Repositories eFlore/Applications.cel

Rev

Rev 153 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 153 Rev 204
1
package org.tela_botanica.client.modeles;
1
package org.tela_botanica.client.modeles;
2
 
2
 
3
import java.util.HashMap;
3
import java.util.HashMap;
4
 
4
 
5
import org.tela_botanica.client.Util;
5
import org.tela_botanica.client.Util;
6
import org.tela_botanica.client.interfaces.FournisseurListe;
6
import org.tela_botanica.client.interfaces.FournisseurListe;
7
import org.tela_botanica.client.interfaces.Rafraichissable;
7
import org.tela_botanica.client.interfaces.Rafraichissable;
8
import org.tela_botanica.client.observation.ObservationModele;
8
import org.tela_botanica.client.observation.ObservationModele;
9
 
9
 
10
import com.google.gwt.http.client.Request;
10
import com.google.gwt.http.client.Request;
11
import com.google.gwt.http.client.RequestBuilder;
11
import com.google.gwt.http.client.RequestBuilder;
12
import com.google.gwt.http.client.RequestCallback;
12
import com.google.gwt.http.client.RequestCallback;
13
import com.google.gwt.http.client.RequestException;
13
import com.google.gwt.http.client.RequestException;
14
import com.google.gwt.http.client.Response;
14
import com.google.gwt.http.client.Response;
-
 
15
import com.google.gwt.http.client.URL;
15
import com.google.gwt.json.client.JSONArray;
16
import com.google.gwt.json.client.JSONArray;
16
import com.google.gwt.json.client.JSONParser;
17
import com.google.gwt.json.client.JSONParser;
17
import com.google.gwt.json.client.JSONString;
18
import com.google.gwt.json.client.JSONString;
18
import com.google.gwt.json.client.JSONValue;
19
import com.google.gwt.json.client.JSONValue;
19
 
20
 
20
/**
21
/**
21
 * DAO  liste noms scientifiques // utilisation d'un cahce memoire pour eviter des requetes inutiles.
22
 * DAO  liste noms scientifiques // utilisation d'un cahce memoire pour eviter des requetes inutiles.
22
 * @author David Delon
23
 * @author David Delon
23
 * 
24
 * 
24
 */
25
 */
25
public class ListeReferentielNomAsynchroneDAO implements FournisseurListe {
26
public class ListeReferentielNomAsynchroneDAO implements FournisseurListe {
26
 
27
 
27
 
28
 
28
	//TODO : limiter taille du cache ? (si pb performance)
29
	//TODO : limiter taille du cache ? (si pb performance)
29
	
30
	
30
	/**
31
	/**
31
	 * Le modèle associé au DAO
32
	 * Le modèle associé au DAO
32
	 */
33
	 */
33
	private ObservationModele observationModele = null ;
34
	private ObservationModele observationModele = null ;
34
	
35
	
35
	
36
	
36
	/**
37
	/**
37
	 * Cache
38
	 * Cache
38
	 * 
39
	 * 
39
	 */
40
	 */
40
	
41
	
41
	private HashMap <String,ListeReferentielNom> cache = new HashMap();
42
	private HashMap <String,ListeReferentielNom> cache = new HashMap();
42
	
43
	
43
	
44
	
44
 
45
 
45
	
46
	
46
	public ListeReferentielNomAsynchroneDAO(ObservationModele observationModele)
47
	public ListeReferentielNomAsynchroneDAO(ObservationModele observationModele)
47
	{
48
	{
48
	
49
	
49
		this.observationModele=observationModele;
50
		this.observationModele=observationModele;
50
	}
51
	}
51
	
52
	
52
	
53
	
53
	/**
54
	/**
54
	 * Envoie une requete au serveur jrest pour obtenir les communes  correspondant
55
	 * Envoie une requete au serveur jrest pour obtenir les communes  correspondant
55
	 * à des critères données en paramètres
56
	 * à des critères données en paramètres
56
	 * @param r le rafraichissable qui demande la mise à jour
57
	 * @param r le rafraichissable qui demande la mise à jour
57
	 * @param criteres un string contenant le terme a rechercher
58
	 * @param criteres un string contenant le terme a rechercher
58
	 */
59
	 */
59
	
60
	
60
	
61
	
61
	public void obtenirListeDonnees(final Rafraichissable r, final String critere)
62
	public void obtenirListeDonnees(final Rafraichissable r, final String critere)
62
	{
63
	{
63
		
64
		
64
		
65
		
65
		ListeReferentielNom referentielNomDataFromCache=null;
66
		ListeReferentielNom referentielNomDataFromCache=null;
66
		
67
		
67
		
68
		
68
		// En cache ?
69
		// En cache ?
69
		if ((referentielNomDataFromCache=getFromCache(critere))!=null) {
70
		if ((referentielNomDataFromCache=getFromCache(critere))!=null) {
70
			r.rafraichir(referentielNomDataFromCache,true);  
71
			r.rafraichir(referentielNomDataFromCache,true);  
71
	    }
72
	    }
72
	    
73
	    
73
		else {
74
		else {
74
	    	  
75
	    	  
75
	    	  
76
	    	  
76
	  
77
	  
77
	    
78
	    
78
		
79
		
79
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, observationModele.getConfig().getServiceBaseUrl()+"/NameSearch/"+critere) ;
80
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, observationModele.getConfig().getServiceBaseUrl()+"/NameSearch/"+URL.encodeComponent(critere)) ;
80
		
81
		
81
		try {
82
		try {
82
			
83
			
83
			rb.sendRequest(null, new RequestCallback() {
84
			rb.sendRequest(null, new RequestCallback() {
84
 
85
 
85
				public void onError(Request request, Throwable exception) {
86
				public void onError(Request request, Throwable exception) {
86
					// TODO Auto-generated method stub
87
					// TODO Auto-generated method stub
87
					
88
					
88
				}
89
				}
89
 
90
 
90
				public void onResponseReceived(Request request,
91
				public void onResponseReceived(Request request,
91
						Response response) {
92
						Response response) {
92
					
93
					
93
					final ListeReferentielNom referentielNomData ;
94
					final ListeReferentielNom referentielNomData ;
94
					
95
					
95
					final JSONValue responseValue = JSONParser.parse(response.getText());
96
					final JSONValue responseValue = JSONParser.parse(response.getText());
96
					
97
					
97
					JSONArray reponse=null;
98
					JSONArray reponse=null;
98
		
99
		
99
		
100
		
100
					// si c'est un tableau
101
					// si c'est un tableau
101
					if ((reponse=responseValue.isArray()) != null) {
102
					if ((reponse=responseValue.isArray()) != null) {
102
						
103
						
103
						JSONArray noms;
104
						JSONArray noms;
104
						final int taillemax = reponse.size();
105
						final int taillemax = reponse.size();
105
						
106
						
106
						referentielNomData = new ListeReferentielNom(taillemax);
107
						referentielNomData = new ListeReferentielNom(taillemax);
107
						
108
						
108
						for (int i = 0; i < taillemax; i++) {
109
						for (int i = 0; i < taillemax; i++) {
109
							if ((noms=reponse.get(i).isArray()) != null) {
110
							if ((noms=reponse.get(i).isArray()) != null) {
110
								
111
								
111
								 
112
								 
112
								
113
								
113
								String nom=Util.toCelString(((JSONString) noms.get(0)).toString());
114
								String nom=Util.toCelString(((JSONString) noms.get(0)).toString());
114
								String numeroNom=Util.toCelString(((JSONString) noms.get(1)).toString());
115
								String numeroNom=Util.toCelString(((JSONString) noms.get(1)).toString());
115
								
116
								
116
								ReferentielNom nomScientifique=new ReferentielNom(nom, numeroNom);
117
								ReferentielNom nomScientifique=new ReferentielNom(nom, numeroNom);
117
				
118
				
118
								// FIXME : et si le numero de nom n'est pas unique ? (cas de multirefrentiel....)
119
								// FIXME : et si le numero de nom n'est pas unique ? (cas de multirefrentiel....)
119
								
120
								
120
								referentielNomData.put(nomScientifique.getNumeroNom(),nomScientifique);
121
								referentielNomData.put(nomScientifique.getNumeroNom(),nomScientifique);
121
		
122
		
122
								
123
								
123
							}
124
							}
124
		
125
		
125
						}
126
						}
126
					} else {
127
					} else {
127
						
128
						
128
						referentielNomData = new ListeReferentielNom(0) ;
129
						referentielNomData = new ListeReferentielNom(0) ;
129
					}
130
					}
130
					
131
					
131
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
132
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
132
					// le premier à les recevoir
133
					// le premier à les recevoir
133
				
134
				
134
					addToCache(critere, referentielNomData);
135
					addToCache(critere, referentielNomData);
135
					
136
					
136
					r.rafraichir(referentielNomData,true);
137
					r.rafraichir(referentielNomData,true);
137
					
138
					
138
				}
139
				}
139
				
140
				
140
				
141
				
141
			}) ;
142
			}) ;
142
			
143
			
143
		} catch(RequestException e) {
144
		} catch(RequestException e) {
144
			
145
			
145
		}
146
		}
146
		
147
		
147
		} // fin else si pas de cache
148
		} // fin else si pas de cache
148
			
149
			
149
	}
150
	}
150
	
151
	
151
	  private void addToCache(String query, ListeReferentielNom result)	  {
152
	  private void addToCache(String query, ListeReferentielNom result)	  {
152
		  cache.put(query.toLowerCase(),result);
153
		  cache.put(query.toLowerCase(),result);
153
	  }
154
	  }
154
 
155
 
155
	  private ListeReferentielNom getFromCache (String query) {
156
	  private ListeReferentielNom getFromCache (String query) {
156
		  return (ListeReferentielNom) cache.get(query.toLowerCase());
157
		  return (ListeReferentielNom) cache.get(query.toLowerCase());
157
	  }
158
	  }
158
 
159
 
159
 
160
 
160
	
161
	
161
}
162
}