Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2653 Rev 2657
1
package org.tela_botanica.client.util;
1
package org.tela_botanica.client.util;
2
 
2
 
3
import java.text.NumberFormat;
3
import java.text.NumberFormat;
4
import java.text.ParsePosition;
4
import java.text.ParsePosition;
5
import java.util.ArrayList;
5
import java.util.ArrayList;
6
import java.util.Collections;
6
import java.util.Collections;
7
import java.util.Comparator;
7
import java.util.Comparator;
8
import java.util.HashMap;
8
import java.util.HashMap;
9
import java.util.Iterator;
9
import java.util.Iterator;
10
import java.util.List;
10
import java.util.List;
11
import java.util.Map;
11
import java.util.Map;
12
import java.util.Vector;
12
import java.util.Vector;
13
 
13
 
14
import org.tela_botanica.client.i18n.Msg;
14
import org.tela_botanica.client.i18n.Msg;
15
import org.tela_botanica.client.modeles.objets.ChampEtendu;
15
import org.tela_botanica.client.modeles.objets.ChampEtendu;
16
import org.tela_botanica.client.modeles.objets.Observation;
16
import org.tela_botanica.client.modeles.objets.Observation;
17
import org.tela_botanica.client.modeles.objets.ReferentielLocalite;
17
import org.tela_botanica.client.modeles.objets.ReferentielLocalite;
18
import org.tela_botanica.client.modeles.objets.ReferentielNom;
18
import org.tela_botanica.client.modeles.objets.ReferentielNom;
19
 
-
 
20
import com.google.gwt.dom.client.Element;
19
 
21
import com.google.gwt.http.client.Response;
20
import com.google.gwt.http.client.Response;
22
import com.google.gwt.json.client.JSONArray;
21
import com.google.gwt.json.client.JSONArray;
23
import com.google.gwt.json.client.JSONObject;
22
import com.google.gwt.json.client.JSONObject;
24
import com.google.gwt.json.client.JSONParser;
23
import com.google.gwt.json.client.JSONParser;
25
import com.google.gwt.json.client.JSONString;
24
import com.google.gwt.json.client.JSONString;
26
import com.google.gwt.json.client.JSONValue;
25
import com.google.gwt.json.client.JSONValue;
27
import com.google.gwt.user.client.DOM;
-
 
28
import com.google.gwt.user.client.ui.RootPanel;
26
import com.google.gwt.user.client.ui.RootPanel;
29
 
27
 
