Subversion Repositories eFlore/Applications.del

Compare Revisions

Ignore whitespace Rev 2170 → Rev 2171

/branches/v1.11-magnesium/src/org/tela_botanica/del/client/composants/moteurrecherche/MoteurRecherchePresenteur.java
New file
0,0 → 1,443
package org.tela_botanica.del.client.composants.moteurrecherche;
 
import java.util.Map;
 
import org.tela_botanica.del.client.cache.CacheClient;
import org.tela_botanica.del.client.composants.formulaires.autocompletion.AutoCompletionComboBoxPresenteur;
import org.tela_botanica.del.client.composants.formulaires.autocompletion.AutoCompletionComboBoxVue;
import org.tela_botanica.del.client.composants.presenteur.Presenteur;
import org.tela_botanica.del.client.composants.rss.RssPresenteur;
import org.tela_botanica.del.client.composants.rss.RssVue;
import org.tela_botanica.del.client.config.Config;
import org.tela_botanica.del.client.modeles.InformationsRecherche;
import org.tela_botanica.del.client.modeles.ModeRecherche;
import org.tela_botanica.del.client.services.ReferentielService;
import org.tela_botanica.del.client.services.rest.PaysService;
import org.tela_botanica.del.client.services.rest.PaysServiceConcret;
import org.tela_botanica.del.client.services.rest.async.PaysCallback;
import org.tela_botanica.del.client.utils.InfosNomPourAutocompletion;
import org.tela_botanica.del.client.utils.UtilitairesAutoCompletionService;
 
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasKeyPressHandlers;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
 
public abstract class MoteurRecherchePresenteur extends Presenteur {
 
public abstract interface Vue extends IsWidget {
public void ajouterVue(HasWidgets composite);
 
public HasClickHandlers getLienRechercheAvancee();
 
public void basculerAffichageZoneCache();
 
public HasClickHandlers getBoutonRechercheSimple();
 
public HasClickHandlers getBoutonFermer();
 
public HasClickHandlers getBoutonVider();
 
public HasClickHandlers getBoutonRechercheAvancee();
 
public HasKeyPressHandlers getChampSaisie();
 
public HasClickHandlers getChampSaisieCliquable();
 
public String getValeurRechercheSimple();
 
public String getLabelRecherche();
 
public String getContientMots();
 
public boolean getPnInscritsSeulement();
 
public String getDepartement();
 
public HasWidgets getCommune();
 
public HasWidgets getTaxon();
 
public String getFamille();
 
public String getGenre();
 
public String getMotCle();
 
public String getAuteur();
 
public String getDate();
public String getPays();
 
public void setValeurRechercheSimple(String valeurRecherche);
 
public void setContientMots(String mots);
 
public void setPnInscritsSeulement(boolean valeur);
 
public void setValeurDepartement(String dpt);
 
public void chargerValeursRecherchePrecedente(InformationsRecherche informationsRecherche);
 
public void focusSaisie();
 
public void nettoyer();
 
public HasKeyPressHandlers getChampsFamille();
 
public HasKeyPressHandlers getChampsGenre();
 
public HasKeyPressHandlers getChampsMotCle();
public HasKeyPressHandlers getChampsMotCleDel();
public HasKeyPressHandlers getChampsMotCleCel();
 
public HasKeyPressHandlers getChampsAuteur();
 
public HasKeyPressHandlers getChampsDate();
public HasKeyPressHandlers getChampsDepartement();
public HasKeyPressHandlers getChampsContientMots();
public HasWidgets getZoneRss();
 
public String getReferentiel();
public void setReferentielLectureSeule(boolean lectureSeule);
 
void remplirListeReferentiels(Map<String, String> listeReferentiels);
 
public String getMotCleDel();
 
public String getMotCleCel();
public void cacherChampsTagsImage();
public void cacherChampsTagsObs();
 
void remplirListePays(Map<String, String> listePays);
 
}
 
private Vue vue;
private Config config;
private ModeRecherche modeRecherche;
private RssPresenteur presenteurRss;
public enum TypeMoteur {
SIMPLE, AVANCEE
};
 
private TypeMoteur typeMoteur = TypeMoteur.SIMPLE;
 
private AutoCompletionComboBoxPresenteur completionTaxonsPresenteur = null;
private AutoCompletionComboBoxPresenteur completionCommunesPresenteur = null;
 
public MoteurRecherchePresenteur(Vue vue, ModeRecherche mode) {
initialiser(vue, mode, new Config());
}
 
public MoteurRecherchePresenteur(AutoCompletionComboBoxPresenteur presenteurAutoCompletionTaxon, AutoCompletionComboBoxPresenteur presenteurAutoCompletionCommunes, Vue vue, ModeRecherche mode, Config configuration) {
completionTaxonsPresenteur = presenteurAutoCompletionTaxon;
completionCommunesPresenteur = presenteurAutoCompletionCommunes;
initialiser(vue, mode, configuration);
}
 
private void initialiser(Vue vue, ModeRecherche mode, Config configuration) {
//TODO : fixer le flux avant de le mettre en place
//this.presenteurRss = new RssPresenteur(new RssVue(), CacheClient.getInstance().getInformationsRechercheObservation(), "S'abonner au flux rss des résultats de cette recherche");
//presenteurRss.go(vue.getZoneRss());
this.vue = vue;
setMode(mode);
this.config = configuration;
if (completionTaxonsPresenteur == null)
this.creerCompletionTaxons();
if (completionCommunesPresenteur == null)
this.creerCompletionCommunes();
gererEvenements();
vue.remplirListeReferentiels(ReferentielService.getReferentiels());
if(CacheClient.getInstance().getReferentielNonModifiable()) {
vue.setReferentielLectureSeule(true);
}
if(CacheClient.getInstance().getListePays() != null) {
vue.remplirListePays(CacheClient.getInstance().getListePays());
} else {
PaysService paysService = new PaysServiceConcret();
paysService.getPays(new PaysCallback() {
@Override
public void surRetour(Map<String, String> listePays) {
CacheClient.getInstance().setListePays(listePays);
MoteurRecherchePresenteur.this.vue.remplirListePays(listePays);
}
});
}
if(estPourRechercheImages()) {
vue.cacherChampsTagsObs();
} else {
vue.cacherChampsTagsImage();
}
}
 
public ModeRecherche getMode() {
return modeRecherche;
}
 
private void setMode(ModeRecherche mode) {
modeRecherche = mode;
}
public TypeMoteur getTypeMoteur() {
return this.typeMoteur;
}
public void setTypeMoteur(TypeMoteur typeMoteur) {
this.typeMoteur = typeMoteur;
}
 
public boolean estPourRechercheImages() {
return (modeRecherche == ModeRecherche.MODE_IMAGE);
}
 
public boolean estPourRechercheObservations() {
return (modeRecherche == ModeRecherche.MODE_OBSERVATION);
}
 
private void creerCompletionTaxons() {
String url = this.config.getServiceBaseUrl() + "nomstaxons";
completionTaxonsPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
protected String effectuerPreTraitementChaineRequete(String requete) {
// si aucun référentiel selectionné, pas de requete
if(vue.getReferentiel() != null && !vue.getReferentiel().equals("")) {
requete = "?masque.nom=" + requete + "&masque.referentiel=" + vue.getReferentiel();
return requete;
} else {
return null;
}
// A décommenter lors de l'utilisation des web services eflore
// return
// RetourAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceEflore(requete);
}
 
@Override
protected InfosNomPourAutocompletion[] parserResultatRequete(Response response) {
return UtilitairesAutoCompletionService.parserResultatRetourSimple(response);
// A décommenter lors de l'utilisation des web services eflore
// return RetourAutoCompletionService.parserRetourOss(response);
}
};
}
 
