Salome HOME
app.root property is removed.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Relation.java
1 package org.splat.dal.bo.kernel;
2 /**
3  * Base implementation of relations between entities.<br/>
4  * A relation makes a typed link from an entity to any kind persistent object. The relations are typed by subclasses of this
5  * abstract class which define the actual object referenced by the relation.<br/>
6  * This Relation base class implements unidirectional relations. The bidirectionality must be implemented in concrete subclasses
7  * by:
8  * <ul>
9  * <li>overriding the isBidirectional() and getReverseClass() methods,</li>
10  * <li>creating the reverse relation in constructors.</li>
11  * </ul>
12  * Relation objects also support dynamic attributes provided by the Any class.
13  * 
14  * @see Entity
15  * @author    Daniel Brunier-Coulin
16  * @copyright OPEN CASCADE 2012
17  */
18
19 import java.util.Iterator;
20
21 public abstract class Relation extends Any {
22
23 //  Persistent fields
24         protected  Entity   owner;        // Study or Document
25
26 //  Transient field
27     protected  Relation reverse;
28
29 //  ==============================================================================================================================
30 //  Constructors
31 //  ==============================================================================================================================
32
33 //  Database fetch constructor
34     protected Relation () {
35 //  ---------------------
36       reverse = null;
37     }
38 //  Initialization constructor
39     protected Relation (Entity from) {
40 //  --------------------------------
41       this.owner   = from;
42       this.reverse = null;          // Initialized by subclasses
43     }
44
45 //  ==============================================================================================================================
46 //  Public member functions
47 //  ==============================================================================================================================
48
49     public Entity getFrom () {
50 //  ------------------------
51       return owner;
52     }
53
54     public Relation getReverse () {
55 //  -----------------------------
56       if (!this.isBidirectional() || reverse != null) return reverse;
57
58       Class<? extends Relation> type = this.getReverseClass();
59       Entity                    to   = (Entity)this.getTo();   // Bidirectional relations are necessarily between Entities
60
61       for (Iterator<Relation> i=to.getAllRelations().iterator(); i.hasNext(); ) {
62         Relation asked = i.next();
63         if (!asked.getClass().equals(type)) continue;
64         if (!asked.getTo().equals(owner))   continue;
65         reverse = asked;
66         reverse.reverse = this;     // For benefiting from this execution
67         return reverse;
68       }
69       return null;
70     }
71
72     public Class<? extends Relation> getReverseClass () {
73 //  ---------------------------------------------------
74       return null;
75     }
76
77     public boolean isBidirectional () {
78 //  ---------------------------------
79       return false;
80     }
81
82 /**
83  * Moves this relation from its current owner entity to the given one.
84  * 
85  * @param nowner the document to which this relation is moved
86  * */
87     public void moveTo (Entity nowner) {
88
89       Entity oldOwner = this.owner;
90       this.owner = nowner;
91       nowner.getAllRelations().add(this);
92       oldOwner.getAllRelations().remove(this);
93
94       if (this.isBidirectional()) {
95         Relation  link = this.getReverse();
96         link.setTo(nowner);
97       }
98     }
99
100 //  ==============================================================================================================================
101 //  Abstract functions
102 //  ==============================================================================================================================
103
104     public    abstract Persistent getTo ();
105     protected abstract void       setTo (Persistent to);         // For the need of the moveTo() method
106 }