Salome HOME
Mapping is fixed. Relation in the mapping is inherited from Persistent now. Versionin...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / Scenario.java
1 package org.splat.dal.bo.som;
2
3 /**
4  * 
5  * @author    Daniel Brunier-Coulin
6  * @copyright OPEN CASCADE 2012
7  */
8
9 import java.util.Calendar;
10 import java.util.Collections;
11 import java.util.Date;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.HashSet;
17 import java.util.Vector;
18
19 import org.splat.dal.bo.kernel.Persistent;
20 import org.splat.dal.bo.kernel.User;
21 import org.splat.kernel.InvalidPropertyException;
22 import org.splat.kernel.MissedPropertyException;
23 import org.splat.kernel.MultiplyDefinedException;
24 import org.splat.som.Step;
25
26 /**
27  * Scenario persistent object.
28  */
29 public class Scenario extends ProjectElement {
30
31         // Persistent fields
32         /**
33          * Persistent property owner. It is a parent study of the scenario.
34          */
35         private Study owner;
36         /**
37          * Persistent property sid. It is an unique identifier in the scope of owner study.
38          */
39         private int sid;
40         /**
41          * Persistent property cuser. It is a user having checked-out the scenario, if done.
42          */
43         private User cuser;
44         /**
45          * The persistent set of scenario knowledge elements.
46          */
47         private Set<KnowledgeElement> kelms = new HashSet<KnowledgeElement>();
48
49         // Transient fields
50         /**
51          * The transient map of knowledge elements grouped by types.
52          */
53         transient private HashMap<Long, List<KnowledgeElement>> known;
54         /**
55          * The scenario transient list of knowledge elements.
56          */
57         transient private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
58         /**
59          * The scenario transient "use case" knowledge element.
60          */
61         transient private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
62
63         // ==============================================================================================================================
64         // Construction
65         // ==============================================================================================================================
66
67         /**
68          * Fields initialization class.
69          */
70         public static class Properties extends Persistent.Properties {
71                 private Study owner = null;
72                 private Scenario previous = null;
73                 private Step base = null;
74                 private String title = null;
75                 private String summary = null;
76                 private User manager = null;
77                 private Date date = null;
78
79                 // - Public services
80
81                 public void clear() {
82                         super.clear();
83                         owner = null;
84                         previous = null;
85                         base = null;
86                         title = null;
87                         summary = null;
88                         manager = null;
89                         date = null;
90                 }
91
92                 // - Protected services
93
94                 public Step getBaseStep() {
95                         return base; // May be null
96                 }
97
98                 public Scenario getInsertAfter() {
99                         return previous; // May be null
100                 }
101
102                 public User getManager() {
103                         return manager;
104                 }
105
106                 public Properties setOwnerStudy(Study owner) {
107                         this.owner = owner;
108                         return this;
109                 }
110
111                 // - Setters of Scenario properties
112
113                 public Properties setBaseStep(Step base)
114                                 throws InvalidPropertyException {
115                         if (!(base.getOwner() instanceof Scenario))
116                                 throw new InvalidPropertyException("step");
117                         this.base = base;
118                         return this;
119                 }
120
121                 public Properties setDate(Date date) {
122                         this.date = date;
123                         return this;
124                 }
125
126                 public Properties setDescription(String summary) {
127                         if (summary.length() > 0)
128                                 this.summary = summary;
129                         return this;
130                 }
131
132                 public Properties setInsertAfter(Scenario previous) {
133                         this.previous = previous;
134                         return this;
135                 }
136
137                 public Properties setManager(User user) {
138                         this.manager = user;
139                         return this;
140                 }
141
142                 public Properties setTitle(String title)
143                                 throws InvalidPropertyException {
144                         if (title.length() == 0)
145                                 throw new InvalidPropertyException("title");
146                         this.title = title;
147                         return this;
148                 }
149
150                 // - Global validity check
151
152                 public void checkValidity() throws MissedPropertyException,
153                                 InvalidPropertyException, MultiplyDefinedException {
154                         if (owner == null)
155                                 throw new MissedPropertyException("owner");
156                         if (title == null)
157                                 throw new MissedPropertyException("title");
158                         if (manager == null)
159                                 throw new MissedPropertyException("manager");
160                 }
161         }
162
163         // Database fetch constructor
164         protected Scenario() {
165                 known = null;
166                 knowl = null;
167                 ucase = null;
168         }
169
170         // Internal constructor
171         public Scenario(Properties sprop) throws MissedPropertyException,
172                         InvalidPropertyException, MultiplyDefinedException {
173                 super(sprop); // Throws one of the above exception if not valid
174                 owner = sprop.owner;
175                 sid = 0;
176                 cuser = null;
177                 title = sprop.title; // Inherited attribute
178                 known = null;
179                 knowl = null; // Initialized when getting all Knowledge Elements
180                 ucase = null;
181
182                 manager = sprop.manager;
183                 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
184                 // InvalidPropertyException("manager");
185
186                 credate = sprop.date; // Inherited attribute
187                 if (credate == null) {
188                         Calendar current = Calendar.getInstance();
189                         credate = current.getTime(); // Today
190                 }
191                 lasdate = credate; // Inherited attribute
192
193                 if (sprop.summary != null)
194                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
195
196                 Scenario[] scene = owner.getScenarii();
197                 for (int i = 0; i < scene.length; i++)
198                         if (scene[i].sid > this.sid)
199                                 this.sid = scene[i].sid;
200                 sid += 1;
201         }
202
203         // ==============================================================================================================================
204         // Public member functions
205         // ==============================================================================================================================
206
207         public List<KnowledgeElement> getAllKnowledgeElements() {
208                 if (knowl == null) {
209                         knowl = new Vector<KnowledgeElement>(kelms.size());
210                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
211                                 KnowledgeElement kelm = i.next();
212                                 if (kelm.getType().equals("usecase"))
213                                         ucase = kelm;
214                                 else
215                                         knowl.add(kelm);
216                         }
217                 }
218                 return Collections.unmodifiableList(knowl);
219         }
220
221         public KnowledgeElement getKnowledgeElement(long l) {
222                 for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
223                         KnowledgeElement mykelm = i.next();
224                         if (l == mykelm.getIndex()) {
225                                 return mykelm;
226                         }
227                 }
228                 return null;
229         }
230
231 /* RKV: Not used
232  *      public List<KnowledgeElement> getKnowledgeElementsOf(
233                         KnowledgeElementType type) {
234                 if (known == null)
235                         known = new HashMap<Long, List<KnowledgeElement>>();
236
237                 long numtype = type.getIndex();
238                 List<KnowledgeElement> listype = known.get(numtype);
239                 if (listype == null) {
240                         listype = new Vector<KnowledgeElement>();
241                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
242                                 KnowledgeElement kelm = i.next();
243                                 if (kelm.getType().getIndex() == numtype)
244                                         listype.add(kelm);
245                         }
246                         known.put(numtype, listype);
247                 }
248                 return listype; // No protection against this object corruption as it would not corrupt the database
249         }
250 */
251         public Study getOwnerStudy() {
252                 // -----------------------------
253                 return owner;
254         }
255
256         /**
257          * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
258          */
259         public String getReference() {
260                 // -----------------------------
261                 return String.valueOf(sid);
262         }
263
264         public User getUser() {
265                 return cuser; // Null if the scenario has not been checked-out
266         }
267
268         public void setUser(User aUser) {
269                 cuser = aUser; // Null if the scenario has not been checked-out
270         }
271
272         public boolean isCheckedout() {
273                 // ------------------------------
274                 return (cuser != null);
275         }
276
277         // ==============================================================================================================================
278         // Private services
279         // ==============================================================================================================================
280         /**
281          * Get the persistent set of knowledge elements.
282          * 
283          * @return the persistent set of scenario knowledge elements
284          */
285         public Set<KnowledgeElement> getKnowledgeElements() {
286                 return kelms;
287         }
288
289         /**
290          * Get the transient list of knowledge elements.
291          * 
292          * @return the transient list of knowledge elements
293          */
294         public List<KnowledgeElement> getKnowledgeElementsList() {
295                 return knowl;
296         }
297
298         /**
299          * Set the scenario transient "use case" knowledge element.
300          * 
301          * @param kelm
302          *            the scenario transient "use case" knowledge element
303          */
304         public void setUcase(KnowledgeElement kelm) {
305                 ucase = kelm;
306         }
307
308         /**
309          * Get the scenario transient "use case" knowledge element.
310          * 
311          * @return the scenario transient "use case" knowledge element
312          */
313         public KnowledgeElement getUcase() {
314                 return ucase;
315         }
316
317         /**
318          * Get the known.
319          * 
320          * @return the known
321          */
322 /* RKV: Not used
323  *      public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {
324                 return known;
325         }
326 */}