private void creerCompletionCommunes() {
String url = this.config.getServiceBaseUrl() + "communes";
completionCommunesPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
protected String effectuerPreTraitementChaineRequete(String requete) {
requete = "?masque.nom=" + requete;
return requete;
}
 
@Override
protected void surSelectionSuggestion(String suggestion) {
setValeur(suggestion);
collecterInfosRecherche();
setValeur(getInformationsRechercheEnCache().getCommune());
vue.setValeurDepartement(getInformationsRechercheEnCache().getDepartement());
}
 
@Override
protected InfosNomPourAutocompletion[] parserResultatRequete(Response response) {
return UtilitairesAutoCompletionService.parserResultatRetourSimple(response);
}
};
}
 
@Override
public void go(HasWidgets composite) {
afficherRequeteEtLancerRecherche();
completionTaxonsPresenteur.go(vue.getTaxon());
completionCommunesPresenteur.go(vue.getCommune());
vue.ajouterVue(composite);
vue.focusSaisie();
}
 
@Override
protected void gererEvenements() {
vue.getLienRechercheAvancee().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
setTypeMoteur(TypeMoteur.AVANCEE);
vue.setContientMots(vue.getValeurRechercheSimple());
vue.basculerAffichageZoneCache();
}
});
 
vue.getBoutonRechercheSimple().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
setTypeMoteur(TypeMoteur.SIMPLE);
collecterInfosRecherche();
afficherRequeteEtLancerRecherche();
}
});
 
vue.getBoutonFermer().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
vue.setValeurRechercheSimple(vue.getContientMots());
vue.basculerAffichageZoneCache();
}
});
 
