Subversion Repositories eFlore/Applications.cel

Rev

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