Subversion Repositories eFlore/Applications.coel

Rev

Rev 1415 | Rev 1428 | 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.collection;
463 jp_milcent 2
 
1041 gduche 3
import java.util.HashMap;
4
 
871 jpm 5
import org.tela_botanica.client.Mediateur;
463 jp_milcent 6
import org.tela_botanica.client.RegistreId;
7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
9
import org.tela_botanica.client.interfaces.Rafraichissable;
935 jpm 10
import org.tela_botanica.client.modeles.Information;
11
import org.tela_botanica.client.modeles.projet.ProjetListe;
1367 cyprien 12
import org.tela_botanica.client.synchronisation.Reponse;
968 jpm 13
import org.tela_botanica.client.util.Debug;
871 jpm 14
import org.tela_botanica.client.util.UtilDAO;
463 jp_milcent 15
 
16
import com.extjs.gxt.ui.client.Registry;
17
import com.google.gwt.core.client.GWT;
871 jpm 18
import com.google.gwt.http.client.URL;
463 jp_milcent 19
import com.google.gwt.json.client.JSONArray;
1041 gduche 20
import com.google.gwt.json.client.JSONNumber;
463 jp_milcent 21
import com.google.gwt.json.client.JSONObject;
22
import com.google.gwt.json.client.JSONValue;
23
 
24
public class CollectionAsyncDao {
25
 
26
	public static final String SERVICE_NOM = "CoelCollection";
871 jpm 27
	private String utilisateurId = null;
28
	private Rafraichissable vueARafraichir = null;
463 jp_milcent 29
 
871 jpm 30
	public CollectionAsyncDao(Rafraichissable vueARafraichirCourrante) {
1367 cyprien 31
		if (Mediateur.DEBUG) System.out.println("|| CollectionAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
871 jpm 32
		vueARafraichir = vueARafraichirCourrante;
33
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
34
	}
35
 
1329 cyprien 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
	 */
1367 cyprien 45
	public void selectionner(final boolean paginationProgressive, final String projetId, final String collectionId, final String nomCollection, final int start, final int nbElements, final Integer seqId) {
1329 cyprien 46
 
463 jp_milcent 47
		// Ajout des paramètres et données à selectionner dans l'URL
1329 cyprien 48
		String nom = (nomCollection == null) ? "%" : nomCollection+"%";
49
		String[] parametres = {projetId, collectionId, nom};
50
 
1041 gduche 51
		HashMap<String, String> restrictions = new HashMap<String, String>();
1329 cyprien 52
 
1041 gduche 53
		if (nbElements != -1)	{
54
			restrictions.put("limit", String.valueOf(nbElements));
55
		}
1329 cyprien 56
 
57
		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
58
		if (paginationProgressive) {
59
 
60
			/** DEFINITION DU TUPLE DE DEPART **/
61
			restrictions.put("start", String.valueOf(start));
62
 
63
			/** CONSTRUCTION DE LA REQUETE **/
64
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
65
 
66
    		/** ENVOI DE LA REQUETE **/
67
    		rb.envoyerRequete(null, new JsonRestRequestCallback()
68
    		{
69
    			/** RECEPTION DE LA REPONSE **/
70
    			public void surReponse(JSONValue responseValue)
71
    			{
72
    				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
73
    				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
1367 cyprien 74
 
75
					if (seqId != null)	{
76
						if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour au sequenceur");
77
						Reponse reponseRequete = new Reponse(responseValue, seqId);
78
						vueARafraichir.rafraichir(reponseRequete);
79
					}
80
					else	{
81
						if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour à "+vueARafraichir.getClass().toString());
82
						vueARafraichir.rafraichir(responseValue);
83
					}
1329 cyprien 84
    			}
85
    		});
86
		}
87
		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
88
		else {
89
 
90
			/** DEFINITION DU TUPLE DE DEPART **/
91
			restrictions.put("start", String.valueOf(start*nbElements));
92
 
93
			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
94
 
95
			rb.envoyerRequete(null, new JsonRestRequestCallback() {
96
				@Override
97
				public void surReponse(JSONValue responseValue) {
98
					if (responseValue != null) {
99
 
100
						JSONObject responseObject = responseValue.isObject();
101
 
102
						if (responseObject != null) {
103
 
104
							// Si la réponse est un tableau, alors c'est une liste de collections qui a été retournée
105
							if (responseObject.get("collections").isArray() != null) {
106
								final JSONArray reponse = responseObject.get("collections").isArray();
107
								CollectionListe collections = new CollectionListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
108
								collections.setTaillePage(nbElements);
109
								collections.setPageCourante(start);
110
 
111
								vueARafraichir.rafraichir(collections);
112
 
113
							// Si la réponse est un objet, alors c'est une unique collection qui a été retournée
114
							} else if (responseObject.get("collections").isObject() != null) {
115
								final JSONObject reponse = responseObject.get("collections").isObject();
116
								Collection collection = new Collection(reponse);
117
								CollectionBotanique collectionBotanique = new CollectionBotanique(reponse);
118
								collection.setBotanique(collectionBotanique);
119
 
120
								Information info = new Information("selection_collection");
121
								info.setDonnee(0, collection);
1367 cyprien 122
 
123
								// et on met à jour le demandeur des données
124
								if (seqId != null)	{
125
									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
126
									Reponse reponseRequete = new Reponse(info, seqId);
127
									vueARafraichir.rafraichir(reponseRequete);
128
								}
129
								else	{
130
									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
131
									vueARafraichir.rafraichir(info);
132
								}
1329 cyprien 133
							}
134
						} else {
135
							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
136
						}
463 jp_milcent 137
					} else {
1329 cyprien 138
						// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
139
						if (collectionId == null) {
140
							CollectionListe collections = new CollectionListe(0);
141
							vueARafraichir.rafraichir(collections);
142
						}
463 jp_milcent 143
					}
144
				}
1329 cyprien 145
			});
146
		}
463 jp_milcent 147
	}
643 jp_milcent 148
 
1262 cyprien 149
	public void ajouter(Collection collection) {
871 jpm 150
		String postDonneesEncodees = construirePost(null, collection);
151
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
152
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
153
			@Override
154
			public void surReponse(JSONValue responseValue) {
155
				if (responseValue.isString() != null) {
156
					Information info = new Information("ajout_collection");
157
					String structureIdOuMessage = responseValue.isString().stringValue();
158
					if (structureIdOuMessage.matches("^[0-9]+$")) {
159
						info.setDonnee(structureIdOuMessage);
160
					} else {
161
						info.setMessage(structureIdOuMessage);
162
					}
163
					vueARafraichir.rafraichir(info);
164
				} else {
165
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
166
				}
167
			}
168
		});
169
	}
