001package org.biopax.paxtools.impl.level3;
002
003import org.biopax.paxtools.model.BioPAXElement;
004import org.biopax.paxtools.model.level3.Entity;
005import org.biopax.paxtools.model.level3.*;
006import org.biopax.paxtools.util.BPCollections;
007import org.biopax.paxtools.util.ClassFilterSet;
008import org.biopax.paxtools.util.DataSourceFieldBridge;
009import org.biopax.paxtools.util.SetStringBridge;
010import org.hibernate.annotations.*;
011import org.hibernate.search.annotations.Analyze;
012import org.hibernate.search.annotations.Field;
013import org.hibernate.search.annotations.FieldBridge;
014import org.hibernate.search.annotations.Store;
015
016import javax.persistence.ElementCollection;
017import javax.persistence.JoinTable;
018import javax.persistence.ManyToMany;
019import java.util.Set;
020
021import static org.biopax.paxtools.util.SetEquivalenceChecker.hasEquivalentIntersection;
022
023
024
025@javax.persistence.Entity
026@Proxy(proxyClass= Entity.class)
027@DynamicUpdate @DynamicInsert
028@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
029public abstract class EntityImpl extends NamedImpl implements Entity
030{
031// ------------------------------ FIELDS ------------------------------
032
033        private Set<Interaction> participantOf;
034        /**
035         * This Set keeps statements describing the availability of this data (e.g. a copyright
036         * statement).
037         */
038        private Set<String> availability;
039
040        /**
041         * This Set keeps statements describing the data sources for this data.
042         */
043        private Set<Provenance> dataSource;
044
045
046        /**
047         * This Set keeps evidence related to this entity
048         */
049
050        private Set<Evidence> evidence;
051
052        /**
053         * Helper object for managing names
054         */
055
056
057
058// --------------------------- CONSTRUCTORS ---------------------------
059
060        public EntityImpl()
061        {
062                this.availability = BPCollections.I.createSet();
063                this.dataSource = BPCollections.I.createSafeSet();
064                this.participantOf = BPCollections.I.createSafeSet();
065                this.evidence = BPCollections.I.createSafeSet();
066        }
067
068// ------------------------ INTERFACE METHODS ------------------------
069
070
071// --------------------- ACCESORS and MUTATORS---------------------
072
073    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
074        @ElementCollection
075        @JoinTable(name="availability")
076        @Field(name=FIELD_AVAILABILITY, analyze=Analyze.YES)
077        @FieldBridge(impl=SetStringBridge.class)
078        public Set<String> getAvailability()
079        {
080                return availability;
081        }
082
083        public void setAvailability(Set<String> availability)
084        {
085                this.availability = availability;
086        }
087
088        public void addAvailability(String availability_text)
089        {
090                if(availability_text != null && availability_text.length() > 0)
091                        availability.add(availability_text);
092        }
093
094        public void removeAvailability(String availability_text)
095        {
096                if(availability_text != null)
097                        this.availability.remove(availability_text);
098        }
099
100    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
101        @ManyToMany(targetEntity = ProvenanceImpl.class)
102        @JoinTable(name="dataSource")
103        @Field(name=FIELD_DATASOURCE, store=Store.YES, analyze=Analyze.NO)
104        @FieldBridge(impl=DataSourceFieldBridge.class)
105        public Set<Provenance> getDataSource()
106        {
107                return dataSource;
108        }
109
110        public void setDataSource(Set<Provenance> dataSource)
111        {
112                this.dataSource = dataSource;
113        }
114
115        public void addDataSource(Provenance dataSource)
116        {
117                if(dataSource != null)
118                        this.dataSource.add(dataSource);
119        }
120
121        public void removeDataSource(Provenance dataSource)
122        {
123                if(dataSource != null)
124                        this.dataSource.remove(dataSource);
125        }
126
127// --------------------- Interface entity ---------------------
128
129        @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
130        @ManyToMany(targetEntity = InteractionImpl.class, mappedBy = "participant")
131        public Set<Interaction> getParticipantOf()
132        {
133                return participantOf;
134        }
135
136        protected void setParticipantOf(Set<Interaction> participantOf)
137        {
138                this.participantOf= participantOf;
139        }
140
141        //
142        // observable interface implementation
143        //
144        /////////////////////////////////////////////////////////////////////////////
145    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
146        @ManyToMany(targetEntity = EvidenceImpl.class)
147        @JoinTable(name="evidence")
148        public Set<Evidence> getEvidence()
149        {
150                return evidence;
151        }
152
153        public void addEvidence(Evidence newEvidence)
154        {
155                if(newEvidence != null)
156                        this.evidence.add(newEvidence);
157        }
158
159        public void removeEvidence(Evidence oldEvidence)
160        {
161                if(oldEvidence != null)
162                        this.evidence.remove(oldEvidence);
163        }
164
165        protected void setEvidence(Set<Evidence> newEvidence)
166        {
167                this.evidence = newEvidence;
168        }
169
170
171        @Override
172        protected boolean semanticallyEquivalent(BioPAXElement element)
173        {
174                boolean equivalance = false;
175                if (element instanceof Entity)
176                {
177                        Entity otherEntity = (Entity) element;
178
179                        equivalance = hasEquivalentIntersection(dataSource, otherEntity.getDataSource())
180                                      && hasEquivalentIntersection(
181                                        new ClassFilterSet<Xref, UnificationXref>(getXref(), UnificationXref.class),
182                                        new ClassFilterSet<Xref, UnificationXref>(otherEntity.getXref(), UnificationXref.class))
183                                      && hasEquivalentIntersection(evidence, otherEntity.getEvidence());
184                }
185                return equivalance;
186        }
187}