30
public class Util {
28
public class Util {
31
 
29
 
32
	public Util() {
30
	public Util() {
33
	}
31
	}
34
	
32
	
35
	public static String getValeurJsonOuVide(JSONObject jo, String index) {
33
	public static String getValeurJsonOuVide(JSONObject jo, String index) {
36
		return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
34
		return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
37
	}
35
	}
38
	
36
	
39
	public static Map<String, ChampEtendu> getMapValeursOuVide(JSONObject jo, String index) {
37
	public static Map<String, ChampEtendu> getMapValeursOuVide(JSONObject jo, String index) {
40
		Map<String, ChampEtendu> mapValeurs = new HashMap<String, ChampEtendu>();
38
		Map<String, ChampEtendu> mapValeurs = new HashMap<String, ChampEtendu>();
41
		if(jo.get(index) != null && jo.get(index).isArray() != null) {	
39
		if(jo.get(index) != null && jo.get(index).isArray() != null) {	
42
			JSONArray tabJo = jo.get(index).isArray();
40
			JSONArray tabJo = jo.get(index).isArray();
43
			for (int i = 0; i < tabJo.size(); i++) {
41
			for (int i = 0; i < tabJo.size(); i++) {
44
				JSONObject champJson = tabJo.get(i).isObject();
42
				JSONObject champJson = tabJo.get(i).isObject();
45
				String cle = champJson.get("cle").isString().stringValue();
43
				String cle = champJson.get("cle").isString().stringValue();
46
				String label = cle;
44
				String label = cle;
47
				String valeur = champJson.get("valeur").isString().stringValue();
45
				String valeur = champJson.get("valeur").isString().stringValue();
48
				ChampEtendu champ = new ChampEtendu(cle, label, valeur);
46
				ChampEtendu champ = new ChampEtendu(cle, label, valeur);
49
				mapValeurs.put(cle, champ);
47
				mapValeurs.put(cle, champ);
50
			}
48
			}
51
 
49
 
52
		}
50
		}
53
		return mapValeurs;
51
		return mapValeurs;
54
	}
52
	}
55
	
53
	
56
	public static String convertirChampsEtendusEnChaineRequete(Map<String, ChampEtendu> map) {
54
	public static String convertirChampsEtendusEnChaineRequete(Map<String, ChampEtendu> map) {
57
		String json = "";
55
		String json = "";
58
	    if (map != null && !map.isEmpty()) {
56
	    if (map != null && !map.isEmpty()) {
59
	    	JSONArray jsonArr = new JSONArray();
57
	    	JSONArray jsonArr = new JSONArray();
60
	    	int i = 0;
58
	    	int i = 0;
61
	        for (Map.Entry<String, ChampEtendu> entry: map.entrySet()) {
59
	        for (Map.Entry<String, ChampEtendu> entry: map.entrySet()) {
62
	        	jsonArr.set(i, convertirChampEtenduEnJson(entry.getValue()));
60
	        	jsonArr.set(i, convertirChampEtenduEnJson(entry.getValue()));
63
	        	i++;
61
	        	i++;
64
	        }
62
	        }
65
	        json = jsonArr.toString();
63
	        json = jsonArr.toString();
66
	    }
64
	    }
67
	    return json;
65
	    return json;
68
	}
66
	}
69
	
67
	
70
	public static JSONObject convertirChampEtenduEnJson(ChampEtendu champEtendu) {
68
	public static JSONObject convertirChampEtenduEnJson(ChampEtendu champEtendu) {
71
		JSONObject jsonObj = new JSONObject();
69
		JSONObject jsonObj = new JSONObject();
72
		jsonObj.put("cle", new JSONString(champEtendu.getCle()));
70
		jsonObj.put("cle", new JSONString(champEtendu.getCle()));
73
		jsonObj.put("label", new JSONString(champEtendu.getLabel()));
71
		jsonObj.put("label", new JSONString(champEtendu.getLabel()));
74
		jsonObj.put("valeur", new JSONString(champEtendu.getValeur()));
72
		jsonObj.put("valeur", new JSONString(champEtendu.getValeur()));
75
        return jsonObj;
73
        return jsonObj;
76
	}
74
	}
77
	
75
	
78
	public static boolean jsonNonNull(JSONObject jo, String index) {
76
	public static boolean jsonNonNull(JSONObject jo, String index) {
79
		return (jo != null && 
77
		return (jo != null && 
80
				jo.get(index) != null && 
78
				jo.get(index) != null && 
81
				jo.get(index).isNull() == null
79
				jo.get(index).isNull() == null
82
			   );			
80
			   );			
83
	}
81
	}
84
	
82
	
85
	public static String formaterLieu(Observation obs, String modeleLieu) {
83
	public static String formaterLieu(Observation obs, String modeleLieu) {
86
		
84
		
87
		String lieuModele = modeleLieu;
85
		String lieuModele = modeleLieu;
88
		
86
		
89
		String localite = obs.getLocalite(); 
87
		String localite = obs.getLocalite(); 
90
		String lieuDit = obs.getLieudit();
88
		String lieuDit = obs.getLieudit();
91
		String station = obs.getStation();
89
		String station = obs.getStation();
92
		
90
		
93
		String lieulocaliteFormate = "";
91
		String lieulocaliteFormate = "";
94
		String lieuDitFormate = "";
92
		String lieuDitFormate = "";
95
		String stationFormatee = "";
93
		String stationFormatee = "";
96
		
94
		
97
		if(localite != null && !localite.contains("000null") && !localite.trim().equals("")) {
95
		if(localite != null && !localite.contains("000null") && !localite.trim().equals("")) {
98
			String	idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
96
			String	idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
99
			if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
97
			if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
100
 
98
 
101
				idLoc = idLoc.replaceAll("%","");
99
				idLoc = idLoc.replaceAll("%","");
102
				idLoc = idLoc.replaceAll("\"","");
100
				idLoc = idLoc.replaceAll("\"","");
103
				idLoc = idLoc.replace('\\',' ');
101
				idLoc = idLoc.replace('\\',' ');
104
				idLoc = idLoc.trim();
102
				idLoc = idLoc.trim();
105
				if(idLoc.length() > 2) {
103
				if(idLoc.length() > 2) {
106
					idLoc = idLoc.substring(0,2);
104
					idLoc = idLoc.substring(0,2);
107
				}
105
				}
108
				lieulocaliteFormate += idLoc+" - ";
106
				lieulocaliteFormate += idLoc+" - ";
109
			}
107
			}
110
			lieulocaliteFormate += localite;
108
			lieulocaliteFormate += localite;
111
			lieuModele = lieuModele.replaceAll("IDLOCLOCALITE", lieulocaliteFormate);
109
			lieuModele = lieuModele.replaceAll("IDLOCLOCALITE", lieulocaliteFormate);
112
		} else {
110
		} else {
113
			
111
			
114
			lieuModele = lieuModele.replaceAll("IDLOCLOCALITE,", lieulocaliteFormate);
112
			lieuModele = lieuModele.replaceAll("IDLOCLOCALITE,", lieulocaliteFormate);
115
		}
113
		}
116
		
114
		
117
		if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
115
		if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
118
			lieuDitFormate += lieuDit;
116
			lieuDitFormate += lieuDit;
119
			lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
117
			lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
120
		} else {
118
		} else {
121
			lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
119
			lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
122
		}
120
		}
123
		
121
		
124
		if(station != null && !station.contains("000null") && !station.trim().equals("")) {
122
		if(station != null && !station.contains("000null") && !station.trim().equals("")) {
125
			stationFormatee += station;
123
			stationFormatee += station;
126
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
124
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
127
		} else {
125
		} else {
128
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
126
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
129
		}
127
		}
130
		
128
		
131
		lieuModele = lieuModele.trim();
129
		lieuModele = lieuModele.trim();
132
		lieuModele = lieuModele.replaceAll(",$","");
130
		lieuModele = lieuModele.replaceAll(",$","");
133
		lieuModele = lieuModele.replaceAll(",^$",", ");
131
		lieuModele = lieuModele.replaceAll(",^$",", ");
134
		
132
		
135
		return lieuModele;
133
		return lieuModele;
136
	}
134
	}
