Subversion Repositories eFlore/Applications.coel

Rev

Rev 1038 | Rev 1319 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1038 Rev 1292
1
package org.tela_botanica.client.modeles;
1
package org.tela_botanica.client.modeles;
2
 
2
 
3
import java.util.HashMap;
3
import java.util.HashMap;
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;
8
import org.tela_botanica.client.util.UtilDAO;
9
import org.tela_botanica.client.util.UtilDAO;
9
 
10
 
10
import com.google.gwt.json.client.JSONArray;
11
import com.google.gwt.json.client.JSONArray;
-
 
12
import com.google.gwt.json.client.JSONBoolean;
-
 
13
import com.google.gwt.json.client.JSONNumber;
11
import com.google.gwt.json.client.JSONObject;
14
import com.google.gwt.json.client.JSONObject;
12
import com.google.gwt.json.client.JSONString;
15
import com.google.gwt.json.client.JSONString;
13
import com.google.gwt.json.client.JSONValue;
16
import com.google.gwt.json.client.JSONValue;
14
 
17
 
15
public class ValeurListeAsyncDao {
18
public class ValeurListeAsyncDao {
16
	
19
	
-
 
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>>();
17
	private static HashMap<String, ValeurListe> ontologieCache = new HashMap<String, ValeurListe>();
23
	
18
	private static final String SERVICE_NOM = "CoelValeurListe";
24
	private static final String SERVICE_NOM = "CoelValeurListe";
-
 
25
	private Rafraichissable vueARafraichir = null;
-
 
26
	private static ValeurListe tempListe = new ValeurListe();
-
 
27
	private int cptPage = 0;
19
	private Rafraichissable vueARafraichir = null;
28
	private int limiteJREST = 150;
20
	
29
 
21
	public ValeurListeAsyncDao(Rafraichissable vueCourante) {
30
	public ValeurListeAsyncDao(Rafraichissable vueCourante) {
22
		vueARafraichir = vueCourante;
31
		vueARafraichir = vueCourante;
23
	}
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			
-
 
64
			if (ontologieCache.get(id) != null) {
-
 
65
				vueARafraichir.rafraichir(ontologieCache.get(id));
-
 
66
			}
-
 
67
		}
-
 
68
	}
24
	
69
	
25
	public void obtenirListe(Integer cle)	{
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)	{
26
		selectionner("id", cle, null, null);
79
		selectionner("nom", cle, null, null, pagination, recherche, start, limit);
27
	}
80
	}
28
	
81
	
29
	public void selectionner(String type, Integer cle, String abv, String idValeur) {
82
	public void selectionner(String type, Integer cle, String abv, String idValeur) {
-
 
83
		selectionner(type, cle, abv, idValeur, false, null, -1, -1);
-
 
84
	}
30
		
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
 
31
		// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
88
		// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
32
		// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
89
		// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
33
		final String cleParent = cle + (abv == null ? "" : abv);
90
		final String cleParentPourCache = cle + (abv == null ? "" : abv);
-
 
91
		final String cleParent = cle+"";
34
		
92
		
35
		if (ontologieCache.containsKey(cleParent)) {
93
		String nom = "";
36
			vueARafraichir.rafraichir(ontologieCache.get(cleParent));
94
		if ( (recherche == null) || (recherche.equals("")) ) nom = "";
-
 
95
		else {
-
 
96
			nom = recherche+"%";
-
 
97
		}
-
 
98
 
-
 
99
		String abreviation = "";
-
 
100
		String paramAbv = "";
37
    	} else {
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;
38
    		String paramAbv = (type.equals("id") ? null : abv);
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");
39
    		String[] parametres = {type, cleParent.toString(), paramAbv, idValeur};
118
			
40
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
119
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
-
 
120
    		rb.envoyerRequete(null, new JsonRestRequestCallback()
41
    		rb.envoyerRequete(null, new JsonRestRequestCallback() {
121
    		{
42
    			@Override
122
    			@Override
43
    			public void surReponse(JSONValue responseValue) {
-
 
44
    				if (responseValue.isObject() != null) {
-
 
45
						final JSONObject reponse = responseValue.isObject();
-
 
46
						JSONString listeId = reponse.get("id").isString();
-
 
47
						JSONArray listeValeurs = reponse.get("valeurs").isArray();
123
    			public void surReponse(JSONValue responseValue)
48
						if (listeId != null)	{
-
 
49
							// Transformation du tableau JSON réponse en Liste
-
 
50
							ValeurListe liste = new ValeurListe(listeId, listeValeurs);
-
 
51
							// Stockage en cache
-
 
52
							ontologieCache.put(cleParent, liste);
-
 
53
							// et on met à jour le demandeur des données
124
    			{	    			
54
							vueARafraichir.rafraichir(liste);
-
 
55
						}
-
 
56
					}
125
    				vueARafraichir.rafraichir(responseValue);
57
    			}
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
-
 
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
	    			}
58
    		});
202
	    		});
-
 
203
	    	}
59
    	}
204
		}
60
	}
-
 
61
	
-
 
62
}
205
	}
-
 
206
}