Subversion Repositories eFlore/Applications.del

Rev

Rev 2054 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.del.client.cache;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.tela_botanica.del.client.config.Config;
import org.tela_botanica.del.client.gestionhistorique.ConstantesNavigation;
import org.tela_botanica.del.client.modeles.Image;
import org.tela_botanica.del.client.modeles.InformationsRecherche;
import org.tela_botanica.del.client.modeles.ModeTri;
import org.tela_botanica.del.client.modeles.Observation;
import org.tela_botanica.del.client.modeles.Protocole;
import org.tela_botanica.del.client.modeles.Utilisateur;
import org.tela_botanica.del.client.utils.URLUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;

public class CacheClient {

        private List<Protocole> listeProtocoles;
        
        private Observation observationCourante;
        private Image imageCourante;
        private String taxonPourRechercheEflore;
        private String idProtocoleEnAttente = null;
        private Protocole protocoleCourant;
        private String referentielCourant;
        private int numPageRechercheImage = 0;
        private int nbTotalImagesRecherchees = 0;
        
        private boolean referentielNonModifiable = false;

        private int pasPagination = 12;
        private int pageCouranteRechercheImage = 1;
        private int pageCouranteRechercheObservations = 1;
        private InformationsRecherche informationsRechercheImage;
        private InformationsRecherche informationsRechercheObservation;
        private Utilisateur utilisateur;
        private String pageCourante = "";
        private String statut;
        private String[] statutsPossibles = {
                ConstantesNavigation.PARAM_TYPE_TOUS, 
                ConstantesNavigation.PARAM_TYPE_A_DETERMINER,
                ConstantesNavigation.PARAM_TYPE_A_CONFIRMER,
                ConstantesNavigation.PARAM_TYPE_VALIDEES,
                ConstantesNavigation.PARAM_TYPE_MONACTIVITE
        };
        private ModeTri modeTri = ModeTri.TRI_ASCENDANT;
        
        private static String titreOngletNavigateur = "";
        
        // Pour plus de simplicité on stocke une liste de pays où
        // la clé est le code iso et la valeur le pays
        // et une autre ou c'est l'inverse
        private Map<String, String> listePays;
        private Map<String, String> listePaysInversee;
        
        public void supprimerFiltreStatut() {
                this.statut = null;
        }
        
        public void setFiltreStatut(String statut) {
                this.statut = statut;
        }
        
        public String getFiltreStatut() {
                return this.statut;
        }
        
        public String getPageCourante() {
                return pageCourante;
        }

        public void setPageCourante(String pageCourante) {
                pageCourante = pageCourante.replaceAll("#", "");
                pageCourante = pageCourante.replaceAll("page_validation", ConstantesNavigation.PAGE_DETAIL_OBS);
                pageCourante = pageCourante.replaceAll("page_validation_pictoflora", ConstantesNavigation.PAGE_DETAIL_IMG);
                this.pageCourante = pageCourante;
        }

        private static CacheClient instance;

        private CacheClient() {
                instance = this;
        }

