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