Subversion Repositories eFlore/Applications.cel

Rev

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

Rev Author Line No. Line
12 david 1
package org.tela_botanica.client.modeles;
2
 
3
import org.tela_botanica.client.Util;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
5
import org.tela_botanica.client.observation.ObservationModele;
6
 
32 jpm 7
import com.google.gwt.http.client.Request;
8
import com.google.gwt.http.client.RequestBuilder;
9
import com.google.gwt.http.client.RequestCallback;
10
import com.google.gwt.http.client.RequestException;
11
import com.google.gwt.http.client.Response;
12 david 12
import com.google.gwt.json.client.JSONArray;
13
import com.google.gwt.json.client.JSONParser;
14
import com.google.gwt.json.client.JSONString;
15
import com.google.gwt.json.client.JSONValue;
16
 
17
/**
18
 * DAO la liste des observations attachées a un observateur
19
 * @author David Delon
20 david 20
 * TODO : se servir de ObservationDAO pour la lecture unitaire
12 david 21
 *
22
 */
23
public class ListeObservationAsynchroneDAO {
24
 
25
 
26
 
27
 
28
	/**
29
	 * Le modèle associé au DAO
30
	 */
31
	private ObservationModele observationModele = null ;
32
 
33
	public ListeObservationAsynchroneDAO(ObservationModele observationModele)
34
	{
35
 
36
		this.observationModele=observationModele;
37
	}
38
 
39
 
40
	/**
41
	 * Envoie une requete au serveur jrest pour obtenir le nombre d'observation correspondant
42
	 * à des critères données en paramètres
43
	 * @param r le rafraichissable qui demande la mise à jour
44
	 * @param criteres un tableau nom/valeur des critères pour les observations
45
	 */
46
	public void obtenirListeObservation(final Rafraichissable r, String utilisateur, String[][] criteres)
47
	{
48
 
49
 
50
		String requete = "" ;
51
 
52
		if(criteres != null)
53
		{
54
			// on construit les paramètres du get avec les critères (&critere1=valeur1&critere2=valeur2 etc...)
55
			// ils contiennent limite et taille page
56
 
57
			for (int i = 0; i < criteres.length; i++) {
58
 
59
				if(!criteres[i][1].equals(""))
60
				{
61
					if(i!= 0)
62
					{
63
						requete += "&";
64
					}
65
					requete += criteres[i][0]+"="+criteres[i][1] ;
66
				}
67
			}
32 jpm 68
		}
12 david 69
 
32 jpm 70
		// on envoie le get asynchrone
71
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,observationModele.getConfig().getServiceBaseUrl()
72
				+"/InventoryObservationList/"+utilisateur+"/"+requete) ;
12 david 73
 
32 jpm 74
		try {
75
			rb.sendRequest(null, new RequestCallback() {
76
 
77
				public void onError(Request request, Throwable exception) {
78
					// TODO Auto-generated method stub
12 david 79
 
32 jpm 80
				}
81
 
82
				public void onResponseReceived(Request request,
83
						Response response) {
12 david 84
 
32 jpm 85
					final ListeObservation observationData ;
86
					final JSONValue responseValue = JSONParser.parse(response.getText());
87
 
88
					JSONArray reponse=null;
12 david 89
 
32 jpm 90
					// si c'est un tableau
91
					if ((reponse=responseValue.isArray()) != null) {
12 david 92
 
32 jpm 93
						JSONArray observation;
94
						final int taillemax = reponse.size();
95
 
96
						observationData = new ListeObservation(taillemax);
97
 
98
						for (int i = 0; i < taillemax; i++) {
20 david 99
 
32 jpm 100
							if ((observation=reponse.get(i).isArray()) != null) {
101
 
102
								String transmis=((JSONString) observation.get(13)).stringValue();
103
								String identifiantLocalite=((JSONString) observation.get(14)).toString();
104
								String nomSaisi=Util.toCelString(((JSONString) observation.get(0)).toString());
105
								String nomRetenu=Util.toCelString(((JSONString) observation.get(2)).toString());
106
								String numeroNomenclaturalSaisi=((JSONString) observation.get(1)).stringValue();
107
								String numeroNomenclaturalRetenu=((JSONString) observation.get(3)).stringValue();
108
								String numeroTaxonomique=((JSONString) observation.get(4)).stringValue();
109
								String famille=Util.toCelString(((JSONString) observation .get(5)).toString());
110
								String localite=Util.toCelString(((JSONString) observation .get(6)).toString());
111
								String lieudit=Util.toCelString(((JSONString) observation .get(9)).toString());
112
								String station=Util.toCelString(((JSONString) observation .get(10)).toString());
113
								String milieu=Util.toCelString(((JSONString) observation .get(11)).toString());
114
								String commentaire=Util.toCelString(((JSONString) observation .get(12)).toString());
115
								String date=((JSONString) observation .get(8)).stringValue();
116
								String numeroOrdre=((JSONString) observation.get(7)).stringValue();
117
 
118
								Observation obs=new Observation(transmis, nomSaisi, nomRetenu, numeroNomenclaturalSaisi, numeroNomenclaturalRetenu ,numeroTaxonomique, famille, localite, identifiantLocalite, lieudit, station, milieu, commentaire, date, numeroOrdre);
119
 
120
								observationData.put(obs.getNumeroOrdre(),obs);
44 david 121
 
122
 
32 jpm 123
							}
124
 
12 david 125
						}
32 jpm 126
					} else {
127
 
128
						observationData = new ListeObservation(0) ;
12 david 129
					}
32 jpm 130
 
131
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
132
					// le premier à les recevoir
133
					r.rafraichir(observationData,true);
134
				}
135
			}) ;
136
 
137
		} catch (RequestException e) {
138
			// TODO Auto-generated catch block
139
			e.printStackTrace();
140
		}
12 david 141
	}
142
 
143
}