Subversion Repositories eFlore/Applications.coel

Rev

Rev 1383 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles.collection;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.tela_botanica.client.modeles.aDonnee;
import org.tela_botanica.client.modeles.personne.Personne;
import org.tela_botanica.client.modeles.publication.Publication;
import org.tela_botanica.client.util.Debug;

import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.json.client.JSONObject;

public class CollectionAPublication extends aDonnee {
        
        private static final long serialVersionUID = 1L;
        
        public static final String PREFIXE = "ccapu";
        private boolean removePrefix = true;
        private Publication publicationLiee = null;
        public static String[] champsObligatoires = {"ccapu_id_collection", "ccapu_id_publication"};
        
        public CollectionAPublication() {
                initialiser(new JSONObject());
        }
        
        public CollectionAPublication(boolean removePrefix) {
                this.removePrefix = removePrefix;
                initialiser(new JSONObject());
        }
                        
        public CollectionAPublication(JSONObject collectionAPublicationListe) {
                initialiser(collectionAPublicationListe);
        }
        
        public CollectionAPublication(ModelData model, boolean removePrefix)
        {
                this.removePrefix = removePrefix;
                
                Map<String, Object> a = model.getProperties();
                
                Set<String> cles = a.keySet();
                Iterator<String> it = cles.iterator();
                while (it.hasNext()) {
                        String cle = it.next();
                        if (a.get(cle) != null) {
                                String cleObjet = "";
                                if (removePrefix) {
                                        cleObjet = cle.replaceFirst("^"+CollectionAPublication.PREFIXE+"_", "");
                                        cleObjet = cleObjet.replaceFirst("^"+PREFIXE+"_", "");
                                }
                                else {
                                        cleObjet = cle;
                                }
                                this.set(cleObjet, a.get(cle));
                        }
                }

                setPublication(new Publication(model, removePrefix));
                this.set("_etat_", a.get("_etat_"));
        }
        
        @Override
        protected String getPrefixe() {
                return PREFIXE;
        }
        
        protected String[] getChampsObligatoires()      {
                return champsObligatoires;
        }
        
        private void initialiser(JSONObject collectionAPublicationListe) {
                setPublication(new Publication(collectionAPublicationListe));
                initialiserChampsPourGrille();
                initialiserModele(collectionAPublicationListe);
        }
        
        // PUBLICATION
        public Publication getPublication() {
                return publicationLiee;
        }
        public void setPublication(Publication publication) {
                publicationLiee = publication;
                initialiserChampsPourGrille();
                if (publication != null) {
                        setIdPublication(publication.getId());
                }
        }
        
        private void initialiserChampsPourGrille() {
                if (removePrefix) {
                        set("fmt_auteur", getPublication().getAuteur());
                        set("titre", getPublication().getTitre());
                        set("collection", getPublication().getCollection());
                        set("_editeur_", "");
                        set("_annee_", "");
                        set("indication_nvt", getPublication().getIndicationNvt());
                        set("fascicule", getPublication().getFascicule());
                        set("truk_pages", getPublication().getPages());
                        set("_etat_", "");
                } else {
                        set(Publication.PREFIXE+"_fmt_auteur", getPublication().getAuteur());
                        set(Publication.PREFIXE+"_titre", getPublication().getTitre());
                        set(Publication.PREFIXE+"_collection", getPublication().getCollection());
                        set("_editeur_", "");
                        set("_annee_", "");
                        set(Publication.PREFIXE+"_indication_nvt", getPublication().getIndicationNvt());
                        set(Publication.PREFIXE+"_fascicule", getPublication().getFascicule());
                        set(Publication.PREFIXE+"_truk_pages", getPublication().getPages());
                        set("_etat_", "");
                }
        }
        
        // ID
        /** Génère un identifiant de CollectionAPersonne.
         * 
         * C'est une concaténation des clés primaires de la table coel_collection_a_personne séparées par un tiret "-".
         * 
         * @return identifiant unique d'une relation "collection à personne".
         */
        public String getId() {
                String idCollection = getIdCollection();
                String idPublication = getIdPublication();
                if (idCollection.equals("") && idPublication.equals("")) {
                        return null;
                } else {
                        return (idCollection+"-"+idPublication);
                }
        }
        
        //+---------------------------------------------------------------------------------------------------------------+
        // CHAMPS PROVENANT de la TABLE COEL_COLLECTION_A_PERSONNE
        
        // ID COLLECTION
        public String getIdCollection() {
                if (removePrefix) return renvoyerValeurCorrecte("id_collection");
                else return renvoyerValeurCorrecte(PREFIXE+"_id_collection");
        }
        public void setIdCollection(String idCollection) {
                if (removePrefix) set("id_collection", idCollection);
                else set(PREFIXE+"_id_collection", idCollection);
        }
        
        // ID PUBLICATION
        public String getIdPublication() {
                if (removePrefix) return renvoyerValeurCorrecte("id_publication");
                else return renvoyerValeurCorrecte(PREFIXE+"_id_publication");
        }
        public void setIdPublication(String ip) {
                if (removePrefix) set("id_publication", ip);
                else set(PREFIXE+"_id_publication", ip);
        }
                        
}