Subversion Repositories eFlore/Applications.coel

Rev

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