Subversion Repositories eFlore/Applications.del

Rev

Rev 1689 | 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.InformationsRecherche;
import org.tela_botanica.del.client.modeles.Observation;
import org.tela_botanica.del.client.modeles.VoteDetermination;
import org.tela_botanica.del.client.services.rest.async.DepublicationObservationCallBack;
import org.tela_botanica.del.client.services.rest.async.ObservationsCallback;
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.RequestBuilderWithCredentials;

public class ObservationServiceConcret implements ObservationService {

        private String baseUrl;

        public ObservationServiceConcret() {
                Config config = new Config();
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        public ObservationServiceConcret(Config config) {
                this.baseUrl = config.getServiceBaseUrl();
        }
        
        @Override
        public void getObservations(InformationsRecherche infos, int debut, int fin, String statut, ObservationsCallback callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, baseUrl + "observations" + assemblerChaineRequete(infos, debut, fin, statut));
                callback.setMode(ModeRequete.LECTURE);
                try {
                        rb.sendRequest(null, callback);
                } catch (Exception e) {
                        // TODO: handle exception
                }
        }

        private String assemblerChaineRequete(InformationsRecherche infos, int debut, int fin, String statut) {
                String chaineRequete = "?navigation.depart=" + debut + "&navigation.limite=" + (fin - debut);
                if (statut != null && !statut.equals("tous")) {
                        chaineRequete += "&masque.type=" + statut;
                }
                chaineRequete+= infos.versChaineRequete();

                return chaineRequete;
        }

        @Override
        public void getObservation(String idObservation, ObservationsCallback callback) {
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, baseUrl + "observations/"+idObservation);
                callback.setMode(ModeRequete.LECTURE);
                try {
                        rb.sendRequest(null, callback);
                } catch (Exception e) {
                        // TODO: handle exception
                }
        }
        
        public void depublier(Observation observation, DepublicationObservationCallBack callback) {
                String urlService = baseUrl+"observations/"+observation.getId();
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.POST, urlService);
                                
                callback.setMode(ModeRequete.MODIFICATION);
                String chainePost = "transmission=0";
                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("observation", "depublication", "{\"observation\": {\"id\": \"" + observation.getId() + "\", \"auteur\": \"" + observation.getAuteur() + "\"}}");
        }
}