Subversion Repositories eFlore/Applications.cel

Rev

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

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