Subversion Repositories eFlore/Applications.coel

Rev

Rev 763 | Rev 935 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 763 Rev 767
1
package org.tela_botanica.client.modeles;
1
package org.tela_botanica.client.modeles;
2
 
2
 
3
import org.tela_botanica.client.Mediateur;
3
import org.tela_botanica.client.Mediateur;
4
import org.tela_botanica.client.RegistreId;
4
import org.tela_botanica.client.RegistreId;
5
import org.tela_botanica.client.http.JsonRestRequestBuilder;
5
import org.tela_botanica.client.http.JsonRestRequestBuilder;
6
import org.tela_botanica.client.http.JsonRestRequestCallback;
6
import org.tela_botanica.client.http.JsonRestRequestCallback;
7
import org.tela_botanica.client.interfaces.Rafraichissable;
7
import org.tela_botanica.client.interfaces.Rafraichissable;
8
import org.tela_botanica.client.util.UtilDAO;
8
import org.tela_botanica.client.util.UtilDAO;
9
 
9
 
10
import com.extjs.gxt.ui.client.Registry;
10
import com.extjs.gxt.ui.client.Registry;
11
import com.extjs.gxt.ui.client.widget.Info;
-
 
12
import com.google.gwt.core.client.GWT;
11
import com.google.gwt.core.client.GWT;
13
import com.google.gwt.http.client.Request;
-
 
14
import com.google.gwt.http.client.RequestBuilder;
-
 
15
import com.google.gwt.http.client.RequestCallback;
-
 
16
import com.google.gwt.http.client.RequestException;
-
 
17
import com.google.gwt.http.client.Response;
-
 
18
import com.google.gwt.http.client.URL;
12
import com.google.gwt.http.client.URL;
19
import com.google.gwt.json.client.JSONArray;
13
import com.google.gwt.json.client.JSONArray;
20
import com.google.gwt.json.client.JSONParser;
-
 
21
import com.google.gwt.json.client.JSONValue;
14
import com.google.gwt.json.client.JSONValue;
22
 
15
 
23
public class StructureAPersonneAsyncDao {
16
public class StructureAPersonneAsyncDao {
24
	private static final String SERVICE_NOM = "CoelStructureAPersonne";
17
	private static final String SERVICE_NOM = "CoelStructureAPersonne";
25
 
18
 
26
	private String utilisateurId = null;
19
	private String utilisateurId = null;
27
	private Rafraichissable vueARafraichir = null;
20
	private Rafraichissable vueARafraichir = null;
28
 
21
 
29
	public StructureAPersonneAsyncDao(Rafraichissable vueARafraichirCourrante) {
22
	public StructureAPersonneAsyncDao(Rafraichissable vueARafraichirCourrante) {
30
		vueARafraichir = vueARafraichirCourrante;
23
		vueARafraichir = vueARafraichirCourrante;
31
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
24
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
32
	}
25
	}
33
	
26
	
34
	public void selectionner(final String structureId, final String roleId) {
27
	public void selectionner(final String structureId, final String roleId) {
35
		String[] parametres = {structureId, roleId};
28
		String[] parametres = {structureId, roleId};
36
		
29
		
37
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
30
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
38
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
31
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
39
			@Override
32
			@Override
40
			public void surReponse(JSONValue responseValue) {
33
			public void surReponse(JSONValue responseValue) {
41
				Information info = new Information("liste_structure_a_personne");
34
				Information info = new Information("liste_structure_a_personne");
42
				if (responseValue != null) {
35
				if (responseValue != null) {
43
					// Si la requête est un succès, reception d'un tableau
36
					// Si la requête est un succès, reception d'un tableau
44
					if (responseValue.isArray() != null) {
37
					if (responseValue.isArray() != null) {
45
						final JSONArray reponse = responseValue.isArray();
38
						final JSONArray reponse = responseValue.isArray();
46
						// Transformation du tableau JSON réponse en ListeInstitution
39
						// Transformation du tableau JSON réponse en ListeInstitution
47
						StructureAPersonneListe personnel = new StructureAPersonneListe(reponse);
40
						StructureAPersonneListe personnel = new StructureAPersonneListe(reponse);
48
						info.setDonnee(0, personnel);
41
						info.setDonnee(0, personnel);
49
						// et on met à jour le demandeur des données
42
						// et on met à jour le demandeur des données
50
						vueARafraichir.rafraichir(info);
43
						vueARafraichir.rafraichir(info);
51
					} else {
44
					} else {
52
						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un talbeau JSON et vaut : "+responseValue.toString(), null);
45
						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un talbeau JSON et vaut : "+responseValue.toString(), null);
53
					}
46
					}
54
				} else {
47
				} else {
55
					if (structureId == null) {
48
					if (structureId == null) {
56
						// 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
49
						// 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
57
						info.setMessage("Aucun personnel");
50
						info.setMessage("Aucun personnel");
58
						vueARafraichir.rafraichir(info);
51
						vueARafraichir.rafraichir(info);
59
					}
52
					}
60
				}
53
				}
61
			}
54
			}
62
		});
55
		});
