Subversion Repositories eFlore/Applications.del

Rev

Rev 1162 | Go to most recent revision | 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 com.google.gwt.http.client.RequestBuilder;
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) {
                RequestBuilder rb = new RequestBuilder(RequestBuilder.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 = "?masque.image="+idImage;
                return chaineRequete;
        }

        @Override
        public void ajouterMotCleImage(String idImage, String motCle,
                        AjoutMotsClesImageCallBack callback) {
                String urlService = baseUrl+"mots-cles/";
                RequestBuilder rb = new RequestBuilder(RequestBuilder.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 ?
                }

        }

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

}