Salome HOME
Edit/Delete of the description/comments functionalities are implemented
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Relation.java
index 9d8d22df54beddf8b511af3b1dd57bc22a41fe2f..c5bc21ef8338955fca60bd11ce2749555a21306f 100644 (file)
@@ -1,10 +1,10 @@
 package org.splat.dal.bo.kernel;
+
+
 /**
- * Base implementation of relations between entities.<br/>
- * A relation makes a typed link from an entity to any kind persistent object. The relations are typed by subclasses of this
- * abstract class which define the actual object referenced by the relation.<br/>
- * This Relation base class implements unidirectional relations. The bidirectionality must be implemented in concrete subclasses
- * by:
+ * Base implementation of relations between entities.<br/> A relation makes a typed link from an entity to any kind persistent object. The
+ * relations are typed by subclasses of this abstract class which define the actual object referenced by the relation.<br/> This Relation
+ * base class implements unidirectional relations. The bidirectionality must be implemented in concrete subclasses by:
  * <ul>
  * <li>overriding the isBidirectional() and getReverseClass() methods,</li>
  * <li>creating the reverse relation in constructors.</li>
@@ -12,104 +12,89 @@ package org.splat.dal.bo.kernel;
  * Relation objects also support dynamic attributes provided by the Any class.
  * 
  * @see Entity
- * @author    Daniel Brunier-Coulin
+ * @author Daniel Brunier-Coulin
  * @copyright OPEN CASCADE 2012
  */
-
-import java.util.Iterator;
-
-import org.hibernate.Session;
-import org.splat.dal.dao.kernel.Database;
-
-
 public abstract class Relation extends Any {
 
-//  Persistent fields
-       protected  Entity   owner;        // Study or Document
-
-//  Transient field
-    protected  Relation reverse;
-
-//  ==============================================================================================================================
-//  Constructors
-//  ==============================================================================================================================
-
-//  Database fetch constructor
-    protected Relation () {
-//  ---------------------
-      reverse = null;
-    }
-//  Initialization constructor
-    protected Relation (Entity from) {
-//  --------------------------------
-      super((Attribute)null);       // For building the collection of attributes
-      this.owner   = from;
-      this.reverse = null;          // Initialized by subclasses
-    }
-
-//  ==============================================================================================================================
-//  Public member functions
-//  ==============================================================================================================================
-
-    public Entity getFrom () {
-//  ------------------------
-      return owner;
-    }
-
-    public Relation getReverse () {
-//  -----------------------------
-      if (!this.isBidirectional() || reverse != null) return reverse;
-
-      Class<? extends Relation> type = this.getReverseClass();
-      Entity                    to   = (Entity)this.getTo();   // Bidirectional relations are necessarily between Entities
-
-      for (Iterator<Relation> i=to.getAllRelations().iterator(); i.hasNext(); ) {
-       Relation asked = i.next();
-       if (!asked.getClass().equals(type)) continue;
-       if (!asked.getTo().equals(owner))   continue;
-       reverse = asked;
-       reverse.reverse = this;     // For benefiting from this execution
-       return reverse;
-      }
-      return null;
-    }
-
-    public Class<? extends Relation> getReverseClass () {
-//  ---------------------------------------------------
-      return null;
-    }
-
-    public boolean isBidirectional () {
-//  ---------------------------------
-      return false;
-    }
-
-/**
- * Moves this relation from its current owner entity to the given one.
- * 
- * @param nowner the document to which this relation is moved
- * */
-    public void moveTo (Entity nowner) {
-//  ----------------------------------
-      Session  session = Database.getSession();
-
-      this.owner = nowner;
-      nowner.getAllRelations().add(this);
-//    myold.getAllRelations().remove(this);  Harmful as it leads to remove this relation from the database (!?)
-      session.update(this);
-      session.update(nowner);
-
-      if (this.isBidirectional()) {
-       Relation  link = this.getReverse();
-       link.setTo(nowner);
-       session.update(link);
-      }
-    }
-
-//  ==============================================================================================================================
-//  Abstract functions
-//  ==============================================================================================================================
-
-    public    abstract Persistent getTo ();
-    protected abstract void       setTo (Persistent to);         // For the need of the moveTo() method
+       // Persistent fields
+       protected Entity owner; // Study or Document
+
+       // Transient field
+       protected Relation reverse;
+
+       // ==============================================================================================================================
+       // Constructors
+       // ==============================================================================================================================
+
+       // Database fetch constructor
+       protected Relation() {
+               reverse = null;
+       }
+
+       // Initialization constructor
+       protected Relation(final Entity from) {
+               this.owner = from;
+               this.reverse = null; // Initialized by subclasses
+       }
+
+       // ==============================================================================================================================
+       // Public member functions
+       // ==============================================================================================================================
+
+       public Entity getFrom() {
+               return owner;
+       }
+
+       public Relation getReverse() {
+               if (this.isBidirectional() && reverse == null) {
+                       Class<? extends Relation> type = this.getReverseClass();
+                       Entity to = (Entity) this.getTo(); // Bidirectional relations are necessarily between Entities
+
+                       for (Relation asked : to.getAllRelations()) {
+                               if (asked.getClass().equals(type)
+                                               && asked.getTo().equals(owner)) {
+                                       reverse = asked;
+                                       reverse.reverse = this; // For benefiting from this execution
+                                       break;
+                               }
+                       }
+               }
+               return reverse;
+       }
+
+       public Class<? extends Relation> getReverseClass() {
+               return null;
+       }
+
+       public boolean isBidirectional() {
+               return false;
+       }
+
+       /**
+        * Moves this relation from its current owner entity to the given one.
+        * 
+        * @param nowner
+        *            the document to which this relation is moved
+        */
+       public void moveTo(final Entity nowner) {
+
+               Entity oldOwner = this.owner;
+               this.owner = nowner;
+               nowner.getAllRelations().add(this);
+               oldOwner.getAllRelations().remove(this);
+
+               if (this.isBidirectional()) {
+                       Relation link = this.getReverse();
+                       link.setTo(nowner);
+               }
+       }
+
+       // ==============================================================================================================================
+       // Abstract functions
+       // ==============================================================================================================================
+
+       public abstract Persistent getTo();
+
+       public abstract void setTo(Persistent to); // For the need of the moveTo() method
 }
\ No newline at end of file