Subversion Repositories eFlore/Applications.cel

Rev

Rev 2553 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
734 aurelien 1
package org.tela_botanica.client.util;
2 aperonnet 2
 
2558 aurelien 3
import java.text.NumberFormat;
4
import java.text.ParsePosition;
2392 aurelien 5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.Comparator;
1542 aurelien 8
import java.util.HashMap;
2392 aurelien 9
import java.util.List;
1542 aurelien 10
import java.util.Map;
2392 aurelien 11
import java.util.Vector;
1542 aurelien 12
 
1572 aurelien 13
import org.tela_botanica.client.modeles.objets.ChampEtendu;
989 aurelien 14
import org.tela_botanica.client.modeles.objets.Observation;
642 aurelien 15
 
2553 mathias 16
import com.google.gwt.dom.client.Element;
1572 aurelien 17
import com.google.gwt.json.client.JSONArray;
1282 aurelien 18
import com.google.gwt.json.client.JSONObject;
19
import com.google.gwt.json.client.JSONString;
2553 mathias 20
import com.google.gwt.user.client.DOM;
21
import com.google.gwt.user.client.ui.RootPanel;
1282 aurelien 22
 
2 aperonnet 23
public class Util {
24
 
25
	public Util() {
26
	}
642 aurelien 27
 
1282 aurelien 28
	public static String getValeurJsonOuVide(JSONObject jo, String index) {
29
		return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
30
	}
31
 
1572 aurelien 32
	public static Map<String, ChampEtendu> getMapValeursOuVide(JSONObject jo, String index) {
33
		Map<String, ChampEtendu> mapValeurs = new HashMap<String, ChampEtendu>();
34
		if(jo.get(index) != null && jo.get(index).isArray() != null) {
35
			JSONArray tabJo = jo.get(index).isArray();
36
			for (int i = 0; i < tabJo.size(); i++) {
37
				JSONObject champJson = tabJo.get(i).isObject();
38
				String cle = champJson.get("cle").isString().stringValue();
2392 aurelien 39
				String label = cle;
1572 aurelien 40
				String valeur = champJson.get("valeur").isString().stringValue();
41
				ChampEtendu champ = new ChampEtendu(cle, label, valeur);
42
				mapValeurs.put(cle, champ);
1542 aurelien 43
			}
1572 aurelien 44
 
1542 aurelien 45
		}
46
		return mapValeurs;
47
	}
48
 
1572 aurelien 49
	public static String convertirChampsEtendusEnChaineRequete(Map<String, ChampEtendu> map) {
50
		String json = "";
51
	    if (map != null && !map.isEmpty()) {
52
	    	JSONArray jsonArr = new JSONArray();
53
	    	int i = 0;
54
	        for (Map.Entry<String, ChampEtendu> entry: map.entrySet()) {
55
	        	jsonArr.set(i, convertirChampEtenduEnJson(entry.getValue()));
56
	        	i++;
57
	        }
58
	        json = jsonArr.toString();
59
	    }
60
	    return json;
1549 aurelien 61
	}
62
 
1572 aurelien 63
	public static JSONObject convertirChampEtenduEnJson(ChampEtendu champEtendu) {
64
		JSONObject jsonObj = new JSONObject();
65
		jsonObj.put("cle", new JSONString(champEtendu.getCle()));
66
		jsonObj.put("label", new JSONString(champEtendu.getLabel()));
67
		jsonObj.put("valeur", new JSONString(champEtendu.getValeur()));
68
        return jsonObj;
69
	}
70
 
1282 aurelien 71
	public static boolean jsonNonNull(JSONObject jo, String index) {
72
		return (jo != null &&
73
				jo.get(index) != null &&
74
				jo.get(index).isNull() == null
75
			   );
76
	}
77
 
642 aurelien 78
	public static String formaterLieu(Observation obs, String modeleLieu) {
79
 
80
		String lieuModele = modeleLieu;
81
 
82
		String commune = obs.getLocalite();
83
		String lieuDit = obs.getLieudit();
84
		String station = obs.getStation();
85
 
86
		String lieuCommuneFormate = "";
87
		String lieuDitFormate = "";
88
		String stationFormatee = "";
89
 
90
		if(commune != null && !commune.contains("000null") && !commune.trim().equals("")) {
91
			String	idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
92
			if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
93
 
94
				idLoc = idLoc.replaceAll("%","");
95
				idLoc = idLoc.replaceAll("\"","");
96
				idLoc = idLoc.replace('\\',' ');
97
				idLoc = idLoc.trim();
1332 aurelien 98
				if(idLoc.length() > 2) {
99
					idLoc = idLoc.substring(0,2);
100
				}
642 aurelien 101
				lieuCommuneFormate += idLoc+" - ";
102
			}
103
			lieuCommuneFormate += commune;
104
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE", lieuCommuneFormate);
105
		} else {
106
 
107
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE,", lieuCommuneFormate);
108
		}
109
 
110
		if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
111
			lieuDitFormate += lieuDit;
112
			lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
113
		} else {
114
			lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
115
		}