170
 
171
	public void modifier(Collection collection) {
1292 cyprien 172
		String postDonneesEncodees = construirePost(collection.getId(), collection);
871 jpm 173
		String[] parametres = {collection.getId()};
174
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
175
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
643 jp_milcent 176
			@Override
871 jpm 177
			public void surReponse(JSONValue responseValue) {
643 jp_milcent 178
				// Si la requête est un succès, reception d'une chaine
871 jpm 179
				if (responseValue.isString() != null) {
180
					Information info = new Information("modif_collection");
181
					info.setMessage(responseValue.isString().stringValue());
182
					vueARafraichir.rafraichir(info);
643 jp_milcent 183
				} else {
871 jpm 184
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
643 jp_milcent 185
				}
186
			}
187
		});
188
	}
871 jpm 189
 
190
	public void supprimer(String collectionsId) {
191
		String[] parametres = {utilisateurId, collectionsId};
192
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
193
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
194
			@Override
195
			public void surReponse(JSONValue responseValue) {
196
				if (responseValue.isString() != null) {
197
					Information info = new Information("suppression_collection");
198
					info.setMessage(responseValue.isString().stringValue());
199
					vueARafraichir.rafraichir(info);
200
				} else {
201
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
202
				}
203
			}
204
		});
205
	}
206
 
