Rev 1496 | 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.PropositionDetermination;
import org.tela_botanica.del.client.modeles.VoteDetermination;
import org.tela_botanica.del.client.services.rest.async.AjoutVoteDeterminationCallback;
import org.tela_botanica.del.client.services.rest.async.ListeVotesDeterminationCallback;
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;
import com.google.gwt.user.client.Window;
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, AjoutVoteDeterminationCallback 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/";
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.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 ?
}
// stats
Analytics.evenement("vote", "ajout", "{\"vote\": {\"valeur\": \"" + voteDetermination.getVote() + "\", \"auteur\": \"" + voteDetermination.getAuteur()
+ "\"}, \"proposition\": {\"auteur\": \"" + voteDetermination.getPropositionDetermination().getAuteur() + "\", \"nom_sci\": \"" + voteDetermination.getPropositionDetermination().getEspece() + "\", \"referentiel\": \"" + voteDetermination.getPropositionDetermination().getReferentiel() + "\"}}");
}
@Override
public void modifierVote(VoteDetermination voteDetermination, AjoutVoteDeterminationCallback 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/";
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.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 ?
}
// stats
Analytics.evenement("vote", "modification", "{\"vote\": {\"valeur\": \"" + voteDetermination.getVote() + "\", \"auteur\": \"" + voteDetermination.getAuteur()
+ "\"}, \"proposition\": {\"auteur\": \"" + voteDetermination.getPropositionDetermination().getAuteur() + "\", \"nom_sci\": \"" + voteDetermination.getPropositionDetermination().getEspece() + "\", \"referentiel\": \"" + voteDetermination.getPropositionDetermination().getReferentiel() + "\"}}");
}
private String assemblerChaineRequeteAjoutModif(VoteDetermination voteDetermination) {
String chaineRequete = "utilisateur="+voteDetermination.getContributeur()+"&"+
"valeur="+voteDetermination.getVote();
return chaineRequete;
}
@Override
public void consulterVotesPourPropositionDetermination(PropositionDetermination propositionDetermination, ListeVotesDeterminationCallback callback) {
String idObservation = propositionDetermination.getObservation().getId();
String idProposition = propositionDetermination.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/";
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, urlService);
callback.setMode(ModeRequete.LECTURE);
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 ?
}
}
}