137
	
135
	
138
	public static String obtenirIdLocAPartirChaineLocalite(String localite) {
136
	public static String obtenirIdLocAPartirChaineLocalite(String localite) {
139
		
137
		
140
		String idLoc = "";	
138
		String idLoc = "";	
141
		String[] depCom = localite.split(" ");
139
		String[] depCom = localite.split(" ");
142
		if(depCom.length > 1) {
140
		if(depCom.length > 1) {
143
			idLoc = depCom[1].replace('(', ' ');
141
			idLoc = depCom[1].replace('(', ' ');
144
		} else {
142
		} else {
145
			idLoc = "";
143
			idLoc = "";
146
		}
144
		}
147
		
145
		
148
		idLoc = idLoc.replace(')', ' ');
146
		idLoc = idLoc.replace(')', ' ');
149
		idLoc = idLoc.trim();
147
		idLoc = idLoc.trim();
150
		idLoc = idLoc.replace('\\',' ');
148
		idLoc = idLoc.replace('\\',' ');
151
		idLoc = idLoc.trim();
149
		idLoc = idLoc.trim();
152
		
150
		
153
		return idLoc;
151
		return idLoc;
154
	}
152
	}
155
	
153
	
156
	public static String formaterDepartement(String depAFormater) {	
154
	public static String formaterDepartement(String depAFormater) {	
157
		String dep = "";
155
		String dep = "";
158
		try
156
		try
159
		{
157
		{
160
			int nDep = Integer.parseInt(depAFormater);
158
			int nDep = Integer.parseInt(depAFormater);
161
			if(nDep > 0 && nDep < 110) {
159
			if(nDep > 0 && nDep < 110) {
162
				dep = depAFormater ;
160
				dep = depAFormater ;
163
			}
161
			}
164
			
162
			
165
			if(depAFormater.length() == 4) {
163
			if(depAFormater.length() == 4) {
166
				dep = "0"+depAFormater;
164
				dep = "0"+depAFormater;
167
			}
165
			}
168
			
166
			
169
			dep = depAFormater.substring(0,2);
167
			dep = depAFormater.substring(0,2);
170
		}
168
		}
171
		catch(NumberFormatException e)
169
		catch(NumberFormatException e)
172
		{
170
		{
173
			// rien à faire
171
			// rien à faire
174
		}
172
		}
175
		
173
		
176
		return dep;
174
		return dep;
177
	}
175
	}
178
	
176
	
179
	public static String supprimerChaineIdLocalite(String chaineLocaliteComplete) {
177
	public static String supprimerChaineIdLocalite(String chaineLocaliteComplete) {
180
		return chaineLocaliteComplete.replaceAll(" \\([a-zA-Z0-9]*\\)", "");
178
		return chaineLocaliteComplete.replaceAll(" \\([a-zA-Z0-9]*\\)", "");
181
	}
179
	}
182
	
180
	
183
	public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) {
181
	public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) {
184
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("") && chaineZoneGeo.replaceAll("INSEE-C:", "").length() >= 2) ? 
182
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("") && chaineZoneGeo.replaceAll("INSEE-C:", "").length() >= 2) ? 
185
						chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2) : 
183
						chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2) : 
186
						chaineZoneGeo;
184
						chaineZoneGeo;
187
	}
185
	}
188
	
186
	
189
	public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) {
187
	public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) {
190
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo;
188
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo;
191
	}
189
	}
192
	
190
	
193
	/***
191
	/***
194
	 * Fusionne les éléments d'un tableau en une chaîne
192
	 * Fusionne les éléments d'un tableau en une chaîne
195
	 * @param delim : la chaîne de séparation
193
	 * @param delim : la chaîne de séparation
196
	 * @param args : la tableau
194
	 * @param args : la tableau
197
	 * @return la chaîne fusionnée
195
	 * @return la chaîne fusionnée
198
	 */
196
	 */
199
	public static String implode(String delim, String[] args){
197
	public static String implode(String delim, String[] args){
200
		StringBuffer sb = new StringBuffer();
198
		StringBuffer sb = new StringBuffer();
201
		
199
		
202
		int lgArgs = args.length;
200
		int lgArgs = args.length;
203
		
201
		
204
		for(int i = 0; i < lgArgs; i++){
202
		for(int i = 0; i < lgArgs; i++){
205
			if (i > 0) {
203
			if (i > 0) {
206
				sb.append(delim);
204
				sb.append(delim);
207
			}
205
			}
208
			
206
			
209
			sb.append(args[i]);
207
			sb.append(args[i]);
210
		}
208
		}
211
		
209
		
212
		return sb.toString();
210
		return sb.toString();
213
	}
211
	}
214
	
212
	
215
	public static boolean filtreValide(String[] filtre) {
213
	public static boolean filtreValide(String[] filtre) {
216
		
214
		
217
		return (filtre.length == 2 && 
215
		return (filtre.length == 2 && 
218
		filtre[0] != null &&
216
		filtre[0] != null &&
219
		!filtre[0].equals("") &&
217
		!filtre[0].equals("") &&
220
		filtre[1] != null &&
218
		filtre[1] != null &&
221
		!filtre[1].equals(""));
219
		!filtre[1].equals(""));
222
	}
220
	}
223
	
221
	
