Subversion Repositories eFlore/Applications.cel

Rev

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

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