Subversion Repositories eFlore/Applications.cel

Rev

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

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