Subversion Repositories eFlore/Applications.coel

Rev

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