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 // --------------------------------
42 this.reverse = null; // Initialized by subclasses
45 // ==============================================================================================================================
46 // Public member functions
47 // ==============================================================================================================================
49 public Entity getFrom () {
50 // ------------------------
54 public Relation getReverse () {
55 // -----------------------------
56 if (!this.isBidirectional() || reverse != null) return reverse;
58 Class<? extends Relation> type = this.getReverseClass();
59 Entity to = (Entity)this.getTo(); // Bidirectional relations are necessarily between Entities
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;
66 reverse.reverse = this; // For benefiting from this execution
72 public Class<? extends Relation> getReverseClass () {
73 // ---------------------------------------------------
77 public boolean isBidirectional () {
78 // ---------------------------------
83 * Moves this relation from its current owner entity to the given one.
85 * @param nowner the document to which this relation is moved
87 public void moveTo (Entity nowner) {
89 Entity oldOwner = this.owner;
91 nowner.getAllRelations().add(this);
92 oldOwner.getAllRelations().remove(this);
94 if (this.isBidirectional()) {
95 Relation link = this.getReverse();
100 // ==============================================================================================================================
101 // Abstract functions
102 // ==============================================================================================================================
104 public abstract Persistent getTo ();
105 protected abstract void setTo (Persistent to); // For the need of the moveTo() method