Subversion Repositories eFlore/Applications.cel

Rev

Rev 89 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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