1 package org.splat.dal.bo.kernel;
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
9 * <li>overriding the isBidirectional() and getReverseClass() methods,</li>
10 * <li>creating the reverse relation in constructors.</li>
12 * Relation objects also support dynamic attributes provided by the Any class.
15 * @author Daniel Brunier-Coulin
16 * @copyright OPEN CASCADE 2012
19 import java.util.Iterator;
21 public abstract class Relation extends Any {
24 protected Entity owner; // Study or Document
27 protected Relation reverse;
29 // ==============================================================================================================================
31 // ==============================================================================================================================
33 // Database fetch constructor
34 protected Relation () {
35 // ---------------------
38 // Initialization constructor
39 protected Relation (Entity from) {
40 // --------------------------------
41 super((Attribute)null); // For building the collection of attributes
43 this.reverse = null; // Initialized by subclasses
46 // ==============================================================================================================================
47 // Public member functions
48 // ==============================================================================================================================
50 public Entity getFrom () {
51 // ------------------------
55 public Relation getReverse () {
56 // -----------------------------
57 if (!this.isBidirectional() || reverse != null) return reverse;
59 Class<? extends Relation> type = this.getReverseClass();
60 Entity to = (Entity)this.getTo(); // Bidirectional relations are necessarily between Entities
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;
67 reverse.reverse = this; // For benefiting from this execution
73 public Class<? extends Relation> getReverseClass () {
74 // ---------------------------------------------------
78 public boolean isBidirectional () {
79 // ---------------------------------
84 * Moves this relation from its current owner entity to the given one.
86 * @param nowner the document to which this relation is moved
88 public void moveTo (Entity nowner) {
89 //RKV Session session = Database.getCurSession();
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);
97 if (this.isBidirectional()) {
98 Relation link = this.getReverse();
100 //RKV session.update(link);
104 // ==============================================================================================================================
105 // Abstract functions
106 // ==============================================================================================================================
108 public abstract Persistent getTo ();
109 protected abstract void setTo (Persistent to); // For the need of the moveTo() method