Subversion Repositories eFlore/Applications.cel

Rev

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

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