Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 989 Rev 1293
1
package org.tela_botanica.client.modeles.dao;
1
package org.tela_botanica.client.modeles.dao;
2
 
2
 
3
import org.tela_botanica.client.interfaces.Rafraichissable;
3
import org.tela_botanica.client.interfaces.Rafraichissable;
4
import org.tela_botanica.client.modeles.objets.Configuration;
4
import org.tela_botanica.client.modeles.objets.Configuration;
5
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
5
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation;
-
 
6
import org.tela_botanica.client.util.Util;
6
 
-
 
7
import com.google.gwt.core.client.GWT;
7
 
8
import com.google.gwt.http.client.Request;
8
import com.google.gwt.http.client.Request;
9
import com.google.gwt.http.client.RequestBuilder;
9
import com.google.gwt.http.client.RequestBuilder;
10
import com.google.gwt.http.client.RequestCallback;
10
import com.google.gwt.http.client.RequestCallback;
11
import com.google.gwt.http.client.RequestException;
11
import com.google.gwt.http.client.RequestException;
12
import com.google.gwt.http.client.Response;
12
import com.google.gwt.http.client.Response;
13
import com.google.gwt.http.client.URL;
13
import com.google.gwt.http.client.URL;
14
import com.google.gwt.json.client.JSONArray;
-
 
15
import com.google.gwt.json.client.JSONObject;
14
import com.google.gwt.json.client.JSONObject;
16
import com.google.gwt.json.client.JSONParser;
15
import com.google.gwt.json.client.JSONParser;
17
import com.google.gwt.json.client.JSONValue;
16
import com.google.gwt.json.client.JSONValue;
18
import com.google.gwt.maps.client.geom.LatLng;
-
 
19
import com.google.gwt.user.client.Window;
17
import com.google.gwt.user.client.Window;
20
 
18
 
21
public class InformationCommuneDAO {
19
public class InformationCommuneDAO {
22
 
20
 
23
	private final String NOM_SERVICE = "CoordSearch";
21
	private final String NOM_SERVICE = "CoordSearch";
24
	private final String CODE_PAYS = "FR";
22
	private final String CODE_PAYS = "FR";
25
 
23
 
26
	Rafraichissable r = null;
24
	Rafraichissable r = null;
27
 
25
 
28
	public InformationCommuneDAO(Rafraichissable r) {
26
	public InformationCommuneDAO(Rafraichissable r) {
29
		this.r = r;
27
		this.r = r;
30
	}
28
	}
31
 
29
 
32
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
30
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
33
			final double lat) {
31
			final double lat) {
34
 
32
 
35
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
33
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
36
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
34
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
37
				+ URL.encode("" + lng) + "/*/*/";
35
				+ URL.encode("" + lng) + "/*/*/";
38
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
36
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
39
 
37
 
40
		try {
38
		try {
41
			rb.sendRequest(null, new RequestCallback() {
39
			rb.sendRequest(null, new RequestCallback() {
-
 
40
 
42
 
41
				@Override
43
				public void onError(Request request, Throwable exception) {
42
				public void onError(Request request, Throwable exception) {
44
					Window.alert(exception.getMessage());
43
					Window.alert(exception.getMessage());
45
				}
44
				}
-
 
45
 
46
 
46
				@Override
47
				public void onResponseReceived(Request request,
47
				public void onResponseReceived(Request request,
48
						Response response) {
48
						Response response) {
49
					
49
					
50
					EntiteGeographiqueObservation infos;
50
					EntiteGeographiqueObservation infos;
51
					String idLocalite = "";
51
					String idLocalite = "";
52
					String nomCommune = "";
52
					String nomCommune = "";
53
 
53
 
54
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
54
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
55
						Window.alert("Requete mal formée");
55
						Window.alert("Requete mal formée");
56
					} else {
56
					} else {
57
						
57
						
58
						if(response.getText().equals("")) {
58
						if(response.getText().equals("")) {
59
 
59
 
60
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
60
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
61
							infos.setLat(""+lat);
61
							infos.setLat(""+lat);
62
							infos.setLon(""+lng);
62
							infos.setLon(""+lng);
63
		
63
		
64
							r.rafraichir(infos, false);
64
							r.rafraichir(infos, false);
65
						}
65
						}
66
						
66
						
67
						final JSONValue responseValue = JSONParser
67
						final JSONValue responseValue = JSONParser
68
								.parse(response.getText());
68
								.parse(response.getText());
69
 
69
 
70
						if (responseValue.isObject() != null) {
70
						if (responseValue.isObject() != null) {
71
							
71
							
72
							JSONObject objectRetour = responseValue.isObject();
72
							JSONObject objectRetour = responseValue.isObject();
73
							
73
							
74
							if(objectRetour.get("nom").isString() != null) {
74
							if(objectRetour.get("nom").isString() != null) {
75
								nomCommune = objectRetour.get("nom").isString().stringValue();
75
								nomCommune = objectRetour.get("nom").isString().stringValue();
76
							}
76
							}
77
							
77
							
78
							if(objectRetour.get("code_insee").isString() != null) {
78
							if(objectRetour.get("code_insee").isString() != null) {
79
								idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
79
								idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
80
							}
80
							}
81
						}
81
						}
82
					}
82
					}
83
					
83
					
84
					infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
84
					infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
85
					infos.setLat(""+lat);
85
					infos.setLat(""+lat);
86
					infos.setLon(""+lng);
86
					infos.setLon(""+lng);
87
 
87
 
88
					r.rafraichir(infos, false);
88
					r.rafraichir(infos, false);
89
				}
89
				}
90
			});
90
			});
91
		} catch (RequestException e) {
91
		} catch (RequestException e) {
92
			Window.alert(e.getMessage());
92
			Window.alert(e.getMessage());
93
		}
93
		}
