001package org.biopax.paxtools.impl.level3;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.XReferrable;
005import org.biopax.paxtools.model.level3.Xref;
006import org.biopax.paxtools.util.BPCollections;
007import org.hibernate.annotations.*;
008import org.hibernate.search.annotations.Analyze;
009import org.hibernate.search.annotations.Boost;
010import org.hibernate.search.annotations.Field;
011
012import javax.persistence.Column;
013import javax.persistence.Entity;
014import javax.persistence.ManyToMany;
015import javax.persistence.Transient;
016import java.util.Set;
017
018
019@Entity
020@Proxy(proxyClass= Xref.class)
021@DynamicUpdate @DynamicInsert
022@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
023public abstract class XrefImpl extends L3ElementImpl implements Xref
024{
025
026        private String db;
027        private String dbVersion;
028        private String idVersion;
029        private String refId;
030        private Set<XReferrable> xrefOf;
031
032        /**
033         * Constructor.
034         */
035        public XrefImpl()
036        {
037                this.xrefOf = BPCollections.I.createSafeSet();
038        }
039
040        @Override
041        protected boolean semanticallyEquivalent(BioPAXElement other)
042        {
043                if(!(other instanceof Xref)) return false;
044                
045                final Xref anXref = (Xref) other;
046
047                return
048                        (db != null ?
049                                db.equalsIgnoreCase(anXref.getDb()) :
050                                anXref.getDb() == null)
051                                &&
052                                (refId != null ?
053                                        refId.equals(anXref.getId()) :
054                                        anXref.getId() == null)
055                                &&
056                                (dbVersion != null ?
057                                        dbVersion.equalsIgnoreCase(anXref.getDbVersion()) :
058                                        anXref.getDbVersion() == null)
059                                &&
060                                (idVersion != null ?
061                                        idVersion.equals(anXref.getIdVersion()) :
062                                        anXref.getIdVersion() == null);
063        }
064
065    @Override
066        public int equivalenceCode()
067        {
068                int result = 29 + (db != null ? db.hashCode() : 0);
069                result = 29 * result + (dbVersion != null ? dbVersion.hashCode() : 0);
070                result = 29 * result + (idVersion != null ? idVersion.hashCode() : 0);
071                result = 29 * result + (refId != null ? refId.hashCode() : 0);
072                return result;
073        }
074
075        
076        @Field(name=FIELD_XREFDB, analyze=Analyze.YES)
077    public String getDb()
078        {
079                return db;
080        }
081
082        public void setDb(String db)
083        {
084                this.db = db;
085        }
086
087    public String getDbVersion()
088        {
089                return dbVersion;
090        }
091
092        public void setDbVersion(String dbVersion)
093        {
094                this.dbVersion = dbVersion;
095        }
096
097        public String getIdVersion()
098        {
099                return idVersion;
100        }
101
102        public void setIdVersion(String idVersion)
103        {
104                this.idVersion = idVersion;
105        }
106
107        //getId() caused exceptions in the indexer; so we changed to getIdx/setIdx pair
108    @Field(name=FIELD_XREFID, analyze=Analyze.YES)
109    @Boost(1.1f)
110    @Column(name="id")
111        public String getIdx()
112        {
113                return refId;
114        }
115
116        public void setIdx(String id)
117        {
118                this.refId = id;
119        }
120    
121    @Transient //non-transient 'getId()' used to cause Hibernate/Search trouble; so getIdx() was created.
122    public String getId()
123        {
124                return refId;
125        }
126
127        public void setId(String id)
128        {
129                this.refId = id;
130        }
131
132
133        @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
134        @ManyToMany(targetEntity = XReferrableImpl.class, mappedBy = "xref")
135        public Set<XReferrable> getXrefOf()
136        {
137                return xrefOf;
138        }
139
140        protected void setXrefOf(Set<XReferrable> xrefOf)
141        {
142                this.xrefOf = xrefOf;
143        }
144
145        @Override
146        public String toString() {
147                return getDb() + 
148                ((getDbVersion()==null)? "" : "." + getDbVersion()) 
149                + ":" + getId() + 
150                ((getIdVersion()==null)? "" : "." + getIdVersion());
151        }
152        
153}