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