Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.modeles;

import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.util.UtilDAO;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

public class ValeurListeAsyncDao {
        private static final String SERVICE_NOM = "CoelValeurListe";
        
        private Rafraichissable vue = null;
        
        public ValeurListeAsyncDao() {
                // TODO Auto-generated constructor stub
        }
        
        public ValeurListeAsyncDao(Rafraichissable vueARafraichir) {
                vue = vueARafraichir;
        }
        
        public void obtenirListe(Integer cle)   {
                selectionner("id", cle, "*", "*");
        }
        
        public void selectionner(String type, Integer cleParent, String abv, String idValeur) {
        
        // Ajout des paramètres et données à selectionner dans l'URL
                String[] parametres = new String[4];
                parametres[0] = type;
                parametres[1] = cleParent.toString();
                parametres[2] = "*";
                
        if (!type.equals("id")) {
                parametres[2] = abv;
        }
        
        parametres[3] = idValeur;
        
        
        
        RequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
                try {
                        rb.sendRequest(null, new RequestCallback() {

                                public void onError(Request request, Throwable exception) {
                                        // TODO Auto-generated method stub

                                }

                                public void onResponseReceived(Request request, Response response) {
                                        
                                        final JSONValue responseValue = JSONParser.parse(response.getText());

                                        // Si la requête est un succès, reception d'un tableau
                                        if (responseValue.isObject() != null) {
                                                
                                                try {
                                                        final JSONObject reponse = responseValue.isObject();
                                                        JSONString listeId = reponse.get("id").isString();
                                                        JSONArray listeValeurs = reponse.get("valeurs").isArray();
                                                        if (listeId != null)    {
                                                                // Transformation du tableau JSON réponse en Liste
                                                                ValeurListe liste = new ValeurListe(listeId, listeValeurs);
                                                                // et on met à jour le demandeur des données
                                                                vue.rafraichir(liste);
                                                        }
                                                } catch (NullPointerException e) {
                                                        e.printStackTrace();
                                                }
                                        }

                                }
                        });
                } catch (RequestException e) {
                        e.printStackTrace();
                }
        }
        
        
}