116
 
117
		if(station != null && !station.contains("000null") && !station.trim().equals("")) {
118
			stationFormatee += station;
119
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
120
		} else {
121
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
122
		}
123
 
124
		lieuModele = lieuModele.trim();
125
		lieuModele = lieuModele.replaceAll(",$","");
126
		lieuModele = lieuModele.replaceAll(",^$",", ");
127
 
128
		return lieuModele;
129
	}
130
 
2558 aurelien 131
	public static String obtenirIdLocAPartirChaineCommune(String commune) {
1542 aurelien 132
 
2558 aurelien 133
		String idLoc = "";
134
		String[] depCom = commune.split(" ");
135
		if(depCom.length > 1) {
136
			idLoc = depCom[1].replace('(', ' ');
1542 aurelien 137
		} else {
2558 aurelien 138
			idLoc = "";
1542 aurelien 139
		}
140
 
2558 aurelien 141
		idLoc = idLoc.replace(')', ' ');
142
		idLoc = idLoc.trim();
143
		idLoc = idLoc.replace('\\',' ');
144
		idLoc = idLoc.trim();
1542 aurelien 145
 
2558 aurelien 146
		return idLoc;
147
	}
148
 
149
	public static String formaterDepartement(String depAFormater) {
150
		String dep = "";
1542 aurelien 151
		try
152
		{
2558 aurelien 153
			int nDep = Integer.parseInt(depAFormater);
1542 aurelien 154
			if(nDep > 0 && nDep < 110) {
2558 aurelien 155
				dep = depAFormater ;
1542 aurelien 156
			}
157
 
2558 aurelien 158
			if(depAFormater.length() == 4) {
159
				dep = "0"+depAFormater;
1542 aurelien 160
			}
161
 
2558 aurelien 162
			dep = depAFormater.substring(0,2);
1542 aurelien 163
		}
164
		catch(NumberFormatException e)
165
		{
2558 aurelien 166
			// rien à faire
1542 aurelien 167
		}
168
 
2558 aurelien 169
		return dep;
1542 aurelien 170
	}
171
 
2558 aurelien 172
	public static String supprimerChaineIdLocalite(String chaineLocaliteComplete) {
173
		return chaineLocaliteComplete.replaceAll(" \\([a-zA-Z0-9]*\\)", "");
642 aurelien 174
	}
673 aurelien 175
 
962 aurelien 176
	public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) {
1672 aurelien 177
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("") && chaineZoneGeo.replaceAll("INSEE-C:", "").length() >= 2) ?
178
						chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2) :
179
						chaineZoneGeo;
962 aurelien 180
	}
181
 
182
	public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) {
183
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo;
184
	}
185
 
673 aurelien 186
	/***
187
	 * Fusionne les éléments d'un tableau en une chaîne
188
	 * @param delim : la chaîne de séparation
189
	 * @param args : la tableau
190
	 * @return la chaîne fusionnée
191
	 */
