]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/kernel/Any.java
Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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.splat.kernel.InvalidPropertyException;
17 import org.splat.kernel.MissedPropertyException;
18 import org.splat.kernel.MultiplyDefinedException;
19 import org.splat.kernel.ObjectProperties;
20
21
22 public abstract class Any extends Persistent {
23
24     private  Set<Attribute> attributes;
25
26 //  ==============================================================================================================================
27 //  Constructors
28 //  ==============================================================================================================================
29
30 //  Database fetch constructor.
31     protected Any () {
32     }
33 //  Initialization constructors
34     protected Any (ObjectProperties oprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
35 //  --------------------------------------
36       super(oprop);
37       attributes = new HashSet<Attribute>();
38     }
39     protected Any (Attribute... field) {
40 //  ----------------------------------
41       attributes = new HashSet<Attribute>();
42       for (int i=0; i<field.length; i++) {
43         if (field[i] == null) continue;     // Happen when newing an Any object without property 
44         if (field[i].getFrom().equals(this)) attributes.add(field[i]);
45       }
46     }
47
48 //  ==============================================================================================================================
49 //  Public member functions
50 //  ==============================================================================================================================
51
52     public Attribute getAttribute (Class<? extends Attribute> type) {
53 //  ---------------------------------------------------------------
54       for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
55         Attribute field = i.next();
56         if (field.getClass().equals(type)) return field;
57       }
58       return null;
59     }
60
61 //  ==============================================================================================================================
62 //  Protected services
63 //  ==============================================================================================================================
64
65     protected boolean removeAttribute (Attribute field) {
66 //  ---------------------------------------------------
67       for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
68         if (!i.next().equals(field)) continue;
69         i.remove();
70         //RKV        if (this.isSaved()) Database.getCurSession().update(this);
71         return true;
72       }
73       return false;
74     }
75
76     public boolean setAttribute (Attribute field) {
77 //  ------------------------------------------------
78       Class<?> type    = field.getClass();
79 //RKV      Session  session = Database.getCurSession();
80
81       if (!field.getFrom().equals(this)) return false;
82       for (Iterator<Attribute> i=attributes.iterator(); i.hasNext(); ) {
83         if (!i.next().getClass().equals(type)) continue;
84         i.remove();
85         break;
86       }
87       attributes.add(field);
88       if (this.isSaved()) {
89         //RKV        if (!field.isSaved()) session.save(field);
90         //RKV           session.update(this);
91       }     // Else, when saving this, Hibernate will propagate the operation
92       return true;
93     }
94 }