Subversion Repositories eFlore/Applications.cel

Rev

Rev 1549 | 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
 
1542 aurelien 3
import java.util.HashMap;
1549 aurelien 4
import java.util.Iterator;
1542 aurelien 5
import java.util.Map;
6
 
1572 aurelien 7
import org.tela_botanica.client.modeles.objets.ChampEtendu;
989 aurelien 8
import org.tela_botanica.client.modeles.objets.Observation;
642 aurelien 9
 
1549 aurelien 10
import com.google.gwt.http.client.URL;
1572 aurelien 11
import com.google.gwt.json.client.JSONArray;
1282 aurelien 12
import com.google.gwt.json.client.JSONObject;
13
import com.google.gwt.json.client.JSONString;
14
 
2 aperonnet 15
public class Util {
16
 
17
	public Util() {
18
	}
642 aurelien 19
 
1282 aurelien 20
	public static String getValeurJsonOuVide(JSONObject jo, String index) {
21
		return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : "";
22
	}
23
 
1572 aurelien 24
	public static Map<String, ChampEtendu> getMapValeursOuVide(JSONObject jo, String index) {
25
		Map<String, ChampEtendu> mapValeurs = new HashMap<String, ChampEtendu>();
26
		if(jo.get(index) != null && jo.get(index).isArray() != null) {
27
			JSONArray tabJo = jo.get(index).isArray();
28
			for (int i = 0; i < tabJo.size(); i++) {
29
				JSONObject champJson = tabJo.get(i).isObject();
30
				String cle = champJson.get("cle").isString().stringValue();
31
				String label = champJson.get("label").isString().stringValue();
32
				String valeur = champJson.get("valeur").isString().stringValue();
33
				ChampEtendu champ = new ChampEtendu(cle, label, valeur);
34
				mapValeurs.put(cle, champ);
1542 aurelien 35
			}
1572 aurelien 36
 
1542 aurelien 37
		}
38
		return mapValeurs;
39
	}
40
 
1572 aurelien 41
	public static String convertirChampsEtendusEnChaineRequete(Map<String, ChampEtendu> map) {
42
		String json = "";
43
	    if (map != null && !map.isEmpty()) {
44
	    	JSONArray jsonArr = new JSONArray();
45
	    	int i = 0;
46
	        for (Map.Entry<String, ChampEtendu> entry: map.entrySet()) {
47
	        	jsonArr.set(i, convertirChampEtenduEnJson(entry.getValue()));
48
	        	i++;
49
	        }
50
	        json = jsonArr.toString();
51
	    }
52
	    return json;
1549 aurelien 53
	}
54
 
1572 aurelien 55
	public static JSONObject convertirChampEtenduEnJson(ChampEtendu champEtendu) {
56
		JSONObject jsonObj = new JSONObject();
57
		jsonObj.put("cle", new JSONString(champEtendu.getCle()));
58
		jsonObj.put("label", new JSONString(champEtendu.getLabel()));
59
		jsonObj.put("valeur", new JSONString(champEtendu.getValeur()));
60
        return jsonObj;
61
	}
62
 
1282 aurelien 63
	public static boolean jsonNonNull(JSONObject jo, String index) {
64
		return (jo != null &&
65
				jo.get(index) != null &&
66
				jo.get(index).isNull() == null
67
			   );
68
	}
69
 
642 aurelien 70
	public static String formaterLieu(Observation obs, String modeleLieu) {
71
 
72
		String lieuModele = modeleLieu;
73
 
74
		String commune = obs.getLocalite();
75
		String lieuDit = obs.getLieudit();
76
		String station = obs.getStation();
77
 
78
		String lieuCommuneFormate = "";
79
		String lieuDitFormate = "";
80
		String stationFormatee = "";
81
 
82
		if(commune != null && !commune.contains("000null") && !commune.trim().equals("")) {
83
			String	idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/");
84
			if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) {
85
 
86
				idLoc = idLoc.replaceAll("%","");
87
				idLoc = idLoc.replaceAll("\"","");
88
				idLoc = idLoc.replace('\\',' ');
89
				idLoc = idLoc.trim();
1332 aurelien 90
				if(idLoc.length() > 2) {
91
					idLoc = idLoc.substring(0,2);
92
				}
642 aurelien 93
				lieuCommuneFormate += idLoc+" - ";
94
			}
95
			lieuCommuneFormate += commune;
96
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE", lieuCommuneFormate);
97
		} else {
98
 
99
			lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE,", lieuCommuneFormate);
100
		}
101
 
102
		if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) {
103
			lieuDitFormate += lieuDit;
104
			lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate);
105
		} else {
106
			lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate);
107
		}
108
 
109
		if(station != null && !station.contains("000null") && !station.trim().equals("")) {
110
			stationFormatee += station;
111
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
112
		} else {
113
			lieuModele = lieuModele.replaceAll("STATION", stationFormatee);
114
		}
115
 
116
		lieuModele = lieuModele.trim();
117
		lieuModele = lieuModele.replaceAll(",$","");
118
		lieuModele = lieuModele.replaceAll(",^$",", ");
119
 
120
		return lieuModele;
121
	}
122
 