192
	public static String implode(String delim, String[] args){
193
		StringBuffer sb = new StringBuffer();
194
 
195
		int lgArgs = args.length;
196
 
197
		for(int i = 0; i < lgArgs; i++){
198
			if (i > 0) {
199
				sb.append(delim);
200
			}
201
 
202
			sb.append(args[i]);
203
		}
204
 
205
		return sb.toString();
206
	}
207
 
208
	public static boolean filtreValide(String[] filtre) {
209
 
210
		return (filtre.length == 2 &&
211
		filtre[0] != null &&
212
		!filtre[0].equals("") &&
213
		filtre[1] != null &&
214
		!filtre[1].equals(""));
215
	}
216
 
217
	public static String renvoyerMois(int numMois) {
218
 
219
		switch (numMois) {
220
		case 1:
221
			return "janvier" ;
222
		case 2:
223
			return "fevrier" ;
224
		case 3:
225
			return "mars" ;
226
		case 4:
227
			return "avril" ;
228
		case 5:
229
			return "mai" ;
230
		case 6:
231
			return "juin" ;
232
		case 7:
233
			return "juillet" ;
234
		case 8:
235
			return "août" ;
236
		case 9:
237
			return "septembre" ;
238
		case 10:
239
			return "octobre" ;
240
		case 11:
241
			return "novembre" ;
242
		case 12:
243
			return "décembre" ;
244
		default:
245
			return "Inconnu" ;
246
		}
247
	}
248
 
249
	public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) {
250
 
251
		String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%');
252
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%");
253
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n');
254
 
255
		return chaineAvecSautsDeLignesBienEncodes;
256
	}
257
 
258
	public static boolean verifierDateFormatCel(String dateAVerifier) {
259
 
260
		String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier);
261
		String[] tabDate = dateRemplacee.split("/");
262
 
263
		boolean retour = false;
264
 
265
		if(tabDate.length == 3) {
1282 aurelien 266
			//TODO: faire un parsing de date qui fonctionne mieux car
267
			// on peut saisir un 31 novembre par exemple
268
			// mais l'api date de java est mal gérée par gwt
673 aurelien 269
			try {
270
				int jour = Integer.parseInt(tabDate[0]);
271
				int mois = Integer.parseInt(tabDate[1]);
272
				int annee = Integer.parseInt(tabDate[2]);
273
 
1010 aurelien 274
				if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) {
673 aurelien 275
					retour = true;
276
				}
277
			} catch (Exception e) {
278
 
279
			}
280
		}
281
 
282
		return retour;
283
	}
284
 
285
	public static String remplacerSeparateursDateFormatCel(String date) {
286
 
287
		String dateRemplacee = date.replaceAll("-", "/");
288
 
289
	    return dateRemplacee;
290
	}
1572 aurelien 291
 
292
	public static boolean estZero(String s) {
293
		boolean estZero = false;
294
	    try {
295
	    	Double dou = Double.parseDouble(s);
296
	    	estZero = (dou == 0);
297
	    } catch(NumberFormatException e) {
298
 
299
	    }
300
	    return estZero;
301
	}
302
 
2558 aurelien 303
	public static boolean estUnNombre(String s) {
304
		//http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java
305
		try {
306
			double d = Double.parseDouble(s);
307
		} catch(NumberFormatException nfe) {
308
			return false;
309
		}
310
		return true;
311
	}
312
 
1572 aurelien 313
	public static String formaterNombre(String s) {
314
		s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
315
		return s;
316
	}
2276 mathias 317
 
318
	// Prend un nombre décimal avec le spéparateur spécifié et le tronque à n décimales
319
	public static String tronquerNombrePourAffichage(String nombre, int decimales, char separateur) {
320
		String retour = nombre;
321
		int posSep = nombre.indexOf(separateur);
322
		if (posSep >= 0) {
323
			int taille = posSep + decimales + 1;
324
			if (nombre.length() < taille) {
325
				taille = nombre.length();
326
			}
327
			retour = nombre.substring(0, taille);
328
		}
329
		return retour;
330
	}
