Subversion Repositories eFlore/Applications.del

Rev

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

package org.tela_botanica.del.client.services.rest;

import org.tela_botanica.del.client.cache.CacheClient;
import org.tela_botanica.del.client.config.Config;
import org.tela_botanica.del.client.services.rest.async.AjoutMotsClesImageCallback;
import org.tela_botanica.del.client.services.rest.async.ListeMotsClesImageCallBack;
import org.tela_botanica.del.client.services.rest.async.PHPCallback.ModeRequete;
import org.tela_botanica.del.client.utils.Analytics;
import org.tela_botanica.del.client.services.rest.async.SuppressionMotCleImageCallback;

import org.tela_botanica.del.client.services.RequestBuilderWithCredentials;
import com.google.gwt.http.client.URL;

public class MotsClesServiceConcret implements MotsClesService {

        private String baseUrl;
        
        public MotsClesServiceConcret() {
                Config config = new Config();
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        public MotsClesServiceConcret(Config config) {
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        @Override
        public void getMotsClesImage(String idImage,
                        ListeMotsClesImageCallBack callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, baseUrl + "mots-cles" + assemblerChaineRequete(idImage));
                callback.setMode(ModeRequete.LECTURE);
                try {
                        rb.sendRequest(null, callback);
                } catch (Exception e) {
                        // TODO: handle exception
                }
        }
        
        private String assemblerChaineRequete(String idImage) {
                String chaineRequete = "?image="+idImage;
                return chaineRequete;
        }

        @Override
        public void ajouterMotCleImage(String idImage, String motCle,
                        AjoutMotsClesImageCallback callback) {
                String urlService = baseUrl+"mots-cles/";
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.PUT, urlService);
                
                callback.setMode(ModeRequete.AJOUT);
                String chainePost = assemblerChaineRequeteAjout(idImage, motCle);
                try {
                        rb.sendRequest(chainePost, callback);
                } catch (Exception e) {
                        //TODO: quoi faire si la requete est mal formée coté client avant d'être envoyée ?
                }
                // stats
                Analytics.evenement("mot-cle-image", "ajout", "{\"mot-cle\": \"" + motCle + "\", \"image\": \"" + idImage + "\"}");
        }

        private String assemblerChaineRequeteAjout(String idImage, String motCle) {
                return "image="+URL.encodeQueryString(idImage)+
                           "&mot_cle="+URL.encodeQueryString(motCle)+
                           "&"+URL.encodeQueryString("auteur.id")+"="+URL.encodeQueryString(CacheClient.getInstance().getUtilisateur().getId());
        }

        @Override
        public void supprimerMotCleImage(String idMotCle, SuppressionMotCleImageCallback callback) {
                String urlService = baseUrl+"mots-cles/"+idMotCle;
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.DELETE, urlService);
                
                callback.setMode(ModeRequete.SUPPRESSION);
                try {
                        rb.sendRequest(null, callback);
                } catch (Exception e) {
                        //TODO: quoi faire si la requete est mal formée coté client avant d'être envoyée ?
                }
                // stats
                Analytics.evenement("mot-cle-image", "suppression", "{\"id_mot-cle\": \"" + idMotCle + "\"}");
        }
}