1 |
package org.tela_botanica.del.client.utils;
|
1 |
package org.tela_botanica.del.client.utils;
|
2 |
|
2 |
|
3 |
import java.util.HashMap;
|
3 |
import java.util.LinkedHashMap;
|
- |
|
4 |
import java.util.Map;
|
4 |
import java.util.Map;
|
5 |
|
5 |
|
6 |
import com.google.gwt.core.client.GWT;
|
6 |
import com.google.gwt.http.client.Response;
|
7 |
import com.google.gwt.http.client.Response;
|
7 |
import com.google.gwt.json.client.JSONArray;
|
8 |
import com.google.gwt.json.client.JSONArray;
|
8 |
import com.google.gwt.json.client.JSONObject;
|
9 |
import com.google.gwt.json.client.JSONObject;
|
9 |
import com.google.gwt.json.client.JSONParser;
|
10 |
import com.google.gwt.json.client.JSONParser;
|
10 |
import com.google.gwt.json.client.JSONValue;
|
11 |
import com.google.gwt.json.client.JSONValue;
|
- |
|
12 |
import com.google.gwt.user.client.Window;
|
11 |
|
13 |
|
12 |
public class UtilitairesAutoCompletionService {
|
14 |
public class UtilitairesAutoCompletionService {
|
13 |
|
15 |
|
14 |
public static String effectuerPreTraitementChaineRequeteGenreEspeceSlash(String requete) {
|
16 |
public static String effectuerPreTraitementChaineRequeteGenreEspeceSlash(String requete) {
|
15 |
String chaineTraitee = requete;
|
17 |
String chaineTraitee = requete;
|
16 |
String[] parties = requete.split(" ", 2);
|
18 |
String[] parties = requete.split(" ", 2);
|
17 |
|
19 |
|
18 |
if(parties.length == 2) {
|
20 |
if(parties.length == 2) {
|
19 |
if(parties[1].trim().isEmpty()) {
|
21 |
if(parties[1].trim().isEmpty()) {
|
20 |
parties[1] = "*";
|
22 |
parties[1] = "*";
|
21 |
}
|
23 |
}
|
22 |
chaineTraitee = parties[0]+"/"+parties[1];
|
24 |
chaineTraitee = parties[0]+"/"+parties[1];
|
23 |
}
|
25 |
}
|
24 |
|
26 |
|
25 |
return chaineTraitee;
|
27 |
return chaineTraitee;
|
26 |
}
|
28 |
}
|
27 |
|
29 |
|
28 |
public static String effectuerPreTraitementChaineRequeteGenreEspeceEflore(String requete) {
|
30 |
public static String effectuerPreTraitementChaineRequeteGenreEspeceEflore(String requete) {
|
29 |
|
31 |
|
30 |
String chaineTraitee = "?recherche=etendue&ns.structure=au&retour.format=oss&masque="+requete;
|
32 |
String chaineTraitee = "?recherche=etendue&ns.structure=au&retour.format=oss&masque="+requete;
|
31 |
return chaineTraitee;
|
33 |
return chaineTraitee;
|
32 |
}
|
34 |
}
|
33 |
|
35 |
|
34 |
public static Map<String, String> extraireTaxonsNumNomsResultatRetourSimple(Response response) {
|
36 |
public static Map<String, InfosNomPourAutocompletion> extraireTaxonsNumNomsResultatRetourSimple(Response response) {
|
35 |
JSONObject responseValue = JSONParser.parseStrict(response.getText()).isObject();
|
37 |
JSONObject responseValue = JSONParser.parseStrict(response.getText()).isObject();
|
36 |
JSONArray noms = responseValue.get("resultats").isArray();
|
38 |
JSONArray noms = responseValue.get("resultats").isArray();
|
37 |
|
39 |
|
38 |
final int taillemax = noms.size();
|
40 |
final int taillemax = noms.size();
|
- |
|
41 |
// LinkedHashMap préserve l'ordre
|
39 |
Map<String, String> retourTaxons = new HashMap<String, String>(taillemax);
|
42 |
Map<String, InfosNomPourAutocompletion> retourTaxons = new LinkedHashMap<String, InfosNomPourAutocompletion>(taillemax);
|
40 |
|
43 |
|
- |
|
44 |
for (int i = 0; i < taillemax; i++) {
|
- |
|
45 |
// comment rendre compliqué un format fait pour être simple
|
41 |
for (int i = 0; i < taillemax; i++) {
|
46 |
JSONObject obj = noms.get(i).isObject();
|
- |
|
47 |
String ns = obj.get("ns").isString().stringValue();
|
42 |
String nom = (noms.get(i).isArray().get(0).isString().stringValue());
|
48 |
double nn = obj.get("nn").isNumber().doubleValue();
|
- |
|
49 |
boolean retenu = obj.get("retenu").isBoolean().booleanValue();
|
43 |
String taxon = (noms.get(i).isArray().get(1).isString().stringValue());
|
50 |
// empile, Gérard !
|
44 |
retourTaxons.put(nom, taxon);
|
51 |
retourTaxons.put(ns, new InfosNomPourAutocompletion(nn, ns, retenu));
|
45 |
}
|
52 |
}
|
46 |
return retourTaxons;
|
53 |
return retourTaxons;
|
47 |
}
|
54 |
}
|
48 |
|
55 |
|
49 |
public static String[] parserResultatRetourSimple(Response response) {
|
56 |
public static InfosNomPourAutocompletion[] parserResultatRetourSimple(Response response) {
|
50 |
JSONObject responseValue = JSONParser.parseStrict(response.getText()).isObject();
|
57 |
JSONObject responseValue = JSONParser.parseStrict(response.getText()).isObject();
|
51 |
JSONArray noms = responseValue.get("resultats").isArray();
|
58 |
JSONArray noms = responseValue.get("resultats").isArray();
|
52 |
|
- |
|
53 |
String[] valeurs = new String[0];
|
59 |
|
54 |
final int taillemax = noms.size();
|
60 |
final int taillemax = noms.size();
|
- |
|
61 |
InfosNomPourAutocompletion[] valeurs = new InfosNomPourAutocompletion[taillemax];
|
- |
|
62 |
|
55 |
valeurs = new String[taillemax];
|
63 |
// Eh les gars, si on refaisait le même truc ici que celui qu'on vient de faire en haut ?
|
- |
|
64 |
for (int i = 0; i < taillemax; i++) {
|
- |
|
65 |
// comment rendre comp... euh j'ai déjà dit ça il y a 20 lignes, non ?
|
56 |
for (int i = 0; i < taillemax; i++) {
|
66 |
JSONObject obj = noms.get(i).isObject();
|
- |
|
67 |
String ns = obj.get("ns").isString().stringValue();
|
- |
|
68 |
double nn = obj.get("nn").isNumber().doubleValue();
|
- |
|
69 |
boolean retenu = obj.get("retenu").isBoolean().booleanValue();
|
- |
|
70 |
// empile, Marcel !
|
57 |
valeurs[i] = (noms.get(i).isArray().get(0).isString().stringValue());
|
71 |
valeurs[i] = new InfosNomPourAutocompletion(nn, ns, retenu);
|
58 |
}
|
72 |
}
|
59 |
return valeurs;
|
73 |
return valeurs;
|
60 |
}
|
74 |
}
|
61 |
|
75 |
|
62 |
public static String[] parserRetourSimple(Response response) {
|
76 |
public static String[] parserRetourSimple(Response response) {
|
63 |
final JSONValue responseValue = JSONParser.parseStrict(response.getText());
|
77 |
final JSONValue responseValue = JSONParser.parseStrict(response.getText());
|
64 |
JSONArray noms;
|
78 |
JSONArray noms;
|
65 |
String[] valeurs = new String[0];
|
79 |
String[] valeurs = new String[0];
|
66 |
|
80 |
|
67 |
if ((noms=responseValue.isArray()) != null) {
|
81 |
if ((noms=responseValue.isArray()) != null) {
|
68 |
|
82 |
|
69 |
final int taillemax = noms.size();
|
83 |
final int taillemax = noms.size();
|
70 |
valeurs = new String[taillemax];
|
84 |
valeurs = new String[taillemax];
|
71 |
for (int i = 0; i < taillemax; i++) {
|
85 |
for (int i = 0; i < taillemax; i++) {
|
72 |
valeurs[i] = (noms.get(i).isArray().get(0).isString().stringValue());
|
86 |
valeurs[i] = (noms.get(i).isArray().get(0).isString().stringValue());
|
73 |
}
|
87 |
}
|
74 |
}
|
88 |
}
|
75 |
return valeurs;
|
89 |
return valeurs;
|
76 |
}
|
90 |
}
|
77 |
|
91 |
|
78 |
public static String[] parserRetourOss(Response response) {
|
92 |
public static String[] parserRetourOss(Response response) {
|
79 |
JSONValue retourJson = JSONParser.parseStrict(response.getText());
|
93 |
JSONValue retourJson = JSONParser.parseStrict(response.getText());
|
80 |
JSONArray tableauResultat = retourJson.isArray().get(1).isArray();
|
94 |
JSONArray tableauResultat = retourJson.isArray().get(1).isArray();
|
81 |
|
95 |
|
82 |
String[] suggestions = new String[tableauResultat.size()];
|
96 |
String[] suggestions = new String[tableauResultat.size()];
|
83 |
for (int i = 0; i < tableauResultat.size(); i++) {
|
97 |
for (int i = 0; i < tableauResultat.size(); i++) {
|
84 |
suggestions[i] = tableauResultat.get(i).isString().stringValue();
|
98 |
suggestions[i] = tableauResultat.get(i).isString().stringValue();
|
85 |
}
|
99 |
}
|
86 |
|
100 |
|
87 |
return suggestions;
|
101 |
return suggestions;
|
88 |
}
|
102 |
}
|
89 |
}
|
103 |
}
|