New file |
0,0 → 1,115 |
package org.tela_botanica.client.modeles.commentaire; |
|
import org.tela_botanica.client.Mediateur; |
import org.tela_botanica.client.RegistreId; |
import org.tela_botanica.client.http.JsonRestRequestBuilder; |
import org.tela_botanica.client.http.JsonRestRequestCallback; |
import org.tela_botanica.client.interfaces.Rafraichissable; |
import org.tela_botanica.client.modeles.Information; |
import org.tela_botanica.client.util.UtilDAO; |
|
import com.extjs.gxt.ui.client.Registry; |
import com.google.gwt.core.client.GWT; |
import com.google.gwt.json.client.JSONArray; |
import com.google.gwt.json.client.JSONObject; |
import com.google.gwt.json.client.JSONValue; |
|
public class CommentaireAsyncDao { |
private static final String SERVICE_NOM = "CoelCommentaire"; |
|
private String utilisateurId = null; |
private Rafraichissable vueARafraichir = null; |
|
public CommentaireAsyncDao(Rafraichissable vueARafraichirCourrante) { |
vueARafraichir = vueARafraichirCourrante ; |
utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId(); |
} |
|
public void selectionner(final String commentaireId, String projetId) { |
String[] parametres = {projetId, commentaireId}; |
|
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres); |
rb.envoyerRequete(null, new JsonRestRequestCallback() { |
@Override |
public void surReponse(JSONValue responseValue) { |
if (responseValue != null) { |
// Si la requête est un succès, réception d'un objet ou d'un tableau |
if (responseValue.isObject() != null) { |
final JSONObject reponse = responseValue.isObject(); |
// Transformation du tableau JSON réponse en ListeInstitution |
Commentaire commentaire = new Commentaire(reponse); |
// et on met à jour le demandeur des données |
vueARafraichir.rafraichir(commentaire); |
} else if (responseValue.isArray() != null) { |
final JSONArray reponse = responseValue.isArray(); |
CommentaireListe commentaires = new CommentaireListe(reponse); |
vueARafraichir.rafraichir(commentaires); |
} else { |
GWT.log("La réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null); |
} |
} else { |
// Dans le cas, où nous demandons toutes les publication et qu'il n'y en a pas, nous retournons un objet vide |
if (commentaireId == null) { |
CommentaireListe commentaires = new CommentaireListe(0); |
vueARafraichir.rafraichir(commentaires); |
} |
} |
} |
}); |
} |
|
public void ajouter(Commentaire commentaire) { |
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId; |
|
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM); |
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() { |
@Override |
public void surReponse(JSONValue reponseValeur) { |
traiterReponse(reponseValeur, "ajout_commentaire"); |
} |
}) ; |
} |
|
public void modifier(Commentaire commentaire) { |
String[] parametres = {commentaire.getId()}; |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres); |
|
String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId; |
|
rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() { |
@Override |
public void surReponse(JSONValue reponseValeur) { |
traiterReponse(reponseValeur, "modif_commentaire"); |
} |
}); |
} |
|
public void supprimer(String commentairesId) { |
String[] parametres = {utilisateurId, commentairesId}; |
final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres); |
rb.envoyerRequeteSuppression(new JsonRestRequestCallback() { |
@Override |
public void surReponse(JSONValue reponseValeur) { |
traiterReponse(reponseValeur, "suppression_commentaire"); |
} |
}); |
} |
|
private void traiterReponse(JSONValue reponseValeur, String type) { |
Information info = new Information(type); |
// Si la requête est un succès, réception d'une chaîne |
if (reponseValeur.isString() != null) { |
info.setDonnee(reponseValeur.isString().stringValue()); |
String idOuMessage = reponseValeur.isString().stringValue(); |
if (idOuMessage.matches("^[0-9]+$")) { |
info.setDonnee(idOuMessage); |
} else { |
info.setMessage(idOuMessage); |
} |
} else { |
info.setDeboguage("La réponse n'est pas une chaine JSON."); |
} |
vueARafraichir.rafraichir(info); |
} |
|
} |