224
	public static String renvoyerMois(int numMois) {
222
	public static String renvoyerMois(int numMois) {
225
			
223
			
226
		if(numMois >= 1 && numMois <= 12) {
224
		if(numMois >= 1 && numMois <= 12) {
227
			return Msg.get("mois-"+numMois);
225
			return Msg.get("mois-"+numMois);
228
		} else {
226
		} else {
229
			return Msg.get("mois-inconnu") ;
227
			return Msg.get("mois-inconnu") ;
230
		}
228
		}
231
	}
229
	}
232
	
230
	
233
	public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) {
231
	public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) {
234
		
232
		
235
		String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%');
233
		String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%');
236
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%");
234
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%");
237
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n');
235
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n');
238
		
236
		
239
		return chaineAvecSautsDeLignesBienEncodes;
237
		return chaineAvecSautsDeLignesBienEncodes;
240
	}
238
	}
241
	
239
	
242
	public static boolean verifierDateFormatCel(String dateAVerifier) {
240
	public static boolean verifierDateFormatCel(String dateAVerifier) {
243
		
241
		
244
		String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier);	
242
		String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier);	
245
		String[] tabDate = dateRemplacee.split("/");
243
		String[] tabDate = dateRemplacee.split("/");
246
		
244
		
247
		boolean retour = false;
245
		boolean retour = false;
248
		
246
		
249
		if(tabDate.length == 3) {
247
		if(tabDate.length == 3) {
250
			//TODO: faire un parsing de date qui fonctionne mieux car 
248
			//TODO: faire un parsing de date qui fonctionne mieux car 
251
			// on peut saisir un 31 novembre par exemple
249
			// on peut saisir un 31 novembre par exemple
252
			// mais l'api date de java est mal gérée par gwt
250
			// mais l'api date de java est mal gérée par gwt
253
			try {
251
			try {
254
				int jour = Integer.parseInt(tabDate[0]);
252
				int jour = Integer.parseInt(tabDate[0]);
255
				int mois = Integer.parseInt(tabDate[1]);
253
				int mois = Integer.parseInt(tabDate[1]);
256
				int annee = Integer.parseInt(tabDate[2]);
254
				int annee = Integer.parseInt(tabDate[2]);
257
				
255
				
258
				if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) {
256
				if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) {
259
					retour = true;
257
					retour = true;
260
				}
258
				}
261
			} catch (Exception e) {
259
			} catch (Exception e) {
262
 
260
 
263
			}
261
			}
264
		}
262
		}
265
		    
263
		    
266
		return retour;
264
		return retour;
267
	}
265
	}
268
	
266
	
269
	public static String remplacerSeparateursDateFormatCel(String date) {
267
	public static String remplacerSeparateursDateFormatCel(String date) {
270
		
268
		
271
		String dateRemplacee = date.replaceAll("-", "/");
269
		String dateRemplacee = date.replaceAll("-", "/");
272
	    
270
	    
273
	    return dateRemplacee;
271
	    return dateRemplacee;
274
	}
272
	}
275
	
273
	
276
	public static boolean estZero(String s) {
274
	public static boolean estZero(String s) {
277
		boolean estZero = false;
275
		boolean estZero = false;
278
	    try { 
276
	    try { 
279
	    	Double dou = Double.parseDouble(s); 
277
	    	Double dou = Double.parseDouble(s); 
280
	    	estZero = (dou == 0);
278
	    	estZero = (dou == 0);
281
	    } catch(NumberFormatException e) { 
279
	    } catch(NumberFormatException e) { 
282
 
280
 
283
	    }
281
	    }
284
	    return estZero;
282
	    return estZero;
285
	}
283
	}
286
	
284
	
287
	public static boolean estUnNombre(String s) {
285
	public static boolean estUnNombre(String s) {
288
		//http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java
286
		//http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java
289
		try {  
287
		try {  
290
			double d = Double.parseDouble(s);  
288
			double d = Double.parseDouble(s);  
291
		} catch(NumberFormatException nfe) {  
289
		} catch(NumberFormatException nfe) {  
292
			return false;  
290
			return false;  
293
		}  
291
		}  
294
		return true;  
292
		return true;  
295
	}
293
	}
296
	
294
	
297
	public static String formaterNombre(String s) {
295
	public static String formaterNombre(String s) {
298
		s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
296
		s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
299
		return s;
297
		return s;
300
	}
298
	}
301
 
299
 
302
	// Prend un nombre décimal avec le spéparateur spécifié et le tronque à n décimales
300
	// Prend un nombre décimal avec le spéparateur spécifié et le tronque à n décimales
303
	public static String tronquerNombrePourAffichage(String nombre, int decimales, char separateur) {
301
	public static String tronquerNombrePourAffichage(String nombre, int decimales, char separateur) {
304
		String retour = nombre;
302
		String retour = nombre;
305
		int posSep = nombre.indexOf(separateur);
303
		int posSep = nombre.indexOf(separateur);
306
		if (posSep >= 0) {
304
		if (posSep >= 0) {
307
			int taille = posSep + decimales + 1;
305
			int taille = posSep + decimales + 1;
308
			if (nombre.length() < taille) {
306
			if (nombre.length() < taille) {
309
				taille = nombre.length();
307
				taille = nombre.length();
310
			}
308
			}
311
			retour = nombre.substring(0, taille);
309
			retour = nombre.substring(0, taille);
312
		}
310
		}
313
		return retour;
311
		return retour;
314
	}
312
	}
