1 package org.splat.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 import org.hibernate.Session;
24 public abstract class Relation extends Any {
27 protected Entity owner; // Study or Document
30 protected Relation reverse;
32 // ==============================================================================================================================
34 // ==============================================================================================================================
36 // Database fetch constructor
37 protected Relation () {
38 // ---------------------
41 // Initialization constructor
42 protected Relation (Entity from) {
43 // --------------------------------
44 super((Attribute)null); // For building the collection of attributes
46 this.reverse = null; // Initialized by subclasses
49 // ==============================================================================================================================
50 // Public member functions
51 // ==============================================================================================================================
53 public Entity getFrom () {
54 // ------------------------
58 public Relation getReverse () {
59 // -----------------------------
60 if (!this.isBidirectional() || reverse != null) return reverse;
62 Class<? extends Relation> type = this.getReverseClass();
63 Entity to = (Entity)this.getTo(); // Bidirectional relations are necessarily between Entities
65 for (Iterator<Relation> i=to.getAllRelations().iterator(); i.hasNext(); ) {
66 Relation asked = i.next();
67 if (!asked.getClass().equals(type)) continue;
68 if (!asked.getTo().equals(owner)) continue;
70 reverse.reverse = this; // For benefiting from this execution
76 public Class<? extends Relation> getReverseClass () {
77 // ---------------------------------------------------
81 public boolean isBidirectional () {
82 // ---------------------------------
87 * Moves this relation from its current owner entity to the given one.
89 * @param nowner the document to which this relation is moved
91 public void moveTo (Entity nowner) {
92 // ----------------------------------
93 Session session = Database.getSession();
96 nowner.getAllRelations().add(this);
97 // myold.getAllRelations().remove(this); Harmful as it leads to remove this relation from the database (!?)
99 session.update(nowner);
101 if (this.isBidirectional()) {
102 Relation link = this.getReverse();
104 session.update(link);
108 // ==============================================================================================================================
109 // Abstract functions
110 // ==============================================================================================================================
112 public abstract Persistent getTo ();
113 protected abstract void setTo (Persistent to); // For the need of the moveTo() method