Subversion Repositories eFlore/Applications.del

Rev

Rev 500 | Rev 545 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.del.client.composants.moteurrecherche;

import org.tela_botanica.del.client.cache.CacheClient;
import org.tela_botanica.del.client.composants.formulaires.AutoCompletionComboBoxPresenteur;
import org.tela_botanica.del.client.composants.formulaires.AutoCompletionComboBoxVue;
import org.tela_botanica.del.client.composants.presenteur.Presenteur;
import org.tela_botanica.del.client.modeles.InformationsRecherche;
import org.tela_botanica.del.client.modeles.ModeRecherche;
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 abstract void ajouterVue(HasWidgets composite);
                public abstract HasClickHandlers getLienRechercheAvancee();
                public abstract void basculerAffichageZoneCache();
                public abstract HasClickHandlers getBoutonRechercheSimple();
                public abstract HasClickHandlers getBoutonFermer();
                public abstract HasClickHandlers getBoutonVider();
                public abstract HasClickHandlers getBoutonRechercheAvancee();
                public abstract HasKeyPressHandlers getChampSaisie();
                public abstract HasClickHandlers getChampSaisieCliquable();
                public abstract String getValeurRechercheSimple();
                public String getLabelRecherche();
                public String getContientMots();
                public String getDepartement();
                public HasWidgets getCommune();
                public HasWidgets getTaxon();
                public String getFamille();
                public String getGenre();
                public String getTag();
                public String getMotCle();
                public String getAuteur();
                public String getDate();
                public void setValeurRechercheSimple(String valeurRecherche);
                public void setContientMots(String mots);
                public void setValeurDepartement(String dpt);
                public void chargerValeursRecherchePrecedente(InformationsRecherche informationsRecherche);
                public void focusSaisie();
                public void nettoyer();
        }

        private final Vue vue;
        private ModeRecherche modeRecherche;
        public enum TypeMoteur {SIMPLE, AVANCEE};
        private TypeMoteur typeMoteur = TypeMoteur.SIMPLE;

        private AutoCompletionComboBoxPresenteur autoCompletionNomTaxonsPresenteur;
        private AutoCompletionComboBoxPresenteur autoCompletionCommunesPresenteur;
        
        public MoteurRecherchePresenteur(Vue vue, ModeRecherche mode) {
                
                AutoCompletionComboBoxPresenteur completionNomTaxonsPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), UtilitairesAutoCompletionService.urlServiceCompletionNomLocale) {

                        protected String effectuerPreTraitementChaineRequete(String requete) {
                                return UtilitairesAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceSlash(requete);
                                // A décommenter lors de l'utilisation des web services eflore
                                // return
                                // RetourAutoCompletionService.effectuerPreTraitementChaineRequeteGenreEspeceEflore(requete);
                        }

                        @Override
                        protected String[] parserResultatRequete(Response response) {
                                return UtilitairesAutoCompletionService.parserRetourSimple(response);
                                // A décommenter lors de l'utilisation des web services eflore
                                // return RetourAutoCompletionService.parserRetourOss(response);
                        }
                };
                
                AutoCompletionComboBoxPresenteur completionCommunesPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), UtilitairesAutoCompletionService.urlServiceCompletionCommunes) {

                        protected String effectuerPreTraitementChaineRequete(String requete) {
                                return requete;
                        }
                        
                        @Override
                        protected void surSelectionSuggestion(String suggestion) {
                                String[] tableauCommuneDpt = suggestion.split(" ");
                                if(tableauCommuneDpt.length == 2) {
                                        String dpt = tableauCommuneDpt[1].replaceAll("\\(", "").replaceAll("\\)", "");
                                        getVue().setValeurDepartement(dpt);
                                        setValeur(tableauCommuneDpt[0]);
                                }
                        }

                        @Override
                        protected String[] parserResultatRequete(Response response) {
                                return UtilitairesAutoCompletionService.parserRetourSimple(response);
                        }
                };
                this.vue = vue;
                setMode(mode);

                autoCompletionCommunesPresenteur = completionCommunesPresenteur;
                autoCompletionNomTaxonsPresenteur = completionNomTaxonsPresenteur;
                gererEvenements();
        }
        
        public MoteurRecherchePresenteur(AutoCompletionComboBoxPresenteur presenteurAutoCompletionTaxon, 
                                                                        AutoCompletionComboBoxPresenteur presenteurAutoCompletionCommunes,
                                                                        Vue vue, ModeRecherche mode) {
                this.vue = vue;
                setMode(mode);
                autoCompletionNomTaxonsPresenteur = presenteurAutoCompletionTaxon;
                autoCompletionCommunesPresenteur = presenteurAutoCompletionCommunes;
                gererEvenements();
        }
        
        private Vue getVue() {
                return vue;
        }
        
        public ModeRecherche getMode() {
                return modeRecherche;
        }
        
        private void setMode(ModeRecherche mode) {
                modeRecherche = mode;
        }
        
        public void setTypeMoteur(TypeMoteur typeMoteur) {
                this.typeMoteur = typeMoteur;
        }

        public boolean estPourRechercheImages() {
                return (modeRecherche == ModeRecherche.MODE_IMAGE);
        }

        public boolean estPourRechercheObservations() {
                return (modeRecherche == ModeRecherche.MODE_OBSERVATION);
        }       

        @Override
        public void go(HasWidgets composite) {
                afficherRequeteEtLancerRecherche();
                autoCompletionNomTaxonsPresenteur.go(vue.getTaxon());
                autoCompletionCommunesPresenteur.go(vue.getCommune());
                vue.ajouterVue(composite);
                vue.focusSaisie();
        }

        @Override
        protected void gererEvenements() {
                vue.getLienRechercheAvancee().addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                                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("");
                                }
                        }
                });
        }
        
        private void nettoyerChamps() {
                autoCompletionCommunesPresenteur.nettoyer();
                autoCompletionNomTaxonsPresenteur.nettoyer();
                vue.nettoyer();
        }

        public void collecterInfosRecherche() {
                InformationsRecherche informationRecherche = new InformationsRecherche();
                informationRecherche.setTaxon(autoCompletionNomTaxonsPresenteur.getValeur());
                informationRecherche.setDepartement(vue.getDepartement());
                informationRecherche.setCommune(autoCompletionCommunesPresenteur.getValeur());
                informationRecherche.setFamille(vue.getFamille());
                informationRecherche.setGenre(vue.getGenre());
                informationRecherche.setTag(vue.getTag());
                informationRecherche.setMotClef(vue.getMotCle());
                informationRecherche.setAuteur(vue.getAuteur());
                informationRecherche.setDate(vue.getDate());
                informationRecherche.setRechercheLibre(this.getRechercheLibre());

                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 disponilbe");
                }
                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) {
                        autoCompletionNomTaxonsPresenteur.setValeur(informationsRecherche.getTaxon());
                        vue.chargerValeursRecherchePrecedente(informationsRecherche);
                }
                lancerRecherche();
        }

        public abstract void lancerRecherche();
}