Rev 254 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.tela_botanica.client.modeles;import java.util.HashMap;import org.tela_botanica.client.interfaces.FournisseurListe;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.http.client.URL;import com.google.gwt.json.client.JSONArray;import com.google.gwt.json.client.JSONParser;import com.google.gwt.json.client.JSONString;import com.google.gwt.json.client.JSONValue;/*** DAO la liste des communes. Utilisation d'un cache pour eviter les requetes inutiles* @author David Delon**/public class ListeReferentielCommuneAsynchroneDAO implements FournisseurListe {/*** Le modèle associé au DAO.*/private ObservationModele observationModele = null;/*** Cache**/private HashMap<String,ListeReferentielCommune> cache = new HashMap();/*** Constructeur.* @param obs : Modele*/public ListeReferentielCommuneAsynchroneDAO(final ObservationModele obs) {this.observationModele = obs;}/*** Envoie une requete au serveur jrest pour obtenir les communes correspondant* à des critères données en paramètres.* @param r le rafraichissable qui demande la mise à jour* @param critere un string contenant le terme a rechercher*/public final void obtenirListeDonnees(final Rafraichissable r, final String critere) {ListeReferentielCommune referentielCommuneDataFromCache=null;// En cache ?if ((referentielCommuneDataFromCache=getFromCache(critere))!=null) {r.rafraichir(referentielCommuneDataFromCache,true);}else {RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, Configuration.getServiceBaseUrl() + "/LocationSearch/" + URL.encodeComponent(critere));try {rb.sendRequest(null, new RequestCallback() {public void onError(final Request request, final Throwable exception) {// TODO Auto-generated method stub}public void onResponseReceived(final Request request,final Response response) {final ListeReferentielCommune referentielCommuneData;final JSONValue responseValue = JSONParser.parse(response.getText());JSONArray reponse = null;// si c'est un tableauif ((reponse = responseValue.isArray()) != null) {JSONArray communes;final int taillemax = reponse.size();referentielCommuneData = new ListeReferentielCommune(taillemax);for (int i = 0; i < taillemax; i++) {if ((communes = reponse.get(i).isArray()) != null) {String commune = ((JSONString) communes.get(0)).stringValue();String departement = ((JSONString) communes.get(1)).stringValue();ReferentielCommune com = new ReferentielCommune(commune, departement);referentielCommuneData.put(com.getCommune() + com.getDepartement(), com);}}} else {referentielCommuneData = new ListeReferentielCommune(0);}// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est// le premier à les recevoiraddToCache(critere, referentielCommuneData);r.rafraichir(referentielCommuneData, true);}});} catch (RequestException e) {e.printStackTrace();}} // Fin else si pas de cache}private void addToCache(String query, ListeReferentielCommune result) {cache.put(query.toLowerCase(),result);}private ListeReferentielCommune getFromCache (String query) {return (ListeReferentielCommune) cache.get(query.toLowerCase());}}