vue.getBoutonVider().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
nettoyerChamps();
InformationsRecherche infosRecherche = new InformationsRecherche();
if (estPourRechercheImages()) {
CacheClient.getInstance().setInformationsRechercheImage(infosRecherche);
} else if (estPourRechercheObservations()) {
CacheClient.getInstance().setInformationsRechercheObservation(infosRecherche);
}
}
});
 
vue.getBoutonRechercheAvancee().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
setTypeMoteur(TypeMoteur.AVANCEE);
collecterInfosRecherche();
vue.basculerAffichageZoneCache();
afficherRequeteEtLancerRecherche();
}
});
 
vue.getChampSaisie().addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
setTypeMoteur(TypeMoteur.SIMPLE);
collecterInfosRecherche();
afficherRequeteEtLancerRecherche();
}
}
});
 
vue.getChampSaisieCliquable().addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (vue.getValeurRechercheSimple().equals(vue.getLabelRecherche())) {
vue.setValeurRechercheSimple("");
}
}
});
 
vue.getChampsAuteur().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsDate().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsFamille().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsGenre().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsMotCle().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsMotCleCel().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsMotCleDel().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsDepartement().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
vue.getChampsContientMots().addKeyPressHandler(creerGestionnaireEvenementToucheEntree());
 
}
 
public KeyPressHandler creerGestionnaireEvenementToucheEntree() {
return new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
vue.basculerAffichageZoneCache();
collecterInfosRecherche();
afficherRequeteEtLancerRecherche();
}
}
};
}
 
private void nettoyerChamps() {
completionCommunesPresenteur.nettoyer();
completionTaxonsPresenteur.nettoyer();
vue.nettoyer();
}
 
public void collecterInfosRecherche() {
InformationsRecherche informationRecherche = new InformationsRecherche();
informationRecherche.setRechercheLibre(this.getRechercheLibre());
if (this.typeMoteur == TypeMoteur.AVANCEE) {
informationRecherche.setTaxon(completionTaxonsPresenteur.getValeur());
informationRecherche.setDepartement(vue.getDepartement());
informationRecherche.setCommune(completionCommunesPresenteur.getValeur());
informationRecherche.setFamille(vue.getFamille());
informationRecherche.setGenre(vue.getGenre());
if(estPourRechercheObservations()) {
informationRecherche.setMotClef(vue.getMotCle());
} else {
informationRecherche.setMotClefDel(vue.getMotCleDel());
informationRecherche.setMotClefCel(vue.getMotCleCel());
}
informationRecherche.setTag(vue.getMotCle());
informationRecherche.setAuteur(vue.getAuteur());
informationRecherche.setDate(vue.getDate());
informationRecherche.setReferentiel(vue.getReferentiel());
informationRecherche.setPays(vue.getPays());
informationRecherche.setPnInscritsSeulement(vue.getPnInscritsSeulement());
}
if(CacheClient.getInstance().getReferentielNonModifiable()) {
informationRecherche.setReferentiel(CacheClient.getInstance().getReferentielCourant());
}
 
if (estPourRechercheImages()) {
CacheClient.getInstance().setPageCouranteRechercheImages(1);
CacheClient.getInstance().setInformationsRechercheImage(informationRecherche);
} else if (estPourRechercheObservations()) {
CacheClient.getInstance().setPageCouranteRechercheObservations(1);
CacheClient.getInstance().setInformationsRechercheObservation(informationRecherche);
}
}
 
private String getRechercheLibre() {
String rechercheLibre = "";
switch (this.typeMoteur) {
case SIMPLE:
rechercheLibre = vue.getValeurRechercheSimple();
break;
case AVANCEE:
rechercheLibre = vue.getContientMots();
break;
default:
// TODO : voir comment gérer les exceptions proprement
// throw new
// Exception("Le type de moteur de recherche indiqué n'est pas disponible");
}
return rechercheLibre;
}
 
private InformationsRecherche getInformationsRechercheEnCache() {
if (estPourRechercheImages()) {
return CacheClient.getInstance().getInformationsRechercheImage();
} else if (estPourRechercheObservations()) {
return CacheClient.getInstance().getInformationsRechercheObservation();
}
return null;
}
 
public void afficherRequeteEtLancerRecherche() {
InformationsRecherche informationsRecherche = getInformationsRechercheEnCache();
if (informationsRecherche != null) {
completionTaxonsPresenteur.setValeur(informationsRecherche.getTaxon());
completionCommunesPresenteur.setValeur(informationsRecherche.getCommune());
vue.chargerValeursRecherchePrecedente(informationsRecherche);
}
lancerRecherche();
//presenteurRss.genererLien(informationsRecherche);
}
 
public abstract void lancerRecherche();
}