Subversion Repositories eFlore/Applications.del

Rev

Rev 560 | Rev 581 | 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.config.Config;
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 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 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();

                public HasKeyPressHandlers getChampsFamille();

                public HasKeyPressHandlers getChampsGenre();

                public HasKeyPressHandlers getChampsTag();

                public HasKeyPressHandlers getChampsMotCle();

                public HasKeyPressHandlers getChampsAuteur();

                public HasKeyPressHandlers getChampsDate();

        }

        private Vue vue;
        private Config config;
        private ModeRecherche modeRecherche;

        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);
        }

        public MoteurRecherchePresenteur(AutoCompletionComboBoxPresenteur presenteurAutoCompletionTaxon, AutoCompletionComboBoxPresenteur presenteurAutoCompletionCommunes, Vue vue, ModeRecherche mode) {
                completionTaxonsPresenteur = presenteurAutoCompletionTaxon;
                completionCommunesPresenteur = presenteurAutoCompletionCommunes;
                initialiser(vue, mode);
        }

        private void initialiser(Vue vue, ModeRecherche mode) {
                this.vue = vue;
                setMode(mode);
                this.config = new Config();
                if (completionTaxonsPresenteur == null)
                        this.creerCompletionTaxons();
                if (completionCommunesPresenteur == null)
                        this.creerCompletionCommunes();
                gererEvenements();
        }

        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);
        }

        private void creerCompletionTaxons() {
                String url = this.config.getUrl("nomSciCompletionService");
                completionTaxonsPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
                        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);
                        }
                };
        }

        private void creerCompletionCommunes() {
                String url = this.config.getUrl("communeCompletionService");
                completionCommunesPresenteur = new AutoCompletionComboBoxPresenteur(new AutoCompletionComboBoxVue(), url) {
                        protected String effectuerPreTraitementChaineRequete(String requete) {
                                return requete;
                        }

                        @Override
                        protected void surSelectionSuggestion(String suggestion) {
                                setValeur(suggestion);
                                collecterInfosRecherche();
                                setValeur(getInformationsRechercheEnCache().getCommune());
                                vue.setValeurDepartement(getInformationsRechercheEnCache().getDepartement());
                        }

                        @Override
                        protected String[] parserResultatRequete(Response response) {
                                return UtilitairesAutoCompletionService.parserRetourSimple(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(createKeyboardAdvancedSearchHandler());

                vue.getChampsDate().addKeyPressHandler(createKeyboardAdvancedSearchHandler());

                vue.getChampsFamille().addKeyPressHandler(createKeyboardAdvancedSearchHandler());

                vue.getChampsGenre().addKeyPressHandler(createKeyboardAdvancedSearchHandler());

                vue.getChampsMotCle().addKeyPressHandler(createKeyboardAdvancedSearchHandler());

                vue.getChampsTag().addKeyPressHandler(createKeyboardAdvancedSearchHandler());

        }

        public KeyPressHandler createKeyboardAdvancedSearchHandler() {
                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());
                        informationRecherche.setTag(vue.getTag());
                        informationRecherche.setMotClef(vue.getMotCle());
                        informationRecherche.setAuteur(vue.getAuteur());
                        informationRecherche.setDate(vue.getDate());
                }

                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) {
                        completionTaxonsPresenteur.setValeur(informationsRecherche.getTaxon());
                        completionCommunesPresenteur.setValeur(informationsRecherche.getCommune());
                        vue.chargerValeursRecherchePrecedente(informationsRecherche);
                }
                lancerRecherche();
        }

        public abstract void lancerRecherche();
}