Subversion Repositories eFlore/Applications.coel

Rev

Rev 581 | Rev 773 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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