New file |
0,0 → 1,257 |
package org.tela_botanica.client.util; |
|
import java.util.HashMap; |
import java.util.Map; |
|
import org.tela_botanica.client.modeles.objets.ListeObservation; |
import org.tela_botanica.client.modeles.objets.Observation; |
|
import com.google.gwt.json.client.JSONArray; |
import com.google.gwt.json.client.JSONObject; |
import com.google.gwt.json.client.JSONString; |
|
public class Util { |
|
public Util() { |
} |
|
public static String getValeurJsonOuVide(JSONObject jo, String index) { |
return jsonNonNull(jo, index) ? ((JSONString)jo.get(index)).stringValue() : ""; |
} |
|
public static Map<String, String> getMapValeursOuVide(JSONArray jo, String index) { |
Map<String, String> mapValeurs = new HashMap<String, String>(); |
final int taillemax = jo.size(); |
JSONObject temp = null; |
for (int i = 0; i < taillemax; i++) { |
if ((temp = jo.get(i).isObject()) != null) { |
mapValeurs.put(temp.get("cle").isString().stringValue(), temp.get("valeur").isString().stringValue()); |
} |
} |
return mapValeurs; |
} |
|
public static boolean jsonNonNull(JSONObject jo, String index) { |
return (jo != null && |
jo.get(index) != null && |
jo.get(index).isNull() == null |
); |
} |
|
public static String formaterLieu(Observation obs, String modeleLieu) { |
|
String lieuModele = modeleLieu; |
|
String commune = obs.getLocalite(); |
String lieuDit = obs.getLieudit(); |
String station = obs.getStation(); |
|
String lieuCommuneFormate = ""; |
String lieuDitFormate = ""; |
String stationFormatee = ""; |
|
if(commune != null && !commune.contains("000null") && !commune.trim().equals("")) { |
String idLoc =obs.getIdentifiantLocalite().replaceAll(" ","/"); |
if(idLoc != null && !idLoc.contains("000null") && !idLoc.trim().equals("")) { |
|
idLoc = idLoc.replaceAll("%",""); |
idLoc = idLoc.replaceAll("\"",""); |
idLoc = idLoc.replace('\\',' '); |
idLoc = idLoc.trim(); |
if(idLoc.length() > 2) { |
idLoc = idLoc.substring(0,2); |
} |
lieuCommuneFormate += idLoc+" - "; |
} |
lieuCommuneFormate += commune; |
lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE", lieuCommuneFormate); |
} else { |
|
lieuModele = lieuModele.replaceAll("IDLOCCOMMUNE,", lieuCommuneFormate); |
} |
|
if(lieuDit != null && !lieuDit.contains("000null") && !lieuDit.trim().equals("")) { |
lieuDitFormate += lieuDit; |
lieuModele = lieuModele.replaceAll("LIEUDIT", lieuDitFormate); |
} else { |
lieuModele = lieuModele.replaceAll("LIEUDIT,", lieuDitFormate); |
} |
|
if(station != null && !station.contains("000null") && !station.trim().equals("")) { |
stationFormatee += station; |
lieuModele = lieuModele.replaceAll("STATION", stationFormatee); |
} else { |
lieuModele = lieuModele.replaceAll("STATION", stationFormatee); |
} |
|
lieuModele = lieuModele.trim(); |
lieuModele = lieuModele.replaceAll(",$",""); |
lieuModele = lieuModele.replaceAll(",^$",", "); |
|
return lieuModele; |
} |
|
public static String obtenirDepartementAPartirChaineCommune(String departement, String commune) { |
|
String dep = ""; |
|
if(departement == null) { |
departement = ""; |
} |
|
if(departement.equals("000null") || departement.equals("")) { |
|
String[] depCom = commune.split(" "); |
if(depCom.length > 1) { |
dep = depCom[1].replace('(', ' '); |
} else { |
dep = ""; |
} |
} else { |
dep = departement; |
} |
|
dep = dep.replace(')', ' '); |
dep = dep.trim(); |
dep = dep.replace('\\',' '); |
dep = dep.trim(); |
|
try |
{ |
int nDep = Integer.parseInt(dep); |
if(nDep > 0 && nDep < 110) { |
departement = dep ; |
} |
|
if(departement.length() == 4) { |
departement = "0"+departement; |
} |
|
departement = departement.substring(0,2); |
} |
catch(NumberFormatException e) |
{ |
departement = "" ; |
} |
|
return departement; |
} |
|
public static String supprimerNumDepartementChaineLocalite(String chaineLocaliteComplete) { |
return chaineLocaliteComplete.replaceAll(" \\([0-9]*\\)", ""); |
} |
|
public static String convertirChaineZoneGeoVersDepartement(String chaineZoneGeo) { |
return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", "").substring(0, 2): chaineZoneGeo; |
} |
|
public static String convertirChaineZoneGeoVersCodeInsee(String chaineZoneGeo) { |
return (!chaineZoneGeo.equals("000null") && !chaineZoneGeo.equals("")) ? chaineZoneGeo.replaceAll("INSEE-C:", ""): chaineZoneGeo; |
} |
|
/*** |
* Fusionne les éléments d'un tableau en une chaîne |
* @param delim : la chaîne de séparation |
* @param args : la tableau |
* @return la chaîne fusionnée |
*/ |
public static String implode(String delim, String[] args){ |
StringBuffer sb = new StringBuffer(); |
|
int lgArgs = args.length; |
|
for(int i = 0; i < lgArgs; i++){ |
if (i > 0) { |
sb.append(delim); |
} |
|
sb.append(args[i]); |
} |
|
return sb.toString(); |
} |
|
public static boolean filtreValide(String[] filtre) { |
|
return (filtre.length == 2 && |
filtre[0] != null && |
!filtre[0].equals("") && |
filtre[1] != null && |
!filtre[1].equals("")); |
} |
|
public static String renvoyerMois(int numMois) { |
|
switch (numMois) { |
case 1: |
return "janvier" ; |
case 2: |
return "fevrier" ; |
case 3: |
return "mars" ; |
case 4: |
return "avril" ; |
case 5: |
return "mai" ; |
case 6: |
return "juin" ; |
case 7: |
return "juillet" ; |
case 8: |
return "août" ; |
case 9: |
return "septembre" ; |
case 10: |
return "octobre" ; |
case 11: |
return "novembre" ; |
case 12: |
return "décembre" ; |
default: |
return "Inconnu" ; |
} |
} |
|
public static String remplacerSautsDeligneMalEncodes(String chaineAvecSautsDeLignesMalEncodes) { |
|
String chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesMalEncodes.replace('\\','%'); |
chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replaceAll("%n","%"); |
chaineAvecSautsDeLignesBienEncodes = chaineAvecSautsDeLignesBienEncodes.replace('%','\n'); |
|
return chaineAvecSautsDeLignesBienEncodes; |
} |
|
public static boolean verifierDateFormatCel(String dateAVerifier) { |
|
String dateRemplacee = remplacerSeparateursDateFormatCel(dateAVerifier); |
String[] tabDate = dateRemplacee.split("/"); |
|
boolean retour = false; |
|
if(tabDate.length == 3) { |
//TODO: faire un parsing de date qui fonctionne mieux car |
// on peut saisir un 31 novembre par exemple |
// mais l'api date de java est mal gérée par gwt |
try { |
int jour = Integer.parseInt(tabDate[0]); |
int mois = Integer.parseInt(tabDate[1]); |
int annee = Integer.parseInt(tabDate[2]); |
|
if(jour <= 31 && mois <= 12 && tabDate[2].length() == 4) { |
retour = true; |
} |
} catch (Exception e) { |
|
} |
} |
|
return retour; |
} |
|
public static String remplacerSeparateursDateFormatCel(String date) { |
|
String dateRemplacee = date.replaceAll("-", "/"); |
|
return dateRemplacee; |
} |
} |