Subversion Repositories eFlore/Applications.cel

Rev

Rev 239 | Rev 258 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 239 Rev 253
Line 1... Line 1...
1
package org.tela_botanica.client.modeles;
1
package org.tela_botanica.client.modeles;
Line 2... Line 2...
2
 
2
 
3
import org.tela_botanica.client.interfaces.Rafraichissable;
-
 
Line 4... Line 3...
4
import org.tela_botanica.client.observation.ObservationModele;
3
import org.tela_botanica.client.interfaces.Rafraichissable;
5
 
4
 
6
import com.google.gwt.http.client.Request;
5
import com.google.gwt.http.client.Request;
7
import com.google.gwt.http.client.RequestBuilder;
6
import com.google.gwt.http.client.RequestBuilder;
Line 14... Line 13...
14
import com.google.gwt.json.client.JSONParser;
13
import com.google.gwt.json.client.JSONParser;
15
import com.google.gwt.json.client.JSONValue;
14
import com.google.gwt.json.client.JSONValue;
16
import com.google.gwt.maps.client.geom.LatLng;
15
import com.google.gwt.maps.client.geom.LatLng;
17
import com.google.gwt.user.client.Window;
16
import com.google.gwt.user.client.Window;
Line 18... Line -...
18
 
-
 
19
 
-
 
20
 
17
 
21
public class InformationCommuneDAO {
18
public class InformationCommuneDAO {
22
	
19
 
23
	private final String NOM_SERVICE = "CoordSearch";
20
	private final String NOM_SERVICE = "CoordSearch";
24
	private final String CODE_PAYS = "FR";
21
	private final String CODE_PAYS = "FR";
25
	
22
 
26
	ObservationModele oModele = null;
23
	Rafraichissable r = null;
27
	
24
 
28
	public InformationCommuneDAO(ObservationModele m) {
25
	public InformationCommuneDAO(Rafraichissable r) {
29
		oModele = m ;
26
		this.r = r;
Line 30... Line 27...
30
	}
27
	}
-
 
28
 
31
 
29
	public void obtenirCommunePlusProche(final Rafraichissable r, double lng,
32
	public void obtenirCommunePlusProche(final Rafraichissable r, double lng,double lat) {
30
			double lat) {
-
 
31
 
-
 
32
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
33
		
33
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
34
	      String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/"+URL.encode(""+lat)+"/"+URL.encode(""+lng)+"/*/*/";
34
				+ URL.encode("" + lng) + "/*/*/";
35
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(adresseAppel));
35
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
36
		
36
 
37
		try {
37
		try {
38
				rb.sendRequest(null, new RequestCallback() {
38
			rb.sendRequest(null, new RequestCallback() {
39
				
39
 
40
				public void onError(Request request, Throwable exception) {				
40
				public void onError(Request request, Throwable exception) {
41
					Window.alert(exception.getMessage());
41
					Window.alert(exception.getMessage());
42
				}
42
				}
-
 
43
 
43
	
44
				public void onResponseReceived(Request request,
44
				public void onResponseReceived(Request request, Response response) {
45
						Response response) {
45
					
46
 
46
					if(response.getStatusCode() == Response.SC_BAD_REQUEST) {
-
 
47
						Window.alert("Requete mal formée");
47
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
48
					}
48
						Window.alert("Requete mal formée");
-
 
49
					} else {
49
					else {
50
						final JSONValue responseValue = JSONParser
50
						final JSONValue responseValue = JSONParser.parse(response.getText());
51
								.parse(response.getText());
51
						
52
 
52
						if(responseValue.isObject() != null) {
53
						if (responseValue.isObject() != null) {
-
 
54
							JSONObject resultat = responseValue.isObject();
53
							JSONObject resultat = responseValue.isObject() ;
55
							JSONArray tableauCommune = resultat.get("geonames")
-
 
56
									.isArray();
-
 
57
							JSONObject objetCommune = tableauCommune.get(0)
-
 
58
									.isObject();
-
 
59
 
-
 
60
							/*String valeurCommune = objetCommune.get("name")
-
 
61
									.isString().stringValue();
-
 
62
							r.rafraichir(valeurCommune, false);*/
-
 
63
							
Line -... Line 64...
-
 
64
							String pays = objetCommune.get("countryName").isString()
-
 
65
							.stringValue();
-
 
66
							
-
 
67
							if(!pays.equals("France")){
-
 
68
								r.rafraichir("Aucune commune française n'a été trouvée à cet emplacement", false);
-
 
69
								return;
-
 
70
							}
54
							JSONArray tableauCommune = resultat.get("geonames").isArray();
71
								
-
 
72
							Double lng = objetCommune.get("lng").isNumber()
-
 
73
							.doubleValue();
-
 
74
							Double lat = objetCommune.get("lat").isNumber()
-
 
75
									.doubleValue();
-
 
76
							String nomCommune = objetCommune.get(
-
 
77
									"name").isString().stringValue();
-
 
78
							
-
 
79
							String idLocalite = objetCommune.get(
-
 
80
							"adminCode2").isString().stringValue();
55
							JSONObject objetCommune = tableauCommune.get(0).isObject();
81
		
56
							
82
							Object[] infos = { lat, lng, idLocalite, nomCommune};
57
							String valeurCommune = objetCommune.get("name").isString().stringValue();
83
		
58
							r.rafraichir(valeurCommune, false);
84
							r.rafraichir(infos, false);
59
						}
85
						}
60
					}
-
 
61
				}	
86
					}
62
			});
87
				}
63
		}
88
			});
64
		catch (RequestException e) { 
89
		} catch (RequestException e) {
65
			Window.alert(e.getMessage());
-
 
66
		}
-
 
Line 67... Line 90...
67
	}
90
			Window.alert(e.getMessage());
-
 
91
		}
-
 
92
	}
