Salome HOME
52f06cb5059538131f9473a29e777000287fbc92
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Entity.java
1 package org.splat.dal.bo.kernel;
2 /**
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/>
5  * <br/>
6  * Entity objects also support dynamic attributes provided by the Any class.
7  * 
8  * @see Relation
9  * @author    Daniel Brunier-Coulin
10  * @copyright OPEN CASCADE 2012
11  */
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.Vector;
18
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;
25
26
27 public abstract class Entity extends Any {
28
29     private Set<Relation> relations;
30
31 //  ==============================================================================================================================
32 //  Constructors
33 //  ==============================================================================================================================
34
35 //  Database fetch constructor
36     protected Entity () {
37     }
38 //  Initialization constructor
39     protected Entity (ObjectProperties prop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
40 //  ----------------------------------------
41       super(prop);
42       relations = new HashSet<Relation>();
43     }
44
45 //  ==============================================================================================================================
46 //  Public member functions
47 //  ==============================================================================================================================
48
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;
54       }
55       return null;
56     }
57
58     public List<Relation> getRelations (Class<? extends Relation> type) {
59 //  -------------------------------------------------------------------
60       List<Relation> result = new Vector<Relation>();
61
62       for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
63         Relation link = i.next();
64         if (link.getClass().equals(type)) result.add(link);
65       }
66       return result;
67     }
68
69 //  ==============================================================================================================================
70 //  Protected services
71 //  ==============================================================================================================================
72
73     protected Set<Relation> getAllRelations () {
74 //  ------------------------------------------
75       return relations;
76     }
77
78     protected Relation addRelation (Relation link) {
79 //  ----------------------------------------------
80 //      Session  session = Database.getSession();
81
82 //      session.save(link);
83       relations.add(link);
84 //      session.update(this);
85
86       if (link.isBidirectional()) {
87         Entity to = (Entity)link.getTo();   // Bidirectional relation are necessarily between entities
88
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);
94       }
95       return link;
96     }
97
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;
104         i.remove();
105         if (this.isSaved()) Database.getSession().update(this);
106         return;
107       }
108     }
109 }