Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
935 jpm 1
package org.tela_botanica.client.modeles.structure;
121 jpm 2
 
1040 gduche 3
import java.util.HashMap;
4
 
770 jpm 5
import org.tela_botanica.client.Mediateur;
121 jpm 6
import org.tela_botanica.client.RegistreId;
770 jpm 7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
121 jpm 9
import org.tela_botanica.client.interfaces.Rafraichissable;
935 jpm 10
import org.tela_botanica.client.modeles.Information;
1040 gduche 11
import org.tela_botanica.client.modeles.personne.PersonneListe;
935 jpm 12
import org.tela_botanica.client.modeles.projet.ProjetListe;
1319 gduche 13
import org.tela_botanica.client.synchronisation.Reponse;
770 jpm 14
import org.tela_botanica.client.util.UtilDAO;
121 jpm 15
 
16
import com.extjs.gxt.ui.client.Registry;
133 jpm 17
import com.google.gwt.core.client.GWT;
121 jpm 18
import com.google.gwt.http.client.URL;
19
import com.google.gwt.json.client.JSONArray;
156 jp_milcent 20
import com.google.gwt.json.client.JSONObject;
121 jpm 21
import com.google.gwt.json.client.JSONValue;
22
 
23
public class StructureAsyncDao {
268 jp_milcent 24
	private static final String SERVICE_NOM = "CoelStructure";
264 jp_milcent 25
 
770 jpm 26
	private String utilisateurId = null;
27
	private Rafraichissable vueARafraichir = null;
28
 
29
	public StructureAsyncDao(Rafraichissable vue) {
1367 cyprien 30
		if (Mediateur.DEBUG) System.out.println("|| StructureAsyncDao > vueARafraichir = "+vue.getClass().toString());
770 jpm 31
		vueARafraichir = vue;
32
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
33
	}
1329 cyprien 34
 
35
	/**
36
	 *
37
	 * @param paginationProgressive : définit le mode de consultation de la base de données
38
	 * 			- True :	la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
39
	 * 						fur et à mesure de la consultation des données par l'utilisateur.
40
	 * 			- False :	la consultation des données est classique : un seul appel à la base de données est effectué, le retour
41
	 * 						est renvoyé à l'appelant
42
	 * 			// FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
43
	 */
1526 jpm 44
	public void selectionner(final boolean paginationProgressive, final String projetId, final String structureId, final String nomStructure, final String formatRetour, final int start, final int nbElements, final Integer seqId) {
1040 gduche 45
 
1329 cyprien 46
		String nom = (nomStructure == null) ? "%" : nomStructure+"%";
47
		String[] parametres = {projetId, structureId, nom};
48
 
1040 gduche 49
		HashMap<String, String> restrictions = new HashMap<String, String>();
1468 jpm 50
 
1040 gduche 51
		if (nbElements != -1)	{
52
			restrictions.put("limit", String.valueOf(nbElements));
53
		}
54
 
1526 jpm 55
		if ((formatRetour != null) && (formatRetour != "")) {
56
			restrictions.put("formatRetour", formatRetour);
57
		}
58
 
1329 cyprien 59
		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
60
		if (paginationProgressive) {
61
 
62
			/** DEFINITION DU TUPLE DE DEPART **/
63
			restrictions.put("start", String.valueOf(start));
64
 
65
			/** CONSTRUCTION DE LA REQUETE **/
66
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
1319 gduche 67
 
1329 cyprien 68
    		/** ENVOI DE LA REQUETE **/
69
    		rb.envoyerRequete(null, new JsonRestRequestCallback()
70
    		{
71
    			/** RECEPTION DE LA REPONSE **/
72
    			public void surReponse(JSONValue responseValue)
73
    			{
74
					if (seqId != null)	{
75
						Reponse reponseRequete = new Reponse(responseValue, seqId);
76
						vueARafraichir.rafraichir(reponseRequete);
77
					}
78
 
79
    				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
80
    				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
81
					else vueARafraichir.rafraichir(responseValue);
82
    			}
83
    		});
84
		}
85
		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
86
		else {
87
 
88
			/** DEFINITION DU TUPLE DE DEPART **/
89
			restrictions.put("start", String.valueOf(start*nbElements));
90
 
91
			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
92
 
93
			rb.envoyerRequete(null, new JsonRestRequestCallback() {
94
 
95
				public void surReponse(JSONValue responseValue) {
96
 
97
					if (responseValue != null) {
1319 gduche 98
 
1329 cyprien 99
						Information info = new Information("selection_structure");
100
 
101
						JSONObject responseObject = responseValue.isObject();
102
 
103
						if (responseObject != null) {
104
 
105
							// Si la réponse est un tableau, alors c'est une liste de structure qui a été retournée
106
							if (responseObject.get("structures").isArray() != null) {
107
 
108
								JSONObject reponse = responseObject;
109
								StructureListe structures;
110
								structures = new StructureListe(reponse.get("structures").isArray(), reponse.get("nbElements").isNumber(), vueARafraichir);
111
								structures.setTaillePage(nbElements);
112
								structures.setPageCourante(start);
113
 
114
								info.setDonnee(0, structures);
115
 
116
								if (seqId != null)	{
117
									Reponse reponseRequete = new Reponse(info, seqId);
118
									vueARafraichir.rafraichir(reponseRequete);
119
								}
120
								else vueARafraichir.rafraichir(structures);
121
 
122
							// Si la réponse est un objet, alors c'est une unique structure qui a été retournée
123
							} else if (responseObject.get("structures").isObject() != null) {
124
 
125
								JSONObject reponse = responseObject.get("structures").isObject();
126
								Structure structure = new Structure(reponse);
127
								StructureConservation structureConservation = new StructureConservation(reponse);
128
								StructureValorisation structureValorisation = new StructureValorisation(reponse);
129
 
130
								info.setDonnee(0, structure);
131
								info.setDonnee(1, structureConservation);
132
								info.setDonnee(2, structureValorisation);
133
 
134
								if (seqId != null)	{
135
									Reponse reponseRequete = new Reponse(info, seqId);
136
									vueARafraichir.rafraichir(reponseRequete);
137
								}
138
								else vueARafraichir.rafraichir(info);
139
							}
140
						} else {
141
							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
1040 gduche 142
						}
1329 cyprien 143
					} else {
144
						if (structureId == null) {
145
							// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
146
							StructureListe structures = new StructureListe(0);
147
							vueARafraichir.rafraichir(structures);
1319 gduche 148
						}
770 jpm 149
					}
150
				}
1329 cyprien 151
			});
152
		}
770 jpm 153
	}
154
 
1329 cyprien 155
 
770 jpm 156
	public void ajouter(final Structure str, StructureConservation conservation, StructureValorisation valorisation) {
157
		String postDonneesEncodees = construirePost(null, str, conservation, valorisation);
156 jp_milcent 158
 
770 jpm 159
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
160
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
161
			@Override
162
			public void surReponse(JSONValue responseValue) {
163
				if (responseValue.isString() != null) {
164
					Information info = new Information("ajout_structure");
165
					String structureIdOuMessage = responseValue.isString().stringValue();
166
					if (structureIdOuMessage.matches("^[0-9]+$")) {
167
						info.setDonnee(structureIdOuMessage);
156 jp_milcent 168
					} else {
770 jpm 169
						info.setMessage(structureIdOuMessage);
156 jp_milcent 170
					}
770 jpm 171
					vueARafraichir.rafraichir(info);
172
				} else {
173
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
156 jp_milcent 174
				}
770 jpm 175
			}
176
		});
