001package org.biopax.paxtools.impl.level3;
002
003import static org.biopax.paxtools.util.SetEquivalenceChecker.hasEquivalentIntersection;
004
005import org.apache.commons.logging.Log;
006import org.apache.commons.logging.LogFactory;
007import org.biopax.paxtools.model.BioPAXElement;
008import org.biopax.paxtools.model.level3.BioSource;
009import org.biopax.paxtools.model.level3.CellVocabulary;
010import org.biopax.paxtools.model.level3.TissueVocabulary;
011import org.biopax.paxtools.model.level3.UnificationXref;
012import org.biopax.paxtools.model.level3.Xref;
013import org.biopax.paxtools.util.ClassFilterSet;
014import org.hibernate.annotations.Cache;
015import org.hibernate.annotations.CacheConcurrencyStrategy;
016import org.hibernate.annotations.DynamicInsert;
017import org.hibernate.annotations.DynamicUpdate;
018import org.hibernate.annotations.Proxy;
019import org.hibernate.search.annotations.Indexed;
020
021import javax.persistence.Entity;
022import javax.persistence.ManyToOne;
023import javax.persistence.Transient;
024
025@Entity
026@Proxy(proxyClass= BioSource.class)
027@Indexed
028@DynamicUpdate @DynamicInsert
029@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
030public class BioSourceImpl extends NamedImpl implements BioSource
031{
032        private final static Log LOG = LogFactory.getLog(BioSourceImpl.class);
033        
034        private CellVocabulary celltype;
035        private TissueVocabulary tissue;
036
037
038        public BioSourceImpl(){
039        }
040
041        //
042        // BioPAXElement interface implementation
043        //
044        ////////////////////////////////////////////////////////////////////////////
045
046        @Transient
047        public Class<? extends BioSource> getModelInterface()
048        {
049                return BioSource.class;
050        }
051
052        protected boolean semanticallyEquivalent(BioPAXElement element)
053        {
054                if(!(element instanceof BioSource))
055                        return false;
056                
057                final BioSource bioSource = (BioSource) element;
058
059                return
060                        (celltype != null ?
061                                celltype.isEquivalent(bioSource.getCellType()) :
062                                bioSource.getCellType() == null)
063                        &&
064                                (tissue != null ?
065                                        tissue.isEquivalent(bioSource.getTissue()) :
066                                        bioSource.getTissue() == null)
067                        && hasEquivalentIntersection(
068                                new ClassFilterSet<Xref, UnificationXref>(getXref(), UnificationXref.class),
069                                new ClassFilterSet<Xref, UnificationXref>(bioSource.getXref(), UnificationXref.class));
070    }
071
072        public int equivalenceCode()
073        {
074                int result = 29 *super.equivalenceCode() + (celltype != null ? celltype.hashCode() : 0);
075                result = 29 * result + (tissue != null ? tissue.hashCode() : 0);
076                return result;
077        }
078
079        //
080        // BioSource interface implementation
081        //
082        ////////////////////////////////////////////////////////////////////////////
083
084    @ManyToOne(targetEntity = CellVocabularyImpl.class)
085        public CellVocabulary getCellType()
086        {
087                return celltype;
088        }
089
090        public void setCellType(CellVocabulary celltype)
091        {
092                this.celltype = celltype;
093        }
094
095        @ManyToOne(targetEntity = TissueVocabularyImpl.class)
096        public TissueVocabulary getTissue()
097        {
098                return tissue;
099        }
100
101        public void setTissue(TissueVocabulary tissue)
102        {
103                this.tissue = tissue;
104        }
105
106
107        @Override
108        public String toString() {
109                try {
110                        StringBuilder sb = new StringBuilder();
111                        sb.append(getRDFId()).append(" ");
112                        sb.append(getName().toString());
113                        if (tissue != null)
114                                sb.append(" tissue: ").append(tissue.getTerm().toString());
115                        if (celltype != null)
116                                sb.append(" celltype: ").append(celltype.getTerm().toString());
117                        sb.append(" xrefs: ").append(getXref().toString());
118                        return sb.toString();
119                } catch (Exception e) {
120                        // possible issues - when in a persistent context (e.g., lazy collections init...)
121                        LOG.warn("Error in toString(): ", e);
122                        return getRDFId();
123                }
124        }
125}