1542 aurelien 123
	public static String obtenirDepartementAPartirChaineCommune(String departement, String commune) {
124
 
125
		String dep = "";
126
 
127
		if(departement == null) {
128
			departement = "";
129
		}
130
 
131
		if(departement.equals("000null") || departement.equals("")) {
132
 
133
			String[] depCom = commune.split(" ");
134
			if(depCom.length > 1) {
135
				dep = depCom[1].replace('(', ' ');
136
			} else {
137
				dep = "";
138
			}
139
		} else {
140
			dep = departement;
141
		}
142
 
143
		dep = dep.replace(')', ' ');
144
		dep = dep.trim();
145
		dep = dep.replace('\\',' ');
146
		dep = dep.trim();
147
 
148
		try
149
		{
150
			int nDep = Integer.parseInt(dep);
151
			if(nDep > 0 && nDep < 110) {
152
				departement = dep ;
153
			}
154
 
155
			if(departement.length() == 4) {
156
				departement = "0"+departement;
157
			}
158
 
159
			departement = departement.substring(0,2);
160
		}
161
		catch(NumberFormatException e)
162
		{
163
			departement = "" ;
164
		}
165
 
166
		return departement;
167
	}
168
 
642 aurelien 169
	public static String supprimerNumDepartementChaineLocalite(String chaineLocaliteComplete) {
962 aurelien 170
		return chaineLocaliteComplete.replaceAll(" \\([0-9]*\\)", "");
642 aurelien 171
	}
673 aurelien 172
 
962 aurelien 173
	public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) {
174
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2): chaineZoneGeo;
175
	}
176
 
177
	public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) {
178
		return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo;
179
	}
180
 
673 aurelien 181
	/***
182
	 * Fusionne les éléments d'un tableau en une chaîne
183
	 * @param delim : la chaîne de séparation
184
	 * @param args : la tableau
185
	 * @return la chaîne fusionnée
186
	 */
187
	public static String implode(String delim, String[] args){
188
		StringBuffer sb = new StringBuffer();
189
 
190
		int lgArgs = args.length;
191
 
192
		for(int i = 0; i < lgArgs; i++){
193
			if (i > 0) {
194
				sb.append(delim);
195
			}
196
 
197
			sb.append(args[i]);
198
		}
199
 
200
		return sb.toString();
201
	}
202
 
203
	public static boolean filtreValide(String[] filtre) {
204
 
205
		return (filtre.length == 2 &&
206
		filtre[0] != null &&
207
		!filtre[0].equals("") &&
208
		filtre[1] != null &&
209
		!filtre[1].equals(""));
210
	}
211
 
212
	public static String renvoyerMois(int numMois) {
213
 
214
		switch (numMois) {
215
		case 1:
216
			return "janvier" ;
217
		case 2:
218
			return "fevrier" ;
219
		case 3:
220
			return "mars" ;
221
		case 4:
222
			return "avril" ;
223
		case 5:
224
			return "mai" ;
225
		case 6:
226
			return "juin" ;
227
		case 7:
228
			return "juillet" ;
229
		case 8:
230
			return "août" ;
231
		case 9:
232
			return "septembre" ;
233
		case 10:
234
			return "octobre" ;
235
		case 11:
236
			return "novembre" ;
237
		case 12:
238
			return "décembre" ;
239
		default:
240
			return "Inconnu" ;
241
		}
242
	}
243
 
244
	public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) {
245
 
246
		String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%');
247
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%");
248
		chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n');
249
 
250
		return chaineAvecSautsDeLignesBienEncodes;
251
	}
252
 
253
	public static boolean verifierDateFormatCel(String dateAVerifier) {
254
 
255
		String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier);
256
		String[] tabDate = dateRemplacee.split("/");
257
 
258
		boolean retour = false;
259
 
260
		if(tabDate.length == 3) {
1282 aurelien 261
			//TODO: faire un parsing de date qui fonctionne mieux car
262
			// on peut saisir un 31 novembre par exemple
263
			// mais l'api date de java est mal gérée par gwt
673 aurelien 264
			try {
265
				int jour = Integer.parseInt(tabDate[0]);
266
				int mois = Integer.parseInt(tabDate[1]);
267
				int annee = Integer.parseInt(tabDate[2]);
268
 
1010 aurelien 269
				if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) {
673 aurelien 270
					retour = true;
271
				}
272
			} catch (Exception e) {
273
 
274
			}
275
		}
276
 
277
		return retour;
278
	}
279
 
280
	public static String remplacerSeparateursDateFormatCel(String date) {
281
 
282
		String dateRemplacee = date.replaceAll("-", "/");
283
 
284
	    return dateRemplacee;
285
	}
1572 aurelien 286
 
287
	public static boolean estZero(String s) {
288
		boolean estZero = false;
289
	    try {
290
	    	Double dou = Double.parseDouble(s);
291
	    	estZero = (dou == 0);
292
	    } catch(NumberFormatException e) {
293
 
294
	    }
295
	    return estZero;
296
	}
297
 
298
	public static String formaterNombre(String s) {
299
		s = s.indexOf(".") < 0 ? s : s.replaceAll("0*$", "").replaceAll("\\.$", "");
300
		return s;
301
	}
2 aperonnet 302
}