Subversion Repositories eFlore/Applications.coel

Rev

Rev 1173 | Rev 1329 | 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();
865 jpm 32
		GWT.log("ID utilisateur :"+utilisateurId, null);
770 jpm 33
	}
34
 
1319 gduche 35
	public void selectionner(final String projetId, final String structureId, final String nomStructure, final int pageCourante, final int nbElements, final Integer seqId) {
1040 gduche 36
		String[] parametres = {projetId, structureId, nomStructure};
37
 
38
		HashMap<String, String> restrictions = new HashMap<String, String>();
1055 gduche 39
		restrictions.put("start", String.valueOf(pageCourante*nbElements));
1319 gduche 40
		restrictions.put("orderby", "cs_ville");
1040 gduche 41
		if (nbElements != -1)	{
42
			restrictions.put("limit", String.valueOf(nbElements));
43
		}
44
 
45
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
770 jpm 46
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
47
			@Override
48
			public void surReponse(JSONValue responseValue) {
49
				if (responseValue != null) {
50
					Information info = new Information("selection_structure");
51
					// Si la requête est un succès, reception d'un objet ou d'un tableau
1040 gduche 52
					JSONArray responseArray = responseValue.isArray();
53
					if (responseArray.get(1).isObject() != null) {
54
						final JSONObject reponse = responseArray.get(1).isObject();
770 jpm 55
						Structure structure = new Structure(reponse);
56
						StructureConservation structureConservation = new StructureConservation(reponse);
57
						StructureValorisation structureValorisation = new StructureValorisation(reponse);
58
						info.setDonnee(0, structure);
59
						info.setDonnee(1, structureConservation);
60
						info.setDonnee(2, structureValorisation);
1319 gduche 61
 
62
						if (seqId != null)	{
63
							Reponse reponseRequete = new Reponse(info, seqId);
64
							vueARafraichir.rafraichir(reponseRequete);
65
						}
66
						else
770 jpm 67
						vueARafraichir.rafraichir(info);
1319 gduche 68
 
1040 gduche 69
					} else if (responseArray.get(1).isArray() != null) {
770 jpm 70
						final JSONArray reponse = responseValue.isArray();
1040 gduche 71
						StructureListe structures; // = new StructureListe(reponse);
72
						if (reponse.get(1).isObject() != null)	{
73
							structures = new StructureListe(reponse.get(1).isArray());
74
						} else	{
75
							structures = new StructureListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
76
						}
77
						structures.setTaillePage(nbElements);
78
						structures.setPageCourante(pageCourante);
79
						info.setDonnee(0, structures);
1319 gduche 80
 
81
						if (seqId != null)	{
82
							Reponse reponseRequete = new Reponse(info, seqId);
83
							vueARafraichir.rafraichir(reponseRequete);
84
						}
85
						else
770 jpm 86
						vueARafraichir.rafraichir(structures);
87
					} else {
88
						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
89
					}
90
				} else {
91
					if (structureId == null) {
92
						// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
93
						StructureListe structures = new StructureListe(0);
94
						vueARafraichir.rafraichir(structures);
95
					}
96
				}
97
			}
98
		});
99
	}
100
 
101
	public void ajouter(final Structure str, StructureConservation conservation, StructureValorisation valorisation) {
102
		String postDonneesEncodees = construirePost(null, str, conservation, valorisation);
156 jp_milcent 103
 
770 jpm 104
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
105
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
106
			@Override
107
			public void surReponse(JSONValue responseValue) {
108
				if (responseValue.isString() != null) {
109
					Information info = new Information("ajout_structure");
110
					String structureIdOuMessage = responseValue.isString().stringValue();
111
					if (structureIdOuMessage.matches("^[0-9]+$")) {
112
						info.setDonnee(structureIdOuMessage);
156 jp_milcent 113
					} else {
770 jpm 114
						info.setMessage(structureIdOuMessage);
156 jp_milcent 115
					}
770 jpm 116
					vueARafraichir.rafraichir(info);
117
				} else {
118
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
156 jp_milcent 119
				}
770 jpm 120
			}
121
		});
156 jp_milcent 122
	}
770 jpm 123
 