        public void initialiserAvecParametres() {
                String urlCourante = Window.Location.getHref();
                Config config = new Config();
                String urlAppliImg = config.getUrl("pictoflora");
                
                setPageCourante(Location.getHash());
                
                String rechercheLibre = Location.getParameter("masque");
                String famille = Location.getParameter("masque.famille");
                String taxon = Location.getParameter("masque.ns");
                String genre = Location.getParameter("masque.genre");
                String commune = Location.getParameter("masque.commune");
                String dept = Location.getParameter("masque.departement");
                String auteur = Location.getParameter("masque.auteur");
                String date = Location.getParameter("masque.date");
                String tag = Location.getParameter("masque.tag");
                String tagCel = Location.getParameter("masque.tag_cel");
                String tagDel = Location.getParameter("masque.tag_pictoflora");
                String referentiel = Location.getParameter("masque.referentiel");
                String protocole = Location.getParameter("protocole");
                String statutParam = Location.getParameter("masque.type");
                String paysParam = Location.getParameter("masque.pays");
                
                String page = Location.getParameter("page");
                Integer pageInt = null;
                try {
                        pageInt = Integer.parseInt(page);
                } catch (Exception e) {
                        pageInt = null;
                }
                
                String pas = Location.getParameter("pas");      
                Integer pasInt = null;
                if (pas != null) {
                        try {
                                pasInt = Integer.parseInt(pas);
                        } catch (Exception e) {
                                pasInt = null;
                        }
                }
                
                InformationsRecherche rechercheParArguments = new InformationsRecherche();
                rechercheParArguments.setRechercheLibre(rechercheLibre);
                rechercheParArguments.setFamille(famille);
                rechercheParArguments.setTaxon(taxon);
                rechercheParArguments.setGenre(genre);
                rechercheParArguments.setCommune(commune);
                rechercheParArguments.setDepartement(dept);
                rechercheParArguments.setAuteur(auteur);
                rechercheParArguments.setDate(date);
                rechercheParArguments.setTag(tag);
                rechercheParArguments.setPays(paysParam);
                
                if (protocole != null && !protocole.equals("")) {
                        CacheClient.getInstance().setIdProtocoleEnAttente(protocole);
                        rechercheParArguments.setIdProtocoleSelectionne(protocole);
                } else if (URLUtils.getURLSpecialParameterValue() != null) {
                        CacheClient.getInstance().setIdProtocoleEnAttente(URLUtils.getURLSpecialParameterValue());
                        rechercheParArguments.setIdProtocoleSelectionne(URLUtils.getURLSpecialParameterValue());
                }
                
                if (pasInt != null) {
                        setPasPagination(pasInt);
                }
                
                if (urlCourante.contains(urlAppliImg)) {
                        rechercheParArguments.setMotClefCel(tagCel);
                        rechercheParArguments.setMotClefDel(tagDel);
                        if (pageInt != null) {
                                setPageCouranteRechercheImages(pageInt);
                        }
                } else {
                        if (pageInt != null) {
                                setPageCouranteRechercheObservations(pageInt);
                        }
                }
                
                definirOrdreTriCourantParUrl();
                String tri = Location.getParameter("tri");
                if (urlCourante.contains(urlAppliImg)) {
                        if (tri != null) {
                                // Pour PictoFlora
                                if (tri.equals("moyenne-arithmetique")) {
                                        rechercheParArguments.setTriParMoyenneArithmetique(modeTri);
                                } else if (tri.equals("points")) {
                                        rechercheParArguments.setTriParNbPoints(modeTri);
                                } else if (tri.equals("tags")) {
                                        rechercheParArguments.setTriParNbTags(modeTri);
                                } else {
                                        rechercheParArguments.setTriParDatePublication(modeTri);
                                }
                        } else {
                                // Tri par défaut pour PictoFlora
                                rechercheParArguments.setTriParDatePublication(ModeTri.TRI_DESCENDANT);
                        }
                } else {
                        // Pour IdentiPlante
                        if (tri != null && tri.equals("date_observation")) {
                                rechercheParArguments.setTriParDateObservation(modeTri);
                        } else if(tri != null && tri.equals("nb_commentaires")) {
                                rechercheParArguments.setTriParNbCommentaires(modeTri);
                        } else {
                                // Tri par défaut pour IdentiPlante
                                rechercheParArguments.setTriParDatePublication(ModeTri.TRI_DESCENDANT);
                        }
                }
                
                rechercheParArguments.setMotClef(tag);
                
                // si le référentiel est passé dans l'url alors il ne doit pas être modifiable
                if (referentiel != null && !referentiel.equals("")) {
                        setReferentielCourant(referentiel);
                        rechercheParArguments.setReferentiel(referentiel);
                        referentielNonModifiable = true;
                }
                
                if (Location.getParameterMap().size() == 0) {
                        // par défaut l'application s'ouvre sur l'onglet à déterminer
                        statut = ConstantesNavigation.PARAM_TYPE_DEFAUT;
                } else {
                        if (Arrays.asList(statutsPossibles).contains(statutParam)) {
                                statut = statutParam;
                        } else {
                                statut = ConstantesNavigation.PARAM_TYPE_DEFAUT;
                        }
                }
                
                informationsRechercheImage = rechercheParArguments;
                informationsRechercheObservation = rechercheParArguments;
                
                mettreAjourUrlCourante();
        }
        
        private void definirOrdreTriCourantParUrl() {
                String ordre = Location.getParameter("ordre");
                if (ordre != null) {
                        if (ordre.equals("asc")) {
                                this.modeTri = ModeTri.TRI_ASCENDANT;
                        } else if (ordre.equals("desc")) {
                                this.modeTri = ModeTri.TRI_DESCENDANT;
                        }
                }
        }
        
