Subversion Repositories eFlore/Applications.coel

Rev

Rev 1038 | Rev 1319 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1038 Rev 1292
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";
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;
19
	private Rafraichissable vueARafraichir = null;
28
	private int limiteJREST = 150;
Line -... Line 29...
-
 
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			
-
 
64
			if (ontologieCache.get(id) != null) {
20
	
65
				vueARafraichir.rafraichir(ontologieCache.get(id));
21
	public ValeurListeAsyncDao(Rafraichissable vueCourante) {
66
			}
-
 
67
		}
-
 
68
	}
-
 
69
	
-
 
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)	{		
22
		vueARafraichir = vueCourante;
75
		selectionner(type, null, null, identifiantValeur, false, null, -1, -1);
Line 23... Line 76...
23
	}
76
	}
-
 
77
	
-
 
78
	public void obtenirListe(Integer cle, boolean pagination, String recherche, int start, int limit)	{
24
	
79
		selectionner("nom", cle, null, null, pagination, recherche, start, limit);
-
 
80
	}
-
 
81
	
25
	public void obtenirListe(Integer cle)	{
82
	public void selectionner(String type, Integer cle, String abv, String idValeur) {
26
		selectionner("id", cle, null, null);
83
		selectionner(type, cle, abv, idValeur, false, null, -1, -1);
27
	}
84
	}
-
 
85
 
Line 28... Line 86...
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) {
29
	public void selectionner(String type, Integer cle, String abv, String idValeur) {
87
 
30
		
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
		
-
 
93
		String nom = "";
-
 
94
		if ( (recherche == null) || (recherche.equals("")) ) nom = "";
31
		// La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
95
		else {
-
 
96
			nom = recherche+"%";
-
 
97
		}
-
 
98
 
-
 
99
		String abreviation = "";
-
 
100
		String paramAbv = "";
-
 
101
		
32
		// (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
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>();
33
		final String cleParent = cle + (abv == null ? "" : abv);
112
 
34
		
113
		if (pagination)	{
-
 
114
 
35
		if (ontologieCache.containsKey(cleParent)) {
115
			restrictions.put("limit", String.valueOf(limit));
36
			vueARafraichir.rafraichir(ontologieCache.get(cleParent));
116
			restrictions.put("start", String.valueOf(start));
37
    	} else {
-
 
38
    		String paramAbv = (type.equals("id") ? null : abv);
-
 
39
    		String[] parametres = {type, cleParent.toString(), paramAbv, idValeur};
-
 
40
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
-
 
41
    		rb.envoyerRequete(null, new JsonRestRequestCallback() {
117
			restrictions.put("orderby", "cmlv_nom");
42
    			@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();
118
			
48
						if (listeId != null)	{
-
 
49
							// Transformation du tableau JSON réponse en Liste
-
 
50
							ValeurListe liste = new ValeurListe(listeId, listeValeurs);
119
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
51
							// Stockage en cache
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
-
 
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
								}
52
							ontologieCache.put(cleParent, liste);
196
 
-
 
197
								/** Appel à la méthode qui gère le retour à l'appelant ou la suite du chargement **/
53
							// et on met à jour le demandeur des données
198
								chargerListe(getNextPage.booleanValue(), type, liste.getId(), abv, idValeur, pagination, recherche, start, limit);
54
							vueARafraichir.rafraichir(liste);
-
 
55
						}
-
 
56
					}
199
							}
-
 
200
	    				}