1 |
package org.tela_botanica.client.modeles.commentaire;
|
1 |
package org.tela_botanica.client.modeles.commentaire;
|
2 |
|
2 |
|
3 |
import java.util.HashMap;
|
3 |
import java.util.HashMap;
|
4 |
|
4 |
|
5 |
import org.tela_botanica.client.Mediateur;
|
5 |
import org.tela_botanica.client.Mediateur;
|
6 |
import org.tela_botanica.client.RegistreId;
|
6 |
import org.tela_botanica.client.RegistreId;
|
7 |
import org.tela_botanica.client.http.JsonRestRequestBuilder;
|
7 |
import org.tela_botanica.client.http.JsonRestRequestBuilder;
|
8 |
import org.tela_botanica.client.http.JsonRestRequestCallback;
|
8 |
import org.tela_botanica.client.http.JsonRestRequestCallback;
|
9 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
9 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
10 |
import org.tela_botanica.client.modeles.Information;
|
10 |
import org.tela_botanica.client.modeles.Information;
|
11 |
import org.tela_botanica.client.synchronisation.Reponse;
|
11 |
import org.tela_botanica.client.synchronisation.Reponse;
|
12 |
import org.tela_botanica.client.util.UtilDAO;
|
12 |
import org.tela_botanica.client.util.UtilDAO;
|
13 |
|
13 |
|
14 |
import com.extjs.gxt.ui.client.Registry;
|
14 |
import com.extjs.gxt.ui.client.Registry;
|
15 |
import com.google.gwt.core.client.GWT;
|
15 |
import com.google.gwt.core.client.GWT;
|
16 |
import com.google.gwt.json.client.JSONArray;
|
16 |
import com.google.gwt.json.client.JSONArray;
|
17 |
import com.google.gwt.json.client.JSONObject;
|
17 |
import com.google.gwt.json.client.JSONObject;
|
18 |
import com.google.gwt.json.client.JSONValue;
|
18 |
import com.google.gwt.json.client.JSONValue;
|
19 |
|
19 |
|
20 |
public class CommentaireAsyncDao {
|
20 |
public class CommentaireAsyncDao {
|
21 |
private static final String SERVICE_NOM = "CoelCommentaire";
|
21 |
private static final String SERVICE_NOM = "CoelCommentaire";
|
22 |
|
22 |
|
23 |
private String utilisateurId = null;
|
23 |
private String utilisateurId = null;
|
24 |
private Rafraichissable vueARafraichir = null;
|
24 |
private Rafraichissable vueARafraichir = null;
|
25 |
|
25 |
|
26 |
public CommentaireAsyncDao(Rafraichissable vueARafraichirCourrante) {
|
26 |
public CommentaireAsyncDao(Rafraichissable vueARafraichirCourrante) {
|
27 |
if (Mediateur.DEBUG) System.out.println("|| CommentaireAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
|
27 |
if (Mediateur.DEBUG) System.out.println("|| CommentaireAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
|
28 |
vueARafraichir = vueARafraichirCourrante ;
|
28 |
vueARafraichir = vueARafraichirCourrante ;
|
29 |
utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
|
29 |
utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
|
30 |
}
|
30 |
}
|
31 |
|
31 |
|
32 |
public void selectionner(final String commentaireId, String projetId, String titre, final int pageCourante, final int nbElements, final Integer seqId) {
|
32 |
public void selectionner(final String commentaireId, String projetId, String titre, final int pageCourante, final int nbElements, final Integer seqId) {
|
33 |
String[] parametres = {projetId, commentaireId, titre};
|
33 |
String[] parametres = {projetId, commentaireId, titre};
|
34 |
|
34 |
|
35 |
HashMap<String, String> restrictions = new HashMap<String, String>();
|
35 |
HashMap<String, String> restrictions = new HashMap<String, String>();
|
36 |
restrictions.put("start", String.valueOf(pageCourante*nbElements));
|
36 |
restrictions.put("start", String.valueOf(pageCourante*nbElements));
|
37 |
if (nbElements != -1) {
|
37 |
if (nbElements != -1) {
|
38 |
restrictions.put("limit", String.valueOf(nbElements));
|
38 |
restrictions.put("limit", String.valueOf(nbElements));
|
39 |
}
|
39 |
}
|
40 |
|
40 |
|
41 |
restrictions.put("orderby", "ccac_id_collection DESC");
|
41 |
restrictions.put("orderby", "cc_nom ASC");
|
42 |
|
42 |
|
43 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
|
43 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
|
44 |
rb.envoyerRequete(null, new JsonRestRequestCallback() {
|
44 |
rb.envoyerRequete(null, new JsonRestRequestCallback() {
|
45 |
@Override
|
45 |
@Override
|
46 |
public void surReponse(JSONValue responseValue) {
|
46 |
public void surReponse(JSONValue responseValue) {
|
47 |
if (responseValue != null) {
|
47 |
if (responseValue != null) {
|
48 |
// Si la requête est un succès, réception d'un objet ou d'un tableau
|
48 |
// Si la requête est un succès, réception d'un objet ou d'un tableau
|
49 |
JSONArray responseArray = responseValue.isArray();
|
49 |
JSONArray responseArray = responseValue.isArray();
|
50 |
|
50 |
|
51 |
if (responseArray.get(1).isObject() != null) {
|
51 |
if (responseArray.get(1).isObject() != null) {
|
52 |
final JSONObject reponse = responseArray.get(1).isObject();
|
52 |
final JSONObject reponse = responseArray.get(1).isObject();
|
53 |
// Transformation du tableau JSON réponse en ListeInstitution
|
53 |
// Transformation du tableau JSON réponse en ListeInstitution
|
54 |
Commentaire commentaire = new Commentaire(reponse);
|
54 |
Commentaire commentaire = new Commentaire(reponse);
|
55 |
// et on met à jour le demandeur des données
|
55 |
// et on met à jour le demandeur des données
|
56 |
if (seqId != null) {
|
56 |
if (seqId != null) {
|
57 |
Reponse reponseRequete = new Reponse(commentaire, seqId);
|
57 |
Reponse reponseRequete = new Reponse(commentaire, seqId);
|
58 |
vueARafraichir.rafraichir(reponseRequete);
|
58 |
vueARafraichir.rafraichir(reponseRequete);
|
59 |
}
|
59 |
}
|
60 |
else {
|
60 |
else {
|
61 |
vueARafraichir.rafraichir(commentaire);
|
61 |
vueARafraichir.rafraichir(commentaire);
|
62 |
}
|
62 |
}
|
63 |
} else if (responseValue.isArray() != null) {
|
63 |
} else if (responseValue.isArray() != null) {
|
64 |
final JSONArray reponse = responseValue.isArray();
|
64 |
final JSONArray reponse = responseValue.isArray();
|
65 |
CommentaireListe commentaires;
|
65 |
CommentaireListe commentaires;
|
66 |
if (reponse.get(1).isObject() != null) {
|
66 |
if (reponse.get(1).isObject() != null) {
|
67 |
commentaires = new CommentaireListe(reponse.get(1).isArray());
|
67 |
commentaires = new CommentaireListe(reponse.get(1).isArray());
|
68 |
} else {
|
68 |
} else {
|
69 |
commentaires = new CommentaireListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
|
69 |
commentaires = new CommentaireListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
|
70 |
}
|
70 |
}
|
71 |
commentaires.setTaillePage(nbElements);
|
71 |
commentaires.setTaillePage(nbElements);
|
72 |
commentaires.setPageCourante(pageCourante);
|
72 |
commentaires.setPageCourante(pageCourante);
|
73 |
|
73 |
|
74 |
if (seqId != null) {
|
74 |
if (seqId != null) {
|
75 |
Reponse reponseRequete = new Reponse(commentaires, seqId);
|
75 |
Reponse reponseRequete = new Reponse(commentaires, seqId);
|
76 |
vueARafraichir.rafraichir(reponseRequete);
|
76 |
vueARafraichir.rafraichir(reponseRequete);
|
77 |
}
|
77 |
}
|
78 |
else {
|
78 |
else {
|
79 |
vueARafraichir.rafraichir(commentaires);
|
79 |
vueARafraichir.rafraichir(commentaires);
|
80 |
}
|
80 |
}
|
81 |
} else {
|
81 |
} else {
|
82 |
GWT.log("La réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
|
82 |
GWT.log("La réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
|
83 |
}
|
83 |
}
|
84 |
} else {
|
84 |
} else {
|
85 |
// Dans le cas, où nous demandons toutes les publication et qu'il n'y en a pas, nous retournons un objet vide
|
85 |
// Dans le cas, où nous demandons toutes les publication et qu'il n'y en a pas, nous retournons un objet vide
|
86 |
if (commentaireId == null) {
|
86 |
if (commentaireId == null) {
|
87 |
CommentaireListe commentaires = new CommentaireListe(0);
|
87 |
CommentaireListe commentaires = new CommentaireListe(0);
|
88 |
if (seqId != null) {
|
88 |
if (seqId != null) {
|
89 |
Reponse reponseRequete = new Reponse(commentaires, seqId);
|
89 |
Reponse reponseRequete = new Reponse(commentaires, seqId);
|
90 |
vueARafraichir.rafraichir(reponseRequete);
|
90 |
vueARafraichir.rafraichir(reponseRequete);
|
91 |
}
|
91 |
}
|
92 |
else {
|
92 |
else {
|
93 |
vueARafraichir.rafraichir(commentaires);
|
93 |
vueARafraichir.rafraichir(commentaires);
|
94 |
}
|
94 |
}
|
95 |
}
|
95 |
}
|
96 |
}
|
96 |
}
|
97 |
}
|
97 |
}
|
98 |
});
|
98 |
});
|
99 |
}
|
99 |
}
|
100 |
|
100 |
|
101 |
public void ajouter(Commentaire commentaire) {
|
101 |
public void ajouter(Commentaire commentaire) {
|
102 |
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
|
102 |
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
|
103 |
|
103 |
|
104 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
|
104 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
|
105 |
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
|
105 |
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
|
106 |
@Override
|
106 |
@Override
|
107 |
public void surReponse(JSONValue reponseValeur) {
|
107 |
public void surReponse(JSONValue reponseValeur) {
|
108 |
traiterReponse(reponseValeur, "ajout_commentaire");
|
108 |
traiterReponse(reponseValeur, "ajout_commentaire");
|
109 |
}
|
109 |
}
|
110 |
}) ;
|
110 |
}) ;
|
111 |
}
|
111 |
}
|
112 |
|
112 |
|
113 |
public void modifier(Commentaire commentaire) {
|
113 |
public void modifier(Commentaire commentaire) {
|
114 |
String[] parametres = {commentaire.getId()};
|
114 |
String[] parametres = {commentaire.getId()};
|
115 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
|
115 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
|
116 |
|
116 |
|
117 |
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
|
117 |
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
|
118 |
|
118 |
|
119 |
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
|
119 |
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
|
120 |
@Override
|
120 |
@Override
|
121 |
public void surReponse(JSONValue reponseValeur) {
|
121 |
public void surReponse(JSONValue reponseValeur) {
|
122 |
traiterReponse(reponseValeur, "modif_commentaire");
|
122 |
traiterReponse(reponseValeur, "modif_commentaire");
|
123 |
}
|
123 |
}
|
124 |
});
|
124 |
});
|
125 |
}
|
125 |
}
|
126 |
|
126 |
|
127 |
public void supprimer(String commentairesId) {
|
127 |
public void supprimer(String commentairesId) {
|
128 |
String[] parametres = {utilisateurId, commentairesId};
|
128 |
String[] parametres = {utilisateurId, commentairesId};
|
129 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
|
129 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
|
130 |
rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
|
130 |
rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
|
131 |
@Override
|
131 |
@Override
|
132 |
public void surReponse(JSONValue reponseValeur) {
|
132 |
public void surReponse(JSONValue reponseValeur) {
|
133 |
traiterReponse(reponseValeur, "suppression_commentaire");
|
133 |
traiterReponse(reponseValeur, "suppression_commentaire");
|
134 |
}
|
134 |
}
|
135 |
});
|
135 |
});
|
136 |
}
|
136 |
}
|
137 |
|
137 |
|
138 |
private void traiterReponse(JSONValue reponseValeur, String type) {
|
138 |
private void traiterReponse(JSONValue reponseValeur, String type) {
|
139 |
Information info = new Information(type);
|
139 |
Information info = new Information(type);
|
140 |
// Si la requête est un succès, réception d'une chaîne
|
140 |
// Si la requête est un succès, réception d'une chaîne
|
141 |
if (reponseValeur.isString() != null) {
|
141 |
if (reponseValeur.isString() != null) {
|
142 |
String idOuMessage = reponseValeur.isString().stringValue();
|
142 |
String idOuMessage = reponseValeur.isString().stringValue();
|
143 |
if (idOuMessage.matches("^[0-9]+$")) {
|
143 |
if (idOuMessage.matches("^[0-9]+$")) {
|
144 |
info.setDonnee(idOuMessage);
|
144 |
info.setDonnee(idOuMessage);
|
145 |
} else {
|
145 |
} else {
|
146 |
info.setMessage(idOuMessage);
|
146 |
info.setMessage(idOuMessage);
|
147 |
}
|
147 |
}
|
148 |
} else {
|
148 |
} else {
|
149 |
info.setDeboguage("La réponse n'est pas une chaine JSON.");
|
149 |
info.setDeboguage("La réponse n'est pas une chaine JSON.");
|
150 |
}
|
150 |
}
|
151 |
vueARafraichir.rafraichir(info);
|
151 |
vueARafraichir.rafraichir(info);
|
152 |
}
|
152 |
}
|
153 |
|
153 |
|
154 |
}
|
154 |
}
|