Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.modeles.collection;

import java.util.HashMap;

import org.tela_botanica.client.Mediateur;
import org.tela_botanica.client.RegistreId;
import org.tela_botanica.client.http.JsonRestRequestBuilder;
import org.tela_botanica.client.http.JsonRestRequestCallback;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.Information;
import org.tela_botanica.client.modeles.projet.ProjetListe;
import org.tela_botanica.client.synchronisation.Reponse;
import org.tela_botanica.client.util.Debug;
import org.tela_botanica.client.util.UtilDAO;

import com.extjs.gxt.ui.client.Registry;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class CollectionAsyncDao {
        
        public static final String SERVICE_NOM = "CoelCollection";
        private String utilisateurId = null;
        private Rafraichissable vueARafraichir = null;
        
        public CollectionAsyncDao(Rafraichissable vueARafraichirCourrante) {
                if (Mediateur.DEBUG) System.out.println("|| CollectionAsyncDao > vueARafraichir = "+vueARafraichirCourrante.getClass().toString());
                vueARafraichir = vueARafraichirCourrante;
                utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
        }
        
        /**
         * 
         * @param paginationProgressive : définit le mode de consultation de la base de données
         *                      - True :        la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
         *                                              fur et à mesure de la consultation des données par l'utilisateur.
         *                      - False :       la consultation des données est classique : un seul appel à la base de données est effectué, le retour
         *                                              est renvoyé à l'appelant
         *                      // FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
         */
        public void selectionner(final boolean paginationProgressive, final String projetId, final String collectionId, final String nomCollection, final int start, final int nbElements, final Integer seqId) {

                // Ajout des paramètres et données à selectionner dans l'URL
                String nom = (nomCollection == null) ? "%" : nomCollection+"%";
                String[] parametres = {projetId, collectionId, nom};
                
                HashMap<String, String> restrictions = new HashMap<String, String>();
                
                if (nbElements != -1)   {
                        restrictions.put("limit", String.valueOf(nbElements));
                }

                /** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
                if (paginationProgressive) {

                        /** DEFINITION DU TUPLE DE DEPART **/
                        restrictions.put("start", String.valueOf(start));
                        
                        /** CONSTRUCTION DE LA REQUETE **/
                final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);

                /** ENVOI DE LA REQUETE **/
                rb.envoyerRequete(null, new JsonRestRequestCallback()
                {
                        /** RECEPTION DE LA REPONSE **/
                        public void surReponse(JSONValue responseValue)
                        {
                                /** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
                                 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/

                                        if (seqId != null)      {
                                                if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour au sequenceur");
                                                Reponse reponseRequete = new Reponse(responseValue, seqId);
                                                vueARafraichir.rafraichir(reponseRequete);
                                        }
                                        else    {
                                                if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste paginée, retour à "+vueARafraichir.getClass().toString());
                                                vueARafraichir.rafraichir(responseValue);
                                        }
                        }
                });
                }
                /** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
                else {
                        
                        /** DEFINITION DU TUPLE DE DEPART **/
                        restrictions.put("start", String.valueOf(start*nbElements));
                        
                        final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
                        
                        rb.envoyerRequete(null, new JsonRestRequestCallback() {
                                @Override
                                public void surReponse(JSONValue responseValue) {
                                        if (responseValue != null) {

                                                JSONObject responseObject = responseValue.isObject();

                                                if (responseObject != null) {
                                                        
                                                        // Si la réponse est un tableau, alors c'est une liste de collections qui a été retournée
                                                        if (responseObject.get("collections").isArray() != null) {
                                                                final JSONArray reponse = responseObject.get("collections").isArray();
                                                                CollectionListe collections = new CollectionListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
                                                                collections.setTaillePage(nbElements);
                                                                collections.setPageCourante(start);                                                     
                                                                
                                                                vueARafraichir.rafraichir(collections);
                                                                
                                                        // Si la réponse est un objet, alors c'est une unique collection qui a été retournée
                                                        } else if (responseObject.get("collections").isObject() != null) {
                                                                final JSONObject reponse = responseObject.get("collections").isObject();
                                                                Collection collection = new Collection(reponse);
                                                                CollectionBotanique collectionBotanique = new CollectionBotanique(reponse);
                                                                collection.setBotanique(collectionBotanique);
                                                                
                                                                Information info = new Information("selection_collection");
                                                                info.setDonnee(0, collection);
                                                                
                                                                // et on met à jour le demandeur des données
                                                                if (seqId != null)      {
                                                                        if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
                                                                        Reponse reponseRequete = new Reponse(info, seqId);
                                                                        vueARafraichir.rafraichir(reponseRequete);
                                                                }
                                                                else    {
                                                                        if (Mediateur.DEBUG) System.out.println("<-- CollectionAsyncDao > Liste non paginée, retour au sequenceur");
                                                                        vueARafraichir.rafraichir(info);
                                                                }
                                                        }
                                                } else {
                                                        GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
                                                }
                                        } else {
                                                // Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
                                                if (collectionId == null) {
                                                        CollectionListe collections = new CollectionListe(0);
                                                        vueARafraichir.rafraichir(collections);                                                         
                                                }
                                        }
                                }
                        });
                }
        }
        
        public void ajouter(Collection collection) {            
                String postDonneesEncodees = construirePost(null, collection);
                final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);   
                rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
                        @Override
                        public void surReponse(JSONValue responseValue) {
                                if (responseValue.isString() != null) {
                                        Information info = new Information("ajout_collection");
                                        String structureIdOuMessage = responseValue.isString().stringValue();
                                        if (structureIdOuMessage.matches("^[0-9]+$")) {
                                                info.setDonnee(structureIdOuMessage);
                                        } else {
                                                info.setMessage(structureIdOuMessage);
                                        }
                                        vueARafraichir.rafraichir(info);
                                } else {
                                        GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
                                }
                        }
                });
        }
        
        public void modifier(Collection collection) {
                String postDonneesEncodees = construirePost(collection.getId(), collection);
                String[] parametres = {collection.getId()};
                final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
                rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
                        @Override
                        public void surReponse(JSONValue responseValue) {
                                // Si la requête est un succès, reception d'une chaine
                                if (responseValue.isString() != null) {
                                        Information info = new Information("modif_collection");
                                        info.setMessage(responseValue.isString().stringValue());
                                        vueARafraichir.rafraichir(info);
                                } else {
                                        GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
                                }
                        }
                });
        }
        
        public void supprimer(String collectionsId) {
                String[] parametres = {utilisateurId, collectionsId};
                final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
                rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
                        @Override
                        public void surReponse(JSONValue responseValue) {
                                if (responseValue.isString() != null) {
                                        Information info = new Information("suppression_collection");
                                        info.setMessage(responseValue.isString().stringValue());
                                        vueARafraichir.rafraichir(info);
                                } else {
                                        GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
                                }
                        }
                });
        }
        
        private String construirePost(String collectionId, Collection collection) {             
                String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId); 
                        
                if (collection != null) {
                        if (collectionId != null) {
                                postDonnees += "&cc_id_collection=" + URL.encodeComponent(collectionId);
                        }

                        /*
                        postDonnees += "&cpr_abreviation=" + URL.encodeComponent(((ProjetListe) Registry.get(RegistreId.PROJETS)).get(collection.getIdProjet()).getAbreviation());
                        postDonnees += "&cc_ce_projet=" + URL.encodeComponent(collection.getIdProjet()) +
                                "&cc_ce_mere=" + URL.encodeComponent(collection.getCollectionMereId()) +
                                "&cc_ce_structure=" + URL.encodeComponent(collection.getIdStructure()) +
                                "&cc_truk_identifiant_alternatif=" + URL.encodeComponent(collection.getIdAlternatif()) +
                                "&cc_truk_code=" + URL.encodeComponent(collection.getCode()) +
                                "&cc_nom=" + URL.encodeComponent(collection.getNom()) +
                                "&cc_truk_nom_alternatif=" + URL.encodeComponent(collection.getNomAlternatif()) +
                                "&cc_description=" + URL.encodeComponent(collection.getDescription()) +
                                "&cc_description_specialiste=" + URL.encodeComponent(collection.getDescriptionSpecialiste()) +
                                "&cc_historique=" + URL.encodeComponent(collection.getHistorique()) +
                                "&cc_truk_url=" + URL.encodeComponent(collection.getUrls()) +
                                "&cc_truk_groupement_principe=" + URL.encodeComponent(collection.getGroupementPrincipe()) +
                                "&cc_truk_groupement_but=" + URL.encodeComponent(collection.getGroupementBut()) +
                                "&cc_ce_type=" + URL.encodeComponent(collection.getTypeNcd()) +
                                "&cc_ce_type_depot=" + URL.encodeComponent(collection.getTypeDepot()) +
                                "&cc_cote=" + URL.encodeComponent(collection.getCote()) +
                                "&cc_truk_periode_constitution=" + URL.encodeComponent(collection.getPeriodeConstitution()) +
                                "&cc_truk_couverture_lieu=" + URL.encodeComponent(collection.getCouvertureLieu()) +
                                "&cc_ce_specimen_type=" + URL.encodeComponent(collection.getSpecimenType()) +
                                "&cc_specimen_type_nbre=" + URL.encodeComponent(collection.getSpecimenTypeNbre()) +
                                "&cc_ce_specimen_type_nbre_precision=" + URL.encodeComponent(collection.getSpecimenTypeNbrePrecision()) +
                                "&cc_ce_specimen_type_classement=" + URL.encodeComponent(collection.getSpecimenTypeClassement());*/
                        postDonnees += "&" + collection.obtenirChainePOST();
                }
                
                if (collection.getBotanique() != null) {
                        if (collectionId != null) {
                                postDonnees += "&ccb_id_collection=" + URL.encodeComponent(collectionId);
                        }
                        CollectionBotanique collectionBotanique = collection.getBotanique();
                        /*postDonnees += "&ccb_nbre_echantillon=" + URL.encodeComponent(collectionBotanique.getNbreEchantillon()) +
                                "&ccb_ce_truk_type=" + URL.encodeComponent(collectionBotanique.getType()) +
                                "&ccb_truk_unite_rangement=" + URL.encodeComponent(collectionBotanique.getUniteRangement()) +
                                "&ccb_ce_unite_rangement_etat=" + URL.encodeComponent(collectionBotanique.getUniteRangementEtat()) +
                                "&ccb_truk_unite_base=" + URL.encodeComponent(collectionBotanique.getUniteBase()) +
                                "&ccb_truk_conservation_papier_type=" + URL.encodeComponent(collectionBotanique.getConservationPapierType()) +
                                "&ccb_truk_conservation_methode=" + URL.encodeComponent(collectionBotanique.getConservationMethode()) +
                                "&ccb_specimen_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationPourcent()) +
                                "&ccb_etiquette_fixation_pourcent=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationPourcent()) +
                                "&ccb_truk_specimen_fixation_methode=" + URL.encodeComponent(collectionBotanique.getSpecimenFixationMethode()) +
                                "&ccb_truk_etiquette_fixation_support=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSupport()) +
                                "&ccb_truk_etiquette_fixation_specimen=" + URL.encodeComponent(collectionBotanique.getEtiquetteFixationSpecimen()) +
                                "&ccb_truk_etiquette_ecriture=" + URL.encodeComponent(collectionBotanique.getEtiquetteEcriture()) +
                                "&ccb_ce_traitement=" + URL.encodeComponent(collectionBotanique.getTraitement()) +
                                "&ccb_truk_traitement_poison=" + URL.encodeComponent(collectionBotanique.getTraitementPoison()) +
                                "&ccb_truk_traitement_insecte=" + URL.encodeComponent(collectionBotanique.getTraitementInsecte()) +
                                "&ccb_ce_etat_general=" + URL.encodeComponent(collectionBotanique.getEtatGeneral()) +
                                "&ccb_truk_degradation_specimen=" + URL.encodeComponent(collectionBotanique.getDegradationSpecimen()) +
                                "&ccb_truk_degradation_presentation=" + URL.encodeComponent(collectionBotanique.getDegradationPresentation()) +
                                "&ccb_ce_determination=" + URL.encodeComponent(collectionBotanique.getDetermination()) +
                                "&ccb_truk_nature=" + URL.encodeComponent(collectionBotanique.getNature()) +
                                "&ccb_specialite=" + URL.encodeComponent(collectionBotanique.getSpecialite()) +
                                "&ccb_recolte_date_debut=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebut()) +
                                "&ccb_ce_recolte_date_debut_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateDebutType()) +
                                "&ccb_recolte_date_fin=" + URL.encodeComponent(collectionBotanique.getRecolteDateFin()) +
                                "&ccb_ce_recolte_date_fin_type=" + URL.encodeComponent(collectionBotanique.getRecolteDateFinType()) +
                                "&ccb_annotation_classement=" + URL.encodeComponent(collectionBotanique.getClassementAnnotation()) +
                                "&ccb_ce_classement_etat=" + URL.encodeComponent(collectionBotanique.getClassementEtat()) +
                                "&ccb_truk_etiquette_renseignement=" + URL.encodeComponent(collectionBotanique.getEtiquetteRenseignement()) +
                                "&ccb_ce_precision_localite=" + URL.encodeComponent(collectionBotanique.getPrecisionLocalite()) +
                                "&ccb_ce_precision_date=" + URL.encodeComponent(collectionBotanique.getPrecisionDate()) +
                                "&ccb_annotation_diverse=" + URL.encodeComponent(collectionBotanique.getAnnotationsDiverses()) +
                                "&ccb_ce_collection_integre=" + URL.encodeComponent(collectionBotanique.getCollectionIntegre()) +
                                "&ccb_ce_collection_integre_info=" + URL.encodeComponent(collectionBotanique.getCollectionIntegreInfo()) +
                                "&ccb_ce_inventaire=" + URL.encodeComponent(collectionBotanique.getInventaire()) +
                                "&ccb_ce_inventaire_auteur=" + URL.encodeComponent(collectionBotanique.getInventaireAuteur()) +
                                "&ccb_ce_inventaire_forme=" + URL.encodeComponent(collectionBotanique.getInventaireForme()) +
                                "&ccb_inventaire_info=" + URL.encodeComponent(collectionBotanique.getInventaireInfo()) +
                                "&ccb_ce_truk_inventaire_digital=" + URL.encodeComponent(collectionBotanique.getInventaireDigital()) +
                                "&ccb_inventaire_digital_pourcent=" + URL.encodeComponent(collectionBotanique.getInventaireDigitalPourcent()) +
                                "&ccb_ce_inventaire_etat=" + URL.encodeComponent(collectionBotanique.getInventaireEtat()) +
                                "&ccb_inventaire_donnee_type=" + URL.encodeComponent(collectionBotanique.getInventaireDonneesTypes());*/
                        postDonnees += "&" + collectionBotanique.obtenirChainePOST();
                }
                
                return postDonnees;
        }
}