156 jp_milcent 177
	}
770 jpm 178
 
179
	public void modifier(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
180
		String postDonneesEncodees = construirePost(structureId, str, conservation, valorisation);
772 jpm 181
		String[] parametres = {structureId};
182
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
183
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
184
			@Override
185
			public void surReponse(JSONValue responseValue) {
186
				// Si la requête est un succès, reception d'une chaine
187
				if (responseValue.isString() != null) {
188
					Information info = new Information("modif_structure");
189
					info.setMessage(responseValue.isString().stringValue());
190
					vueARafraichir.rafraichir(info);
191
				} else {
192
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
121 jpm 193
				}
772 jpm 194
			}
195
		});
121 jpm 196
	}
770 jpm 197
 
772 jpm 198
	public void supprimer(String structuresId) {
199
		String[] parametres = {utilisateurId, structuresId};
200
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
201
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
202
			@Override
203
			public void surReponse(JSONValue responseValue) {
204
				if (responseValue.isString() != null) {
205
					Information info = new Information("suppression_structure");
206
					info.setMessage(responseValue.isString().stringValue());
207
					vueARafraichir.rafraichir(info);
208
				} else {
209
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
133 jpm 210
				}
772 jpm 211
			}
212
		});
133 jpm 213
	}
169 jp_milcent 214
 