331
 
332
	public static String tronquerNombrePourAffichage(String nombre, int decimales) {
333
		return tronquerNombrePourAffichage(nombre, decimales, '.');
334
	}
2392 aurelien 335
 
336
	// Adapté de http://www.programcreek.com/2011/03/java-method-for-spliting-a-camelcase-string/
337
	public static String formaterCleChampsEtenduPourAffichage(String s) {
338
		char[] cArray = s.toCharArray();
339
 
340
		Vector<Integer> al = new Vector<Integer>();
341
		al.add(0);
342
 
343
		// get all upper case letter index positions
344
		for (int i = 1; i < cArray.length; i++) {
345
			char c = cArray[i];
346
			//add more interested index beyond upper case letter
347
			if (c >= 65 && c <= 90) {
348
				al.add(i);
349
			}
350
		}
351
 
352
		Vector<String> strl = new Vector<String>();
353
 
354
		// this handles the all lower letter case
355
		if (al.size() == 1) {
356
			strl.add(s);
357
			return depilerChaineListee(strl, " ");
358
		}
359
 
360
 
361
		int prev = 0;
362
		int curr = 0;
363
		int begin = 0;
364
		for (int k = 1; k < al.size(); k++) {
365
 
366
			curr = al.get(k);
367
 
368
			if(curr == s.length() - 1){
369
 
370
			}
371
 
372
			if (curr == prev + 1 && curr != s.length() - 1) {
373
				prev = curr;
374
			} else if(curr == prev + 1 &&  curr == s.length() - 1){
375
				strl.add(s.substring(begin, curr+1));
376
			}else {
377
 
378
				strl.add(s.substring(prev, curr));
379
				prev = curr;
380
				begin = curr;
381
				if (k == al.size() - 1) {
382
					strl.add(s.substring(curr, s.length()));
383
				}
384
			}
385
		}
386
 
387
		return depilerChaineListee(strl, " ");
388
	}
389
 
390
	private static String depilerChaineListee(Vector<String> strl, String separateur) {
391
		String s = "";
392
		for(int i = 0; i < strl.size(); i++) {
393
			s += strl.get(i);
394
			if(i != strl.size() - 1) {
395
				s += separateur;
396
			}
397
		}
398
		return s;
399
	}
400
 
401
	public static Map<String, ChampEtendu> trierListeChampsEtendus(Map<String, ChampEtendu> listeChampsEtendus) {
402
		List<String> tmp = new ArrayList<String>(listeChampsEtendus.keySet());
403
		Collections.sort(tmp, new Comparator<String>() {
404
 
405
		    @Override
406
		    public int compare(String arg0, String arg1) {
407
		        return arg0.compareTo(arg1);
408
		    }
409
 
410
		});
411
		return listeChampsEtendus;
412
	}
2401 aurelien 413
 
414
	/**
415
	 * Solution issue de stackoverflow :
416
	 * http://stackoverflow.com/questions/1143951/what-is-the-simplest-way-to-convert-a-java-string-from-all-caps-words-separated
417
	 */
418
	public static String convertirEnChaMot(String s) {
419
		s = s.replaceAll("_", " ");
420
		s = s.replaceAll("-", " ");
421
		String[] parties = s.split(" ");
422
		String chaineChaMot = "";
423
		for (String partie : parties){
424
			chaineChaMot = chaineChaMot + convertirMotEnChaMot(partie);
425
		}
426
		return chaineChaMot;
427
	}
428
 
429
	protected static String convertirMotEnChaMot(String s) {
430
		return s.substring(0, 1).toUpperCase() +
431
	               s.substring(1).toLowerCase();
432
	}
2553 mathias 433
 
434
	public static void curseurAttente() {
435
	    RootPanel.getBodyElement().getStyle().setProperty("cursor", "wait");
436
	}
437
 
438
	public static void curseurParDefaut() {
439
		RootPanel.getBodyElement().getStyle().setProperty("cursor", "default");
440
	}
2 aperonnet 441
}