Salome HOME
3fb81692059c923b7467d899066b7417b3ffd75e
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Relation.java
1 package org.splat.dal.bo.kernel;
2
3
4 /**
5  * Base implementation of relations between entities.<br/> A relation makes a typed link from an entity to any kind persistent object. The
6  * relations are typed by subclasses of this abstract class which define the actual object referenced by the relation.<br/> This Relation
7  * base class implements unidirectional relations. The bidirectionality must be implemented in concrete subclasses 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-2014
17  */
18 public abstract class Relation extends Any {
19
20         // Persistent fields
21         protected Entity owner; // Study or Document
22
23         // Transient field
24         protected Relation reverse;
25
26         // ==============================================================================================================================
27         // Constructors
28         // ==============================================================================================================================
29
30         // Database fetch constructor
31         protected Relation() {
32                 reverse = null;
33         }
34
35         // Initialization constructor
36         protected Relation(final Entity from) {
37                 this.owner = from;
38                 this.reverse = null; // Initialized by subclasses
39         }
40
41         // ==============================================================================================================================
42         // Public member functions
43         // ==============================================================================================================================
44
45         public Entity getFrom() {
46                 return owner;
47         }
48
49         public Relation getReverse() {
50                 if (this.isBidirectional() && reverse == null) {
51                         Class<? extends Relation> type = this.getReverseClass();
52                         Entity to = (Entity) this.getTo(); // Bidirectional relations are necessarily between Entities
53
54                         for (Relation asked : to.getAllRelations()) {
55                                 if (asked.getClass().equals(type)
56                                                 && asked.getTo().equals(owner)) {
57                                         reverse = asked;
58                                         reverse.reverse = this; // For benefiting from this execution
59                                         break;
60                                 }
61                         }
62                 }
63                 return reverse;
64         }
65
66         public Class<? extends Relation> getReverseClass() {
67                 return null;
68         }
69
70         public boolean isBidirectional() {
71                 return false;
72         }
73
74         /**
75          * Moves this relation from its current owner entity to the given one.
76          * 
77          * @param nowner
78          *            the document to which this relation is moved
79          */
80         public void moveTo(final Entity nowner) {
81
82                 Entity oldOwner = this.owner;
83                 this.owner = nowner;
84                 nowner.getAllRelations().add(this);
85                 oldOwner.getAllRelations().remove(this);
86
87                 if (this.isBidirectional()) {
88                         Relation link = this.getReverse();
89                         link.setTo(nowner);
90                 }
91         }
92
93         // ==============================================================================================================================
94         // Abstract functions
95         // ==============================================================================================================================
96
97         public abstract Persistent getTo();
98
99         public abstract void setTo(Persistent to); // For the need of the moveTo() method
100 }