315
 
313
 
316
	public static String tronquerNombrePourAffichage(String nombre, int decimales) {
314
	public static String tronquerNombrePourAffichage(String nombre, int decimales) {
317
		return tronquerNombrePourAffichage(nombre, decimales, '.');
315
		return tronquerNombrePourAffichage(nombre, decimales, '.');
318
	}
316
	}
319
	
317
	
320
	// Adapté de http://www.programcreek.com/2011/03/java-method-for-spliting-a-camelcase-string/
318
	// Adapté de http://www.programcreek.com/2011/03/java-method-for-spliting-a-camelcase-string/
321
	public static String formaterCleChampsEtenduPourAffichage(String s) {
319
	public static String formaterCleChampsEtenduPourAffichage(String s) {
322
		char[] cArray = s.toCharArray();
320
		char[] cArray = s.toCharArray();
323
		 
321
		 
324
		Vector<Integer> al = new Vector<Integer>();
322
		Vector<Integer> al = new Vector<Integer>();
325
		al.add(0);
323
		al.add(0);
326
	 
324
	 
327
		// get all upper case letter index positions
325
		// get all upper case letter index positions
328
		for (int i = 1; i < cArray.length; i++) {
326
		for (int i = 1; i < cArray.length; i++) {
329
			char c = cArray[i];
327
			char c = cArray[i];
330
			//add more interested index beyond upper case letter
328
			//add more interested index beyond upper case letter
331
			if (c >= 65 && c <= 90) {
329
			if (c >= 65 && c <= 90) {
332
				al.add(i);
330
				al.add(i);
333
			}
331
			}
334
		}
332
		}
335
	 
333
	 
336
		Vector<String> strl = new Vector<String>();
334
		Vector<String> strl = new Vector<String>();
337
	 
335
	 
338
		// this handles the all lower letter case
336
		// this handles the all lower letter case
339
		if (al.size() == 1) {
337
		if (al.size() == 1) {
340
			strl.add(s);
338
			strl.add(s);
341
			return depilerChaineListee(strl, " ");
339
			return depilerChaineListee(strl, " ");
342
		}
340
		}
343
	 
341
	 
344
	 
342
	 
345
		int prev = 0;
343
		int prev = 0;
346
		int curr = 0;
344
		int curr = 0;
347
		int begin = 0;
345
		int begin = 0;
348
		for (int k = 1; k < al.size(); k++) {
346
		for (int k = 1; k < al.size(); k++) {
349
	 
347
	 
350
			curr = al.get(k);
348
			curr = al.get(k);
351
	 
349
	 
352
			if(curr == s.length() - 1){
350
			if(curr == s.length() - 1){
353
	 
351
	 
354
			}
352
			}
355
	 
353
	 
356
			if (curr == prev + 1 && curr != s.length() - 1) {
354
			if (curr == prev + 1 && curr != s.length() - 1) {
357
				prev = curr;
355
				prev = curr;
358
			} else if(curr == prev + 1 &&  curr == s.length() - 1){
356
			} else if(curr == prev + 1 &&  curr == s.length() - 1){
359
				strl.add(s.substring(begin, curr+1));
357
				strl.add(s.substring(begin, curr+1));
360
			}else {
358
			}else {
361
	 
359
	 
362
				strl.add(s.substring(prev, curr));
360
				strl.add(s.substring(prev, curr));
363
				prev = curr;
361
				prev = curr;
364
				begin = curr;
362
				begin = curr;
365
				if (k == al.size() - 1) {
363
				if (k == al.size() - 1) {
366
					strl.add(s.substring(curr, s.length()));
364
					strl.add(s.substring(curr, s.length()));
367
				}
365
				}
368
			}
366
			}
369
		}
367
		}
370
	 
368
	 
371
		return depilerChaineListee(strl, " ");
369
		return depilerChaineListee(strl, " ");
372
	}
370
	}
373
	
371
	
374
	private static String depilerChaineListee(Vector<String> strl, String separateur) {
372
	private static String depilerChaineListee(Vector<String> strl, String separateur) {
375
		String s = "";
373
		String s = "";
376
		for(int i = 0; i < strl.size(); i++) {
374
		for(int i = 0; i < strl.size(); i++) {
377
			s += strl.get(i);
375
			s += strl.get(i);
378
			if(i != strl.size() - 1) {
376
			if(i != strl.size() - 1) {
379
				s += separateur;
377
				s += separateur;
380
			}
378
			}
381
		}
379
		}
382
		return s;
380
		return s;
383
	}
381
	}
384
	
382
	
385
	public static Map<String, ChampEtendu> trierListeChampsEtendus(Map<String, ChampEtendu> listeChampsEtendus) {
383
	public static Map<String, ChampEtendu> trierListeChampsEtendus(Map<String, ChampEtendu> listeChampsEtendus) {
386
		List<String> tmp = new ArrayList<String>(listeChampsEtendus.keySet());
384
		List<String> tmp = new ArrayList<String>(listeChampsEtendus.keySet());
387
		Collections.sort(tmp, new Comparator<String>() {
385
		Collections.sort(tmp, new Comparator<String>() {
388
			 
386
			 
389
		    @Override
387
		    @Override
390
		    public int compare(String arg0, String arg1) {
388
		    public int compare(String arg0, String arg1) {
391
		        return arg0.compareTo(arg1);
389
		        return arg0.compareTo(arg1);
392
		    }
390
		    }
393
		 
391
		 
394
		});
392
		});
395
		return listeChampsEtendus;
393
		return listeChampsEtendus;
396
	}
394
	}
