Subversion Repositories eFlore/Applications.coel

Rev

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