Subversion Repositories eFlore/Applications.coel

Rev

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