Subversion Repositories eFlore/Applications.cel

Rev

Rev 2621 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2621 Rev 2694
1
package org.tela_botanica.client.modeles.dao;
1
package org.tela_botanica.client.modeles.dao;
2
 
2
 
3
import org.tela_botanica.client.i18n.Msg;
3
import org.tela_botanica.client.i18n.Msg;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
5
import org.tela_botanica.client.modeles.objets.Configuration;
5
import org.tela_botanica.client.modeles.objets.Configuration;
6
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
6
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
7
import org.tela_botanica.client.util.Util;
7
import org.tela_botanica.client.util.Util;
8
 
8
 
9
import com.google.gwt.http.client.Request;
9
import com.google.gwt.http.client.Request;
10
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
10
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
11
import com.google.gwt.http.client.RequestCallback;
11
import com.google.gwt.http.client.RequestCallback;
12
import com.google.gwt.http.client.RequestException;
12
import com.google.gwt.http.client.RequestException;
13
import com.google.gwt.http.client.Response;
13
import com.google.gwt.http.client.Response;
14
import com.google.gwt.http.client.URL;
14
import com.google.gwt.http.client.URL;
15
import com.google.gwt.json.client.JSONObject;
15
import com.google.gwt.json.client.JSONObject;
16
import com.google.gwt.json.client.JSONParser;
16
import com.google.gwt.json.client.JSONParser;
17
import com.google.gwt.json.client.JSONValue;
17
import com.google.gwt.json.client.JSONValue;
18
import com.google.gwt.user.client.Window;
18
import com.google.gwt.user.client.Window;
19
 
19
 
20
public class InformationLocaliteDAO {
20
public class InformationLocaliteDAO {
21
 
21
 
22
	private final String NOM_SERVICE = "CoordSearch";
22
	private final String NOM_SERVICE = "CoordSearch";
23
	private final String CODE_PAYS = "FR";
23
	private final String CODE_PAYS = "FR";
24
	
24
	
25
	// Ce DAO peut être fréquemment sollicité lors de l'utilisation de la carte
25
	// Ce DAO peut être fréquemment sollicité lors de l'utilisation de la carte
26
	// et peut empiler beaucoup de longues requête, ceci permet donc de les annuler facilement
26
	// et peut empiler beaucoup de longues requête, ceci permet donc de les annuler facilement
27
	private static Request requeteEnCours = null;
27
	private static Request requeteEnCours = null;
28
 
28
 
29
	Rafraichissable r = null;
29
	Rafraichissable r = null;
30
 
30
 
31
	public InformationLocaliteDAO(Rafraichissable r) {
31
	public InformationLocaliteDAO(Rafraichissable r) {
32
		this.r = r;
32
		this.r = r;
33
	}
33
	}
34
 
34
 
35
	public void obtenirLocalitePlusProche(final Rafraichissable r, final double lng,
35
	public void obtenirLocalitePlusProche(final Rafraichissable r, final double lng,
36
			final double lat) {	
36
			final double lat) {	
37
		
37
		
38
		annulerRequeteEnCours();
38
		annulerRequeteEnCours();
39
 
39
 
40
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
40
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
41
				+ NOM_SERVICE + "?lat="+URL.encode("" + lat) + "&lon="+URL.encode("" + lng);
41
				+ NOM_SERVICE + "?lat="+URL.encode("" + lat) + "&lon="+URL.encode("" + lng);
42
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
42
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
43
 
43
 
44
		try {
44
		try {
45
			requeteEnCours = rb.sendRequest(null, new RequestCallback() {
45
			requeteEnCours = rb.sendRequest(null, new RequestCallback() {
46
 
46
 
47
				@Override
47
				@Override
48
				public void onError(Request request, Throwable exception) {
48
				public void onError(Request request, Throwable exception) {
49
					Window.alert(exception.getMessage());
49
					Window.alert(exception.getMessage());
50
					requeteEnCours = null;
50
					requeteEnCours = null;
51
				}
51
				}
52
 
52
 
53
				@Override
53
				@Override
54
				public void onResponseReceived(Request request,
54
				public void onResponseReceived(Request request,
55
						Response response) {
55
						Response response) {
56
					
56
					
57
					EntiteGeographiqueObservation infos;
57
					EntiteGeographiqueObservation infos;
58
					String idLocalite = "";
58
					String idLocalite = "";
59
					String nomLocalite = "";
59
					String nomLocalite = "";
60
					String pays = "";
60
					String pays = "";
61
 
61
 
62
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
62
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
63
						Window.alert(Msg.get("requete-mal-formee"));
63
						Window.alert(Msg.get("requete-mal-formee"));
64
					} else {
64
					} else {
65
						
65
						
66
						if(response.getText().equals("")) {
66
						if(response.getText().equals("")) {
67
 
67
 
68
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
68
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
69
							infos.setLat(""+lat);
69
							infos.setLat(""+lat);
70
							infos.setLon(""+lng);
70
							infos.setLon(""+lng);
71
		
71
		
72
							r.rafraichir(infos, false);
72
							r.rafraichir(infos, false);
73
						}
73
						}
74
						
74
						
75
						final JSONValue responseValue = JSONParser
75
						final JSONValue responseValue = JSONParser
76
								.parse(response.getText());
76
								.parse(response.getText());
77
 
77
 
78
						if (responseValue.isObject() != null) {
78
						if (responseValue.isObject() != null) {
79
							
79
							
80
							JSONObject objectRetour = responseValue.isObject();
80
							JSONObject objectRetour = responseValue.isObject();
81
							
81
							
82
							if(objectRetour.get("nom").isString() != null) {
82
							if(objectRetour.get("nom").isString() != null) {
83
								nomLocalite = objectRetour.get("nom").isString().stringValue();
83
								nomLocalite = objectRetour.get("nom").isString().stringValue();
84
							}
84
							}
85
							
85
							
86
							if(objectRetour.get("code_zone").isString() != null) {
86
							if(objectRetour.get("code_zone").isString() != null) {
87
								idLocalite = objectRetour.get("code_zone").isString().stringValue().substring(0, 2);
87
								idLocalite = objectRetour.get("code_zone").isString().stringValue().substring(0, 2);
88
							}
88
							}
89
							
89
							
90
							if(objectRetour.get("code_pays").isString() != null) {
90
							if(objectRetour.get("code_pays").isString() != null) {
91
								pays = objectRetour.get("code_pays").isString().stringValue();
91
								pays = objectRetour.get("code_pays").isString().stringValue();
92
							}
92
							}
93
						}
93
						}
94
					}
94
					}
95
					
95
					
96
					infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
96
					infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
97
					infos.setLat(""+lat);
97
					infos.setLat(""+lat);
98
					infos.setLon(""+lng);
98
					infos.setLon(""+lng);
99
					infos.setPays(pays);
99
					infos.setPays(pays);
100
 
100
 
101
					r.rafraichir(infos, false);
101
					r.rafraichir(infos, false);
102
				}
102
				}
103
			});
103
			});
104
		} catch (RequestException e) {
104
		} catch (RequestException e) {
105
			Window.alert(e.getMessage());
105
			Window.alert(e.getMessage());
106
		}
106
		}
107
	}
107
	}
