Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / kernel / Any.java
1 package org.splat.dal.bo.kernel;
2
3 /**
4  * Abstract root class of persistent objects supporting dynamic attributes.<br/>
5  * Dynamic attributes are instances of concrete subclasses of Attribute that are assigned to Any objects at run time.
6  * The attributes of a given Any object must all be of different types.
7  * 
8  * @see Attribute
9  * @author    Daniel Brunier-Coulin
10  * @copyright OPEN CASCADE 2012
11  */
12
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.Set;
16
17 import org.splat.kernel.InvalidPropertyException;
18 import org.splat.kernel.MissedPropertyException;
19 import org.splat.kernel.MultiplyDefinedException;
20 import org.splat.kernel.ObjectProperties;
21
22 public abstract class Any extends Persistent {
23
24         private final Set<Attribute> attributes = new HashSet<Attribute>();
25
26         // ==============================================================================================================================
27         // Constructors
28         // ==============================================================================================================================
29
30         // Database fetch constructor.
31         protected Any() {
32                 super();
33         }
34
35         // Initialization constructors
36         protected Any(final ObjectProperties oprop) throws MissedPropertyException,
37                         InvalidPropertyException, MultiplyDefinedException {
38                 // --------------------------------------
39                 super(oprop);
40         }
41
42         protected Any(final Attribute... field) {
43                 for (int i = 0; i < field.length; i++) {
44                         if (field[i] == null) {
45                                 continue; // Happen when newing an Any object without property
46                         }
47                         if (field[i].getFrom().equals(this)) {
48                                 getAttributes().add(field[i]);
49                         }
50                 }
51         }
52
53         // ==============================================================================================================================
54         // Public member functions
55         // ==============================================================================================================================
56
57         public Attribute getAttribute(final Class<? extends Attribute> type) {
58                 Attribute found = null;
59                 for (Attribute field : getAttributes()) {
60                         if (field.getClass().equals(type)) {
61                                 found = field;
62                                 break;
63                         }
64                 }
65                 return found;
66         }
67
68         // ==============================================================================================================================
69         // Protected services
70         // ==============================================================================================================================
71
72         public boolean removeAttribute(final Attribute field) {
73                 boolean res = false;
74                 for (Iterator<Attribute> i = getAttributes().iterator(); i.hasNext();) {
75                         if (i.next().equals(field)) {
76                                 i.remove();
77                                 res = true;
78                                 break;
79                         }
80                 }
81                 return res;
82         }
83
84         public boolean setAttribute(final Attribute field) {
85                 Class<?> type = field.getClass();
86
87                 if (!field.getFrom().equals(this)) {
88                         return false;
89                 }
90                 for (Iterator<Attribute> i = getAttributes().iterator(); i.hasNext();) {
91                         if (i.next().getClass().equals(type)) {
92                                 i.remove();
93                                 break;
94                         }
95                 }
96                 getAttributes().add(field);
97                 return true;
98         }
99
100         /**
101          * Get the attributes.
102          * 
103          * @return the attributes
104          */
105         protected Set<Attribute> getAttributes() {
106                 return attributes;
107         }
108
109         /**
110          * {@inheritDoc}
111          * 
112          * @see org.splat.dal.bo.kernel.Persistent#evict()
113          */
114         @Override
115         public void evict() {
116                 super.evict();
117                 // Evict all attributes of the persistent object
118                 Set<Attribute> tmpSet = new HashSet<Attribute>();
119                 tmpSet.addAll(attributes);
120                 attributes.clear();
121                 for (Attribute field : tmpSet) {
122                         if (field.isSaved()) { // to avoid recursive evict
123                                 field.evict();
124                         }
125                         attributes.add(field);
126                 }
127         }
128 }