Subversion Repositories eFlore/Applications.coel

Rev

Rev 1135 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles.commentaire;

import java.util.HashMap;

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.modeles.structure.StructureListe;
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 titre, final int pageCourante, final int nbElements) {
                String[] parametres = {projetId, commentaireId, titre};
                
                HashMap<String, String> restrictions = new HashMap<String, String>();
                restrictions.put("start", String.valueOf(pageCourante*nbElements));
                if (nbElements != -1)   {
                        restrictions.put("limit", String.valueOf(nbElements));
                }
                
                final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
                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
                                        JSONArray responseArray = responseValue.isArray();
                                        if (responseArray.get(1).isObject() != null) {
                                                final JSONObject reponse = responseArray.get(1).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;
                                                if (reponse.get(1).isObject() != null)  {
                                                        commentaires = new CommentaireListe(reponse.get(1).isArray());
                                                } else  {
                                                        commentaires = new CommentaireListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
                                                }
                                                commentaires.setTaillePage(nbElements);
                                                commentaires.setPageCourante(pageCourante);                                                     

                                                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) {
                        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);
        }

}