Subversion Repositories eFlore/Applications.coel

Compare Revisions

Ignore whitespace Rev 1291 → Rev 1292

/trunk/src/org/tela_botanica/client/modeles/ValeurListeAsyncDao.java
5,9 → 5,12
import org.tela_botanica.client.http.JsonRestRequestBuilder;
import org.tela_botanica.client.http.JsonRestRequestCallback;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.util.Debug;
import org.tela_botanica.client.util.UtilDAO;
 
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
14,49 → 17,190
 
public class ValeurListeAsyncDao {
private static HashMap<String, ValeurListe> ontologieCache = new HashMap<String, ValeurListe>();
private static HashMap<String, Object> ontologieCache = new HashMap<String, Object>();
private static HashMap<String, HashMap<Integer, Object>> pagedOntologieCache = new HashMap<String, HashMap<Integer, Object>>();
private static final String SERVICE_NOM = "CoelValeurListe";
private Rafraichissable vueARafraichir = null;
private static ValeurListe tempListe = new ValeurListe();
private int cptPage = 0;
private int limiteJREST = 150;
 
public ValeurListeAsyncDao(Rafraichissable vueCourante) {
vueARafraichir = vueCourante;
}
public void obtenirListe(Integer cle) {
selectionner("id", cle, null, null);
public static HashMap<String, Object> getOntologieCache() {
return ontologieCache;
}
 
public void chargerListe(boolean nextPage, String type, Integer cle, String abv, String idValeur, boolean pagination, String recherche, int start, int nbElements) {
/** Si nextpage est VRAI, alors cela signifie que la liste est plus grande
que la limite du JREST et l'on doit relancer une requete pour obtenir
la page suivante **/
if (nextPage) {
cptPage++;
selectionner(type, cle, abv, idValeur, pagination, recherche, cptPage*limiteJREST, limiteJREST);
}
/** Sinon cela signifie que le chargement de la liste est terminé et on peut
la mettre en cache et retourner la réponse au demandeur **/
else {
// on met en cache
String id = String.valueOf(cle);
String abreviationStr = "";
if (abv != null) {
abreviationStr = abv;
int indexPoint = abreviationStr.indexOf(".", 0);
abreviationStr = abreviationStr.substring(0, indexPoint);
id = id+abreviationStr;
}
// réinitialiser le compteur
cptPage = 0;
 
// et on met à jour le demandeur des données
if (ontologieCache.get(id) != null) {
vueARafraichir.rafraichir(ontologieCache.get(id));
}
}
}
public void obtenirListe(Integer cle) {
selectionner("id", cle, null, null, false, null, -1, -1);
}
public void obtenirListe(String type, String identifiantValeur) {
selectionner(type, null, null, identifiantValeur, false, null, -1, -1);
}
public void obtenirListe(Integer cle, boolean pagination, String recherche, int start, int limit) {
selectionner("nom", cle, null, null, pagination, recherche, start, limit);
}
public void selectionner(String type, Integer cle, String abv, String idValeur) {
selectionner(type, cle, abv, idValeur, false, null, -1, -1);
}
 
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) {
 
// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
final String cleParent = cle + (abv == null ? "" : abv);
final String cleParentPourCache = cle + (abv == null ? "" : abv);
final String cleParent = cle+"";
if (ontologieCache.containsKey(cleParent)) {
vueARafraichir.rafraichir(ontologieCache.get(cleParent));
} else {
String paramAbv = (type.equals("id") ? null : abv);
String[] parametres = {type, cleParent.toString(), paramAbv, idValeur};
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
rb.envoyerRequete(null, new JsonRestRequestCallback() {
String nom = "";
if ( (recherche == null) || (recherche.equals("")) ) nom = "";
else {
nom = recherche+"%";
}
 
String abreviation = "";
String paramAbv = "";
if (type.equals("ab") || type.equals("abv")) {
int positionPoint = abv.indexOf(".");
if (positionPoint != -1) abreviation = abv.substring(0, positionPoint)+"%";
else abreviation=abv+"%";
}
paramAbv = abreviation;
 
String[] parametres = {type, cleParent, paramAbv, idValeur, nom};
 
HashMap<String, String> restrictions = new HashMap<String, String>();
 
if (pagination) {
 
restrictions.put("limit", String.valueOf(limit));
restrictions.put("start", String.valueOf(start));
restrictions.put("orderby", "cmlv_nom");
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
rb.envoyerRequete(null, new JsonRestRequestCallback()
{
@Override
public void surReponse(JSONValue responseValue) {
if (responseValue.isObject() != null) {
final JSONObject reponse = responseValue.isObject();
JSONString listeId = reponse.get("id").isString();
JSONArray listeValeurs = reponse.get("valeurs").isArray();
if (listeId != null) {
// Transformation du tableau JSON réponse en Liste
ValeurListe liste = new ValeurListe(listeId, listeValeurs);
// Stockage en cache
ontologieCache.put(cleParent, liste);
// et on met à jour le demandeur des données
vueARafraichir.rafraichir(liste);
}
}
public void surReponse(JSONValue responseValue)
{
vueARafraichir.rafraichir(responseValue);
}
});
}
}
else
{
boolean nextPage = (start > 0);
if (nextPage)
{
restrictions.put("start", String.valueOf(start));
restrictions.put("limit", String.valueOf(limit));
restrictions.put("orderby", "cmlv_nom");
}
else
{
restrictions.put("orderby", "cmlv_nom");
}
// si l'on est pas dans un processus de récupération d'une liste
// et si le cache contient déjà la liste recherchée
if (ontologieCache.containsKey(cleParentPourCache) && !nextPage)
{
vueARafraichir.rafraichir(ontologieCache.get(cleParentPourCache));
}
else
{
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
rb.envoyerRequete(null, new JsonRestRequestCallback()
{
@Override
public void surReponse(JSONValue responseValue)
{
if (responseValue.isObject() != null)
{
/** Récuperation des différents paramètres de la réponse JSON **/
final JSONObject reponse = responseValue.isObject();
JSONString listeId = reponse.get("id").isString();
JSONArray listeValeurs = reponse.get("valeurs").isArray();
JSONNumber nbElements = reponse.get("nbElements").isNumber();
JSONBoolean getNextPage = reponse.get("getNextPage").isBoolean();
/** Gestion de l'abreviation (pour la liste des régions) **/
String abreviationStr = "";
if (reponse.get("abreviation") != null) {
abreviationStr = reponse.get("abreviation").isString().stringValue();
int a = abreviationStr.indexOf("%", 1);
abreviationStr = abreviationStr.substring(1, a);
}
else {
abreviationStr = "";
}
 
/** si l'on a bien reçu une liste de valeurs **/
if (listeId != null)
{
/** Transformation du tableau JSON réponse en ValeurListe **/
String id = listeId.stringValue();
ValeurListe liste = new ValeurListe(new JSONString(id), abreviationStr, listeValeurs, nbElements);
 
/** Si la liste existe deja en cache **/
String identifiantCache = (abreviationStr=="") ? id : (id+abreviationStr);
if (ontologieCache.get(id) != null)
{
/** Alors on concatène la liste existante avec celle qu'on vient de recevoir **/
((ValeurListe)ontologieCache.get(identifiantCache)).concatenerListe(liste);
}
/** Sinon on l'insère simplement dans le cache **/
else {
ontologieCache.put(identifiantCache,liste);
}
 
/** Appel à la méthode qui gère le retour à l'appelant ou la suite du chargement **/
chargerListe(getNextPage.booleanValue(), type, liste.getId(), abv, idValeur, pagination, recherche, start, limit);
}
}
}
});
}
}
}
}
}