397
	
395
	
398
	/**
396
	/**
399
	 * Solution issue de stackoverflow : 
397
	 * Solution issue de stackoverflow : 
400
	 * http://stackoverflow.com/questions/1143951/what-is-the-simplest-way-to-convert-a-java-string-from-all-caps-words-separated
398
	 * http://stackoverflow.com/questions/1143951/what-is-the-simplest-way-to-convert-a-java-string-from-all-caps-words-separated
401
	 */
399
	 */
402
	public static String convertirEnChaMot(String s) {	
400
	public static String convertirEnChaMot(String s) {	
403
		s = s.replaceAll("_", " ");
401
		s = s.replaceAll("_", " ");
404
		s = s.replaceAll("-", " ");
402
		s = s.replaceAll("-", " ");
405
		String[] parties = s.split(" ");
403
		String[] parties = s.split(" ");
406
		String chaineChaMot = "";
404
		String chaineChaMot = "";
407
		for (String partie : parties){
405
		for (String partie : parties){
408
			chaineChaMot = chaineChaMot + convertirMotEnChaMot(partie);
406
			chaineChaMot = chaineChaMot + convertirMotEnChaMot(partie);
409
		}
407
		}
410
		return chaineChaMot;
408
		return chaineChaMot;
411
	}
409
	}
412
 
410
 
413
	protected static String convertirMotEnChaMot(String s) {
411
	protected static String convertirMotEnChaMot(String s) {
414
		return s.substring(0, 1).toUpperCase() +
412
		return s.substring(0, 1).toUpperCase() +
415
	               s.substring(1).toLowerCase();
413
	               s.substring(1).toLowerCase();
416
	}
414
	}
417
 
415
 
418
	public static void curseurAttente() {
416
	public static void curseurAttente() {
419
	    RootPanel.getBodyElement().getStyle().setProperty("cursor", "wait");
417
	    RootPanel.getBodyElement().getStyle().setProperty("cursor", "wait");
420
	}
418
	}
421
 
419
 
422
	public static void curseurParDefaut() {
420
	public static void curseurParDefaut() {
423
		RootPanel.getBodyElement().getStyle().setProperty("cursor", "default");
421
		RootPanel.getBodyElement().getStyle().setProperty("cursor", "default");
424
	}
422
	}
425
	
423
	
426
	public static Map<String, String> parserRetourReferentielPerso(Response response) {
424
	public static Map<String, String> parserRetourReferentielPerso(Response response) {
427
		final Map<String, String> referentielData = new HashMap<String, String>();
425
		final Map<String, String> referentielData = new HashMap<String, String>();
428
		final JSONValue responseValue = JSONParser.parse(response.getText());
426
		final JSONValue responseValue = JSONParser.parse(response.getText());
429
		
427
		
430
		JSONArray reponse = null;
428
		JSONArray reponse = null;
431
 
429
 
432
		// si c'est un tableau
430
		// si c'est un tableau
433
		if ((reponse = responseValue.isArray()) != null) {
431
		if ((reponse = responseValue.isArray()) != null) {
434
			
432
			
435
			JSONString elementsRef;
433
			JSONString elementsRef;
436
			final int taillemax = reponse.size();
434
			final int taillemax = reponse.size();
437
			
435
			
438
			for (int i = 0; i < taillemax; i++) {
436
			for (int i = 0; i < taillemax; i++) {
439
				if ((elementsRef = reponse.get(i).isString()) != null) {
437
				if ((elementsRef = reponse.get(i).isString()) != null) {
440
					
438
					
441
					String valeur = elementsRef.stringValue();
439
					String valeur = elementsRef.stringValue();
442
					referentielData.put(i+"", valeur);
440
					referentielData.put(i+"", valeur);
443
							
441
							
444
				}	
442
				}	
445
			}
443
			}
446
		}
444
		}
447
		return referentielData;
445
		return referentielData;
448
	}
446
	}
449
	
447
	
