Subversion Repositories eFlore/Applications.coel

Rev

Rev 1135 | 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.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) {
                vueARafraichir = vueARafraichirCourrante;
                utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
        }
        
        public void selectionner(final String projetId, final String collectionId, final String nomCollection, final int start, final int nbElements) {
                // Ajout des paramètres et données à selectionner dans l'URL
                String[] parametres = {projetId, collectionId, nomCollection};
                HashMap<String, String> restrictions = new HashMap<String, String>();
                restrictions.put("start", String.valueOf(start*nbElements));
                if (nbElements != -1)   {
                        restrictions.put("limit", String.valueOf(nbElements));
                }
                
                final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
                
                rb.envoyerRequete(null, new JsonRestRequestCallback() {
                        @Override
                        public void surReponse(JSONValue responseValue) {
                                if (responseValue != null) {
                                        // Si la requête est un succès, reception d'un objet ou d'un tableau
                                        JSONArray responseArray = responseValue.isArray();
                                        if (responseArray.get(1).isObject() != null) {
                                                final JSONObject reponse = responseArray.get(1).isObject();
                                                Collection collection = new Collection(reponse);
                                                CollectionBotanique collectionBotanique = new CollectionBotanique(reponse);
                                                collection.setBotanique(collectionBotanique);
                                                
                                                Information info = new Information("selection_collection");
                                                info.setDonnee(0, collection);
                                                vueARafraichir.rafraichir(info);
                                        } else if (responseValue.isArray() != null) {
                                                final JSONArray reponse = responseArray.get(1).isArray();
                                                CollectionListe collections = new CollectionListe(reponse, responseArray.get(0).isNumber(), vueARafraichir);
                                                collections.setTaillePage(nbElements);
                                                collections.setPageCourante(start);                                                     
                                                
                                                vueARafraichir.rafraichir(collections);
                                        } 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);
                        }
                        Debug.log("id projet:"+collection.getIdProjet());
                        Debug.log("liste projet:"+((ProjetListe) Registry.get(RegistreId.PROJETS)).get(collection.getIdProjet()).toString());
                        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());
                }
                
                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());
                }
                
                return postDonnees;
        }
}