94
	}
94
	}
95
 
95
 
96
	public void obtenirInfosCommune(final Rafraichissable r,
96
	public void obtenirInfosCommune(final Rafraichissable r,
97
			String valeurCommune, String codePostal) {
97
			String valeurCommune, String codePostal) {
98
		
98
		
99
		codePostal = codePostal.replaceAll("000null", "*");
99
		codePostal = codePostal.replaceAll("000null", "*");
100
		codePostal = codePostal.replaceAll("\"", "");
100
		codePostal = codePostal.replaceAll("\"", "");
101
		
101
		
102
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
102
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
103
		valeurCommune = valeurCommune.replaceAll("000null", "*");
103
		valeurCommune = valeurCommune.replaceAll("000null", "*");
104
		valeurCommune = valeurCommune.replaceAll("\"", "");
104
		valeurCommune = valeurCommune.replaceAll("\"", "");
105
		
105
		
106
		
106
		
107
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
107
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
108
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
108
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
109
				+ URL.encode(codePostal) + "/"
109
				+ URL.encode(codePostal) + "/"
110
				+ URL.encode(CODE_PAYS);
110
				+ URL.encode(CODE_PAYS);
111
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
111
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
112
 
112
 
113
		try {
113
		try {
114
			rb.sendRequest(null, new RequestCallback() {
114
			rb.sendRequest(null, new RequestCallback() {
-
 
115
 
115
 
116
				@Override
116
				public void onError(Request request, Throwable exception) {
117
				public void onError(Request request, Throwable exception) {
117
					Window.alert(exception.getMessage());
118
					Window.alert(exception.getMessage());
118
				}
119
				}
-
 
120
 
119
 
121
				@Override
120
				public void onResponseReceived(Request request,
122
				public void onResponseReceived(Request request,
121
						Response response) {
123
						Response response) {
122
 
124
 
123
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
125
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
124
						r.rafraichir("Impossible de géolocaliser cette observation", false);
126
						r.rafraichir("Impossible de géolocaliser cette observation", false);
125
					} else {
127
					} else {
126
						
128
						
127
						if(response.getText().equals("")) {
129
						if(response.getText().equals("")) {
128
							EntiteGeographiqueObservation infos;
130
							EntiteGeographiqueObservation infos;
129
							String idLocalite = "";
131
							String idLocalite = "";
130
							String nomCommune = "";
132
							String nomCommune = "";
131
 
133
 
132
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
134
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
133
							infos.setLat("");
135
							infos.setLat("");
134
							infos.setLon("");
136
							infos.setLon("");
135
		
137
		
136
							r.rafraichir(infos, false);
138
							r.rafraichir(infos, false);
137
						}
139
						}
138
													
140
													
139
						final JSONValue responseValue = JSONParser
141
						final JSONValue responseValue = JSONParser
140
								.parse(response.getText());
142
								.parse(response.getText());
141
						
143
						
142
						if (responseValue.isObject() != null) {
144
						if (responseValue.isObject() != null) {
143
							
145
							
144
							EntiteGeographiqueObservation infos;
146
							EntiteGeographiqueObservation infos;
145
							String idLocalite = "";
-
 
146
							String nomCommune = "";
-
 
147
							Double lng = 0.0;
147
							Double lng = 0.0;
148
							Double lat = 0.0;
148
							Double lat = 0.0;
149
								
149
								
150
							JSONObject objectRetour = responseValue.isObject();
150
							JSONObject objectRetour = responseValue.isObject();
151
							
-
 
152
							if(objectRetour.get("nom").isString() != null) {
151
							
153
								nomCommune = objectRetour.get("nom").isString().stringValue();
-
 
154
							}
-
 
155
							
-
 
156
							if(objectRetour.get("code_insee").isString() != null) {
152
							String nomCommune = Util.getValeurJsonOuVide(objectRetour, "nom");
157
								idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
-
 
158
							}
-
 
159
							
-
 
160
							if(objectRetour.get("lat").isNumber() != null) {
153
							String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_insee");
161
								lat = objectRetour.get("lat").isNumber().doubleValue();
-
 
162
							}
-
 
163
							
-
 
164
							if(objectRetour.get("lng").isNumber() != null) {
154
							lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0;
165
								lng = objectRetour.get("lng").isNumber().doubleValue();
-
 
166
							}
-
 
167
 
155
							lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0;
168
							
156
		
169
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
157
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
170
							infos.setLat(""+lat);
158
							infos.setLat(""+lat);
171
							infos.setLon(""+lng);
159
							infos.setLon(""+lng);
172
							
160
							
173
							r.rafraichir(infos, false);
161
							r.rafraichir(infos, false);
174
						}
162
						}
175
					}
163
					}
176
				}
164
				}
177
			});
165
			});
178
		} catch (RequestException e) {
166
		} catch (RequestException e) {
179
			Window.alert(e.getMessage());
167
			Window.alert(e.getMessage());
180
		}
168
		}
181
	}
169
	}
182
}
170
}