Subversion Repositories eFlore/Applications.coel

Rev

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