Subversion Repositories eFlore/Applications.cel

Rev

Rev 2392 | 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
 
1572 aurelien 14
import com.google.gwt.json.client.JSONArray;
1282 aurelien 15
import com.google.gwt.json.client.JSONObject;
16
import com.google.gwt.json.client.JSONString;
17
 
2 aperonnet 18
public class Util {
19
 
20
	public Util() {
21
	}
642 aurelien 22
 
1282 aurelien 23
	public static String getValeurJsonOuVide(JSONObject jo, String index) {
24
		return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
25
	}
26
 
1572 aurelien 27
	public static Map<String, ChampEtendu> getMapValeursOuVide(JSONObject jo, String index) {
28
		Map<String, ChampEtendu> mapValeurs = new HashMap<String, ChampEtendu>();
29
		if(jo.get(index) != null && jo.get(index).isArray() != null) {
30
			JSONArray tabJo = jo.get(index).isArray();
31
			for (int i = 0; i < tabJo.size(); i++) {
32
				JSONObject champJson = tabJo.get(i).isObject();
33
				String cle = champJson.get("cle").isString().stringValue();
2392 aurelien 34
				String label = cle;
1572 aurelien 35
				String valeur = champJson.get("valeur").isString().stringValue();
36
				ChampEtendu champ = new ChampEtendu(cle, label, valeur);
37
				mapValeurs.put(cle, champ);
1542 aurelien 38
			}
1572 aurelien 39
 
1542 aurelien 40
		}
41
		return mapValeurs;
42
	}
43
 
1572 aurelien 44
	public static String convertirChampsEtendusEnChaineRequete(Map<String, ChampEtendu> map) {
45
		String json = "";
46
	    if (map != null && !map.isEmpty()) {
47
	    	JSONArray jsonArr = new JSONArray();
48
	    	int i = 0;
49
	        for (Map.Entry<String, ChampEtendu> entry: map.entrySet()) {
50
	        	jsonArr.set(i, convertirChampEtenduEnJson(entry.getValue()));
51
	        	i++;
52
	        }
53
	        json = jsonArr.toString();
54
	    }
55
	    return json;
1549 aurelien 56
	}
57
 
1572 aurelien 58
	public static JSONObject convertirChampEtenduEnJson(ChampEtendu champEtendu) {
59
		JSONObject jsonObj = new JSONObject();
60
		jsonObj.put("cle", new JSONString(champEtendu.getCle()));
61
		jsonObj.put("label", new JSONString(champEtendu.getLabel()));
62
		jsonObj.put("valeur", new JSONString(champEtendu.getValeur()));
63
        return jsonObj;
64
	}
65
 
1282 aurelien 66
	public static boolean jsonNonNull(JSONObject jo, String index) {
67
		return (jo != null &&
68
				jo.get(index) != null &&
69
				jo.get(index).isNull() == null
70
			   );
71
	}
72
 
642 aurelien 73
	public static String formaterLieu(Observation obs, String modeleLieu) {
74
 
75
		String lieuModele = modeleLieu;
76
 
77
		String commune = obs.getLocalite();
78
		String lieuDit = obs.getLieudit();
79
		String station = obs.getStation();
80
 
81
		String lieuCommuneFormate = "";
82
		String lieuDitFormate = "";
83
		String stationFormatee = "";
84
 
85
		if(commune != null && !commune.contains("000null") && !commune.trim().equals("")) {
86
			String	idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
87
			if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
88
 
89
				idLoc = idLoc.replaceAll("%","");
90
				idLoc = idLoc.replaceAll("\"","");
91
				idLoc = idLoc.replace('\\',' ');
92
				idLoc = idLoc.trim();
1332 aurelien 93
				if(idLoc.length() > 2) {
94
					idLoc = idLoc.substring(0,2);
95
				}
642 aurelien 96
				lieuCommuneFormate += idLoc+" - ";
97
			}
98
			lieuCommuneFormate += commune;
99
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE", lieuCommuneFormate);
100
		} else {
101
 
102
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE,", lieuCommuneFormate);
103
		}
104
 
105
		if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
106
			lieuDitFormate += lieuDit;
107
			lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
108
		} else {
109
			lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
110
		}
111
 
112
		if(station != null && !station.contains("000null") && !station.trim().equals("")) {
113
			stationFormatee += station;
114
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
115
		} else {
116
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
117
		}
118
 
119
		lieuModele = lieuModele.trim();
120
		lieuModele = lieuModele.replaceAll(",$","");
121
		lieuModele = lieuModele.replaceAll(",^$",", ");
122
 
123
		return lieuModele;
124
	}
125
 