450
	public static Map<String, ReferentielNom> parserRetourReferentielNomIndexeParNom(Response response) {
448
	public static Map<String, ReferentielNom> parserRetourReferentielNomIndexeParNom(Response response) {
451
		
449
		
452
		final Map<String, ReferentielNom> referentielNomData ;
450
		final Map<String, ReferentielNom> referentielNomData ;
453
		final JSONValue responseValue = JSONParser.parse(response.getText());
451
		final JSONValue responseValue = JSONParser.parse(response.getText());
454
		JSONArray reponse=null;
452
		JSONArray reponse=null;
455
 
453
 
456
		// si c'est un tableau
454
		// si c'est un tableau
457
		if ((reponse=responseValue.isArray()) != null) {
455
		if ((reponse=responseValue.isArray()) != null) {
458
			
456
			
459
			JSONArray noms;
457
			JSONArray noms;
460
			final int taillemax = reponse.size();
458
			final int taillemax = reponse.size();
461
			
459
			
462
			referentielNomData = new HashMap<String, ReferentielNom>(taillemax);
460
			referentielNomData = new HashMap<String, ReferentielNom>(taillemax);
463
			for (int i = 0; i < taillemax; i++) {
461
			for (int i = 0; i < taillemax; i++) {
464
				if ((noms=reponse.get(i).isArray()) != null) {
462
				if ((noms=reponse.get(i).isArray()) != null) {
465
					String nom = ((JSONString) noms.get(0)).stringValue();
463
					String nom = ((JSONString) noms.get(0)).stringValue();
466
					String numeroNom = ((JSONString) noms.get(1)).stringValue();
464
					String numeroNom = ((JSONString) noms.get(1)).stringValue();
467
					String statut= ((JSONString) noms.get(2)).stringValue();
465
					String statut= ((JSONString) noms.get(2)).stringValue();
468
					ReferentielNom nomScientifique = new ReferentielNom(nom, numeroNom, statut);
466
					ReferentielNom nomScientifique = new ReferentielNom(nom, numeroNom, statut);
469
					// FIXME : et si le numero de nom n'est pas unique ? (cas de multirefrentiel....)					
467
					// FIXME : et si le numero de nom n'est pas unique ? (cas de multirefrentiel....)					
470
					referentielNomData.put(nomScientifique.getNom(),nomScientifique);								
468
					referentielNomData.put(nomScientifique.getNom(),nomScientifique);								
471
				}
469
				}
472
			}
470
			}
473
		} else {	
471
		} else {	
474
			referentielNomData = new HashMap<String, ReferentielNom>(0);
472
			referentielNomData = new HashMap<String, ReferentielNom>(0);
475
		}
473
		}
476
		return referentielNomData;
474
		return referentielNomData;
477
	}
475
	}
478
 
476
 
479
	public static Map<String, String> convertirListeReferentielNomVersMap(Map<String, ReferentielNom> referentielNom) {
477
	public static Map<String, String> convertirListeReferentielNomVersMap(Map<String, ReferentielNom> referentielNom) {
480
		Map<String, String> nomMap = new HashMap<String, String>();
478
		Map<String, String> nomMap = new HashMap<String, String>();
481
		for (Iterator<String> it  = referentielNom.keySet().iterator(); it.hasNext();) {
479
		for (Iterator<String> it  = referentielNom.keySet().iterator(); it.hasNext();) {
482
			String cle = it.next();
480
			String cle = it.next();
483
			nomMap.put(cle, referentielNom.get(cle).getNom());
481
			nomMap.put(cle, referentielNom.get(cle).getNom());
484
		}
482
		}
485
		return nomMap;
483
		return nomMap;
486
	}
484
	}
487
	
485
	
488
	public static Map<String, ReferentielLocalite> parserRetourReferentielLocaliteIndexeParNom(Response response) {
486
	public static Map<String, ReferentielLocalite> parserRetourReferentielLocaliteIndexeParNom(Response response) {
489
		
487
		
490
		final Map<String, ReferentielLocalite> referentielLocaliteData;					
488
		final Map<String, ReferentielLocalite> referentielLocaliteData;					
491
		final JSONValue responseValue = JSONParser.parse(response.getText());					
489
		final JSONValue responseValue = JSONParser.parse(response.getText());					
492
		JSONArray reponse = null;
490
		JSONArray reponse = null;
493
 
491
 
494
		// si c'est un tableau
492
		// si c'est un tableau
495
		if ((reponse = responseValue.isArray()) != null) {
493
		if ((reponse = responseValue.isArray()) != null) {
496
			
494
			
497
			JSONArray localites;
495
			JSONArray localites;
498
			final int taillemax = reponse.size();
496
			final int taillemax = reponse.size();
499
			
497
			
500
			referentielLocaliteData = new HashMap<String, ReferentielLocalite>(taillemax);
498
			referentielLocaliteData = new HashMap<String, ReferentielLocalite>(taillemax);
501
			
499
			
502
			for (int i = 0; i < taillemax; i++) {
500
			for (int i = 0; i < taillemax; i++) {
503
				if ((localites = reponse.get(i).isArray()) != null) {
501
				if ((localites = reponse.get(i).isArray()) != null) {
504
					String localite = ((JSONString) localites.get(0)).stringValue();
502
					String localite = ((JSONString) localites.get(0)).stringValue();
505
					String codeLocalite  = ((JSONString) localites.get(1)).stringValue();
503
					String codeLocalite  = ((JSONString) localites.get(1)).stringValue();
506
					ReferentielLocalite com = new ReferentielLocalite(localite, codeLocalite);		
504
					ReferentielLocalite com = new ReferentielLocalite(localite, codeLocalite);		
507
					referentielLocaliteData.put(com.getLocalite() + com.getCodeLocalite(), com);			
505
					referentielLocaliteData.put(com.getLocalite() + com.getCodeLocalite(), com);			
508
				}	
506
				}	
509
			}
507
			}
510
		} else {					
508
		} else {					
511
			referentielLocaliteData = new HashMap<String, ReferentielLocalite>(0);
509
			referentielLocaliteData = new HashMap<String, ReferentielLocalite>(0);
512
		}					
510
		}					
513
		return referentielLocaliteData;
511
		return referentielLocaliteData;
514
	}
512
	}
515
	
513
	
