1 package org.splat.dal.bo.kernel;
3 * Abstract root class of persistent objects supporting relations to other persistent objects.<br/>
4 * Relations are instances of concrete subclasses of Relation that are assigned to Entity objects at run time.<br/>
6 * Entity objects also support dynamic attributes provided by the Any class.
9 * @author Daniel Brunier-Coulin
10 * @copyright OPEN CASCADE 2012
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Vector;
19 import org.hibernate.Session;
20 import org.splat.dal.dao.kernel.Database;
21 import org.splat.kernel.InvalidPropertyException;
22 import org.splat.kernel.MissedPropertyException;
23 import org.splat.kernel.MultiplyDefinedException;
24 import org.splat.kernel.ObjectProperties;
27 public abstract class Entity extends Any {
29 private Set<Relation> relations;
31 // ==============================================================================================================================
33 // ==============================================================================================================================
35 // Database fetch constructor
38 // Initialization constructor
39 protected Entity (ObjectProperties prop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
40 // ----------------------------------------
42 relations = new HashSet<Relation>();
45 // ==============================================================================================================================
46 // Public member functions
47 // ==============================================================================================================================
49 public Relation getFirstRelation (Class<? extends Relation> type) {
50 // -----------------------------------------------------------------
51 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
52 Relation link = i.next();
53 if (link.getClass().equals(type)) return link;
58 public List<Relation> getRelations (Class<? extends Relation> type) {
59 // -------------------------------------------------------------------
60 List<Relation> result = new Vector<Relation>();
62 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
63 Relation link = i.next();
64 if (link.getClass().equals(type)) result.add(link);
69 // ==============================================================================================================================
71 // ==============================================================================================================================
73 protected Set<Relation> getAllRelations () {
74 // ------------------------------------------
78 protected Relation addRelation (Relation link) {
79 // ----------------------------------------------
80 // Session session = Database.getSession();
82 // session.save(link);
84 // session.update(this);
86 if (link.isBidirectional()) {
87 Entity to = (Entity)link.getTo(); // Bidirectional relation are necessarily between entities
89 link = link.getReverse();
90 // session.save(link);
91 // if (to.relations == null) to.relations = new HashSet<Relation>();
92 to.relations.add(link);
93 // session.update(to);
98 protected void removeRelation (Class<? extends Relation> type, Persistent to) {
99 // -----------------------------------------------------------------------------
100 for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
101 Relation link = i.next();
102 if (!link.getClass().equals(type)) continue;
103 if (!link.getTo().equals(to)) continue;
105 if (this.isSaved()) Database.getSession().update(this);