        public void setIdProtocoleEnAttente(String idProtocole) {
                idProtocoleEnAttente = idProtocole;
        }
        
        public String getIdProtocoleEnAttente() {
                return idProtocoleEnAttente;
        }

        public Utilisateur getUtilisateur() {
                if (this.utilisateur == null) {
                        this.utilisateur = new Utilisateur(null, null);
                }
                return this.utilisateur;
        }
        
        public void setUtilisateur(Utilisateur utilisateur) {
                this.utilisateur = utilisateur; 
        }
        
        public void setHome(String home) {
                setPageCourante(home);
        }
        
        public String getHome() {
                return "";
        }
        
        public Observation getObservationCourante() {
                return observationCourante;
        }

        public void setObservationCourante(Observation observationCourante) {
                this.observationCourante = observationCourante;
        }

        public static CacheClient getInstance() {
                if (instance == null) {
                        instance = new CacheClient();
                }
                return instance;
        }

        public String getTaxonPourRechercheEflore() {
                return taxonPourRechercheEflore;
        }

        public void setTaxonPourRechercheEflore(String taxonPourRechercheEflore) {
                this.taxonPourRechercheEflore = taxonPourRechercheEflore;
        }

        public int getNumPageRechercheImage() {
                return numPageRechercheImage;
        }

        // Pour la recherche :
        public int getPasPagination() {
                return pasPagination;
        }

        public void setPasPagination(int pasPagination) {
                this.pasPagination = pasPagination;
        }

        public void setPageCouranteRechercheImages(int pageCouranteRecherche) {
                this.pageCouranteRechercheImage = pageCouranteRecherche;
        }

        public int getPageCouranteRechercheImage() {
                return this.pageCouranteRechercheImage;
        }

        public void setNbTotalImagesRecherchees(int nbTotalImagesRecherchees) {
                this.nbTotalImagesRecherchees = nbTotalImagesRecherchees;
        }

        public int getNbTotalImagesRecherchees() {
                return this.nbTotalImagesRecherchees;
        }

        public Image getImageCourante() {
                return imageCourante;
        }

        public void setImageCourante(Image imageCourante) {
                this.imageCourante = imageCourante;
        }
        
        public void setListeProtocoles(List<Protocole> listeProtocole) {
                this.listeProtocoles = listeProtocole;
        }
        
        public List<Protocole> getListeProtocoles() {
                return this.listeProtocoles;
        }

        public Protocole getProtocoleCourant() {
                return protocoleCourant;
        }

        public void setProtocoleCourant(Protocole protocoleCourant) {
                this.protocoleCourant = protocoleCourant;
        }

        public InformationsRecherche getInformationsRechercheImage() {
                if (informationsRechercheImage == null) {
                        informationsRechercheImage = new InformationsRecherche();
                }
                return informationsRechercheImage;
        }

        public InformationsRecherche getInformationsRechercheObservation() {
                if (informationsRechercheObservation == null) {
                        informationsRechercheObservation = new InformationsRecherche();
                }
                return informationsRechercheObservation;
        }

        public void setInformationsRechercheImage(InformationsRecherche informationsRechercheImage) {
                this.informationsRechercheImage = informationsRechercheImage;
        }

        public void setInformationsRechercheObservation(InformationsRecherche informationsRechercheObservation) {
                this.informationsRechercheObservation = informationsRechercheObservation;
        }

        public int getPageCouranteRechercheObservations() {
                return pageCouranteRechercheObservations;
        }

        public void setPageCouranteRechercheObservations(int pageCouranteRechercheObservations) {
                this.pageCouranteRechercheObservations = pageCouranteRechercheObservations;
        }
        
        public void setReferentielCourant(String referentielCourant) {
                this.referentielCourant = referentielCourant;
        }
        
        public String getReferentielCourant() {
                return referentielCourant;
        }
        
        public void setReferentielNonModifiable(boolean referentielNonModifiable) {
                this.referentielNonModifiable = referentielNonModifiable;
        }
        
        public boolean getReferentielNonModifiable() {
                return referentielNonModifiable;
        }
        
