12 |
david |
1 |
package org.tela_botanica.client.modeles;
|
|
|
2 |
|
|
|
3 |
import org.tela_botanica.client.interfaces.FournisseurListe;
|
|
|
4 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
|
|
5 |
import org.tela_botanica.client.observation.ObservationModele;
|
|
|
6 |
|
32 |
jpm |
7 |
import com.google.gwt.http.client.Request;
|
|
|
8 |
import com.google.gwt.http.client.RequestBuilder;
|
|
|
9 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
10 |
import com.google.gwt.http.client.RequestException;
|
|
|
11 |
import com.google.gwt.http.client.Response;
|
12 |
david |
12 |
import com.google.gwt.json.client.JSONArray;
|
|
|
13 |
import com.google.gwt.json.client.JSONParser;
|
|
|
14 |
import com.google.gwt.json.client.JSONString;
|
|
|
15 |
import com.google.gwt.json.client.JSONValue;
|
|
|
16 |
|
|
|
17 |
/**
|
15 |
david |
18 |
* DAO la liste des communes
|
12 |
david |
19 |
* @author David Delon
|
|
|
20 |
*
|
|
|
21 |
*/
|
|
|
22 |
public class ListeReferentielCommuneAsynchroneDAO implements FournisseurListe {
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Le modèle associé au DAO
|
|
|
29 |
*/
|
|
|
30 |
private ObservationModele observationModele = null ;
|
|
|
31 |
|
|
|
32 |
public ListeReferentielCommuneAsynchroneDAO(ObservationModele observationModele)
|
|
|
33 |
{
|
|
|
34 |
|
|
|
35 |
this.observationModele=observationModele;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Envoie une requete au serveur jrest pour obtenir les communes correspondant
|
|
|
41 |
* à des critères données en paramètres
|
|
|
42 |
* @param r le rafraichissable qui demande la mise à jour
|
|
|
43 |
* @param criteres un string contenant le terme a rechercher
|
|
|
44 |
*/
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
public void obtenirListeDonnees(final Rafraichissable r, String critere)
|
|
|
48 |
{
|
32 |
jpm |
49 |
|
|
|
50 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,observationModele.getConfig().getServiceBaseUrl()+"/LocationSearch/"+critere) ;
|
|
|
51 |
|
|
|
52 |
try {
|
|
|
53 |
|
|
|
54 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
55 |
|
|
|
56 |
public void onError(Request request, Throwable exception) {
|
|
|
57 |
// TODO Auto-generated method stub
|
|
|
58 |
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public void onResponseReceived(Request request,
|
|
|
62 |
Response response) {
|
|
|
63 |
|
|
|
64 |
final ListeReferentielCommune referentielCommuneData ;
|
|
|
65 |
|
|
|
66 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
|
|
67 |
|
|
|
68 |
JSONArray reponse=null;
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
// si c'est un tableau
|
|
|
72 |
if ((reponse=responseValue.isArray()) != null) {
|
|
|
73 |
|
|
|
74 |
JSONArray communes;
|
|
|
75 |
final int taillemax = reponse.size();
|
|
|
76 |
|
|
|
77 |
referentielCommuneData = new ListeReferentielCommune(taillemax);
|
|
|
78 |
|
|
|
79 |
for (int i = 0; i < taillemax; i++) {
|
|
|
80 |
if ((communes=reponse.get(i).isArray()) != null) {
|
|
|
81 |
|
|
|
82 |
String commune=((JSONString) communes.get(0)).stringValue();
|
|
|
83 |
String departement=((JSONString) communes.get(1)).stringValue();
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
ReferentielCommune com=new ReferentielCommune(commune, departement);
|
12 |
david |
87 |
|
32 |
jpm |
88 |
referentielCommuneData.put(com.getCommune()+com.getDepartement(),com);
|
|
|
89 |
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
} else {
|
|
|
93 |
|
|
|
94 |
referentielCommuneData = new ListeReferentielCommune(0) ;
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
|
|
|
98 |
// le premier à les recevoir
|
|
|
99 |
r.rafraichir(referentielCommuneData,true);
|
|
|
100 |
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
}) ;
|
|
|
104 |
|
|
|
105 |
} catch (RequestException e) {
|
|
|
106 |
|
|
|
107 |
e.printStackTrace() ;
|
|
|
108 |
}
|
12 |
david |
109 |
|
|
|
110 |
}
|
|
|
111 |
}
|