Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[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 import org.hibernate.Session;
22 import org.splat.dal.dao.kernel.Database;
23
24
25 public abstract class Relation extends Any {
26
27 //  Persistent fields
28         protected  Entity   owner;        // Study or Document
29
30 //  Transient field
31     protected  Relation reverse;
32
33 //  ==============================================================================================================================
34 //  Constructors
35 //  ==============================================================================================================================
36
37 //  Database fetch constructor
38     protected Relation () {
39 //  ---------------------
40       reverse = null;
41     }
42 //  Initialization constructor
43     protected Relation (Entity from) {
44 //  --------------------------------
45       super((Attribute)null);       // For building the collection of attributes
46       this.owner   = from;
47       this.reverse = null;          // Initialized by subclasses
48     }
49
50 //  ==============================================================================================================================
51 //  Public member functions
52 //  ==============================================================================================================================
53
54     public Entity getFrom () {
55 //  ------------------------
56       return owner;
57     }
58
59     public Relation getReverse () {
60 //  -----------------------------
61       if (!this.isBidirectional() || reverse != null) return reverse;
62
63       Class<? extends Relation> type = this.getReverseClass();
64       Entity                    to   = (Entity)this.getTo();   // Bidirectional relations are necessarily between Entities
65
66       for (Iterator<Relation> i=to.getAllRelations().iterator(); i.hasNext(); ) {
67         Relation asked = i.next();
68         if (!asked.getClass().equals(type)) continue;
69         if (!asked.getTo().equals(owner))   continue;
70         reverse = asked;
71         reverse.reverse = this;     // For benefiting from this execution
72         return reverse;
73       }
74       return null;
75     }
76
77     public Class<? extends Relation> getReverseClass () {
78 //  ---------------------------------------------------
79       return null;
80     }
81
82     public boolean isBidirectional () {
83 //  ---------------------------------
84       return false;
85     }
86
87 /**
88  * Moves this relation from its current owner entity to the given one.
89  * 
90  * @param nowner the document to which this relation is moved
91  * */
92     public void moveTo (Entity nowner) {
93 //  ----------------------------------
94       Session  session = Database.getSession();
95
96       this.owner = nowner;
97       nowner.getAllRelations().add(this);
98 //    myold.getAllRelations().remove(this);  Harmful as it leads to remove this relation from the database (!?)
99       session.update(this);
100       session.update(nowner);
101
102       if (this.isBidirectional()) {
103         Relation  link = this.getReverse();
104         link.setTo(nowner);
105         session.update(link);
106       }
107     }
108
109 //  ==============================================================================================================================
110 //  Abstract functions
111 //  ==============================================================================================================================
112
113     public    abstract Persistent getTo ();
114     protected abstract void       setTo (Persistent to);         // For the need of the moveTo() method
115 }