Subversion Repositories eFlore/Applications.coel

Rev

Rev 1038 | Rev 1319 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
69 jpm 1
package org.tela_botanica.client.modeles;
2
 
570 jp_milcent 3
import java.util.HashMap;
4
 
773 jpm 5
import org.tela_botanica.client.http.JsonRestRequestBuilder;
6
import org.tela_botanica.client.http.JsonRestRequestCallback;
69 jpm 7
import org.tela_botanica.client.interfaces.Rafraichissable;
1292 cyprien 8
import org.tela_botanica.client.util.Debug;
299 gduche 9
import org.tela_botanica.client.util.UtilDAO;
69 jpm 10
 
11
import com.google.gwt.json.client.JSONArray;
1292 cyprien 12
import com.google.gwt.json.client.JSONBoolean;
13
import com.google.gwt.json.client.JSONNumber;
91 jpm 14
import com.google.gwt.json.client.JSONObject;
15
import com.google.gwt.json.client.JSONString;
69 jpm 16
import com.google.gwt.json.client.JSONValue;
17
 
18
public class ValeurListeAsyncDao {
570 jp_milcent 19
 
1292 cyprien 20
	private static HashMap<String, Object> ontologieCache = new HashMap<String, Object>();
21
 
22
	private static HashMap<String, HashMap<Integer, Object>> pagedOntologieCache = new HashMap<String, HashMap<Integer, Object>>();
23
 
268 jp_milcent 24
	private static final String SERVICE_NOM = "CoelValeurListe";
570 jp_milcent 25
	private Rafraichissable vueARafraichir = null;
1292 cyprien 26
	private static ValeurListe tempListe = new ValeurListe();
27
	private int cptPage = 0;
28
	private int limiteJREST = 150;
29
 
570 jp_milcent 30
	public ValeurListeAsyncDao(Rafraichissable vueCourante) {
31
		vueARafraichir = vueCourante;
268 jp_milcent 32
	}
33
 
1292 cyprien 34
	public static HashMap<String, Object> getOntologieCache() {
35
		return ontologieCache;
292 gduche 36
	}
1292 cyprien 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
64
			if (ontologieCache.get(id) != null) {
65
				vueARafraichir.rafraichir(ontologieCache.get(id));
66
			}
67
		}
68
	}
292 gduche 69
 
1292 cyprien 70
	public void obtenirListe(Integer cle)	{
71
		selectionner("id", cle, null, null, false, null, -1, -1);
72
	}
73
 
74
	public void obtenirListe(String type, String identifiantValeur)	{
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);
80
	}
81
 
1038 gduche 82
	public void selectionner(String type, Integer cle, String abv, String idValeur) {
1292 cyprien 83
		selectionner(type, cle, abv, idValeur, false, null, -1, -1);
84
	}
85
 
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
 
1038 gduche 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 ....
1292 cyprien 90
		final String cleParentPourCache = cle + (abv == null ? "" : abv);
91
		final String cleParent = cle+"";
1038 gduche 92
 
1292 cyprien 93
		String nom = "";
94
		if ( (recherche == null) || (recherche.equals("")) ) nom = "";
95
		else {
96
			nom = recherche+"%";
97
		}
98
 
99
		String abreviation = "";
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};
110
 
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
    		{
773 jpm 122
    			@Override
1292 cyprien 123
    			public void surReponse(JSONValue responseValue)
124
    			{
125
    				vueARafraichir.rafraichir(responseValue);
773 jpm 126
    			}
127
    		});
1292 cyprien 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
146
			if (ontologieCache.containsKey(cleParentPourCache) && !nextPage)
147
			{
148
				vueARafraichir.rafraichir(ontologieCache.get(cleParentPourCache));
149
	    	}
150
			else
151
	    	{
152
	    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
153
	    		rb.envoyerRequete(null, new JsonRestRequestCallback()
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) {
170
								abreviationStr = reponse.get("abreviation").isString().stringValue();
171
								int a = abreviationStr.indexOf("%", 1);
172
								abreviationStr = abreviationStr.substring(1, a);
173
							}
174
							else {
175
								abreviationStr = "";
176
							}
177
 
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();
183
								ValeurListe liste = new ValeurListe(new JSONString(id), abreviationStr, listeValeurs, nbElements);
184
 
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 **/
190
									((ValeurListe)ontologieCache.get(identifiantCache)).concatenerListe(liste);
191
								}
192
								/** Sinon on l'insère simplement dans le cache **/
193
								else {
194
									ontologieCache.put(identifiantCache,liste);
195
								}
196
 
197
								/** Appel à la méthode qui gère le retour à l'appelant ou la suite du chargement **/
198
								chargerListe(getNextPage.booleanValue(), type, liste.getId(), abv, idValeur, pagination, recherche, start, limit);
199
							}
200
	    				}
201
	    			}
202
	    		});
203
	    	}
204
		}
69 jpm 205
	}
1292 cyprien 206
}