]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Any.java
Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Any.java
1 package org.splat.dal.bo.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.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;
22
23
24 public abstract class Any extends Persistent {
25
26     private  Set<Attribute> attributes;
27
28 //  ==============================================================================================================================
29 //  Constructors
30 //  ==============================================================================================================================
31
32 //  Database fetch constructor.
33     protected Any () {
34     }
35 //  Initialization constructors
36     protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
37 //  --------------------------------------
38       super(oprop);
39       attributes = new HashSet<Attribute>();
40     }
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]);
47       }
48     }
49
50 //  ==============================================================================================================================
51 //  Public member functions
52 //  ==============================================================================================================================
53
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;
59       }
60       return null;
61     }
62
63 //  ==============================================================================================================================
64 //  Protected services
65 //  ==============================================================================================================================
66
67     protected boolean removeAttribute (Attribute field) {
68 //  ---------------------------------------------------
69       for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
70         if (!i.next().equals(field)) continue;
71         i.remove();
72         if (this.isSaved()) Database.getSession().update(this);
73         return true;
74       }
75       return false;
76     }
77
78     public boolean setAttribute (Attribute field) {
79 //  ------------------------------------------------
80       Class<?> type    = field.getClass();
81       Session  session = Database.getSession();
82
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;
86         i.remove();
87         break;
88       }
89       attributes.add(field);
90       if (this.isSaved()) {
91         if (!field.isSaved()) session.save(field);
92         session.update(this);
93       }     // Else, when saving this, Hibernate will propagate the operation
94       return true;
95     }
96 }