Line 3... |
Line 3... |
3 |
import java.util.HashMap;
|
3 |
import java.util.HashMap;
|
Line 4... |
Line 4... |
4 |
|
4 |
|
5 |
import org.tela_botanica.client.http.JsonRestRequestBuilder;
|
5 |
import org.tela_botanica.client.http.JsonRestRequestBuilder;
|
6 |
import org.tela_botanica.client.http.JsonRestRequestCallback;
|
6 |
import org.tela_botanica.client.http.JsonRestRequestCallback;
|
- |
|
7 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
7 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
8 |
import org.tela_botanica.client.util.Debug;
|
Line 8... |
Line 9... |
8 |
import org.tela_botanica.client.util.UtilDAO;
|
9 |
import org.tela_botanica.client.util.UtilDAO;
|
- |
|
10 |
|
- |
|
11 |
import com.google.gwt.json.client.JSONArray;
|
9 |
|
12 |
import com.google.gwt.json.client.JSONBoolean;
|
10 |
import com.google.gwt.json.client.JSONArray;
|
13 |
import com.google.gwt.json.client.JSONNumber;
|
11 |
import com.google.gwt.json.client.JSONObject;
|
14 |
import com.google.gwt.json.client.JSONObject;
|
Line 12... |
Line 15... |
12 |
import com.google.gwt.json.client.JSONString;
|
15 |
import com.google.gwt.json.client.JSONString;
|
Line 13... |
Line 16... |
13 |
import com.google.gwt.json.client.JSONValue;
|
16 |
import com.google.gwt.json.client.JSONValue;
|
- |
|
17 |
|
- |
|
18 |
public class ValeurListeAsyncDao {
|
- |
|
19 |
|
14 |
|
20 |
private static HashMap<String, Object> ontologieCache = new HashMap<String, Object>();
|
15 |
public class ValeurListeAsyncDao {
|
21 |
|
- |
|
22 |
private static HashMap<String, HashMap<Integer, Object>> pagedOntologieCache = new HashMap<String, HashMap<Integer, Object>>();
|
- |
|
23 |
|
- |
|
24 |
private static final String SERVICE_NOM = "CoelValeurListe";
|
Line 16... |
Line 25... |
16 |
|
25 |
private Rafraichissable vueARafraichir = null;
|
17 |
private static HashMap<String, ValeurListe> ontologieCache = new HashMap<String, ValeurListe>();
|
26 |
private static ValeurListe tempListe = new ValeurListe();
|
18 |
private static final String SERVICE_NOM = "CoelValeurListe";
|
27 |
private int cptPage = 0;
|
Line -... |
Line 28... |
- |
|
28 |
private int limiteJREST = 150;
|
- |
|
29 |
|
- |
|
30 |
public ValeurListeAsyncDao(Rafraichissable vueCourante) {
|
- |
|
31 |
vueARafraichir = vueCourante;
|
- |
|
32 |
}
|
- |
|
33 |
|
- |
|
34 |
public static HashMap<String, Object> getOntologieCache() {
|
- |
|
35 |
return ontologieCache;
|
- |
|
36 |
}
|
- |
|
37 |
|
- |
|
38 |
public void chargerListe(boolean nextPage, String type, Integer cle, String abv, String idValeur, boolean pagination, String recherche, int start, int nbElements) {
|
- |
|
39 |
/** Si nextpage est VRAI, alors cela signifie que la liste est plus grande
|
- |
|
40 |
que la limite du JREST et l'on doit relancer une requete pour obtenir
|
- |
|
41 |
la page suivante **/
|
- |
|
42 |
if (nextPage) {
|
- |
|
43 |
cptPage++;
|
- |
|
44 |
selectionner(type, cle, abv, idValeur, pagination, recherche, cptPage*limiteJREST, limiteJREST);
|
- |
|
45 |
}
|
- |
|
46 |
/** Sinon cela signifie que le chargement de la liste est terminé et on peut
|
- |
|
47 |
la mettre en cache et retourner la réponse au demandeur **/
|
- |
|
48 |
else {
|
- |
|
49 |
// on met en cache
|
- |
|
50 |
String id = String.valueOf(cle);
|
- |
|
51 |
|
- |
|
52 |
String abreviationStr = "";
|
- |
|
53 |
if (abv != null) {
|
- |
|
54 |
abreviationStr = abv;
|
- |
|
55 |
int indexPoint = abreviationStr.indexOf(".", 0);
|
- |
|
56 |
abreviationStr = abreviationStr.substring(0, indexPoint);
|
- |
|
57 |
id = id+abreviationStr;
|
- |
|
58 |
}
|
- |
|
59 |
|
- |
|
60 |
// réinitialiser le compteur
|
- |
|
61 |
cptPage = 0;
|
- |
|
62 |
|
- |
|
63 |
// et on met à jour le demandeur des données
|
19 |
private Rafraichissable vueARafraichir = null;
|
64 |
if (ontologieCache.get(id) != null) {
|
20 |
|
65 |
vueARafraichir.rafraichir(ontologieCache.get(id));
|
- |
|
66 |
}
|
- |
|
67 |
}
|
- |
|
68 |
}
|
- |
|
69 |
|
- |
|
70 |
public void obtenirListe(Integer cle) {
|
- |
|
71 |
selectionner("id", cle, null, null, false, null, -1, -1);
|
- |
|
72 |
}
|
- |
|
73 |
|
21 |
public ValeurListeAsyncDao(Rafraichissable vueCourante) {
|
74 |
public void obtenirListe(String type, String identifiantValeur) {
|
Line 22... |
Line 75... |
22 |
vueARafraichir = vueCourante;
|
75 |
selectionner(type, null, null, identifiantValeur, false, null, -1, -1);
|
- |
|
76 |
}
|
- |
|
77 |
|
- |
|
78 |
public void obtenirListe(Integer cle, boolean pagination, String recherche, int start, int limit) {
|
- |
|
79 |
selectionner("nom", cle, null, null, pagination, recherche, start, limit);
|
Line 23... |
Line 80... |
23 |
}
|
80 |
}
|
24 |
|
81 |
|
25 |
public void obtenirListe(Integer cle) {
|
82 |
public void selectionner(String type, Integer cle, String abv, String idValeur) {
|
- |
|
83 |
selectionner(type, cle, abv, idValeur, false, null, -1, -1);
|
Line 26... |
Line 84... |
26 |
selectionner("id", cle, null, null);
|
84 |
}
|
27 |
}
|
85 |
|
28 |
|
86 |
public void selectionner(final String type, final Integer cle, final String abv, final String idValeur, final boolean pagination, final String recherche, final int start, final int limit) {
|
- |
|
87 |
|
- |
|
88 |
// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
|
- |
|
89 |
// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
|
- |
|
90 |
final String cleParentPourCache = cle + (abv == null ? "" : abv);
|
- |
|
91 |
final String cleParent = cle+"";
|
- |
|
92 |
|
29 |
public void selectionner(String type, Integer cle, String abv, String idValeur) {
|
93 |
String nom = "";
|
- |
|
94 |
if ( (recherche == null) || (recherche.equals("")) ) nom = "";
|
- |
|
95 |
else {
|
- |
|
96 |
nom = recherche+"%";
|
- |
|
97 |
}
|
- |
|
98 |
|
- |
|
99 |
String abreviation = "";
|
30 |
|
100 |
String paramAbv = "";
|
- |
|
101 |
|
- |
|
102 |
if (type.equals("ab") || type.equals("abv")) {
|
- |
|
103 |
int positionPoint = abv.indexOf(".");
|
- |
|
104 |
if (positionPoint != -1) abreviation = abv.substring(0, positionPoint)+"%";
|
- |
|
105 |
else abreviation=abv+"%";
|
- |
|
106 |
}
|
- |
|
107 |
paramAbv = abreviation;
|
- |
|
108 |
|
- |
|
109 |
String[] parametres = {type, cleParent, paramAbv, idValeur, nom};
|
31 |
// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
|
110 |
|
32 |
// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
|
111 |
HashMap<String, String> restrictions = new HashMap<String, String>();
|
- |
|
112 |
|
- |
|
113 |
if (pagination) {
|
- |
|
114 |
|
- |
|
115 |
restrictions.put("limit", String.valueOf(limit));
|
- |
|
116 |
restrictions.put("start", String.valueOf(start));
|
- |
|
117 |
restrictions.put("orderby", "cmlv_nom");
|
- |
|
118 |
|
- |
|
119 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
|
- |
|
120 |
rb.envoyerRequete(null, new JsonRestRequestCallback()
|
- |
|
121 |
{
|
- |
|
122 |
@Override
|
- |
|
123 |
public void surReponse(JSONValue responseValue)
|
- |
|
124 |
{
|
- |
|
125 |
vueARafraichir.rafraichir(responseValue);
|
- |
|
126 |
}
|
- |
|
127 |
});
|
- |
|
128 |
}
|
- |
|
129 |
else
|
- |
|
130 |
{
|
- |
|
131 |
boolean nextPage = (start > 0);
|
- |
|
132 |
|
- |
|
133 |
if (nextPage)
|
- |
|
134 |
{
|
- |
|
135 |
restrictions.put("start", String.valueOf(start));
|
- |
|
136 |
restrictions.put("limit", String.valueOf(limit));
|
- |
|
137 |
restrictions.put("orderby", "cmlv_nom");
|
- |
|
138 |
}
|
- |
|
139 |
else
|
- |
|
140 |
{
|
- |
|
141 |
restrictions.put("orderby", "cmlv_nom");
|
- |
|
142 |
}
|
- |
|
143 |
|
- |
|
144 |
// si l'on est pas dans un processus de récupération d'une liste
|
- |
|
145 |
// et si le cache contient déjà la liste recherchée
|
33 |
final String cleParent = cle + (abv == null ? "" : abv);
|
146 |
if (ontologieCache.containsKey(cleParentPourCache) && !nextPage)
|
34 |
|
147 |
{
|
- |
|
148 |
vueARafraichir.rafraichir(ontologieCache.get(cleParentPourCache));
|
35 |
if (ontologieCache.containsKey(cleParent)) {
|
149 |
}
|
- |
|
150 |
else
|
- |
|
151 |
{
|
36 |
vueARafraichir.rafraichir(ontologieCache.get(cleParent));
|
152 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
|
37 |
} else {
|
153 |
rb.envoyerRequete(null, new JsonRestRequestCallback()
|
38 |
String paramAbv = (type.equals("id") ? null : abv);
|
154 |
{
|
- |
|
155 |
@Override
|
- |
|
156 |
public void surReponse(JSONValue responseValue)
|
- |
|
157 |
{
|
- |
|
158 |
if (responseValue.isObject() != null)
|
- |
|
159 |
{
|
- |
|
160 |
/** Récuperation des différents paramètres de la réponse JSON **/
|
- |
|
161 |
final JSONObject reponse = responseValue.isObject();
|
- |
|
162 |
JSONString listeId = reponse.get("id").isString();
|
- |
|
163 |
JSONArray listeValeurs = reponse.get("valeurs").isArray();
|
- |
|
164 |
JSONNumber nbElements = reponse.get("nbElements").isNumber();
|
- |
|
165 |
JSONBoolean getNextPage = reponse.get("getNextPage").isBoolean();
|
- |
|
166 |
|
- |
|
167 |
/** Gestion de l'abreviation (pour la liste des régions) **/
|
- |
|
168 |
String abreviationStr = "";
|
- |
|
169 |
if (reponse.get("abreviation") != null) {
|
39 |
String[] parametres = {type, cleParent.toString(), paramAbv, idValeur};
|
170 |
abreviationStr = reponse.get("abreviation").isString().stringValue();
|
- |
|
171 |
int a = abreviationStr.indexOf("%", 1);
|
40 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
|
172 |
abreviationStr = abreviationStr.substring(1, a);
|
- |
|
173 |
}
|
41 |
rb.envoyerRequete(null, new JsonRestRequestCallback() {
|
174 |
else {
|
- |
|
175 |
abreviationStr = "";
|
42 |
@Override
|
176 |
}
|
- |
|
177 |
|
43 |
public void surReponse(JSONValue responseValue) {
|
178 |
/** si l'on a bien reçu une liste de valeurs **/
|
- |
|
179 |
if (listeId != null)
|
- |
|
180 |
{
|
- |
|
181 |
/** Transformation du tableau JSON réponse en ValeurListe **/
|
- |
|
182 |
String id = listeId.stringValue();
|
44 |
if (responseValue.isObject() != null) {
|
183 |
ValeurListe liste = new ValeurListe(new JSONString(id), abreviationStr, listeValeurs, nbElements);
|
- |
|
184 |
|
45 |
final JSONObject reponse = responseValue.isObject();
|
185 |
/** Si la liste existe deja en cache **/
|
- |
|
186 |
String identifiantCache = (abreviationStr=="") ? id : (id+abreviationStr);
|
- |
|
187 |
if (ontologieCache.get(id) != null)
|
- |
|
188 |
{
|
- |
|
189 |
/** Alors on concatène la liste existante avec celle qu'on vient de recevoir **/
|
46 |
JSONString listeId = reponse.get("id").isString();
|
190 |
((ValeurListe)ontologieCache.get(identifiantCache)).concatenerListe(liste);
|
47 |
JSONArray listeValeurs = reponse.get("valeurs").isArray();
|
191 |
}
|
48 |
if (listeId != null) {
|
192 |
/** Sinon on l'insère simplement dans le cache **/
|
49 |
// Transformation du tableau JSON réponse en Liste
|
193 |
else {
|
50 |
ValeurListe liste = new ValeurListe(listeId, listeValeurs);
|
194 |
ontologieCache.put(identifiantCache,liste);
|
51 |
// Stockage en cache
|
195 |
}
|
52 |
ontologieCache.put(cleParent, liste);
|
196 |
|
53 |
// et on met à jour le demandeur des données
|
197 |
/** Appel à la méthode qui gère le retour à l'appelant ou la suite du chargement **/
|
54 |
vueARafraichir.rafraichir(liste);
|
198 |
chargerListe(getNextPage.booleanValue(), type, liste.getId(), abv, idValeur, pagination, recherche, start, limit);
|