124
	public void modifier(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
125
		String postDonneesEncodees = construirePost(structureId, str, conservation, valorisation);
772 jpm 126
		String[] parametres = {structureId};
127
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
128
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
129
			@Override
130
			public void surReponse(JSONValue responseValue) {
131
				// Si la requête est un succès, reception d'une chaine
132
				if (responseValue.isString() != null) {
133
					Information info = new Information("modif_structure");
134
					info.setMessage(responseValue.isString().stringValue());
135
					vueARafraichir.rafraichir(info);
136
				} else {
137
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
121 jpm 138
				}
772 jpm 139
			}
140
		});
121 jpm 141
	}
770 jpm 142
 
772 jpm 143
	public void supprimer(String structuresId) {
144
		String[] parametres = {utilisateurId, structuresId};
145
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
146
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
147
			@Override
148
			public void surReponse(JSONValue responseValue) {
149
				if (responseValue.isString() != null) {
150
					Information info = new Information("suppression_structure");
151
					info.setMessage(responseValue.isString().stringValue());
152
					vueARafraichir.rafraichir(info);
153
				} else {
154
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
133 jpm 155
				}
772 jpm 156
			}
157
		});
133 jpm 158
	}
169 jp_milcent 159
 
770 jpm 160
	private String construirePost(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
865 jpm 161
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
162
 
602 jp_milcent 163
		if (str != null) {
612 jp_milcent 164
			if (structureId != null) {
165
				postDonnees += "&cs_id_structure=" + URL.encodeComponent(structureId);
166
			}
865 jpm 167
			postDonnees += "&cpr_abreviation=" + URL.encodeComponent(((ProjetListe) Registry.get(RegistreId.PROJETS)).get(str.getIdProjet()).getAbreviation());
602 jp_milcent 168
			postDonnees += "&cs_ce_projet=" + URL.encodeComponent(str.getIdProjet()) +
169
				"&cs_ce_mere=" + URL.encodeComponent(str.getIdMere()) +
170
				"&cs_guid=" + URL.encodeComponent(str.getGuid()) +
171
				"&cs_truk_identifiant_alternatif=" + URL.encodeComponent(str.getIdAlternatif()) +
172
				"&cs_nom=" + URL.encodeComponent(str.getNom()) +
173
				"&cs_truk_nom_alternatif=" + URL.encodeComponent(str.getNomAlternatif()) +
1173 jpm 174
				"&cs_description=" + URL.encodeComponent(str.getDescription()) +
602 jp_milcent 175
				"&cs_ce_type=" + URL.encodeComponent(str.getType()) +
176
				"&cs_ce_truk_type_prive=" + URL.encodeComponent(str.getTypePrive()) +
177
				"&cs_ce_truk_type_public=" + URL.encodeComponent(str.getTypePublic()) +
178
				"&cs_adresse_01=" + URL.encodeComponent(str.getAdresse()) +
179
				"&cs_adresse_02=" + URL.encodeComponent(str.getAdresseComplement()) +
180
				"&cs_date_fondation=" + URL.encodeComponent(str.getDateFondationFormatMysql()) +
181
				"&cs_code_postal=" + URL.encodeComponent(str.getCodePostal()) +
182
				"&cs_ville=" + URL.encodeComponent(str.getVille()) +
1037 gduche 183
				"&cs_ce_truk_region=" + URL.encodeComponent(str.get("ce_truk_region").toString()) +
602 jp_milcent 184
				"&cs_ce_truk_pays=" + URL.encodeComponent(str.getPays()) +
1173 jpm 185
				"&cs_latitude=" + URL.encodeComponent(str.getLatitude()) +
186
				"&cs_longitude=" + URL.encodeComponent(str.getLongitude()) +
602 jp_milcent 187
				"&cs_truk_telephone=" + URL.encodeComponent(str.getTelephone()) +
188
				"&cs_truk_url=" + URL.encodeComponent(str.getUrl()) +
1173 jpm 189
				"&cs_nbre_personne=" + URL.encodeComponent(Integer.toString(str.getNbrePersonne())) +
190
				"&cs_condition_acces=" + URL.encodeComponent(str.getConditionAcces())+
191
				"&cs_condition_usage=" + URL.encodeComponent(str.getConditionUsage())+
1039 gduche 192
				"&cs_courriel=" + URL.encodeComponent(str.getCourriel());
602 jp_milcent 193
		}
194
		if (conservation != null) {
612 jp_milcent 195
			if (structureId != null) {
196
				postDonnees += "&csc_id_structure=" + URL.encodeComponent(structureId);
197
			}
602 jp_milcent 198
			postDonnees += "&csc_mark_formation=" + URL.encodeComponent(conservation.getFormation()) +
199
				"&csc_formation=" + URL.encodeComponent(conservation.getFormationInfo()) +
200
				"&csc_mark_formation_interet=" + URL.encodeComponent(conservation.getFormationInteret()) +
201
				"&csc_truk_stockage_local=" + URL.encodeComponent(conservation.getStockageLocal()) +
202
				"&csc_truk_stockage_meuble=" + URL.encodeComponent(conservation.getStockageMeuble()) +
203
				"&csc_truk_stockage_parametre=" + URL.encodeComponent(conservation.getStockageParametre()) +
204
				"&csc_mark_collection_commune=" + URL.encodeComponent(conservation.getCollectionCommune()) +
205
				"&csc_truk_collection_autre=" + URL.encodeComponent(conservation.getCollectionAutre()) +
206
				"&csc_mark_acces_controle=" + URL.encodeComponent(conservation.getAccesControle()) +
207
				"&csc_mark_restauration=" + URL.encodeComponent(conservation.getRestauration()) +
208
				"&csc_truk_restauration_operation=" + URL.encodeComponent(conservation.getRestaurationOperation()) +
209
				"&csc_ce_materiel_conservation=" + URL.encodeComponent(conservation.getMaterielConservation()) +
210
				"&csc_truk_materiel_autre=" + URL.encodeComponent(conservation.getMaterielAutre()) +
211
				"&csc_mark_traitement=" + URL.encodeComponent(conservation.getTraitement()) +
212
				"&csc_truk_traitement=" + URL.encodeComponent(conservation.getTraitements()) +
213
				"&csc_mark_acquisition_collection=" + URL.encodeComponent(conservation.getAcquisitionCollection()) +
214
				"&csc_mark_acquisition_echantillon=" + URL.encodeComponent(conservation.getAcquisitionEchantillon()) +
215
				"&csc_mark_acquisition_traitement=" + URL.encodeComponent(conservation.getAcquisitionTraitement()) +
216
				"&csc_truk_acquisition_traitement_poison=" + URL.encodeComponent(conservation.getAcquisitionTraitementPoison()) +
217
				"&csc_truk_acquisition_traitement_insecte=" + URL.encodeComponent(conservation.getAcquisitionTraitementInsecte());
218
		}
219
		if (valorisation != null) {
612 jp_milcent 220
			if (structureId != null) {
632 jp_milcent 221
				postDonnees += "&csv_id_structure=" + URL.encodeComponent(structureId);
612 jp_milcent 222
			}
602 jp_milcent 223
			postDonnees += "&csv_mark_action=" + URL.encodeComponent(valorisation.getAction()) +
224
				"&csv_truk_action=" + URL.encodeComponent(valorisation.getActionInfo()) +
225
				"&csv_publication=" + URL.encodeComponent(valorisation.getPublication()) +
226
				"&csv_collection_autre=" + URL.encodeComponent(valorisation.getCollectionAutre()) +
227
				"&csv_mark_action_future=" + URL.encodeComponent(valorisation.getActionFuture()) +
228
				"&csv_action_future=" + URL.encodeComponent(valorisation.getActionFutureInfo()) +
229
				"&csv_mark_recherche=" + URL.encodeComponent(valorisation.getRecherche()) +
230
				"&csv_truk_recherche_provenance=" + URL.encodeComponent(valorisation.getRechercheProvenance()) +
231
				"&csv_truk_recherche_type=" + URL.encodeComponent(valorisation.getRechercheType()) +
232
				"&csv_mark_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotif()) +
233
				"&csv_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotifInfo()) +
234
				"&csv_mark_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotif()) +
235
				"&csv_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotifInfo());
236
		}
237
		return postDonnees;
238
	}
121 jpm 239
}