Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.modeles.structure;

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.personne.PersonneListe;
import org.tela_botanica.client.modeles.projet.ProjetListe;
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.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class StructureAsyncDao {
        private static final String SERVICE_NOM = "CoelStructure";
        
        private String utilisateurId = null;
        private Rafraichissable vueARafraichir = null;
        
        public StructureAsyncDao(Rafraichissable vue) {
                vueARafraichir = vue;
                utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
                GWT.log("ID utilisateur :"+utilisateurId, null);
        }
        
        public void selectionner(final String projetId, final String structureId, final String nomStructure, final int pageCourante, final int nbElements) {
                String[] parametres = {projetId, structureId, nomStructure};
                
                HashMap<String, String> restrictions = new HashMap<String, String>();
                restrictions.put("start", String.valueOf(pageCourante*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) {
                                        Information info = new Information("selection_structure");
                                        // 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();
                                                Structure structure = new Structure(reponse);
                                                StructureConservation structureConservation = new StructureConservation(reponse);
                                                StructureValorisation structureValorisation = new StructureValorisation(reponse);
                                                info.setDonnee(0, structure);
                                                info.setDonnee(1, structureConservation);
                                                info.setDonnee(2, structureValorisation);
                                                vueARafraichir.rafraichir(info);
                                        } else if (responseArray.get(1).isArray() != null) {
                                                final JSONArray reponse = responseValue.isArray();
                                                StructureListe structures; // = new StructureListe(reponse);
                                                if (reponse.get(1).isObject() != null)  {
                                                        structures = new StructureListe(reponse.get(1).isArray());
                                                } else  {
                                                        structures = new StructureListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
                                                }
                                                structures.setTaillePage(nbElements);
                                                structures.setPageCourante(pageCourante);                                                       
                                                info.setDonnee(0, structures);
                                                
                                                vueARafraichir.rafraichir(structures);
                                        } else {
                                                GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
                                        }
                                } else {
                                        if (structureId == null) {
                                                // Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet vide
                                                StructureListe structures = new StructureListe(0);
                                                vueARafraichir.rafraichir(structures);                                                          
                                        }
                                }
                        }
                });
        }
        
        public void ajouter(final Structure str, StructureConservation conservation, StructureValorisation valorisation) {
                String postDonneesEncodees = construirePost(null, str, conservation, valorisation);

                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_structure");
                                        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(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
                String postDonneesEncodees = construirePost(structureId, str, conservation, valorisation);
                String[] parametres = {structureId};
                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_structure");
                                        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 structuresId) {
                String[] parametres = {utilisateurId, structuresId};
                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_structure");
                                        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 structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {
                String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId); 
                        
                if (str != null) {
                        if (structureId != null) {
                                postDonnees += "&cs_id_structure=" + URL.encodeComponent(structureId);
                        }
                        postDonnees += "&cpr_abreviation=" + URL.encodeComponent(((ProjetListe) Registry.get(RegistreId.PROJETS)).get(str.getIdProjet()).getAbreviation());
                        postDonnees += "&cs_ce_projet=" + URL.encodeComponent(str.getIdProjet()) +
                                "&cs_ce_mere=" + URL.encodeComponent(str.getIdMere()) +
                                "&cs_guid=" + URL.encodeComponent(str.getGuid()) +
                                "&cs_truk_identifiant_alternatif=" + URL.encodeComponent(str.getIdAlternatif()) +
                                "&cs_nom=" + URL.encodeComponent(str.getNom()) +
                                "&cs_truk_nom_alternatif=" + URL.encodeComponent(str.getNomAlternatif()) +
                                "&cs_ce_type=" + URL.encodeComponent(str.getType()) +
                                "&cs_ce_truk_type_prive=" + URL.encodeComponent(str.getTypePrive()) +
                                "&cs_ce_truk_type_public=" + URL.encodeComponent(str.getTypePublic()) +
                                "&cs_adresse_01=" + URL.encodeComponent(str.getAdresse()) +
                                "&cs_adresse_02=" + URL.encodeComponent(str.getAdresseComplement()) +
                                "&cs_date_fondation=" + URL.encodeComponent(str.getDateFondationFormatMysql()) +
                                "&cs_code_postal=" + URL.encodeComponent(str.getCodePostal()) +
                                "&cs_ville=" + URL.encodeComponent(str.getVille()) +
                                "&cs_ce_truk_region=" + URL.encodeComponent(str.get("ce_truk_region").toString()) +
                                "&cs_ce_truk_pays=" + URL.encodeComponent(str.getPays()) +
                                "&cs_truk_telephone=" + URL.encodeComponent(str.getTelephone()) +
                                "&cs_truk_url=" + URL.encodeComponent(str.getUrl()) +
                                "&cs_nbre_personne=" + URL.encodeComponent(Integer.toString(str.getNbrePersonne())) + 
                                "&cs_courriel=" + URL.encodeComponent(str.getCourriel());
                }
                if (conservation != null) {
                        if (structureId != null) {
                                postDonnees += "&csc_id_structure=" + URL.encodeComponent(structureId);
                        }
                        postDonnees += "&csc_mark_formation=" + URL.encodeComponent(conservation.getFormation()) +
                                "&csc_formation=" + URL.encodeComponent(conservation.getFormationInfo()) +
                                "&csc_mark_formation_interet=" + URL.encodeComponent(conservation.getFormationInteret()) +
                                "&csc_truk_stockage_local=" + URL.encodeComponent(conservation.getStockageLocal()) +
                                "&csc_truk_stockage_meuble=" + URL.encodeComponent(conservation.getStockageMeuble()) +
                                "&csc_truk_stockage_parametre=" + URL.encodeComponent(conservation.getStockageParametre()) +
                                "&csc_mark_collection_commune=" + URL.encodeComponent(conservation.getCollectionCommune()) +
                                "&csc_truk_collection_autre=" + URL.encodeComponent(conservation.getCollectionAutre()) +
                                "&csc_mark_acces_controle=" + URL.encodeComponent(conservation.getAccesControle()) +
                                "&csc_mark_restauration=" + URL.encodeComponent(conservation.getRestauration()) +
                                "&csc_truk_restauration_operation=" + URL.encodeComponent(conservation.getRestaurationOperation()) +
                                "&csc_ce_materiel_conservation=" + URL.encodeComponent(conservation.getMaterielConservation()) +
                                "&csc_truk_materiel_autre=" + URL.encodeComponent(conservation.getMaterielAutre()) +
                                "&csc_mark_traitement=" + URL.encodeComponent(conservation.getTraitement()) +
                                "&csc_truk_traitement=" + URL.encodeComponent(conservation.getTraitements()) +
                                "&csc_mark_acquisition_collection=" + URL.encodeComponent(conservation.getAcquisitionCollection()) +
                                "&csc_mark_acquisition_echantillon=" + URL.encodeComponent(conservation.getAcquisitionEchantillon()) +
                                "&csc_mark_acquisition_traitement=" + URL.encodeComponent(conservation.getAcquisitionTraitement()) +
                                "&csc_truk_acquisition_traitement_poison=" + URL.encodeComponent(conservation.getAcquisitionTraitementPoison()) +
                                "&csc_truk_acquisition_traitement_insecte=" + URL.encodeComponent(conservation.getAcquisitionTraitementInsecte());
                }
                if (valorisation != null) {
                        if (structureId != null) {
                                postDonnees += "&csv_id_structure=" + URL.encodeComponent(structureId);
                        }
                        postDonnees += "&csv_mark_action=" + URL.encodeComponent(valorisation.getAction()) +
                                "&csv_truk_action=" + URL.encodeComponent(valorisation.getActionInfo()) +
                                "&csv_publication=" + URL.encodeComponent(valorisation.getPublication()) +
                                "&csv_collection_autre=" + URL.encodeComponent(valorisation.getCollectionAutre()) +
                                "&csv_mark_action_future=" + URL.encodeComponent(valorisation.getActionFuture()) +
                                "&csv_action_future=" + URL.encodeComponent(valorisation.getActionFutureInfo()) +
                                "&csv_mark_recherche=" + URL.encodeComponent(valorisation.getRecherche()) +
                                "&csv_truk_recherche_provenance=" + URL.encodeComponent(valorisation.getRechercheProvenance()) +
                                "&csv_truk_recherche_type=" + URL.encodeComponent(valorisation.getRechercheType()) +
                                "&csv_mark_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotif()) +
                                "&csv_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotifInfo()) +
                                "&csv_mark_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotif()) +
                                "&csv_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotifInfo());
                }
                return postDonnees;
        }
}