001package org.biopax.paxtools.impl.level3;
002
003import org.apache.commons.lang.StringUtils;
004import org.apache.commons.logging.Log;
005import org.apache.commons.logging.LogFactory;
006import org.biopax.paxtools.model.BioPAXElement;
007import org.biopax.paxtools.model.level3.ControlledVocabulary;
008import org.biopax.paxtools.model.level3.UnificationXref;
009import org.biopax.paxtools.model.level3.Xref;
010import org.biopax.paxtools.util.BPCollections;
011import org.biopax.paxtools.util.ClassFilterSet;
012import org.biopax.paxtools.util.SetEquivalenceChecker;
013import org.biopax.paxtools.util.SetStringBridge;
014import org.hibernate.annotations.Cache;
015import org.hibernate.annotations.*;
016import org.hibernate.search.annotations.Analyze;
017import org.hibernate.search.annotations.Field;
018import org.hibernate.search.annotations.FieldBridge;
019import org.hibernate.search.annotations.Indexed;
020
021import javax.persistence.*;
022import javax.persistence.Entity;
023
024import java.util.Set;
025
026@Entity
027@Proxy(proxyClass= ControlledVocabulary.class)
028@Indexed
029@DynamicUpdate @DynamicInsert
030@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
031public class ControlledVocabularyImpl extends XReferrableImpl implements
032        ControlledVocabulary
033{
034        private final static Log LOG = LogFactory.getLog(CellVocabularyImpl.class);
035        
036        private Set<String> term;
037
038        /**
039         * Constructor.
040         */
041        public ControlledVocabularyImpl()
042        {
043                this.term = BPCollections.I.createSet();
044        }
045
046        //
047        // BioPAXElement, Xreferrable implementation
048        //
049        ////////////////////////////////////////////////////////////////////////////
050
051        @Transient
052        public Class<? extends ControlledVocabulary> getModelInterface()
053        {
054                return ControlledVocabulary.class;
055        }
056
057
058        
059        //
060        // ControlledVocabulary interface implementation
061        //
062        ////////////////////////////////////////////////////////////////////////////
063
064        // Property term
065
066    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
067        @ElementCollection(fetch=FetchType.EAGER)
068        @JoinTable(name="term")
069        @Field(name = FIELD_TERM, analyze=Analyze.YES)
070        @FieldBridge(impl=SetStringBridge.class)
071        public Set<String> getTerm()
072        {
073                return term;
074        }
075
076        public void setTerm(Set<String> term)
077        {
078                this.term = term;
079        }
080
081        public void addTerm(String term)
082        {
083                if(term != null && term.length()>0)
084                        this.term.add(term);
085        }
086
087        public void removeTerm(String term)
088        {
089                if(term != null)
090                        this.term.remove(term);
091        }
092        
093        @Override
094        protected boolean semanticallyEquivalent(BioPAXElement element) {
095                if(! (element instanceof ControlledVocabulary)) return false;
096                
097                ControlledVocabulary that = (ControlledVocabulary) element;
098                Set<String> terms = BPCollections.I.createSet();
099                terms.addAll(term);
100                terms.retainAll(that.getTerm());
101
102                
103                return getModelInterface().equals(that.getModelInterface()) 
104                                && (term.isEmpty() && that.getTerm().isEmpty() || !terms.isEmpty() )
105                                && SetEquivalenceChecker.hasEquivalentIntersection(
106                                new ClassFilterSet<Xref, UnificationXref>(getXref(), UnificationXref.class),
107                                new ClassFilterSet<Xref, UnificationXref>(that.getXref(), UnificationXref.class));
108        }
109        
110        @Override
111        public String toString()
112        {
113                String ret = getRDFId();
114                try {
115                        // in a persistent context, there can be lazy collection initialization exception...
116                        if(!term.isEmpty())
117                                ret = getModelInterface().getSimpleName() +
118                                        "_" + StringUtils.join(term, ",");
119                } catch (Exception e) {         
120                        LOG.warn("toString(): ", e);
121                }
122                return ret;
123        }
124}