1262 cyprien 207
	private String construirePost(String collectionId, Collection collection) {
871 jpm 208
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
209
 
210
		if (collection != null) {
211
			if (collectionId != null) {
212
				postDonnees += "&cc_id_collection=" + URL.encodeComponent(collectionId);
213
			}
1262 cyprien 214
 
215
			/*
871 jpm 216
			postDonnees += "&cpr_abreviation=" + URL.encodeComponent(((ProjetListe) Registry.get(RegistreId.PROJETS)).get(collection.getIdProjet()).getAbreviation());
217
			postDonnees += "&cc_ce_projet=" + URL.encodeComponent(collection.getIdProjet()) +
218
				"&cc_ce_mere=" + URL.encodeComponent(collection.getCollectionMereId()) +
219
				"&cc_ce_structure=" + URL.encodeComponent(collection.getIdStructure()) +
220
				"&cc_truk_identifiant_alternatif=" + URL.encodeComponent(collection.getIdAlternatif()) +
221
				"&cc_truk_code=" + URL.encodeComponent(collection.getCode()) +
222
				"&cc_nom=" + URL.encodeComponent(collection.getNom()) +
223
				"&cc_truk_nom_alternatif=" + URL.encodeComponent(collection.getNomAlternatif()) +
224
				"&cc_description=" + URL.encodeComponent(collection.getDescription()) +
225
				"&cc_description_specialiste=" + URL.encodeComponent(collection.getDescriptionSpecialiste()) +
226
				"&cc_historique=" + URL.encodeComponent(collection.getHistorique()) +
227
				"&cc_truk_url=" + URL.encodeComponent(collection.getUrls()) +
228
				"&cc_truk_groupement_principe=" + URL.encodeComponent(collection.getGroupementPrincipe()) +
229
				"&cc_truk_groupement_but=" + URL.encodeComponent(collection.getGroupementBut()) +
230
				"&cc_ce_type=" + URL.encodeComponent(collection.getTypeNcd()) +
231
				"&cc_ce_type_depot=" + URL.encodeComponent(collection.getTypeDepot()) +
232
				"&cc_cote=" + URL.encodeComponent(collection.getCote()) +
956 jpm 233
				"&cc_truk_periode_constitution=" + URL.encodeComponent(collection.getPeriodeConstitution()) +
871 jpm 234
				"&cc_truk_couverture_lieu=" + URL.encodeComponent(collection.getCouvertureLieu()) +
235
				"&cc_ce_specimen_type=" + URL.encodeComponent(collection.getSpecimenType()) +
236
				"&cc_specimen_type_nbre=" + URL.encodeComponent(collection.getSpecimenTypeNbre()) +
237
				"&cc_ce_specimen_type_nbre_precision=" + URL.encodeComponent(collection.getSpecimenTypeNbrePrecision()) +
1173 jpm 238
				"&cc_ce_specimen_type_classement=" + URL.encodeComponent(collection.getSpecimenTypeClassement());*/
239
			postDonnees += "&" + collection.obtenirChainePOST();
871 jpm 240
		}
241
 
242
		if (collection.getBotanique() != null) {
956 jpm 243
			if (collectionId != null) {
244
				postDonnees += "&ccb_id_collection=" + URL.encodeComponent(collectionId);
245
			}
871 jpm 246
			CollectionBotanique collectionBotanique = collection.getBotanique();
1173 jpm 247
			/*postDonnees += "&ccb_nbre_echantillon=" + URL.encodeComponent(collectionBotanique.getNbreEchantillon()) +
871 jpm 248
				"&ccb_ce_truk_type=" + URL.encodeComponent(collectionBotanique.getType()) +
249
				"&ccb_truk_unite_rangement=" + URL.encodeComponent(collectionBotanique.getUniteRangement()) +
250
				"&ccb_ce_unite_rangement_etat=" + URL.encodeComponent(collectionBotanique.getUniteRangementEtat()) +
251
				"&ccb_truk_unite_base=" + URL.encodeComponent(collectionBotanique.getUniteBase()) +
252
				"&ccb_truk_conservation_papier_type=" + URL.encodeComponent(collectionBotanique.getConservationPapierType()) +
253
				"&ccb_truk_conservation_methode=" + URL.encodeComponent(collectionBotanique.getConservationMethode()) +
254
				"&ccb_specimen_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationPourcent()) +
255
				"&ccb_etiquette_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationPourcent()) +
256
				"&ccb_truk_specimen_fixation_methode=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationMethode()) +
257
				"&ccb_truk_etiquette_fixation_support=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSupport()) +
258
				"&ccb_truk_etiquette_fixation_specimen=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSpecimen()) +
259
				"&ccb_truk_etiquette_ecriture=" + URL.encodeComponent(collectionBotanique.getEtiquetteEcriture()) +
260
				"&ccb_ce_traitement=" + URL.encodeComponent(collectionBotanique.getTraitement()) +
261
				"&ccb_truk_traitement_poison=" + URL.encodeComponent(collectionBotanique.getTraitementPoison()) +
262
				"&ccb_truk_traitement_insecte=" + URL.encodeComponent(collectionBotanique.getTraitementInsecte()) +
263
				"&ccb_ce_etat_general=" + URL.encodeComponent(collectionBotanique.getEtatGeneral()) +
264
				"&ccb_truk_degradation_specimen=" + URL.encodeComponent(collectionBotanique.getDegradationSpecimen()) +
265
				"&ccb_truk_degradation_presentation=" + URL.encodeComponent(collectionBotanique.getDegradationPresentation()) +
266
				"&ccb_ce_determination=" + URL.encodeComponent(collectionBotanique.getDetermination()) +
267
				"&ccb_truk_nature=" + URL.encodeComponent(collectionBotanique.getNature()) +
268
				"&ccb_specialite=" + URL.encodeComponent(collectionBotanique.getSpecialite()) +
269
				"&ccb_recolte_date_debut=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebut()) +
270
				"&ccb_ce_recolte_date_debut_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebutType()) +
271
				"&ccb_recolte_date_fin=" + URL.encodeComponent(collectionBotanique.getRecolteDateFin()) +
272
				"&ccb_ce_recolte_date_fin_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateFinType()) +
273
				"&ccb_annotation_classement=" + URL.encodeComponent(collectionBotanique.getClassementAnnotation()) +
274
				"&ccb_ce_classement_etat=" + URL.encodeComponent(collectionBotanique.getClassementEtat()) +
275
				"&ccb_truk_etiquette_renseignement=" + URL.encodeComponent(collectionBotanique.getEtiquetteRenseignement()) +
276
				"&ccb_ce_precision_localite=" + URL.encodeComponent(collectionBotanique.getPrecisionLocalite()) +
277
				"&ccb_ce_precision_date=" + URL.encodeComponent(collectionBotanique.getPrecisionDate()) +
278
				"&ccb_annotation_diverse=" + URL.encodeComponent(collectionBotanique.getAnnotationsDiverses()) +
279
				"&ccb_ce_collection_integre=" + URL.encodeComponent(collectionBotanique.getCollectionIntegre()) +
280
				"&ccb_ce_collection_integre_info=" + URL.encodeComponent(collectionBotanique.getCollectionIntegreInfo()) +
281
				"&ccb_ce_inventaire=" + URL.encodeComponent(collectionBotanique.getInventaire()) +
282
				"&ccb_ce_inventaire_auteur=" + URL.encodeComponent(collectionBotanique.getInventaireAuteur()) +
283
				"&ccb_ce_inventaire_forme=" + URL.encodeComponent(collectionBotanique.getInventaireForme()) +
284
				"&ccb_inventaire_info=" + URL.encodeComponent(collectionBotanique.getInventaireInfo()) +
285
				"&ccb_ce_truk_inventaire_digital=" + URL.encodeComponent(collectionBotanique.getInventaireDigital()) +
286
				"&ccb_inventaire_digital_pourcent=" + URL.encodeComponent(collectionBotanique.getInventaireDigitalPourcent()) +
287
				"&ccb_ce_inventaire_etat=" + URL.encodeComponent(collectionBotanique.getInventaireEtat()) +
1173 jpm 288
				"&ccb_inventaire_donnee_type=" + URL.encodeComponent(collectionBotanique.getInventaireDonneesTypes());*/
289
			postDonnees += "&" + collectionBotanique.obtenirChainePOST();
871 jpm 290
		}
291
 
292
		return postDonnees;
293
	}
463 jp_milcent 294
}