Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / SPlat / src / org / splat / kernel / Entity.java
1 package org.splat.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
21
22 public abstract class Entity extends Any {
23
24     private Set<Relation> relations;
25
26 //  ==============================================================================================================================
27 //  Constructors
28 //  ==============================================================================================================================
29
30 //  Database fetch constructor
31     protected Entity () {
32     }
33 //  Initialization constructor
34     protected Entity (ObjectProperties prop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
35 //  ----------------------------------------
36       super(prop);
37       relations = new HashSet<Relation>();
38     }
39
40 //  ==============================================================================================================================
41 //  Public member functions
42 //  ==============================================================================================================================
43
44     public Relation getFirstRelation (Class<? extends Relation> type) {
45 //  -----------------------------------------------------------------
46       for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
47         Relation link = i.next();
48         if (link.getClass().equals(type)) return link;
49       }
50       return null;
51     }
52
53     public List<Relation> getRelations (Class<? extends Relation> type) {
54 //  -------------------------------------------------------------------
55       List<Relation> result = new Vector<Relation>();
56
57       for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
58         Relation link = i.next();
59         if (link.getClass().equals(type)) result.add(link);
60       }
61       return result;
62     }
63
64 //  ==============================================================================================================================
65 //  Protected services
66 //  ==============================================================================================================================
67
68     protected Set<Relation> getAllRelations () {
69 //  ------------------------------------------
70       return relations;
71     }
72
73     protected Relation addRelation (Relation link) {
74 //  ----------------------------------------------
75       Session  session = Database.getSession();
76
77       session.save(link);
78       relations.add(link);
79       session.update(this);
80
81       if (link.isBidirectional()) {
82         Entity to = (Entity)link.getTo();   // Bidirectional relation are necessarily between entities
83
84         link = link.getReverse();
85         session.save(link);
86 //      if (to.relations == null) to.relations = new HashSet<Relation>();
87         to.relations.add(link);
88         session.update(to);
89       }
90       return link;
91     }
92
93     protected void removeRelation (Class<? extends Relation> type, Persistent to) {
94 //  -----------------------------------------------------------------------------
95       for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
96         Relation link = i.next();
97         if (!link.getClass().equals(type)) continue;
98         if (!link.getTo().equals(to))      continue;
99         i.remove();
100         if (this.isSaved()) Database.getSession().update(this);
101         return;
102       }
103     }
104 }