516
	public static Map<String, String> convertirListeReferentielLocaliteVersMap(Map<String, ReferentielLocalite> referentielLocalite) {
514
	public static Map<String, String> convertirListeReferentielLocaliteVersMap(Map<String, ReferentielLocalite> referentielLocalite) {
517
		Map<String, String> locMap = new HashMap<String, String>();
515
		Map<String, String> locMap = new HashMap<String, String>();
518
		for (Iterator<String> it  = referentielLocalite.keySet().iterator(); it.hasNext();) {
516
		for (Iterator<String> it  = referentielLocalite.keySet().iterator(); it.hasNext();) {
519
			String cle = it.next();
517
			String cle = it.next();
520
			locMap.put(cle, referentielLocalite.get(cle).getLocalite());
518
			locMap.put(cle, referentielLocalite.get(cle).getLocalite());
521
		}
519
		}
522
		return locMap;
520
		return locMap;
523
	}
521
	}
-
 
522
	
-
 
523
	public static Map<String, String> parserRetourImportObs(String json) {
-
 
524
		final JSONValue responseValue = JSONParser.parse(json);
-
 
525
		JSONObject reponse = null;
-
 
526
		
-
 
527
		Map<String, String> retourImport = new HashMap<String,String>();
-
 
528
		// si c'est un objet
-
 
529
		if ((reponse = responseValue.isObject()) != null) {
-
 
530
			Iterator<String> it = reponse.keySet().iterator();
-
 
531
			
-
 
532
			while(it.hasNext()) {
-
 
533
				String cle = it.next();								
-
 
534
				String valeur = reponse.get(cle).isString().stringValue();
-
 
535
				retourImport.put(cle, valeur);
-
 
536
			}
-
 
537
		}
-
 
538
		
-
 
539
		return retourImport;
-
 
540
	}
524
 
541
 
525
	public static native void  LogVersFirebug(Object o) /*-{
542
	public static native void  LogVersFirebug(Object o) /*-{
526
		if (!!($wnd.console && $wnd.console.log)) {
543
		if (!!($wnd.console && $wnd.console.log)) {
527
			console.log(o);
544
			console.log(o);
528
		}
545
		}
529
	}-*/;
546
	}-*/;
530
	
547
	
531
	public static String buildStackTrace(Throwable t, String log) {
548
	public static String buildStackTrace(Throwable t, String log) {
532
	    if (t != null) {
549
	    if (t != null) {
533
		     log += t.getClass().toString();
550
		     log += t.getClass().toString();
534
		     log += t.getMessage();
551
		     log += t.getMessage();
535
		     //
552
		     //
536
		     StackTraceElement[] stackTrace = t.getStackTrace();
553
		     StackTraceElement[] stackTrace = t.getStackTrace();
537
		     if (stackTrace != null) {
554
		     if (stackTrace != null) {
538
		    	 StringBuffer trace = new StringBuffer();
555
		    	 StringBuffer trace = new StringBuffer();
539
		    
556
		    
540
			     for (int i = 0; i < stackTrace.length; i++) {
557
			     for (int i = 0; i < stackTrace.length; i++) {
541
			    	 trace.append(stackTrace[i].getClassName() + "." + stackTrace[i].getMethodName() + "("
558
			    	 trace.append(stackTrace[i].getClassName() + "." + stackTrace[i].getMethodName() + "("
542
			    			 + stackTrace[i].getFileName() + ":" + stackTrace[i].getLineNumber());
559
			    			 + stackTrace[i].getFileName() + ":" + stackTrace[i].getLineNumber());
543
			     }
560
			     }
544
		    
561
		    
545
			     log += trace.toString();
562
			     log += trace.toString();
546
		     }
563
		     }
547
		     //
564
		     //
548
		     Throwable cause = t.getCause();
565
		     Throwable cause = t.getCause();
549
		     if (cause != null && cause != t) {
566
		     if (cause != null && cause != t) {
550
		    	 log += buildStackTrace(cause, "CausedBy:\n");
567
		    	 log += buildStackTrace(cause, "CausedBy:\n");
551
		     }
568
		     }
552
	    }
569
	    }
553
	    return log;
570
	    return log;
554
	}
571
	}
555
	
572
	
556
	public static String obtenirCodeLangueSysteme() {
573
	public static String obtenirCodeLangueSysteme() {
557
		String langueSystemeBrute = obtenirLangueSysteme();
574
		String langueSystemeBrute = obtenirLangueSysteme();
558
		// le navigateur peut éventuellement renvoyer une chaine de la forme
575
		// le navigateur peut éventuellement renvoyer une chaine de la forme
559
		// en-us ou fr-fr par exemple
576
		// en-us ou fr-fr par exemple
560
		String[] lngTab = langueSystemeBrute.split("-");		
577
		String[] lngTab = langueSystemeBrute.split("-");		
561
		return (lngTab.length > 0) ? lngTab[0] : null;
578
		return (lngTab.length > 0) ? lngTab[0] : null;
562
	}
579
	}
563
	
580
	
564
	public static native String obtenirLangueSysteme()  /*-{
581
	public static native String obtenirLangueSysteme()  /*-{
565
		lang = "";
582
		lang = "";
566
		if (!!($wnd.navigator && $wnd.navigator.language)) {
583
		if (!!($wnd.navigator && $wnd.navigator.language)) {
567
			lang = window.navigator.language;
584
			lang = window.navigator.language;
568
		}
585
		}
569
		return lang;
586
		return lang;
570
	}-*/;
587
	}-*/;
571
}
588
}