Subversion Repositories eFlore/Applications.cel

Rev

Rev 235 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles;

import java.util.Iterator;
import java.util.Set;

import org.tela_botanica.client.image.ImageModele;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.observation.ObservationModele;

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.JSONValue;
import com.google.gwt.user.client.Window;

public class MotsClesObsAsynchronesDAO {

        /**
         * Le modele associé au DAO
         */
        private ObservationModele oModele = null;

        public MotsClesObsAsynchronesDAO(ObservationModele om) {
                oModele = om;
        }

        /**
         * Setteur pour le modèle
         * 
         * @param im
         *            le modèle à associer
         */
        public void setIModele(ObservationModele om) {
                oModele = om;
        }
        
        public void obtenirMotsClesObservations(final Rafraichissable r) {
                
                RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, Configuration.getServiceBaseUrl()
                                + "/InventoryKeyWordObsList/" + oModele.getIdentifiant());

                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 on reçoit un tableau
                                        if (responseValue.isArray() != null) {

                                                final JSONArray reponse = responseValue.isArray();
                                                
                                                int tailleMaxMotCle = (int) reponse.get(0).isNumber().doubleValue() ;
                                                r.rafraichir(tailleMaxMotCle, false);
                                                // et on met à jour le demandeur des données
                                                JSONObject motClesPoids = reponse.get(1).isObject();
                                                int nbMotsCles = motClesPoids.size();
                                                Object[][] tabMotsCles = new Object[nbMotsCles][2];
                                                
                                                Set<String> im = motClesPoids.keySet();
                                                int i = 0;
                                                // on la parcourt pour chaque clé
                                                for (Iterator<String> iterator = im.iterator(); iterator.hasNext();) {
                                                        
                                                        String key = iterator.next();
                                                        tabMotsCles[i][0] = key;
                                                        tabMotsCles[i][1] = (int)motClesPoids.get(key).isNumber().doubleValue();
                                                        
                                                        i++;
                                                }
                                                
                                                r.rafraichir(tabMotsCles, false);
                                        }

                                }

                        });
                } catch (RequestException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public void ajouterMotsCles(final Rafraichissable r, String numObsApl,
                        String motsAAjouter) {
                
                String post = "motscles="+motsAAjouter+"&idsobs="+numObsApl;
                RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, Configuration.getServiceBaseUrl()
                                + "/InventoryKeyWordObsList/" + oModele.getIdentifiant());

                try {
                        rb.sendRequest(post, new RequestCallback() {

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

                                }

                                public void onResponseReceived(Request request,
                                                Response response) {

                                        if(response.getText().equals("OK")) {
                                                r.rafraichir("OK", true);
                                        }

                                }

                        });
                } catch (RequestException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
        }
}