1 package org.splat.dal.bo.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.dal.dao.som.Database;
18 import org.splat.kernel.InvalidPropertyException;
19 import org.splat.kernel.MissedPropertyException;
20 import org.splat.kernel.MultiplyDefinedException;
21 import org.splat.kernel.ObjectProperties;
24 public abstract class Any extends Persistent {
26 private Set<Attribute> attributes;
28 // ==============================================================================================================================
30 // ==============================================================================================================================
32 // Database fetch constructor.
35 // Initialization constructors
36 protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
37 // --------------------------------------
39 attributes = new HashSet<Attribute>();
41 protected Any (Attribute... field) {
42 // ----------------------------------
43 attributes = new HashSet<Attribute>();
44 for (int i=0; i<field.length; i++) {
45 if (field[i] == null) continue; // Happen when newing an Any object without property
46 if (field[i].getFrom().equals(this)) attributes.add(field[i]);
50 // ==============================================================================================================================
51 // Public member functions
52 // ==============================================================================================================================
54 public Attribute getAttribute (Class<? extends Attribute> type) {
55 // ---------------------------------------------------------------
56 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
57 Attribute field = i.next();
58 if (field.getClass().equals(type)) return field;
63 // ==============================================================================================================================
65 // ==============================================================================================================================
67 protected boolean removeAttribute (Attribute field) {
68 // ---------------------------------------------------
69 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
70 if (!i.next().equals(field)) continue;
72 if (this.isSaved()) Database.getSession().update(this);
78 public boolean setAttribute (Attribute field) {
79 // ------------------------------------------------
80 Class<?> type = field.getClass();
81 Session session = Database.getSession();
83 if (!field.getFrom().equals(this)) return false;
84 for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
85 if (!i.next().getClass().equals(type)) continue;
89 attributes.add(field);
91 if (!field.isSaved()) session.save(field);
93 } // Else, when saving this, Hibernate will propagate the operation