        public String getUrlPageSignalerMauvaiseId(String id) {
                Config config = new Config();
                String urlSignalerMauvaiseId = config.getUrl("identiplante");
                if (!GWT.isScript()) {
                        urlSignalerMauvaiseId += "?gwt.codesvr="+Location.getParameter("gwt.codesvr")+"";
                }
                return urlSignalerMauvaiseId+"#"+ConstantesNavigation.PAGE_DETAIL_OBS+"~"+id;
                
        }
        
        public void setListePays(Map<String, String> listePays) {
                this.listePays = listePays;
                listePaysInversee = new HashMap<String, String>();
                for(Map.Entry<String, String> entry : listePays.entrySet()){
                        listePaysInversee.put(entry.getValue(), entry.getKey());
                }
        }
        
        public Map<String, String> getListePays() {
                return listePays;
        }
        
        public Map<String, String> getListePaysInversee() {
                return listePaysInversee;
        }
        
        public String genererUrlCourante() {
                String urlCourante = Window.Location.getHref();
                Config config = new Config();
                String urlAppliObs = config.getUrl("identiplante");
                String urlAppliImg = config.getUrl("pictoflora");
                String url = urlCourante;
                String arguments = "";
                
                if (urlCourante.contains(urlAppliImg)) {
                        InformationsRecherche infoRecherche;
                        infoRecherche = getInformationsRechercheImage();
                        
                        if (CacheClient.getInstance().getProtocoleCourant() != null) {
                                infoRecherche.setIdProtocoleSelectionne("" + CacheClient.getInstance().getProtocoleCourant().getId());
                        }
                        
                        arguments = infoRecherche.versChaineRequete();
                        arguments += (arguments.isEmpty()) ? "" : "&";
                        arguments += "page="+getPageCouranteRechercheImage()+"&pas="+getPasPagination();

                        url = config.getUrl("pictoflora");
                } else if (urlCourante.contains(urlAppliObs)) {
                        InformationsRecherche infoRecherche;
                        infoRecherche = getInformationsRechercheObservation();
                        
                        arguments = "masque.type="+statut;
                        arguments += "&page="+getPageCouranteRechercheObservations()+"&pas="+getPasPagination();
                        arguments += "&"+infoRecherche.versChaineRequete();
                        
                        url = config.getUrl("identiplante");
                }
                
                if (!GWT.isScript()) {
                        arguments += "&gwt.codesvr="+Location.getParameter("gwt.codesvr")+"";
                }
                arguments = (arguments.isEmpty()) ? "" : "?" + arguments;
                String signet = getPageCourante().equals("") ? "" : "#" + getPageCourante();
                url += arguments + signet;
                
                // remplacements batards pour corriger l'url
                // TODO: factoriser toute la fonction 
                url = url.replaceAll("&#", "#");
                url = url.replaceAll("&&", "&");
                url = url.replaceAll("\\?&", "?");
                if (url.endsWith("&")) {
                        url = url.substring(0, url.length()-1);
                }
                
                return url;
        }
        
        public void mettreAjourUrlCourante() {
                mettreAJourUrlCouranteSansRecharger(genererUrlCourante());
        }
        
        private static native void mettreAJourUrlCouranteSansRecharger(String nouvelleUrl) /*-{
                // javascript double negative trick
                // pour plus d'info http://stackoverflow.com/questions/4686583/can-someone-explain-this-double-negative-trick
                if (!!($wnd.history && $wnd.history.pushState)) {
                        var currentState = $wnd.history.state;
                        if (currentState != nouvelleUrl) {
                                $wnd.history.pushState(nouvelleUrl, "", nouvelleUrl);
                        }
                }
        }-*/;
        
        public static void remplacerTitreOngletNavigateur(String titre) {
            if (Document.get() != null) {
                titreOngletNavigateur = Document.get().getTitle();
                Document.get().setTitle(titre);
            }
        }
        
        public static void ajouterAuTitreOngletNavigateur(String titreSupplementaire) {
            if (Document.get() != null) {
                titreOngletNavigateur = titreOngletNavigateur.isEmpty() ? Document.get().getTitle() : titreOngletNavigateur;
                Document.get().setTitle(titreOngletNavigateur+titreSupplementaire);
            }
        }
        
        public static void reinitialiserTitreOngletNavigateur() {
            if (Document.get() != null) {
                titreOngletNavigateur = titreOngletNavigateur.isEmpty() ? Document.get().getTitle() : titreOngletNavigateur;
                Document.get().setTitle(titreOngletNavigateur);
            }
        }
}