108
 
108
 
109
	public void obtenirInfosLocalite(final Rafraichissable r,
109
	public void obtenirInfosLocalite(final Rafraichissable r,
110
			String valeurLocalite, String codeLoc) {
110
			String valeurLocalite, String codeLoc) {
111
		
111
		
112
		annulerRequeteEnCours();
112
		annulerRequeteEnCours();
113
		
113
		
114
		codeLoc = codeLoc.replaceAll("000null", "*");
114
		codeLoc = codeLoc.replaceAll("000null", "*");
115
		codeLoc = codeLoc.replaceAll("\"", "");
115
		codeLoc = codeLoc.replaceAll("\"", "");
116
		
116
		
117
		valeurLocalite = valeurLocalite.split(" \\([0-9][0-9]\\)")[0];
117
		valeurLocalite = valeurLocalite.split(" \\([0-9][0-9]\\)")[0];
118
		valeurLocalite = valeurLocalite.replaceAll("000null", "*");
118
		valeurLocalite = valeurLocalite.replaceAll("000null", "*");
119
		valeurLocalite = valeurLocalite.replaceAll("\"", "");
119
		valeurLocalite = valeurLocalite.replaceAll("\"", "");
120
		
120
		
121
		
121
		
122
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
122
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
123
				+ NOM_SERVICE+"?zone="+ URL.encode(valeurLocalite);
123
				+ NOM_SERVICE+"?zone="+ URL.encode(valeurLocalite);
124
		
124
		
125
		if(!codeLoc.trim().isEmpty()) {
125
		if(!codeLoc.trim().isEmpty()) {
126
			// Cas du code de département ou postal
126
			// Cas du code de département ou postal
127
			if(Util.estUnNombre(codeLoc)) {
127
			if(Util.estUnDepartement(codeLoc)) {
128
				adresseAppel += "&code="+URL.encode(codeLoc)+"&pays="+URL.encode(CODE_PAYS);
128
				adresseAppel += "&code="+URL.encode(codeLoc)+"&pays="+URL.encode(CODE_PAYS);
129
			} else {
129
			} else {
130
				//cas du code pays
130
				//cas du code pays
131
				adresseAppel += "&pays="+URL.encode(codeLoc);
131
				adresseAppel += "&pays="+URL.encode(codeLoc);
132
			}
132
			}
133
			
133
			
134
		}
134
		}
135
 
135
 
136
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
136
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
137
 
137
 
138
		try {
138
		try {
139
			requeteEnCours = rb.sendRequest(null, new RequestCallback() {
139
			requeteEnCours = rb.sendRequest(null, new RequestCallback() {
140
 
140
 
141
				@Override
141
				@Override
142
				public void onError(Request request, Throwable exception) {
142
				public void onError(Request request, Throwable exception) {
143
					Window.alert(exception.getMessage());
143
					Window.alert(exception.getMessage());
144
				}
144
				}
145
 
145
 
146
				@Override
146
				@Override
147
				public void onResponseReceived(Request request,
147
				public void onResponseReceived(Request request,
148
						Response response) {
148
						Response response) {
149
 
149
 
150
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
150
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
151
						r.rafraichir(Msg.get("impossible-geolocaliser-obs"), false);
151
						r.rafraichir(Msg.get("impossible-geolocaliser-obs"), false);
152
					} else {													
152
					} else {													
153
						final JSONValue responseValue = JSONParser
153
						final JSONValue responseValue = JSONParser
154
								.parse(response.getText());
154
								.parse(response.getText());
155
						
155
						
156
						if (responseValue.isObject() != null) {
156
						if (responseValue.isObject() != null) {
157
							
157
							
158
							EntiteGeographiqueObservation infos;
158
							EntiteGeographiqueObservation infos;
159
							Double lng = 0.0;
159
							Double lng = 0.0;
160
							Double lat = 0.0;
160
							Double lat = 0.0;
161
								
161
								
162
							JSONObject objectRetour = responseValue.isObject();
162
							JSONObject objectRetour = responseValue.isObject();
163
							
163
							
164
							String nomLocalite = Util.getValeurJsonOuVide(objectRetour, "nom");
164
							String nomLocalite = Util.getValeurJsonOuVide(objectRetour, "nom");
165
							String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_zone");
165
							String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_zone");
166
							lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0;
166
							lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0;
167
							lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0;
167
							lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0;
168
							String pays = Util.getValeurJsonOuVide(objectRetour, "code_pays");
168
							String pays = Util.getValeurJsonOuVide(objectRetour, "code_pays");
169
							
169
							
170
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
170
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
171
							infos.setLat(""+lat);
171
							infos.setLat(""+lat);
172
							infos.setLon(""+lng);
172
							infos.setLon(""+lng);
173
							infos.setPays(pays);
173
							infos.setPays(pays);
174
							
174
							
175
							r.rafraichir(infos, false);
175
							r.rafraichir(infos, false);
176
						} else {
176
						} else {
177
							EntiteGeographiqueObservation infos;
177
							EntiteGeographiqueObservation infos;
178
							String idLocalite = "";
178
							String idLocalite = "";
179
							String nomLocalite = "";
179
							String nomLocalite = "";
180
 
180
 
181
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
181
							infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null);
182
							infos.setLat("");
182
							infos.setLat("");
183
							infos.setLon("");
183
							infos.setLon("");
184
		
184
		
185
							r.rafraichir(infos, false);
185
							r.rafraichir(infos, false);
186
						}
186
						}
187
					}
187
					}
188
				}
188
				}
189
			});
189
			});
190
		} catch (RequestException e) {
190
		} catch (RequestException e) {
191
			Window.alert(e.getMessage());
191
			Window.alert(e.getMessage());
192
		}
192
		}
193
	}
193
	}
194
	
194
	
195
	public static void annulerRequeteEnCours() {
195
	public static void annulerRequeteEnCours() {
196
		if(requeteEnCours != null) {
196
		if(requeteEnCours != null) {
197
			requeteEnCours.cancel();
197
			requeteEnCours.cancel();
198
			requeteEnCours = null;
198
			requeteEnCours = null;
199
		}
199
		}
200
	}
200
	}
201
}
201
}