Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Entity.java
diff --git a/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java b/Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Entity.java
new file mode 100644 (file)
index 0000000..dd190ca
--- /dev/null
@@ -0,0 +1,109 @@
+package org.splat.dal.bo.kernel;
+/**
+ * Abstract root class of persistent objects supporting relations to other persistent objects.<br/>
+ * Relations are instances of concrete subclasses of Relation that are assigned to Entity objects at run time.<br/>
+ * <br/>
+ * Entity objects also support dynamic attributes provided by the Any class.
+ * 
+ * @see Relation
+ * @author    Daniel Brunier-Coulin
+ * @copyright OPEN CASCADE 2012
+ */
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
+
+import org.hibernate.Session;
+import org.splat.dal.dao.kernel.Database;
+import org.splat.kernel.InvalidPropertyException;
+import org.splat.kernel.MissedPropertyException;
+import org.splat.kernel.MultiplyDefinedException;
+import org.splat.kernel.ObjectProperties;
+
+
+public abstract class Entity extends Any {
+
+    private Set<Relation> relations;
+
+//  ==============================================================================================================================
+//  Constructors
+//  ==============================================================================================================================
+
+//  Database fetch constructor
+    protected Entity () {
+    }
+//  Initialization constructor
+    protected Entity (ObjectProperties prop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
+//  ----------------------------------------
+      super(prop);
+      relations = new HashSet<Relation>();
+    }
+
+//  ==============================================================================================================================
+//  Public member functions
+//  ==============================================================================================================================
+
+    public Relation getFirstRelation (Class<? extends Relation> type) {
+//  -----------------------------------------------------------------
+      for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
+       Relation link = i.next();
+       if (link.getClass().equals(type)) return link;
+      }
+      return null;
+    }
+
+    public List<Relation> getRelations (Class<? extends Relation> type) {
+//  -------------------------------------------------------------------
+      List<Relation> result = new Vector<Relation>();
+
+      for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
+       Relation link = i.next();
+       if (link.getClass().equals(type)) result.add(link);
+      }
+      return result;
+    }
+
+//  ==============================================================================================================================
+//  Protected services
+//  ==============================================================================================================================
+
+    protected Set<Relation> getAllRelations () {
+//  ------------------------------------------
+      return relations;
+    }
+
+    protected Relation addRelation (Relation link) {
+//  ----------------------------------------------
+      Session  session = Database.getSession();
+
+      session.save(link);
+      relations.add(link);
+      session.update(this);
+
+      if (link.isBidirectional()) {
+       Entity to = (Entity)link.getTo();   // Bidirectional relation are necessarily between entities
+
+        link = link.getReverse();
+       session.save(link);
+//      if (to.relations == null) to.relations = new HashSet<Relation>();
+       to.relations.add(link);
+       session.update(to);
+      }
+      return link;
+    }
+
+    protected void removeRelation (Class<? extends Relation> type, Persistent to) {
+//  -----------------------------------------------------------------------------
+      for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
+       Relation link = i.next();
+       if (!link.getClass().equals(type)) continue;
+       if (!link.getTo().equals(to))      continue;
+       i.remove();
+        if (this.isSaved()) Database.getSession().update(this);
+       return;
+      }
+    }
+}
\ No newline at end of file