Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
65 david 1
package org.tela_botanica.client.modeles;
2
 
3
import org.tela_botanica.client.interfaces.FournisseurListe;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
5
import org.tela_botanica.client.observation.ObservationModele;
965 aurelien 6
import org.tela_botanica.client.util.Util;
65 david 7
 
965 aurelien 8
import com.google.gwt.core.client.GWT;
65 david 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;
965 aurelien 15
import com.google.gwt.json.client.JSONObject;
65 david 16
import com.google.gwt.json.client.JSONParser;
17
import com.google.gwt.json.client.JSONString;
18
import com.google.gwt.json.client.JSONValue;
70 david 19
import com.google.gwt.user.client.Window;
65 david 20
 
21
/**
22
 * DAO la liste des communes.
23
 * @author David Delon
24
 *
25
 */
26
public class ListeEntiteGeographiqueObservationAsynchroneDAO  {
27
 
28
 
29
 
30
	/**
31
	 * Le modèle associé au DAO.
32
	 */
33
	private ObservationModele observationModele = null;
34
 
35
 
36
	/**
37
	 *  Constructeur.
38
	 * @param obs : Modele
39
	 */
40
 
41
	public ListeEntiteGeographiqueObservationAsynchroneDAO(final ObservationModele obs) {
42
		this.observationModele = obs;
43
	}
44
 
45
 
46
	/**
47
	 * Envoie une requete au serveur jrest pour obtenir les communes  correspondant
48
	 * à des critères données en paramètres.
49
	 * @param r le rafraichissable qui demande la mise à jour
50
	 * @param critere un string contenant le terme a rechercher
51
	 */
52
 
53
 
54
	public final void obtenirListeEntitesGeographiques(final Rafraichissable r, final String utilisateur) {
55
 
70 david 56
 
57
 
140 aurelien 58
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, Configuration.getServiceBaseUrl() + "/InventoryLocationList/" + utilisateur);
65 david 59
 
70 david 60
 
65 david 61
		try {
62
 
63
			rb.sendRequest(null, new RequestCallback() {
64
 
65
				public void onError(final Request request, final Throwable exception) {
66
					// TODO Auto-generated method stub
67
 
68
				}
69
 
70
				public void onResponseReceived(final Request request,
71
						final Response response) {
72
 
70 david 73
 
65 david 74
					final ListeEntiteGeographiqueObservation listeEntiteGeographiqueObservation;
75
 
76
					final JSONValue responseValue = JSONParser.parse(response.getText());
77
 
78
					JSONArray reponse = null;
79
 
80
 
81
					// si c'est un tableau
82
					if ((reponse = responseValue.isArray()) != null) {
83
 
965 aurelien 84
						JSONObject entites;
65 david 85
						final int taillemax = reponse.size();
86
 
87
						listeEntiteGeographiqueObservation = new ListeEntiteGeographiqueObservation(taillemax);
88
 
89
						for (int i = 0; i < taillemax; i++) {
965 aurelien 90
							if ((entites = reponse.get(i).isObject()) != null) {
65 david 91
 
965 aurelien 92
								String idZoneGeo = Util.convertirChaineZoneGeoVersDepartement(((JSONString) entites.get("ce_zone_geo")).stringValue());
93
								String zoneGeo  = ((JSONString) entites.get("zone_geo")).stringValue();
94
								String lieuDit  = ((JSONString) entites.get("lieudit")).stringValue();
95
								String station  = ((JSONString) entites.get("station")).stringValue();
65 david 96
 
965 aurelien 97
								EntiteGeographiqueObservation ent = new EntiteGeographiqueObservation(idZoneGeo, zoneGeo, lieuDit, station);
140 aurelien 98
								listeEntiteGeographiqueObservation.put(ent);
65 david 99
							}
100
						}
101
					} else {
102
 
103
						listeEntiteGeographiqueObservation = new ListeEntiteGeographiqueObservation(0);
104
 
105
					}
106
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
107
					// le premier à les recevoir
108
 
109
					r.rafraichir(listeEntiteGeographiqueObservation, true);
110
 
111
				}
112
 
113
			});
114
 
115
		} catch (RequestException e) {
116
 
117
			e.printStackTrace();
118
		}
119
 
120
	}
121
}