770 jpm 215
	private String construirePost(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
865 jpm 216
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
217
 
602 jp_milcent 218
		if (str != null) {
612 jp_milcent 219
			if (structureId != null) {
220
				postDonnees += "&cs_id_structure=" + URL.encodeComponent(structureId);
221
			}
1369 cyprien 222
 
223
			postDonnees += "&cpr_abreviation=" + URL.encodeComponent(str.getAbreviationProjet());
602 jp_milcent 224
			postDonnees += "&cs_ce_projet=" + URL.encodeComponent(str.getIdProjet()) +
225
				"&cs_ce_mere=" + URL.encodeComponent(str.getIdMere()) +
226
				"&cs_guid=" + URL.encodeComponent(str.getGuid()) +
227
				"&cs_truk_identifiant_alternatif=" + URL.encodeComponent(str.getIdAlternatif()) +
228
				"&cs_nom=" + URL.encodeComponent(str.getNom()) +
229
				"&cs_truk_nom_alternatif=" + URL.encodeComponent(str.getNomAlternatif()) +
1173 jpm 230
				"&cs_description=" + URL.encodeComponent(str.getDescription()) +
602 jp_milcent 231
				"&cs_ce_type=" + URL.encodeComponent(str.getType()) +
232
				"&cs_ce_truk_type_prive=" + URL.encodeComponent(str.getTypePrive()) +
233
				"&cs_ce_truk_type_public=" + URL.encodeComponent(str.getTypePublic()) +
234
				"&cs_adresse_01=" + URL.encodeComponent(str.getAdresse()) +
235
				"&cs_adresse_02=" + URL.encodeComponent(str.getAdresseComplement()) +
236
				"&cs_date_fondation=" + URL.encodeComponent(str.getDateFondationFormatMysql()) +
237
				"&cs_code_postal=" + URL.encodeComponent(str.getCodePostal()) +
238
				"&cs_ville=" + URL.encodeComponent(str.getVille()) +
1037 gduche 239
				"&cs_ce_truk_region=" + URL.encodeComponent(str.get("ce_truk_region").toString()) +
602 jp_milcent 240
				"&cs_ce_truk_pays=" + URL.encodeComponent(str.getPays()) +
1173 jpm 241
				"&cs_latitude=" + URL.encodeComponent(str.getLatitude()) +
242
				"&cs_longitude=" + URL.encodeComponent(str.getLongitude()) +
602 jp_milcent 243
				"&cs_truk_telephone=" + URL.encodeComponent(str.getTelephone()) +
244
				"&cs_truk_url=" + URL.encodeComponent(str.getUrl()) +
1173 jpm 245
				"&cs_nbre_personne=" + URL.encodeComponent(Integer.toString(str.getNbrePersonne())) +
246
				"&cs_condition_acces=" + URL.encodeComponent(str.getConditionAcces())+
247
				"&cs_condition_usage=" + URL.encodeComponent(str.getConditionUsage())+
1039 gduche 248
				"&cs_courriel=" + URL.encodeComponent(str.getCourriel());
602 jp_milcent 249
		}
250
		if (conservation != null) {
612 jp_milcent 251
			if (structureId != null) {
252
				postDonnees += "&csc_id_structure=" + URL.encodeComponent(structureId);
253
			}
602 jp_milcent 254
			postDonnees += "&csc_mark_formation=" + URL.encodeComponent(conservation.getFormation()) +
255
				"&csc_formation=" + URL.encodeComponent(conservation.getFormationInfo()) +
256
				"&csc_mark_formation_interet=" + URL.encodeComponent(conservation.getFormationInteret()) +
257
				"&csc_truk_stockage_local=" + URL.encodeComponent(conservation.getStockageLocal()) +
258
				"&csc_truk_stockage_meuble=" + URL.encodeComponent(conservation.getStockageMeuble()) +
259
				"&csc_truk_stockage_parametre=" + URL.encodeComponent(conservation.getStockageParametre()) +
260
				"&csc_mark_collection_commune=" + URL.encodeComponent(conservation.getCollectionCommune()) +
261
				"&csc_truk_collection_autre=" + URL.encodeComponent(conservation.getCollectionAutre()) +
262
				"&csc_mark_acces_controle=" + URL.encodeComponent(conservation.getAccesControle()) +
263
				"&csc_mark_restauration=" + URL.encodeComponent(conservation.getRestauration()) +
264
				"&csc_truk_restauration_operation=" + URL.encodeComponent(conservation.getRestaurationOperation()) +
265
				"&csc_ce_materiel_conservation=" + URL.encodeComponent(conservation.getMaterielConservation()) +
266
				"&csc_truk_materiel_autre=" + URL.encodeComponent(conservation.getMaterielAutre()) +
267
				"&csc_mark_traitement=" + URL.encodeComponent(conservation.getTraitement()) +
268
				"&csc_truk_traitement=" + URL.encodeComponent(conservation.getTraitements()) +
269
				"&csc_mark_acquisition_collection=" + URL.encodeComponent(conservation.getAcquisitionCollection()) +
270
				"&csc_mark_acquisition_echantillon=" + URL.encodeComponent(conservation.getAcquisitionEchantillon()) +
271
				"&csc_mark_acquisition_traitement=" + URL.encodeComponent(conservation.getAcquisitionTraitement()) +
272
				"&csc_truk_acquisition_traitement_poison=" + URL.encodeComponent(conservation.getAcquisitionTraitementPoison()) +
273
				"&csc_truk_acquisition_traitement_insecte=" + URL.encodeComponent(conservation.getAcquisitionTraitementInsecte());
274
		}
275
		if (valorisation != null) {
612 jp_milcent 276
			if (structureId != null) {
632 jp_milcent 277
				postDonnees += "&csv_id_structure=" + URL.encodeComponent(structureId);
612 jp_milcent 278
			}
602 jp_milcent 279
			postDonnees += "&csv_mark_action=" + URL.encodeComponent(valorisation.getAction()) +
280
				"&csv_truk_action=" + URL.encodeComponent(valorisation.getActionInfo()) +
281
				"&csv_publication=" + URL.encodeComponent(valorisation.getPublication()) +
282
				"&csv_collection_autre=" + URL.encodeComponent(valorisation.getCollectionAutre()) +
283
				"&csv_mark_action_future=" + URL.encodeComponent(valorisation.getActionFuture()) +
284
				"&csv_action_future=" + URL.encodeComponent(valorisation.getActionFutureInfo()) +
285
				"&csv_mark_recherche=" + URL.encodeComponent(valorisation.getRecherche()) +
286
				"&csv_truk_recherche_provenance=" + URL.encodeComponent(valorisation.getRechercheProvenance()) +
287
				"&csv_truk_recherche_type=" + URL.encodeComponent(valorisation.getRechercheType()) +
288
				"&csv_mark_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotif()) +
289
				"&csv_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotifInfo()) +
290
				"&csv_mark_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotif()) +
291
				"&csv_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotifInfo());
292
		}
293
		return postDonnees;
294
	}
121 jpm 295
}