Salome HOME
Refactoring: kernel and som are moved to Siman-Common.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / Any.java
1 package org.splat.kernel;
2 /**
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.
6  * 
7  * @see Attribute
8  * @author    Daniel Brunier-Coulin
9  * @copyright OPEN CASCADE 2012
10  */
11
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import org.hibernate.Session;
17 import org.splat.som.Database;
18
19
20 public abstract class Any extends Persistent {
21
22     private  Set<Attribute> attributes;
23
24 //  ==============================================================================================================================
25 //  Constructors
26 //  ==============================================================================================================================
27
28 //  Database fetch constructor.
29     protected Any () {
30     }
31 //  Initialization constructors
32     protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
33 //  --------------------------------------
34       super(oprop);
35       attributes = new HashSet<Attribute>();
36     }
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]);
43       }
44     }
45
46 //  ==============================================================================================================================
47 //  Public member functions
48 //  ==============================================================================================================================
49
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;
55       }
56       return null;
57     }
58
59 //  ==============================================================================================================================
60 //  Protected services
61 //  ==============================================================================================================================
62
63     protected boolean removeAttribute (Attribute field) {
64 //  ---------------------------------------------------
65       for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
66         if (!i.next().equals(field)) continue;
67         i.remove();
68         if (this.isSaved()) Database.getSession().update(this);
69         return true;
70       }
71       return false;
72     }
73
74     protected boolean setAttribute (Attribute field) {
75 //  ------------------------------------------------
76       Class<?> type    = field.getClass();
77       Session  session = Database.getSession();
78
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;
82         i.remove();
83         break;
84       }
85       attributes.add(field);
86       if (this.isSaved()) {
87         if (!field.isSaved()) session.save(field);
88         session.update(this);
89       }     // Else, when saving this, Hibernate will propagate the operation
90       return true;
91     }
92 }