Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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       super((Attribute)null);       // For building the collection of attributes
42       this.owner   = from;
43       this.reverse = null;          // Initialized by subclasses
44     }
45
46 //  ==============================================================================================================================
47 //  Public member functions
48 //  ==============================================================================================================================
49
50     public Entity getFrom () {
51 //  ------------------------
52       return owner;
53     }
54
55     public Relation getReverse () {
56 //  -----------------------------
57       if (!this.isBidirectional() || reverse != null) return reverse;
58
59       Class<? extends Relation> type = this.getReverseClass();
60       Entity                    to   = (Entity)this.getTo();   // Bidirectional relations are necessarily between Entities
61
62       for (Iterator<Relation> i=to.getAllRelations().iterator(); i.hasNext(); ) {
63         Relation asked = i.next();
64         if (!asked.getClass().equals(type)) continue;
65         if (!asked.getTo().equals(owner))   continue;
66         reverse = asked;
67         reverse.reverse = this;     // For benefiting from this execution
68         return reverse;
69       }
70       return null;
71     }
72
73     public Class<? extends Relation> getReverseClass () {
74 //  ---------------------------------------------------
75       return null;
76     }
77
78     public boolean isBidirectional () {
79 //  ---------------------------------
80       return false;
81     }
82
83 /**
84  * Moves this relation from its current owner entity to the given one.
85  * 
86  * @param nowner the document to which this relation is moved
87  * */
88     public void moveTo (Entity nowner) {
89         //RKV      Session  session = Database.getCurSession();
90
91       this.owner = nowner;
92       nowner.getAllRelations().add(this);
93 //    myold.getAllRelations().remove(this);  Harmful as it leads to remove this relation from the database (!?)
94     //RKV      session.update(this);
95     //RKV      session.update(nowner);
96
97       if (this.isBidirectional()) {
98         Relation  link = this.getReverse();
99         link.setTo(nowner);
100         //RKV  session.update(link);
101       }
102     }
103
104 //  ==============================================================================================================================
105 //  Abstract functions
106 //  ==============================================================================================================================
107
108     public    abstract Persistent getTo ();
109     protected abstract void       setTo (Persistent to);         // For the need of the moveTo() method
110 }