Subversion Repositories eFlore/Applications.del

Rev

Rev 869 | Rev 899 | 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.config.Config;
import org.tela_botanica.del.client.modeles.VoteDetermination;
import org.tela_botanica.del.client.services.rest.async.VoteDeterminationCallback;
import org.tela_botanica.del.client.services.rest.async.VoteDeterminationCallback.ModeRequete;

import com.google.gwt.http.client.RequestBuilder;

public class VoteDeterminationServiceConcret implements VoteDeterminationService {

        private String baseUrl;

        public VoteDeterminationServiceConcret() {
                Config config = new Config();
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        public VoteDeterminationServiceConcret(Config config) {
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        @Override
        public void ajouterVote(VoteDetermination voteDetermination, VoteDeterminationCallback callback) {
                String idObservation = voteDetermination.getPropositionDetermination().getObservation().getId();
                String idProposition = voteDetermination.getPropositionDetermination().getId();
                //cas de la proposition de détermination crée automatiquement à partir de l'obs
                if(idProposition == null) {
                        idProposition = "0";
                }
                
                String urlService = baseUrl+"observations/"+idObservation+"/"+idProposition+"/vote/";
                RequestBuilder rb = new RequestBuilder(RequestBuilder.PUT, urlService);
                
                callback.setMode(ModeRequete.AJOUT);
                String chainePost = assemblerChaineRequeteAjoutModif(voteDetermination);
                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 ?
                }
        }

        @Override
        public void modifierVote(VoteDetermination voteDetermination, VoteDeterminationCallback callback) {     
                String idObservation = voteDetermination.getPropositionDetermination().getObservation().getId();
                String idProposition = voteDetermination.getPropositionDetermination().getId();
                //cas de la proposition de détermination crée automatiquement à partir de l'obs
                if(idProposition == null) {
                        idProposition = "0";
                }
                
                String urlService = baseUrl+"observations/"+idObservation+"/"+idProposition+"/vote/";
                RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, urlService);
                                
                callback.setMode(ModeRequete.MODIFICATION);
                String chainePost = assemblerChaineRequeteAjoutModif(voteDetermination);
                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 assemblerChaineRequeteAjoutModif(VoteDetermination voteDetermination) {
                String chaineRequete = "utilisateur="+voteDetermination.getContributeur()+"&"+
                                                                "valeur="+voteDetermination.getVote();
                return chaineRequete;
        }
}