1542 aurelien 126
	public static String obtenirDepartementAPartirChaineCommune(String departement, String commune) {
127
 
128
		String dep = "";
129
 
130
		if(departement == null) {
131
			departement = "";
132
		}
133
 
134
		if(departement.equals("000null") || departement.equals("")) {
135
 
136
			String[] depCom = commune.split(" ");
137
			if(depCom.length > 1) {
138
				dep = depCom[1].replace('(', ' ');
139
			} else {
140
				dep = "";
141
			}
142
		} else {
143
			dep = departement;
144
		}
145
 
146
		dep = dep.replace(')', ' ');
147
		dep = dep.trim();
148
		dep = dep.replace('\\',' ');
149
		dep = dep.trim();
150
 
151
		try
152
		{
153
			int nDep = Integer.parseInt(dep);
154
			if(nDep > 0 && nDep < 110) {
155
				departement = dep ;
156
			}
157
 
158
			if(departement.length() == 4) {
159
				departement = "0"+departement;
160
			}
161
 
162
			departement = departement.substring(0,2);
163
		}
164
		catch(NumberFormatException e)
165
		{
166
			departement = "" ;
167
		}
168
 
169
		return departement;
170
	}
171
 
642 aurelien 172
	public static String supprimerNumDepartementChaineLocalite(String chaineLocaliteComplete) {
962 aurelien 173
		return chaineLocaliteComplete.replaceAll(" \\([0-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
 
303
	public static String formaterNombre(String s) {
304
		s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
305
		return s;
306
	}
2276 mathias 307
 
308
	// Prend un nombre décimal avec le spéparateur spécifié et le tronque à n décimales
309
	public static String tronquerNombrePourAffichage(String nombre, int decimales, char separateur) {
310
		String retour = nombre;
311
		int posSep = nombre.indexOf(separateur);
312
		if (posSep >= 0) {
313
			int taille = posSep + decimales + 1;
314
			if (nombre.length() < taille) {
315
				taille = nombre.length();
316
			}
317
			retour = nombre.substring(0, taille);
318
		}
319
		return retour;
320
	}
321
 
322
	public static String tronquerNombrePourAffichage(String nombre, int decimales) {
323
		return tronquerNombrePourAffichage(nombre, decimales, '.');
324
	}
2392 aurelien 325
 
326
	// Adapté de http://www.programcreek.com/2011/03/java-method-for-spliting-a-camelcase-string/
327
	public static String formaterCleChampsEtenduPourAffichage(String s) {
328
		char[] cArray = s.toCharArray();
329
 
330
		Vector<Integer> al = new Vector<Integer>();
331
		al.add(0);
332
 
333
		// get all upper case letter index positions
334
		for (int i = 1; i < cArray.length; i++) {
335
			char c = cArray[i];
336
			//add more interested index beyond upper case letter
337
			if (c >= 65 && c <= 90) {
338
				al.add(i);
339
			}
340
		}
341
 
342
		Vector<String> strl = new Vector<String>();
343
 
344
		// this handles the all lower letter case
345
		if (al.size() == 1) {
346
			strl.add(s);
347
			return depilerChaineListee(strl, " ");
348
		}
349
 
350
 
351
		int prev = 0;
352
		int curr = 0;
353
		int begin = 0;
354
		for (int k = 1; k < al.size(); k++) {
355
 
356
			curr = al.get(k);
357
 
358
			if(curr == s.length() - 1){
359
 
360
			}
361
 
362
			if (curr == prev + 1 && curr != s.length() - 1) {
363
				prev = curr;
364
			} else if(curr == prev + 1 &&  curr == s.length() - 1){
365
				strl.add(s.substring(begin, curr+1));
366
			}else {
367
 
368
				strl.add(s.substring(prev, curr));
369
				prev = curr;
370
				begin = curr;
371
				if (k == al.size() - 1) {
372
					strl.add(s.substring(curr, s.length()));
373
				}
374
			}
375
		}
376
 
377
		return depilerChaineListee(strl, " ");
378
	}
379
 
380
	private static String depilerChaineListee(Vector<String> strl, String separateur) {
381
		String s = "";
382
		for(int i = 0; i < strl.size(); i++) {
383
			s += strl.get(i);
384
			if(i != strl.size() - 1) {
385
				s += separateur;
386
			}
387
		}
388
		return s;
389
	}
390
 
391
	public static Map<String, ChampEtendu> trierListeChampsEtendus(Map<String, ChampEtendu> listeChampsEtendus) {
392
		List<String> tmp = new ArrayList<String>(listeChampsEtendus.keySet());
393
		Collections.sort(tmp, new Comparator<String>() {
394
 
395
		    @Override
396
		    public int compare(String arg0, String arg1) {
397
		        return arg0.compareTo(arg1);
398
		    }
399
 
400
		});
401
		return listeChampsEtendus;
402
	}
2401 aurelien 403
 
404
	/**
405
	 * Solution issue de stackoverflow :
406
	 * http://stackoverflow.com/questions/1143951/what-is-the-simplest-way-to-convert-a-java-string-from-all-caps-words-separated
407
	 */
408
	public static String convertirEnChaMot(String s) {
409
		s = s.replaceAll("_", " ");
410
		s = s.replaceAll("-", " ");
411
		String[] parties = s.split(" ");
412
		String chaineChaMot = "";
413
		for (String partie : parties){
414
			chaineChaMot = chaineChaMot + convertirMotEnChaMot(partie);
415
		}
416
		return chaineChaMot;
417
	}
418
 
419
	protected static String convertirMotEnChaMot(String s) {
420
		return s.substring(0, 1).toUpperCase() +
421
	               s.substring(1).toLowerCase();
422
	}
2 aperonnet 423
}