63
	}
56
	}
64
 
57
 
65
	public void ajouter(final Rafraichissable vue, String utilisateurId, String structureId, StructureAPersonne personnel) {
58
	public void ajouter(String structureId, StructureAPersonne personnel) {
66
		String postDonneesEncodees = construirePost(utilisateurId, structureId, personnel);
59
		String postDonneesEncodees = construirePost(structureId, personnel);
67
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);	
60
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);	
68
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
61
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
69
			@Override
62
			@Override
70
			public void surReponse(JSONValue responseValue) {							
63
			public void surReponse(JSONValue responseValue) {							
71
				// Si la requête est un succès, reception d'une chaine
64
				// Si la requête est un succès, reception d'une chaine
72
				if (responseValue.isString() != null) {
65
				if (responseValue.isString() != null) {
73
					Information info = new Information("ajout_structure_a_personne");
66
					Information info = new Information("ajout_structure_a_personne");
74
					info.setMessage(responseValue.isString().stringValue());
67
					info.setMessage(responseValue.isString().stringValue());
75
					vue.rafraichir(info);
68
					vueARafraichir.rafraichir(info);
76
				} else {
69
				} else {
77
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
70
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
78
				}
71
				}
79
			}
72
			}
80
		});
73
		});
81
	}
74
	}
82
	
75
	
83
	public void modifier(final Rafraichissable vueARafraichir, String utilisateurId, StructureAPersonne personnel) {
76
	public void modifier(StructureAPersonne personnel) {
84
		final String url = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl() + 
-
 
85
		SERVICE_NOM + "/" +
-
 
86
		personnel.getIdStructure() + "/" +
-
 
87
		personnel.getIdPersonne() + "/" +
-
 
88
		personnel.getIdRole()
-
 
89
		;
-
 
90
	
77
		String[] parametres = {personnel.getIdStructure(), personnel.getIdPersonne(), personnel.getIdRole()};
91
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, url);
78
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
92
		
79
		
93
		String postDonneesEncodees = construirePost(utilisateurId, personnel.getIdStructure(), personnel);
-
 
94
	
80
		String postDonneesEncodees = construirePost(personnel.getIdStructure(), personnel);
95
		try {
-
 
96
			rb.sendRequest(postDonneesEncodees, new RequestCallback() {
-
 
97
	
-
 
98
				public void onError(Request request, Throwable exception) {
-
 
99
					// Gestion des exceptions déclenchées par l'exécution de la requête 
-
 
100
					GWT.log("Erreur à l'exécution du service "+SERVICE_NOM+" (modif)", exception);
81
		
101
					Info.display("Erreur de Requête", "Une erreur s'est produite lors de l'exécution de la requête.");
-
 
102
				}
82
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
103
				
-
 
104
				public void onErrorHTTP(Request request, Response reponse) {
-
 
105
					// Gestion des erreurs HTTP renvoyé par Apache ou JRest 
-
 
106
					Information info = new Information("erreur_jrest", JSONParser.parse(reponse.getText()).isArray());
-
 
107
					GWT.log("Erreur JREST - Code "+reponse.getStatusCode()+"\n"+info.getMessages().toString(), null);
-
 
108
					Info.display("Erreur JREST - Code "+reponse.getStatusCode(), info.toString());
-
 
109
				}
-
 
110
	
-
 
111
				public void onResponseReceived(Request request, Response response) {
-
 
112
					// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanise d'erreur HTTP
-
 
113
					if (response.getStatusCode() != 200) {
-
 
114
						onErrorHTTP(request, response);
83
			@Override
115
					} else {
-
 
116
						Information info = new Information("modif_structure_a_personne");
-
 
117
						if (response.getHeader("X-DebugJrest-Data") != null && response.getHeader("X-DebugJrest-Data").length() != 0) {
-
 
118
							final JSONValue reponseEnteteDeboguage = JSONParser.parse(response.getHeader("X-DebugJrest-Data"));
-
 
119
							if (reponseEnteteDeboguage.isArray() != null) {
-
 
120
								info.setDeboguages(reponseEnteteDeboguage.isArray());
-
 
121
								GWT.log("DEBOGUAGE:\n"+info.getDeboguages().toString(), null);
-
 
122
							}
-
 
123
						}
-
 
124
						if (response.getText().length() != 0 && response.getText() != null) {
-
 
125
							final JSONValue responseValue = JSONParser.parse(response.getText());
84
			public void surReponse(JSONValue responseValue) {
126
							
85
				Information info = new Information("modif_structure_a_personne");
127
							// Si la requête est un succès, reception d'une chaine
86
				// Si la requête est un succès, reception d'une chaine
128
							if (responseValue.isString() != null) {
87
				if (responseValue.isString() != null) {
129
								info.setMessage(responseValue.isString().stringValue());
88
					info.setMessage(responseValue.isString().stringValue());
130
								vueARafraichir.rafraichir(info);
89
					vueARafraichir.rafraichir(info);
131
							} else {
-
 
132
								GWT.log(url+"\n\tLa réponse n'est pas une chaine JSON.", null);
-
 
133
							}
-
 
134
						} else {
-
 
135
							GWT.log(url, null);
-
 
136
							if (response.getText().length() == 0) {
-
 
137
								GWT.log("\tLa réponse a une taille de 0", null);
-
 
138
							}
-
 
139
							if (response.getText() == null) {
-
 
140
								GWT.log("\tLa réponse vaul null", null);
-
 
141
							}
-
 
142
						}
90
				} else {
143
					}
-
 
144
				}
91
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
145
				
-
 
146
			}) ;
-
 
147
		} catch (RequestException e) {
-
 
148
			// Gestion des exceptions déclenchées par la création de la requête (url non-valide ?)
-
 
149
			GWT.log("Erreur à la création du service "+SERVICE_NOM+" (modif)", e);
92
				}
150
			Info.display("Erreur de Requête", "Une erreur s'est produite lors de la création de la requête.");
93
			}
151
		}
94
		});
