Salome HOME
app.root property is removed.
[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 = new HashSet<Relation>();
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       super(prop);
39     }
40
41 //  ==============================================================================================================================
42 //  Public member functions
43 //  ==============================================================================================================================
44
45     public Relation getFirstRelation (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)) return link;
50       }
51       return null;
52     }
53
54     public List<Relation> getRelations (Class<? extends Relation> type) {
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       return relations;
70     }
71
72     protected Relation addRelation (Relation link) {
73
74       relations.add(link);
75
76       if (link.isBidirectional()) {
77         Entity to = (Entity)link.getTo();   // Bidirectional relation are necessarily between entities
78
79         link = link.getReverse();
80         to.relations.add(link);
81       }
82       return link;
83     }
84
85     protected void removeRelation (Class<? extends Relation> type, Persistent to) {
86       for (Iterator<Relation> i=relations.iterator(); i.hasNext();) {
87         Relation link = i.next();
88         if (!link.getClass().equals(type)) continue;
89         if (!link.getTo().equals(to))      continue;
90         i.remove();
91         return;
92       }
93     }
94 }