-
 
93
 
-
 
94
	public void obtenirInfosCommune(final Rafraichissable r,
-
 
95
			String valeurCommune, String codePostal) {
-
 
96
		
-
 
97
		codePostal = codePostal.replaceAll("000null", "*");
68
	
98
		codePostal = codePostal.replaceAll("\"", "");
69
 
99
		valeurCommune = valeurCommune.replaceAll("000null", "*");
-
 
100
		valeurCommune = valeurCommune.replaceAll("\"", "");
-
 
101
		
-
 
102
		
70
 
103
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
Line 71... Line 104...
71
	public void obtenirInfosCommune(final Rafraichissable r, String valeurCommune) {
104
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
72
 
105
				+ URL.encode(codePostal) + "/"
73
		String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/*/*/"+URL.encode(valeurCommune)+"/"+CODE_PAYS;
106
				+ URL.encode(CODE_PAYS);
74
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(adresseAppel));
107
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
75
 
108
 
76
		try {
109
		try {
77
			rb.sendRequest(null, new RequestCallback() {
110
			rb.sendRequest(null, new RequestCallback() {
78
			
111
 
-
 
112
				public void onError(Request request, Throwable exception) {
79
				public void onError(Request request, Throwable exception) {
113
					Window.alert(exception.getMessage());
80
					Window.alert(exception.getMessage());			
114
				}
81
				}
115
 
82
	
-
 
83
				public void onResponseReceived(Request request, Response response) {
116
				public void onResponseReceived(Request request,
84
					
117
						Response response) {
-
 
118
 
85
					if(response.getStatusCode() == Response.SC_BAD_REQUEST) {
119
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
86
						Window.alert("Requete mal formée");
120
						r.rafraichir("Impossible de géolocaliser cette observation", false);
-
 
121
					} else {
87
					}
122
						final JSONValue responseValue = JSONParser
88
					else {
123
								.parse(response.getText());
-
 
124
 
89
						final JSONValue responseValue = JSONParser.parse(response.getText());
125
						if (responseValue.isObject() != null) {
90
						
126
 
91
						if(responseValue.isObject() != null) {
127
							JSONObject resultat = responseValue.isObject();
-
 
128
							JSONArray tableauCommune = resultat.get(
-
 
129
									"postalCodes").isArray();
-
 
130
 
-
 
131
							if (tableauCommune.get(0) != null) {
-
 
132
								JSONObject objetCommune = tableauCommune.get(0)
-
 
133
										.isObject();
-
 
134
 
-
 
135
								Double lng = objetCommune.get("lng").isNumber()
Line 92... Line 136...
92
							JSONObject resultat = responseValue.isObject() ;
136
										.doubleValue();
93
							JSONArray tableauCommune = resultat.get("postalCodes").isArray();
137
								Double lat = objetCommune.get("lat").isNumber()
-
 
138
										.doubleValue();
94
							
139
								String nomCommune = objetCommune.get(
-
 
140
										"placeName").isString().stringValue();
95
							if(tableauCommune.get(0) != null) {
141
								
96
								JSONObject objetCommune = tableauCommune.get(0).isObject();
142
								String idLocalite = objetCommune.get(
97
								
143
								"adminCode2").isString().stringValue();
98
								double lng = objetCommune.get("lng").isNumber().doubleValue();
144
 
99
								double lat = objetCommune.get("lat").isNumber().doubleValue();
145
								Object[] infos = { lat, lng, idLocalite, nomCommune};
100
								String nomCommune = objetCommune.get("postalCode").isString().stringValue();
146
 
101
		
147
 
102
								r.rafraichir(nomCommune, false);
-
 
103
							}
148
								r.rafraichir(infos, false);
104
						}
149
							}
105
					}
150
						}
106
				}	
151
					}
107
			});
152
				}
108
		}
-