152
	}
95
	}
153
	
-
 
154
	public void supprimer(final Rafraichissable vue, String utilisateurId, String idStrAPer) {
-
 
155
		// Ajout des paramètres et données à supprimer dans l'URL
-
 
156
		final String url = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl() + 
-
 
157
			SERVICE_NOM + "/" +
-
 
158
			utilisateurId + "/" +
-
 
159
			idStrAPer +
-
 
160
			"";
96
	
161
		
-
 
162
		// DELETE n'étant pas disponible comme méthode HTTP, nous utilisons POST avec le paramètre action=DELETE
-
 
163
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, url);
-
 
164
		String postDonnees = "action=DELETE";
97
	public void supprimer(String idStrAPer) {
165
		
-
 
166
		try {
-
 
167
			rb.sendRequest(postDonnees, new RequestCallback() {
-
 
168
 
-
 
169
				public void onError(Request request, Throwable exception) {
-
 
170
					// Gestion des exceptions déclenchées par l'exécution de la requête 
98
		String[] parametres = {utilisateurId, idStrAPer};
171
					GWT.log("Erreur à l'exécution du service "+SERVICE_NOM+" (suppression)", exception);
-
 
172
					Info.display("Erreur de Requête", "Une erreur s'est produite lors de l'exécution de la requête.");
99
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
173
				}
-
 
174
 
-
 
175
				public void onErrorHTTP(Request request, Response reponse) {
-
 
176
					// Gestion des erreurs HTTP renvoyé par Apache ou JRest 
-
 
177
					Information info = new Information("erreur_jrest", JSONParser.parse(reponse.getText()).isArray());
-
 
178
					GWT.log("Erreur JREST - Code "+reponse.getStatusCode()+"\n"+info.getMessages().toString(), null);
-
 
179
					Info.display("Erreur JREST - Code "+reponse.getStatusCode(), info.toString());
-
 
180
				}
-
 
181
				
100
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
182
				public void onResponseReceived(Request request, Response response) {
-
 
183
					// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanise d'erreur HTTP
-
 
184
					if (response.getStatusCode() != 200) {
101
			@Override
185
						onErrorHTTP(request, response);
-
 
186
					} else {
-
 
187
						Information info = new Information("suppression_structure_a_personne");
-
 
188
						if (response.getHeader("X-DebugJrest-Data") != null && response.getHeader("X-DebugJrest-Data").length() != 0) {
-
 
189
							final JSONValue reponseEnteteDeboguage = JSONParser.parse(response.getHeader("X-DebugJrest-Data"));
-
 
190
							if (reponseEnteteDeboguage.isArray() != null) {
-
 
191
								info.setDeboguages(reponseEnteteDeboguage.isArray());
-
 
192
								GWT.log("DEBOGUAGE:\n"+info.getDeboguages().toString(), null);
-
 
193
							}
-
 
194
						}
-
 
195
						if (response.getText().length() != 0 && response.getText() != null) {
-
 
196
							final JSONValue responseValue = JSONParser.parse(response.getText());
-
 
197
							
102
			public void surReponse(JSONValue responseValue) {
198
							// Si la requête est un succès, reception d'une chaine
103
				if (responseValue.isString() != null) {
199
							if (responseValue.isString() != null) {
104
					Information info = new Information("suppression_structure_a_personne");
200
								info.setMessage(responseValue.isString().stringValue());
105
					info.setMessage(responseValue.isString().stringValue());
201
								vue.rafraichir(info);
-
 
202
							} else {
-
 
203
								GWT.log(url+"\n\tLa réponse n'est pas une chaine JSON.", null);
-
 
204
							}
-
 
205
						} else {
-
 
206
							GWT.log(url, null);
-
 
207
							if (response.getText().length() == 0) {
-
 
208
								GWT.log("\tLa réponse a une taille de 0", null);
-
 
209
							}
-
 
210
							if (response.getText() == null) {
-
 
211
								GWT.log("\tLa réponse vaul null", null);
-
 
212
							}
106
					vueARafraichir.rafraichir(info);
213
						}
-
 
214
					}
107
				} else {
215
				}
-
 
216
				
-
 
217
			}) ;
-
 
218
		} catch (RequestException e) {
-
 
219
			// Gestion des exceptions déclenchées par la création de la requête (url non-valide ?)
108
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
220
			GWT.log("Erreur à la création du service "+SERVICE_NOM+" (suppression)", e);
-
 
221
			Info.display("Erreur de Requête", "Une erreur s'est produite lors de la création de la requête.");
109
				}
222
		}
110
			}
223
		
111
		});
