Subversion Repositories eFlore/Applications.del

Rev

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

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

import java.util.List;

import org.tela_botanica.del.client.config.Config;
import org.tela_botanica.del.client.modeles.VoteProtocole;
import org.tela_botanica.del.client.services.rest.async.AjoutVoteProtocoleCallback;
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.VoteProtocoleCallback;

import org.tela_botanica.del.client.services.RequestBuilderWithCredentials;

public class VoteProtocoleServiceConcret implements VoteProtocoleService {

        private String baseUrl;

        public VoteProtocoleServiceConcret() {
                Config config = new Config();
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        public VoteProtocoleServiceConcret(Config config) {
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        @Override
        public List<VoteProtocole> getVote(String idImage) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public List<VoteProtocole> getVotePourImageEtProtocole(String idImage,
                        String protocolName) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
        public void ajouterVote(String idImage, VoteProtocole voteProtocole, AjoutVoteProtocoleCallback callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.PUT, baseUrl+"images/"+idImage+"/votes/");
                callback.setMode(ModeRequete.AJOUT);
                String chainePost = assemblerChaineRequeteAjoutModif(voteProtocole);
                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("vote", "ajout", "{\"valeur\": \"" + voteProtocole.getVote() + "\", \"auteur\": \"" + voteProtocole.getAuteur() + "\", \"id_image\": \"" + idImage + "\"}");
        }

        private String assemblerChaineRequeteAjoutModif(VoteProtocole voteProtocole) {
                String chaineRequete = "utilisateur="+voteProtocole.getContributeur()+"&"+
                                                                "protocole="+voteProtocole.getIdProtocole()+"&"+
                                                                "valeur="+voteProtocole.getVote();
                return chaineRequete;
        }
        
        @Override
        public void modifierVote(String idImage, VoteProtocole voteProtocole, VoteProtocoleCallback callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.POST, baseUrl+"images/"+idImage+"/votes/");
                callback.setMode(ModeRequete.MODIFICATION);
                String chainePost = assemblerChaineRequeteAjoutModif(voteProtocole);
                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("vote", "modification", "{\"valeur\": \"" + voteProtocole.getVote() + "\", \"auteur\": \"" + voteProtocole.getAuteur() + "\", \"id_image\": \"" + idImage + "\"}");
        }

        @Override
        public void supprimerVote(String idImage, VoteProtocole voteProtocole,
                        VoteProtocoleCallback callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.DELETE, baseUrl+"images/"+idImage+"/votes/"+voteProtocole.getId());
                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("vote", "suppression", "{\"valeur\": \"" + voteProtocole.getVote() + "\", \"auteur\": \"" + voteProtocole.getAuteur() + "\", \"id_image\": \"" + idImage + "\"}");
        }
}