Subversion Repositories eFlore/Applications.cel

Rev

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