1 package org.splat.kernel;
3 * Abstract root class of persistent objects supporting dynamic attributes.<br/>
4 * Dynamic attributes are instances of concrete subclasses of Attribute that are assigned to Any objects at run time.
5 * The attributes of a given Any object must all be of different types.
8 * @author Daniel Brunier-Coulin
9 * @copyright OPEN CASCADE 2012
12 import java.util.HashSet;
13 import java.util.Iterator;
16 import org.hibernate.Session;
17 import org.splat.som.Database;
20 public abstract class Any extends Persistent {
22 private Set<Attribute> attributes;
24 // ==============================================================================================================================
26 // ==============================================================================================================================
28 // Database fetch constructor.
31 // Initialization constructors
32 protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
33 // --------------------------------------
35 attributes = new HashSet<Attribute>();
37 protected Any (Attribute... field) {
38 // ----------------------------------
39 attributes = new HashSet<Attribute>();
40 for (int i=0; i<field.length; i++) {
41 if (field[i] == null) continue; // Happen when newing an Any object without property
42 if (field[i].getFrom().equals(this)) attributes.add(field[i]);
46 // ==============================================================================================================================
47 // Public member functions
48 // ==============================================================================================================================
50 public Attribute getAttribute (Class<? extends Attribute> type) {
51 // ---------------------------------------------------------------
52 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
53 Attribute field = i.next();
54 if (field.getClass().equals(type)) return field;
59 // ==============================================================================================================================
61 // ==============================================================================================================================
63 protected boolean removeAttribute (Attribute field) {
64 // ---------------------------------------------------
65 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
66 if (!i.next().equals(field)) continue;
68 if (this.isSaved()) Database.getSession().update(this);
74 protected boolean setAttribute (Attribute field) {
75 // ------------------------------------------------
76 Class<?> type = field.getClass();
77 Session session = Database.getSession();
79 if (!field.getFrom().equals(this)) return false;
80 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
81 if (!i.next().getClass().equals(type)) continue;
85 attributes.add(field);
87 if (!field.isSaved()) session.save(field);
89 } // Else, when saving this, Hibernate will propagate the operation