Subversion Repositories eFlore/Applications.coel

Rev

Rev 1511 | Rev 1613 | 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 **/
1428 cyprien 74
 
1367 cyprien 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 {
1428 cyprien 89
 
1329 cyprien 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
							// Si la réponse est un tableau, alors c'est une liste de collections qui a été retournée
104
							if (responseObject.get("collections").isArray() != null) {
105
								final JSONArray reponse = responseObject.get("collections").isArray();
106
								CollectionListe collections = new CollectionListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
107
								collections.setTaillePage(nbElements);
108
								collections.setPageCourante(start);
109
 
110
								vueARafraichir.rafraichir(collections);
111
 
112
							// Si la réponse est un objet, alors c'est une unique collection qui a été retournée
113
							} else if (responseObject.get("collections").isObject() != null) {
114
								final JSONObject reponse = responseObject.get("collections").isObject();
115
								Collection collection = new Collection(reponse);
116
								CollectionBotanique collectionBotanique = new CollectionBotanique(reponse);
117
								collection.setBotanique(collectionBotanique);
118
 
119
								Information info = new Information("selection_collection");
120
								info.setDonnee(0, collection);
1367 cyprien 121
 
122
								// et on met à jour le demandeur des données
123
								if (seqId != null)	{
124
									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
125
									Reponse reponseRequete = new Reponse(info, seqId);
126
									vueARafraichir.rafraichir(reponseRequete);
1513 jpm 127
								}
128
								else	{
1428 cyprien 129
									if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour à "+vueARafraichir.getClass().toString());
1367 cyprien 130
									vueARafraichir.rafraichir(info);
131
								}
1329 cyprien 132
							}
133
						} else {
1513 jpm 134
							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
1329 cyprien 135
						}
463 jp_milcent 136
					} else {
1329 cyprien 137
						// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
138
						if (collectionId == null) {
139
							CollectionListe collections = new CollectionListe(0);
140
							vueARafraichir.rafraichir(collections);
141
						}
463 jp_milcent 142
					}
143
				}
1329 cyprien 144
			});
145
		}
463 jp_milcent 146
	}
643 jp_milcent 147
 
1262 cyprien 148
	public void ajouter(Collection collection) {
871 jpm 149
		String postDonneesEncodees = construirePost(null, collection);
150
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
151
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
152
			@Override
153
			public void surReponse(JSONValue responseValue) {
154
				if (responseValue.isString() != null) {
155
					Information info = new Information("ajout_collection");
156
					String structureIdOuMessage = responseValue.isString().stringValue();
157
					if (structureIdOuMessage.matches("^[0-9]+$")) {
158
						info.setDonnee(structureIdOuMessage);
159
					} else {
160
						info.setMessage(structureIdOuMessage);
161
					}
162
					vueARafraichir.rafraichir(info);
163
				} else {
164
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
165
				}
166
			}
167
		});
168
	}
169
 
170
	public void modifier(Collection collection) {
1292 cyprien 171
		String postDonneesEncodees = construirePost(collection.getId(), collection);
871 jpm 172
		String[] parametres = {collection.getId()};
173
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
174
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
643 jp_milcent 175
			@Override
871 jpm 176
			public void surReponse(JSONValue responseValue) {
643 jp_milcent 177
				// Si la requête est un succès, reception d'une chaine
871 jpm 178
				if (responseValue.isString() != null) {
179
					Information info = new Information("modif_collection");
180
					info.setMessage(responseValue.isString().stringValue());
181
					vueARafraichir.rafraichir(info);
643 jp_milcent 182
				} else {
871 jpm 183
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
643 jp_milcent 184
				}
185
			}
186
		});
187
	}
871 jpm 188
 
189
	public void supprimer(String collectionsId) {
190
		String[] parametres = {utilisateurId, collectionsId};
191
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
192
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
193
			@Override
194
			public void surReponse(JSONValue responseValue) {
195
				if (responseValue.isString() != null) {
196
					Information info = new Information("suppression_collection");
197
					info.setMessage(responseValue.isString().stringValue());
198
					vueARafraichir.rafraichir(info);
199
				} else {
200
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
201
				}
202
			}
203
		});
204
	}
205
 
