Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.modeles.publication;

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.collection.CollectionAPublication;
import org.tela_botanica.client.modeles.personne.Personne;
import org.tela_botanica.client.util.Debug;

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

public class PublicationAPersonne extends aDonnee {

        private static final long serialVersionUID = 7769105365939978129L;
        
        public static final String PREFIXE = "cpuap";
        private boolean removePrefix = true;
        public static final String ROLE_AUTEUR = "2360";
        //FIXME: insérer en base de données une valeur cohérente pour l'identifiant ci-dessous
        public static final String ROLE_SUJET = "30762";
        
        private Personne personneLiee = null;
        private Publication publicationLiee = null;
        public static String[] champsObligatoires = {"cpuap_id_personne", "cpuap_id_publication", "cpuap_id_role"};
        
        public PublicationAPersonne() {
                new PublicationAPersonne(new JSONObject());
        }
        
        public PublicationAPersonne(boolean removePrefix) {
                this.removePrefix = removePrefix;
                new PublicationAPersonne(new JSONObject());
        }
        
        public PublicationAPersonne(JSONObject pubAPersListe) {
                Personne personne = new Personne(pubAPersListe);
                setPersonne(personne);
                publicationLiee = new Publication(pubAPersListe);
                
                // l'objet JSON est une table de hachage
                Set<String> im = pubAPersListe.keySet();

                // Parcourt pour chaque clé
                for (Iterator<String> it = im.iterator(); it.hasNext();) {
                        // Si elle est associée à une valeur, nous l'ajoutons
                        String cle = it.next();
                        String cleObjet = "";
                        if (removePrefix) cleObjet = cle.replaceFirst("^"+getPrefixe()+"_", "");
                        else cleObjet=cle;
                        // Valeur vide par défaut
                        String valeur = "";
                        if (pubAPersListe.get(cle).isString() != null) {
                                valeur = pubAPersListe.get(cle).isString().stringValue();
                        }                       
                        this.set(cleObjet, valeur);
                }
                
                initialiserChampsPourGrille();
        }
        
        public PublicationAPersonne(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("^"+Personne.PREFIXE+"_", "");
                                        cleObjet = cle.replaceFirst("^"+Publication.PREFIXE+"_", "");
                                        cleObjet = cleObjet.replaceFirst("^"+PREFIXE+"_", "");
                                }
                                else {
                                        cleObjet = cle;
                                }
                                this.set(cleObjet, a.get(cle));
                        }
                }

                setPersonne(new Personne(model, removePrefix));
                setPublicationLiee(new Publication(model, removePrefix));
                this.set("_role_", a.get("_role_"));
                this.set("_etat_", a.get("_etat_"));
        }
        
        private void initialiserChampsPourGrille() {
                if (removePrefix) {
                        set("fmt_auteur", publicationLiee.getAuteur());
                        set("titre", publicationLiee.getTitre());
                        set("collection", publicationLiee.getCollection());
                        set("_editeur_", "");
                        set("_annee_", "");
                        set("indication_nvt", publicationLiee.getIndicationNvt());
                        set("fascicule", publicationLiee.getFascicule());
                        set("truk_pages", publicationLiee.getPages());
                        set("_etat_", "");
                        set("_role_", this.get("id_role"));
                } else {
                        set("cpu_fmt_auteur", publicationLiee.getAuteur());
                        set("cpu_titre", publicationLiee.getTitre());
                        set("cpu_collection", publicationLiee.getCollection());
                        set("_editeur_", "");
                        set("_annee_", "");
                        set("cpu_indication_nvt", publicationLiee.getIndicationNvt());
                        set("cpu_fascicule", publicationLiee.getFascicule());
                        set("cpu_truk_pages", publicationLiee.getPages());
                        set("_etat_", "");
                        set("_role_", this.get("id_role"));
                }
        }
        
        
        @Override
        protected String getPrefixe() {
                return PREFIXE;
        }

        protected String[] getChampsObligatoires()      {
                return champsObligatoires;
        }
        
        public Personne getPersonne() {
                return personneLiee;
        }
        
        public void setPersonne(Personne personne) {
                setPersonne(personne, false);
        }
        
        public void setPersonne(Personne personne, boolean integrerProprietes) {
                personneLiee = personne;
                if (personne != null) {
                        setIdPersonne(personne.getId());
                }
                
                if (integrerProprietes) {
                        Map<String, Object> a = personne.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("^"+Personne.PREFIXE+"_", "");
                                        }
                                        else {
                                                cleObjet = cle;
                                        }
                                        this.set(cleObjet, a.get(cle));
                                }
                        }
                }
        }
        
        // ID
        public String getId() {
                String idPublication = getIdPublication();
                String idPersonne = getIdPersonne();
                String idRole = getIdRole();
                if (idPublication.equals("") && idPersonne.equals("") && idRole.equals("")) {
                        return null;
                } else {
                        return (idPublication+"-"+idPersonne+"-"+idRole);
                }
        }
        
        // ID PUBLICATION
        public String getIdPublication() {
                if (removePrefix) return renvoyerValeurCorrecte("id_publication");
                else return renvoyerValeurCorrecte("cpuap_id_publication");
        }
        public void setIdPublication(String id) {
                if (removePrefix) set("id_publication", id);
                else set("cpuap_id_publication", id);
        }
        
        // PUBLICATION LIEE
        public Publication getPublicationLiee() {
                return this.publicationLiee;
        }
        
        // LIER PUBLICATION
        public void setPublicationLiee(Publication publication) {
                this.publicationLiee = publication;
                initialiserChampsPourGrille();
        }
        
        // ROLE
        public String getRole() {
                String role = this.get("_role_");
                if (role != null)       {
                        return role;
                } else  {
                        return "";
                }
        }
        
        // ID PERSONNE
        public String getIdPersonne() {
                if (removePrefix) return renvoyerValeurCorrecte("id_personne");
                else return renvoyerValeurCorrecte("cpuap_id_personne");
        }
        public void setIdPersonne(String id) {
                if (removePrefix) set("id_personne", id);
                else set(PREFIXE+"_id_personne", id);
        }
        
        // ID RôLE
        public String getIdRole() {
                return renvoyerValeurCorrecte("id_role");
        }
        public void setIdRole(String id) {
                set("id_role", id);
        }
                
        // TYPE
        public String getType() {
                return renvoyerValeurCorrecte("ce_truk_type");
        }
        public void setType(String type) {
                set("ce_truk_type", type);
        }
        public void setFonction(String type, String valeur) {
                setChaineDenormaliseUnique("ce_truk_type", type, valeur);
        }
        
        // ORDRE DES AUTEURS
        public String getOrdreAuteurs() {
                if (removePrefix) return renvoyerValeurCorrecte("ordre");
                else return renvoyerValeurCorrecte(PREFIXE+"_ordre");
        }
        public void setOrdreAuteurs(String ordre) {
                if (ordre.matches("[0-9]+")) {
                        if (removePrefix) this.set("ordre", ordre);
                        else this.set(PREFIXE+"_ordre", ordre);
                        
                        this.set("_ordre_", ordre);
                }
        }
}