Subversion Repositories eFlore/Applications.coel

Rev

Rev 1764 | 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;
208 jp_milcent 2
 
1468 jpm 3
import java.util.HashMap;
4
 
763 jpm 5
import org.tela_botanica.client.Mediateur;
208 jp_milcent 6
import org.tela_botanica.client.RegistreId;
763 jpm 7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
208 jp_milcent 9
import org.tela_botanica.client.interfaces.Rafraichissable;
935 jpm 10
import org.tela_botanica.client.modeles.Information;
1319 gduche 11
import org.tela_botanica.client.synchronisation.Reponse;
1210 cyprien 12
import org.tela_botanica.client.util.Debug;
763 jpm 13
import org.tela_botanica.client.util.UtilDAO;
208 jp_milcent 14
 
15
import com.extjs.gxt.ui.client.Registry;
16
import com.google.gwt.core.client.GWT;
231 jp_milcent 17
import com.google.gwt.http.client.URL;
208 jp_milcent 18
import com.google.gwt.json.client.JSONArray;
1468 jpm 19
import com.google.gwt.json.client.JSONObject;
208 jp_milcent 20
import com.google.gwt.json.client.JSONValue;
21
 
22
public class StructureAPersonneAsyncDao {
231 jp_milcent 23
	private static final String SERVICE_NOM = "CoelStructureAPersonne";
763 jpm 24
 
25
	private String utilisateurId = null;
26
	private Rafraichissable vueARafraichir = null;
27
 
28
	public StructureAPersonneAsyncDao(Rafraichissable vueARafraichirCourrante) {
1367 cyprien 29
		if (Mediateur.DEBUG) System.out.println("|| StructureAPersonneAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
763 jpm 30
		vueARafraichir = vueARafraichirCourrante;
31
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
32
	}
1468 jpm 33
 
231 jp_milcent 34
 
1468 jpm 35
	public void selectionner(final boolean paginationProgressive, final String structureId, final String roleId, final String recherche, final int start, final int nbElements, final Integer seqId) {
1513 jpm 36
 
763 jpm 37
		String[] parametres = {structureId, roleId};
38
 
1468 jpm 39
		HashMap<String, String> restrictions = new HashMap<String, String>();
40
 
41
		if (nbElements != -1)	{
42
			restrictions.put("limit", String.valueOf(nbElements));
43
		}
44
 
45
		restrictions.put("orderby", "cp_nom");
46
 
47
		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
48
		if (paginationProgressive) {
49
 
50
			/** DEFINITION DU TUPLE DE DEPART **/
51
			restrictions.put("start", String.valueOf(start));
52
 
53
			/** CONSTRUCTION DE LA REQUETE **/
54
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
55
 
56
    		/** ENVOI DE LA REQUETE **/
57
    		rb.envoyerRequete(null, new JsonRestRequestCallback()
58
    		{
59
    			/** RECEPTION DE LA REPONSE **/
60
    			public void surReponse(JSONValue responseValue)
61
    			{
62
    				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
63
    				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
64
					if (seqId != null)	{
65
						if (Mediateur.DEBUG) System.out.println("<-- StructureAPersonneAsyncDao > Liste paginée, retour au sequenceur");
66
						Reponse reponseRequete = new Reponse(responseValue, seqId);
67
						vueARafraichir.rafraichir(reponseRequete);
68
					}
69
					else	{
70
						if (Mediateur.DEBUG) System.out.println("<-- StructureAPersonneAsyncDao > Liste paginée, retour à "+vueARafraichir.getClass().toString());
71
						vueARafraichir.rafraichir(responseValue);
72
					}
73
    			}
74
    		});
75
		}
76
		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
77
		else {
78
 
79
			/** DEFINITION DU TUPLE DE DEPART **/
80
			restrictions.put("start", String.valueOf(start*nbElements));
81
 
82
			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
83
 
84
			rb.envoyerRequete(null, new JsonRestRequestCallback() {
85
				@Override
86
				public void surReponse(JSONValue responseValue) {
87
 
88
					Information info = new Information("liste_structure_a_personne");
89
 
90
					if (responseValue != null) {
91
 
92
						JSONObject responseObject = responseValue.isObject();
93
 
94
						if (responseObject != null) {
95
 
96
							// Si la réponse est un tableau, alors c'est une liste de Structures qui a été retournée
97
							if (responseObject.get("structuresAPersonne").isArray() != null) {
98
 
99
								final JSONArray reponse = responseObject.get("structuresAPersonne").isArray();
100
								// Transformation du tableau JSON réponse en ListeInstitution
101
								StructureAPersonneListe personnes = new StructureAPersonneListe(reponse);
102
								info.setDonnee(0, personnes);
103
								// et on met à jour le demandeur des données
104
								if (seqId != null)	{
105
									Reponse reponseRequete = new Reponse(info, seqId);
106
									vueARafraichir.rafraichir(reponseRequete);
107
								}
108
								else	{
109
									vueARafraichir.rafraichir(info);
110
								}
111
 
112
							// Si la réponse est un objet, alors c'est une unique Structure qui a été retournée
113
							} else if (responseObject.get("structuresAPersonne").isObject() != null) {
114
								GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un tableau JSON et vaut : "+responseValue.toString(), null);
115
							}
116
						}
117
					} else {
118
						// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
119
						if (structureId == null) {
120
							// Dans le cas, où nous demandons toutes les relations Structure à Personne et qu'il n'y en a pas, nous retournons un message d'information
121
							info.setMessage("Aucun personnel");
1319 gduche 122
							vueARafraichir.rafraichir(info);
123
						}
208 jp_milcent 124
					}
125
				}
1468 jpm 126
			});
127
		}
128
	}
231 jp_milcent 129
 
1468 jpm 130
 
1292 cyprien 131
	public void ajouter(String structureId, StructureAPersonne personnel) {
767 jpm 132
		String postDonneesEncodees = construirePost(structureId, personnel);
763 jpm 133
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
134
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
135
			@Override
136
			public void surReponse(JSONValue responseValue) {
137
				// Si la requête est un succès, reception d'une chaine
138
				if (responseValue.isString() != null) {
139
					Information info = new Information("ajout_structure_a_personne");
140
					info.setMessage(responseValue.isString().stringValue());
767 jpm 141
					vueARafraichir.rafraichir(info);
763 jpm 142
				} else {
143
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
231 jp_milcent 144
				}
763 jpm 145
			}
1292 cyprien 146
		});
231 jp_milcent 147
	}