1262 cyprien 206
	private String construirePost(String collectionId, Collection collection) {
871 jpm 207
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
208
 
209
		if (collection != null) {
210
			if (collectionId != null) {
211
				postDonnees += "&cc_id_collection=" + URL.encodeComponent(collectionId);
212
			}
1262 cyprien 213
 
214
			/*
871 jpm 215
			postDonnees += "&cpr_abreviation=" + URL.encodeComponent(((ProjetListe) Registry.get(RegistreId.PROJETS)).get(collection.getIdProjet()).getAbreviation());
216
			postDonnees += "&cc_ce_projet=" + URL.encodeComponent(collection.getIdProjet()) +
217
				"&cc_ce_mere=" + URL.encodeComponent(collection.getCollectionMereId()) +
218
				"&cc_ce_structure=" + URL.encodeComponent(collection.getIdStructure()) +
219
				"&cc_truk_identifiant_alternatif=" + URL.encodeComponent(collection.getIdAlternatif()) +
220
				"&cc_truk_code=" + URL.encodeComponent(collection.getCode()) +
221
				"&cc_nom=" + URL.encodeComponent(collection.getNom()) +
222
				"&cc_truk_nom_alternatif=" + URL.encodeComponent(collection.getNomAlternatif()) +
223
				"&cc_description=" + URL.encodeComponent(collection.getDescription()) +
224
				"&cc_description_specialiste=" + URL.encodeComponent(collection.getDescriptionSpecialiste()) +
225
				"&cc_historique=" + URL.encodeComponent(collection.getHistorique()) +
226
				"&cc_truk_url=" + URL.encodeComponent(collection.getUrls()) +
227
				"&cc_truk_groupement_principe=" + URL.encodeComponent(collection.getGroupementPrincipe()) +
228
				"&cc_truk_groupement_but=" + URL.encodeComponent(collection.getGroupementBut()) +
229
				"&cc_ce_type=" + URL.encodeComponent(collection.getTypeNcd()) +
230
				"&cc_ce_type_depot=" + URL.encodeComponent(collection.getTypeDepot()) +
231
				"&cc_cote=" + URL.encodeComponent(collection.getCote()) +
956 jpm 232
				"&cc_truk_periode_constitution=" + URL.encodeComponent(collection.getPeriodeConstitution()) +
871 jpm 233
				"&cc_truk_couverture_lieu=" + URL.encodeComponent(collection.getCouvertureLieu()) +
234
				"&cc_ce_specimen_type=" + URL.encodeComponent(collection.getSpecimenType()) +
235
				"&cc_specimen_type_nbre=" + URL.encodeComponent(collection.getSpecimenTypeNbre()) +
236
				"&cc_ce_specimen_type_nbre_precision=" + URL.encodeComponent(collection.getSpecimenTypeNbrePrecision()) +
1173 jpm 237
				"&cc_ce_specimen_type_classement=" + URL.encodeComponent(collection.getSpecimenTypeClassement());*/
238
			postDonnees += "&" + collection.obtenirChainePOST();
871 jpm 239
		}
240
 
241
		if (collection.getBotanique() != null) {
956 jpm 242
			if (collectionId != null) {
243
				postDonnees += "&ccb_id_collection=" + URL.encodeComponent(collectionId);
244
			}
871 jpm 245
			CollectionBotanique collectionBotanique = collection.getBotanique();
1173 jpm 246
			/*postDonnees += "&ccb_nbre_echantillon=" + URL.encodeComponent(collectionBotanique.getNbreEchantillon()) +
871 jpm 247
				"&ccb_ce_truk_type=" + URL.encodeComponent(collectionBotanique.getType()) +
248
				"&ccb_truk_unite_rangement=" + URL.encodeComponent(collectionBotanique.getUniteRangement()) +
249
				"&ccb_ce_unite_rangement_etat=" + URL.encodeComponent(collectionBotanique.getUniteRangementEtat()) +
250
				"&ccb_truk_unite_base=" + URL.encodeComponent(collectionBotanique.getUniteBase()) +
251
				"&ccb_truk_conservation_papier_type=" + URL.encodeComponent(collectionBotanique.getConservationPapierType()) +
252
				"&ccb_truk_conservation_methode=" + URL.encodeComponent(collectionBotanique.getConservationMethode()) +
253
				"&ccb_specimen_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationPourcent()) +
254
				"&ccb_etiquette_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationPourcent()) +
255
				"&ccb_truk_specimen_fixation_methode=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationMethode()) +
256
				"&ccb_truk_etiquette_fixation_support=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSupport()) +
257
				"&ccb_truk_etiquette_fixation_specimen=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSpecimen()) +
258
				"&ccb_truk_etiquette_ecriture=" + URL.encodeComponent(collectionBotanique.getEtiquetteEcriture()) +
259
				"&ccb_ce_traitement=" + URL.encodeComponent(collectionBotanique.getTraitement()) +
260
				"&ccb_truk_traitement_poison=" + URL.encodeComponent(collectionBotanique.getTraitementPoison()) +
261
				"&ccb_truk_traitement_insecte=" + URL.encodeComponent(collectionBotanique.getTraitementInsecte()) +
262
				"&ccb_ce_etat_general=" + URL.encodeComponent(collectionBotanique.getEtatGeneral()) +
263
				"&ccb_truk_degradation_specimen=" + URL.encodeComponent(collectionBotanique.getDegradationSpecimen()) +
264
				"&ccb_truk_degradation_presentation=" + URL.encodeComponent(collectionBotanique.getDegradationPresentation()) +
265
				"&ccb_ce_determination=" + URL.encodeComponent(collectionBotanique.getDetermination()) +
266
				"&ccb_truk_nature=" + URL.encodeComponent(collectionBotanique.getNature()) +
267
				"&ccb_specialite=" + URL.encodeComponent(collectionBotanique.getSpecialite()) +
268
				"&ccb_recolte_date_debut=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebut()) +
269
				"&ccb_ce_recolte_date_debut_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebutType()) +
270
				"&ccb_recolte_date_fin=" + URL.encodeComponent(collectionBotanique.getRecolteDateFin()) +
271
				"&ccb_ce_recolte_date_fin_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateFinType()) +
272
				"&ccb_annotation_classement=" + URL.encodeComponent(collectionBotanique.getClassementAnnotation()) +
273
				"&ccb_ce_classement_etat=" + URL.encodeComponent(collectionBotanique.getClassementEtat()) +
274
				"&ccb_truk_etiquette_renseignement=" + URL.encodeComponent(collectionBotanique.getEtiquetteRenseignement()) +
275
				"&ccb_ce_precision_localite=" + URL.encodeComponent(collectionBotanique.getPrecisionLocalite()) +
276
				"&ccb_ce_precision_date=" + URL.encodeComponent(collectionBotanique.getPrecisionDate()) +
277
				"&ccb_annotation_diverse=" + URL.encodeComponent(collectionBotanique.getAnnotationsDiverses()) +
278
				"&ccb_ce_collection_integre=" + URL.encodeComponent(collectionBotanique.getCollectionIntegre()) +
279
				"&ccb_ce_collection_integre_info=" + URL.encodeComponent(collectionBotanique.getCollectionIntegreInfo()) +
280
				"&ccb_ce_inventaire=" + URL.encodeComponent(collectionBotanique.getInventaire()) +
281
				"&ccb_ce_inventaire_auteur=" + URL.encodeComponent(collectionBotanique.getInventaireAuteur()) +
282
				"&ccb_ce_inventaire_forme=" + URL.encodeComponent(collectionBotanique.getInventaireForme()) +
283
				"&ccb_inventaire_info=" + URL.encodeComponent(collectionBotanique.getInventaireInfo()) +
284
				"&ccb_ce_truk_inventaire_digital=" + URL.encodeComponent(collectionBotanique.getInventaireDigital()) +
285
				"&ccb_inventaire_digital_pourcent=" + URL.encodeComponent(collectionBotanique.getInventaireDigitalPourcent()) +
286
				"&ccb_ce_inventaire_etat=" + URL.encodeComponent(collectionBotanique.getInventaireEtat()) +
1173 jpm 287
				"&ccb_inventaire_donnee_type=" + URL.encodeComponent(collectionBotanique.getInventaireDonneesTypes());*/
288
			postDonnees += "&" + collectionBotanique.obtenirChainePOST();
871 jpm 289
		}
290
 
291
		return postDonnees;
292
	}
463 jp_milcent 293
}