001package org.biopax.paxtools.impl.level3;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import org.biopax.paxtools.model.BioPAXElement;
006import org.biopax.paxtools.model.level3.EntityReference;
007import org.biopax.paxtools.model.level3.PhysicalEntity;
008import org.biopax.paxtools.model.level3.SimplePhysicalEntity;
009import org.biopax.paxtools.util.BPCollections;
010import org.hibernate.annotations.*;
011
012import javax.persistence.Entity;
013import javax.persistence.ManyToOne;
014import javax.persistence.Transient;
015import java.util.Set;
016
017@Entity
018@Proxy(proxyClass= SimplePhysicalEntity.class)
019@DynamicUpdate @DynamicInsert
020public abstract class SimplePhysicalEntityImpl extends PhysicalEntityImpl
021                implements SimplePhysicalEntity
022{
023        private EntityReference entityReference;
024        Log log = LogFactory.getLog(SimplePhysicalEntityImpl.class);
025        public SimplePhysicalEntityImpl() {
026        }
027        
028        @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
029        @ManyToOne(targetEntity = EntityReferenceImpl.class)
030        public EntityReference getEntityReferenceX()
031        {
032                return entityReference;
033        }
034        public void setEntityReferenceX(EntityReference entityReference) {
035                this.entityReference = entityReference;
036        }
037
038        @Transient
039        public Set<EntityReference> getGenericEntityReferences()
040        {
041                Set<EntityReference> ger = BPCollections.I.createSet();
042                EntityReference er = this.getEntityReference();
043                if(er!=null)
044                {
045                        ger.add(er);
046                        ger.addAll(er.getMemberEntityReference());
047                }
048                for (PhysicalEntity pe : this.getMemberPhysicalEntity())
049                {
050                        if(pe instanceof SimplePhysicalEntity)
051                        ger.addAll(((SimplePhysicalEntity) pe).getGenericEntityReferences());
052                        else
053                                log.error("Member PE is of different class! Skipping..");
054                }
055                return ger;
056        }
057
058        @Transient
059        public EntityReference getEntityReference()
060        {
061                return entityReference;
062        }
063
064        public void setEntityReference(EntityReference entityReference)
065        {
066                if (this.entityReference != null)
067                {
068                        synchronized (this.entityReference) {
069                                this.entityReference.getEntityReferenceOf().remove(this);
070                        }
071                }
072                this.entityReference = entityReference;
073                if (this.entityReference != null)
074                {
075                        synchronized (this.entityReference) {
076                                this.entityReference.getEntityReferenceOf().add(this);
077                        }
078                }
079        }
080
081
082        @Override
083        public int equivalenceCode()
084        {
085       
086        return this.entityReference==null? hashCode():31 * super.locationAndFeatureCode() +
087                       entityReference.equivalenceCode();
088        }
089
090        @Override
091        protected boolean semanticallyEquivalent(BioPAXElement element)
092        {
093                if(!(element instanceof SimplePhysicalEntity))
094                        return false;
095                
096                SimplePhysicalEntity that = (SimplePhysicalEntity) element;
097                return ( (that.getEntityReference()!=null)
098                                        ? that.getEntityReference().isEquivalent(getEntityReference())
099                                        : getEntityReference() == null
100                                ) && super.semanticallyEquivalent(element);
101        }
102}