Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1918 Rev 2558
Line 18... Line 18...
18
 
18
 
Line 19... Line 19...
19
public class InformationCommuneDAO {
19
public class InformationCommuneDAO {
20
 
20
 
-
 
21
	private final String NOM_SERVICE = "CoordSearch";
-
 
22
	private final String CODE_PAYS = "FR";
-
 
23
	
-
 
24
	// Ce DAO peut être fréquemment sollicité lors de l'utilisation de la carte
Line 21... Line 25...
21
	private final String NOM_SERVICE = "CoordSearch";
25
	// et peut empiler beaucoup de longues requête, ceci permet donc de les annuler facilement
Line 22... Line 26...
22
	private final String CODE_PAYS = "FR";
26
	private static Request requeteEnCours = null;
23
 
27
 
24
	Rafraichissable r = null;
28
	Rafraichissable r = null;
Line 25... Line 29...
25
 
29
 
26
	public InformationCommuneDAO(Rafraichissable r) {
30
	public InformationCommuneDAO(Rafraichissable r) {
-
 
31
		this.r = r;
-
 
32
	}
Line 27... Line 33...
27
		this.r = r;
33
 
28
	}
34
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
29
 
35
			final double lat) {	
30
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
36
		
Line 31... Line 37...
31
			final double lat) {
37
		annulerRequeteEnCours();
32
 
38
 
Line 33... Line 39...
33
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
39
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
34
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
40
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
35
				+ URL.encode("" + lng) + "/*/*/";
41
				+ URL.encode("" + lng) + "/*/*/";
-
 
42
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
36
		RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel);
43
 
Line 37... Line 44...
37
 
44
		try {
38
		try {
45
			requeteEnCours = rb.sendRequest(null, new RequestCallback() {
39
			rb.sendRequest(null, new RequestCallback() {
46
 
Line 40... Line 47...
40
 
47
				@Override
41
				@Override
48
				public void onError(Request request, Throwable exception) {
42
				public void onError(Request request, Throwable exception) {
49
					Window.alert(exception.getMessage());
-
 
50
					requeteEnCours = null;
Line 43... Line 51...
43
					Window.alert(exception.getMessage());
51
				}
44
				}
52
 
45
 
53
				@Override
Line 73... Line 81...
73
							
81
							
74
							if(objectRetour.get("nom").isString() != null) {
82
							if(objectRetour.get("nom").isString() != null) {
75
								nomCommune = objectRetour.get("nom").isString().stringValue();
83
								nomCommune = objectRetour.get("nom").isString().stringValue();
Line 76... Line 84...
76
							}
84
							}
77
							
85
							
-
 
86
							if(objectRetour.get("code_zone").isString() != null) {
-
 
87
								idLocalite = objectRetour.get("code_zone").isString().stringValue().substring(0, 2);
-
 
88
							}
-
 
89
							
78
							if(objectRetour.get("code_insee").isString() != null) {
90
							if(objectRetour.get("code_pays").isString() != null) {
79
								idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
91
								pays = objectRetour.get("code_pays").isString().stringValue();
80
							}
92
							}
Line 81... Line 93...
81
						}
93
						}
82
					}
94
					}
83
					
95
					
-
 
96
					infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
Line 84... Line 97...
84
					infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
97
					infos.setLat(""+lat);
85
					infos.setLat(""+lat);
98
					infos.setLon(""+lng);
86
					infos.setLon(""+lng);
99
					infos.setPays(pays);
87
 
100
 
88
					r.rafraichir(infos, false);
101
					r.rafraichir(infos, false);
89
				}
102
				}
90
			});
103
			});
Line 91... Line 104...
91
		} catch (RequestException e) {
104
		} catch (RequestException e) {
92
			Window.alert(e.getMessage());
105
			Window.alert(e.getMessage());
Line -... Line 106...
-
 
106
		}
-
 
107
	}
93
		}
108
 
94
	}
109
	public void obtenirInfosCommune(final Rafraichissable r,
Line 95... Line 110...
95
 
110
			String valeurCommune, String codeLoc) {
96
	public void obtenirInfosCommune(final Rafraichissable r,
111
		
97
			String valeurCommune, String codePostal) {
112
		annulerRequeteEnCours();
Line 98... Line 113...
98
		
113
		
99
		codePostal = codePostal.replaceAll("000null", "*");
114
		codeLoc = codeLoc.replaceAll("000null", "*");
-
 
115
		codeLoc = codeLoc.replaceAll("\"", "");
-
 
116
		
-
 
117
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
100
		codePostal = codePostal.replaceAll("\"", "");
118
		valeurCommune = valeurCommune.replaceAll("000null", "*");
-
 
119
		valeurCommune = valeurCommune.replaceAll("\"", "");
-
 
120
		
-
 
121
		
101
		
122
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
-
 
123
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/";
-
 
124
		
-
 
125
		if(!codeLoc.trim().isEmpty()) {
-
 
126
			// Cas du code de département ou postal
102
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
127
			if(Util.estUnNombre(codeLoc)) {
Line 103... Line 128...
103
		valeurCommune = valeurCommune.replaceAll("000null", "*");
128
				adresseAppel += URL.encode(codeLoc)+"/"+URL.encode(CODE_PAYS);
104
		valeurCommune = valeurCommune.replaceAll("\"", "");
129
			} else {
Line 105... Line 130...
105
		
130
				//cas du code pays
106
		
131
				adresseAppel += "*/"+URL.encode(codeLoc);
107
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
132
			}
108
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
133
			
Line 135... Line 160...
135
							Double lat = 0.0;
160
							Double lat = 0.0;
Line 136... Line 161...
136
								
161
								
Line 137... Line 162...
137
							JSONObject objectRetour = responseValue.isObject();
162
							JSONObject objectRetour = responseValue.isObject();
138
							
163
							
139
							String nomCommune = Util.getValeurJsonOuVide(objectRetour, "nom");
164
							String nomCommune = Util.getValeurJsonOuVide(objectRetour, "nom");
140
							String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_insee");
165
							String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_zone");
-
 
166
							lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0;
141
							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;
142
							lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0;
168
							String pays = Util.getValeurJsonOuVide(objectRetour, "code_pays");
143
		
169
							
144
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
170
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
-
 
171
							infos.setLat(""+lat);
Line 145... Line 172...
145
							infos.setLat(""+lat);
172
							infos.setLon(""+lng);
146
							infos.setLon(""+lng);
173
							infos.setPays(pays);
147
							
174
							
148
							r.rafraichir(infos, false);
175
							r.rafraichir(infos, false);
Line 162... Line 189...
162
			});
189
			});
163
		} catch (RequestException e) {
190
		} catch (RequestException e) {
164
			Window.alert(e.getMessage());
191
			Window.alert(e.getMessage());
165
		}
192
		}
166
	}
193
	}
-
 
194
	
-
 
195
	public static void annulerRequeteEnCours() {
-
 
196
		if(requeteEnCours != null) {
-
 
197
			requeteEnCours.cancel();
-
 
198
			requeteEnCours = null;
-
 
199
		}
-
 
200
	}
167
}
201
}