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