224
	}
112
	}
225
	
113
	
226
	private String construirePost(String utilisateurId, String structureId, StructureAPersonne personnel) {
114
	private String construirePost(String structureId, StructureAPersonne personnel) {
227
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
115
		String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);
228
		if (!personnel.getIdPersonne().equals("")) {
116
		if (!personnel.getIdPersonne().equals("")) {
229
			postDonnees += "&csap_id_personne=" + URL.encodeComponent(personnel.getIdPersonne()) +
117
			postDonnees += "&csap_id_personne=" + URL.encodeComponent(personnel.getIdPersonne()) +
230
				"&cp_id_personne=" + URL.encodeComponent(personnel.getIdPersonne());
118
				"&cp_id_personne=" + URL.encodeComponent(personnel.getIdPersonne());
231
		}
119
		}
232
		postDonnees += "&csap_id_structure=" + URL.encodeComponent(structureId) +
120
		postDonnees += "&csap_id_structure=" + URL.encodeComponent(structureId) +
233
			"&csap_id_role=" + URL.encodeComponent(personnel.getIdRole()) +	
121
			"&csap_id_role=" + URL.encodeComponent(personnel.getIdRole()) +	
234
			"&csap_ce_truk_fonction=" + URL.encodeComponent(personnel.getFonction()) +
122
			"&csap_ce_truk_fonction=" + URL.encodeComponent(personnel.getFonction()) +
235
			"&csap_service=" + URL.encodeComponent(personnel.getService()) +
123
			"&csap_service=" + URL.encodeComponent(personnel.getService()) +
236
			"&csap_ce_truk_statut=" + URL.encodeComponent(personnel.getStatut()) +
124
			"&csap_ce_truk_statut=" + URL.encodeComponent(personnel.getStatut()) +
237
			"&csap_mark_contact=" + URL.encodeComponent(personnel.getContact()) +
125
			"&csap_mark_contact=" + URL.encodeComponent(personnel.getContact()) +
238
			"&csap_bota_travail_hebdo_tps=" + URL.encodeComponent(personnel.getBotaTravailHebdoTps()) +
126
			"&csap_bota_travail_hebdo_tps=" + URL.encodeComponent(personnel.getBotaTravailHebdoTps()) +
239
			"&cp_ce_projet=" + URL.encodeComponent(personnel.getIdProjetPersonne()) +
127
			"&cp_ce_projet=" + URL.encodeComponent(personnel.getIdProjetPersonne()) +
240
			"&cp_prenom=" + URL.encodeComponent(personnel.getPrenom()) +
128
			"&cp_prenom=" + URL.encodeComponent(personnel.getPrenom()) +
241
			"&cp_nom=" + URL.encodeComponent(personnel.getNom()) +
129
			"&cp_nom=" + URL.encodeComponent(personnel.getNom()) +
242
			"&cp_truk_telephone=" + URL.encodeComponent(personnel.getTelephone()) +
130
			"&cp_truk_telephone=" + URL.encodeComponent(personnel.getTelephone()) +
243
			"&cp_truk_courriel=" + URL.encodeComponent(personnel.getCourriel()) +
131
			"&cp_truk_courriel=" + URL.encodeComponent(personnel.getCourriel()) +
244
			"&cp_ce_truk_specialite=" + URL.encodeComponent(personnel.getSpecialite()) +
132
			"&cp_ce_truk_specialite=" + URL.encodeComponent(personnel.getSpecialite()) +
245
			"";
133
			"";
246
		return postDonnees;
134
		return postDonnees;
247
	}
135
	}
248
}
136
}