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 import org.hibernate.Session;
22 import org.splat.dal.dao.kernel.Database;
25 public abstract class Relation extends Any {
28 protected Entity owner; // Study or Document
31 protected Relation reverse;
33 // ==============================================================================================================================
35 // ==============================================================================================================================
37 // Database fetch constructor
38 protected Relation () {
39 // ---------------------
42 // Initialization constructor
43 protected Relation (Entity from) {
44 // --------------------------------
45 super((Attribute)null); // For building the collection of attributes
47 this.reverse = null; // Initialized by subclasses
50 // ==============================================================================================================================
51 // Public member functions
52 // ==============================================================================================================================
54 public Entity getFrom () {
55 // ------------------------
59 public Relation getReverse () {
60 // -----------------------------
61 if (!this.isBidirectional() || reverse != null) return reverse;
63 Class<? extends Relation> type = this.getReverseClass();
64 Entity to = (Entity)this.getTo(); // Bidirectional relations are necessarily between Entities
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;
71 reverse.reverse = this; // For benefiting from this execution
77 public Class<? extends Relation> getReverseClass () {
78 // ---------------------------------------------------
82 public boolean isBidirectional () {
83 // ---------------------------------
88 * Moves this relation from its current owner entity to the given one.
90 * @param nowner the document to which this relation is moved
92 public void moveTo (Entity nowner) {
93 // ----------------------------------
94 Session session = Database.getSession();
97 nowner.getAllRelations().add(this);
98 // myold.getAllRelations().remove(this); Harmful as it leads to remove this relation from the database (!?)
100 session.update(nowner);
102 if (this.isBidirectional()) {
103 Relation link = this.getReverse();
105 session.update(link);
109 // ==============================================================================================================================
110 // Abstract functions
111 // ==============================================================================================================================
113 public abstract Persistent getTo ();
114 protected abstract void setTo (Persistent to); // For the need of the moveTo() method