Subversion Repositories eFlore/Applications.coel

Rev

Rev 69 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles;

import org.tela_botanica.client.RegistreId;
import org.tela_botanica.client.interfaces.Rafraichissable;

import com.extjs.gxt.ui.client.Registry;
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.JSONParser;
import com.google.gwt.json.client.JSONValue;

public class StructureListeAsyncDao {

        private StructureListe institutions = null;
        private Rafraichissable rafraichissement = null;
        
        public StructureListeAsyncDao(Rafraichissable r) {
                rafraichissement = r;
        }
        
        public void obtenirListeInstitution() {
        // Demande de toutes les structures
        String url = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
                RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url+"CoelStructureListe/");

                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) {
                                        Boolean defaut = true;
                                        if (response.getText().length() != 0 && response.getText() != null) {
                                                final JSONValue responseValue = JSONParser.parse(response.getText());
        
                                                // Si la requête est un succès, reception d'un tableau
                                                if (responseValue.isArray() != null) {
                                                        final JSONArray reponse = responseValue.isArray();
                                                        // Transformation du tableau JSON réponse en ListeInstitution
                                                        institutions = new StructureListe(reponse);
                                                        // et on met à jour le demandeur des données
                                                        rafraichissement.rafraichir(institutions);
                                                        // Tout c'est bien déroulé, on courcircuite l'affichage par défaut
                                                        defaut = false;
                                                }
                                        }
                                        if (defaut == true) {
                                                institutions = new StructureListe(0);
                                                rafraichissement.rafraichir(institutions);
                                        }
                                }
                        });
                } catch (RequestException e) {
                        e.printStackTrace();
                }
        }
}