69 |
jpm |
1 |
package org.tela_botanica.client.modeles;
|
|
|
2 |
|
570 |
jp_milcent |
3 |
import java.util.HashMap;
|
|
|
4 |
|
69 |
jpm |
5 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
299 |
gduche |
6 |
import org.tela_botanica.client.util.UtilDAO;
|
69 |
jpm |
7 |
|
|
|
8 |
import com.google.gwt.http.client.Request;
|
|
|
9 |
import com.google.gwt.http.client.RequestBuilder;
|
|
|
10 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
11 |
import com.google.gwt.http.client.RequestException;
|
|
|
12 |
import com.google.gwt.http.client.Response;
|
|
|
13 |
import com.google.gwt.json.client.JSONArray;
|
91 |
jpm |
14 |
import com.google.gwt.json.client.JSONObject;
|
69 |
jpm |
15 |
import com.google.gwt.json.client.JSONParser;
|
91 |
jpm |
16 |
import com.google.gwt.json.client.JSONString;
|
69 |
jpm |
17 |
import com.google.gwt.json.client.JSONValue;
|
|
|
18 |
|
|
|
19 |
public class ValeurListeAsyncDao {
|
570 |
jp_milcent |
20 |
|
|
|
21 |
private static HashMap<Integer, ValeurListe> ontologieCache = new HashMap<Integer, ValeurListe>();
|
268 |
jp_milcent |
22 |
private static final String SERVICE_NOM = "CoelValeurListe";
|
570 |
jp_milcent |
23 |
private Rafraichissable vueARafraichir = null;
|
69 |
jpm |
24 |
|
268 |
jp_milcent |
25 |
public ValeurListeAsyncDao() {
|
|
|
26 |
// TODO Auto-generated constructor stub
|
69 |
jpm |
27 |
}
|
|
|
28 |
|
570 |
jp_milcent |
29 |
public ValeurListeAsyncDao(Rafraichissable vueCourante) {
|
|
|
30 |
vueARafraichir = vueCourante;
|
268 |
jp_milcent |
31 |
}
|
|
|
32 |
|
299 |
gduche |
33 |
public void obtenirListe(Integer cle) {
|
303 |
gduche |
34 |
selectionner("id", cle, "*", "*");
|
292 |
gduche |
35 |
}
|
|
|
36 |
|
570 |
jp_milcent |
37 |
public void selectionner(String type, final Integer cleParent, String abv, String idValeur) {
|
|
|
38 |
if (ontologieCache.containsKey(cleParent)) {
|
|
|
39 |
vueARafraichir.rafraichir((ValeurListe) ontologieCache.get(cleParent));
|
|
|
40 |
} else {
|
|
|
41 |
// Ajout des paramètres et données à selectionner dans l'URL
|
|
|
42 |
String[] parametres = new String[4];
|
|
|
43 |
parametres[0] = type;
|
|
|
44 |
parametres[1] = cleParent.toString();
|
|
|
45 |
parametres[2] = "*";
|
|
|
46 |
|
|
|
47 |
if (!type.equals("id")) {
|
|
|
48 |
parametres[2] = abv;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
parametres[3] = idValeur;
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
RequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
|
|
|
56 |
try {
|
|
|
57 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
58 |
|
|
|
59 |
public void onError(Request request, Throwable exception) {
|
|
|
60 |
// TODO Auto-generated method stub
|
|
|
61 |
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public void onResponseReceived(Request request, Response response) {
|
303 |
gduche |
65 |
|
570 |
jp_milcent |
66 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
|
|
67 |
|
|
|
68 |
// Si la requête est un succès, reception d'un tableau
|
|
|
69 |
if (responseValue.isObject() != null) {
|
|
|
70 |
|
|
|
71 |
try {
|
|
|
72 |
final JSONObject reponse = responseValue.isObject();
|
|
|
73 |
JSONString listeId = reponse.get("id").isString();
|
|
|
74 |
JSONArray listeValeurs = reponse.get("valeurs").isArray();
|
|
|
75 |
if (listeId != null) {
|
|
|
76 |
// Transformation du tableau JSON réponse en Liste
|
|
|
77 |
ValeurListe liste = new ValeurListe(listeId, listeValeurs);
|
|
|
78 |
// Stockage en cache
|
|
|
79 |
ontologieCache.put(cleParent, liste);
|
|
|
80 |
// et on met à jour le demandeur des données
|
|
|
81 |
vueARafraichir.rafraichir(liste);
|
|
|
82 |
}
|
|
|
83 |
} catch (NullPointerException e) {
|
|
|
84 |
e.printStackTrace();
|
303 |
gduche |
85 |
}
|
91 |
jpm |
86 |
}
|
570 |
jp_milcent |
87 |
|
69 |
jpm |
88 |
}
|
570 |
jp_milcent |
89 |
});
|
|
|
90 |
} catch (RequestException e) {
|
|
|
91 |
e.printStackTrace();
|
|
|
92 |
}
|
|
|
93 |
}
|
69 |
jpm |
94 |
}
|
292 |
gduche |
95 |
|
|
|
96 |
|
69 |
jpm |
97 |
}
|