Subversion Repositories eFlore/Applications.cel

Rev

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

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