148
 
767 jpm 149
	public void modifier(StructureAPersonne personnel) {
150
		String[] parametres = {personnel.getIdStructure(), personnel.getIdPersonne(), personnel.getIdRole()};
151
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
231 jp_milcent 152
 
767 jpm 153
		String postDonneesEncodees = construirePost(personnel.getIdStructure(), personnel);
154
 
155
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
156
			@Override
157
			public void surReponse(JSONValue responseValue) {
158
				Information info = new Information("modif_structure_a_personne");
159
				// Si la requête est un succès, reception d'une chaine
160
				if (responseValue.isString() != null) {
161
					info.setMessage(responseValue.isString().stringValue());
162
					vueARafraichir.rafraichir(info);
163
				} else {
164
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
231 jp_milcent 165
				}
767 jpm 166
			}
167
		});
231 jp_milcent 168
	}
169
 
767 jpm 170
	public void supprimer(String idStrAPer) {
171
		String[] parametres = {utilisateurId, idStrAPer};
172
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
173
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
174
			@Override
175
			public void surReponse(JSONValue responseValue) {
176
				if (responseValue.isString() != null) {
177
					Information info = new Information("suppression_structure_a_personne");
178
					info.setMessage(responseValue.isString().stringValue());
179
					vueARafraichir.rafraichir(info);
180
				} else {
181
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
231 jp_milcent 182
				}
767 jpm 183
			}
184
		});
231 jp_milcent 185
	}
602 jp_milcent 186
 
767 jpm 187
	private String construirePost(String structureId, StructureAPersonne personnel) {
602 jp_milcent 188
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
609 jp_milcent 189
		if (!personnel.getIdPersonne().equals("")) {
190
			postDonnees += "&csap_id_personne=" + URL.encodeComponent(personnel.getIdPersonne()) +
191
				"&cp_id_personne=" + URL.encodeComponent(personnel.getIdPersonne());
192
		}
611 jp_milcent 193
		postDonnees += "&csap_id_structure=" + URL.encodeComponent(structureId) +
194
			"&csap_id_role=" + URL.encodeComponent(personnel.getIdRole()) +
602 jp_milcent 195
			"&csap_ce_truk_fonction=" + URL.encodeComponent(personnel.getFonction()) +
196
			"&csap_service=" + URL.encodeComponent(personnel.getService()) +
197
			"&csap_ce_truk_statut=" + URL.encodeComponent(personnel.getStatut()) +
198
			"&csap_mark_contact=" + URL.encodeComponent(personnel.getContact()) +
199
			"&csap_bota_travail_hebdo_tps=" + URL.encodeComponent(personnel.getBotaTravailHebdoTps()) +
200
			"&cp_prenom=" + URL.encodeComponent(personnel.getPrenom()) +
201
			"&cp_nom=" + URL.encodeComponent(personnel.getNom()) +
1210 cyprien 202
			"&cp_fmt_nom_complet=" + URL.encodeComponent(personnel.getNomComplet()) +
602 jp_milcent 203
			"&cp_truk_telephone=" + URL.encodeComponent(personnel.getTelephone()) +
204
			"&cp_truk_courriel=" + URL.encodeComponent(personnel.getCourriel()) +
205
			"&cp_ce_truk_specialite=" + URL.encodeComponent(personnel.getSpecialite()) +
1329 cyprien 206
			"&cp_ce_deces=" + URL.encodeComponent(personnel.getDeces()) +
602 jp_milcent 207
			"";
208
		return postDonnees;
209
	}
208 jp_milcent 210
}