Subversion Repositories eFlore/Applications.coel

Rev

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