Salome HOME
f473093bc1395c7d0fe114a03cf0a5fa8e645060
[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;
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                 kelms = new HashSet<KnowledgeElement>();
182
183                 manager = sprop.manager;
184                 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
185                 // InvalidPropertyException("manager");
186
187                 credate = sprop.date; // Inherited attribute
188                 if (credate == null) {
189                         Calendar current = Calendar.getInstance();
190                         credate = current.getTime(); // Today
191                 }
192                 lasdate = credate; // Inherited attribute
193
194                 if (sprop.summary != null)
195                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
196
197                 Scenario[] scene = owner.getScenarii();
198                 for (int i = 0; i < scene.length; i++)
199                         if (scene[i].sid > this.sid)
200                                 this.sid = scene[i].sid;
201                 sid += 1;
202         }
203
204         // ==============================================================================================================================
205         // Public member functions
206         // ==============================================================================================================================
207
208         public List<KnowledgeElement> getAllKnowledgeElements() {
209                 if (knowl == null) {
210                         knowl = new Vector<KnowledgeElement>(kelms.size());
211                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
212                                 KnowledgeElement kelm = i.next();
213                                 if (kelm.getType().equals("usecase"))
214                                         ucase = kelm;
215                                 else
216                                         knowl.add(kelm);
217                         }
218                 }
219                 return Collections.unmodifiableList(knowl);
220         }
221
222         public KnowledgeElement getKnowledgeElement(long l) {
223                 for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
224                         KnowledgeElement mykelm = i.next();
225                         if (l == mykelm.getIndex()) {
226                                 return mykelm;
227                         }
228                 }
229                 return null;
230         }
231
232 /* RKV: Not used
233  *      public List<KnowledgeElement> getKnowledgeElementsOf(
234                         KnowledgeElementType type) {
235                 if (known == null)
236                         known = new HashMap<Long, List<KnowledgeElement>>();
237
238                 long numtype = type.getIndex();
239                 List<KnowledgeElement> listype = known.get(numtype);
240                 if (listype == null) {
241                         listype = new Vector<KnowledgeElement>();
242                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
243                                 KnowledgeElement kelm = i.next();
244                                 if (kelm.getType().getIndex() == numtype)
245                                         listype.add(kelm);
246                         }
247                         known.put(numtype, listype);
248                 }
249                 return listype; // No protection against this object corruption as it would not corrupt the database
250         }
251 */
252         public Study getOwnerStudy() {
253                 // -----------------------------
254                 return owner;
255         }
256
257         /**
258          * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
259          */
260         public String getReference() {
261                 // -----------------------------
262                 return String.valueOf(sid);
263         }
264
265         public User getUser() {
266                 return cuser; // Null if the scenario has not been checked-out
267         }
268
269         public void setUser(User aUser) {
270                 cuser = aUser; // Null if the scenario has not been checked-out
271         }
272
273         public boolean isCheckedout() {
274                 // ------------------------------
275                 return (cuser != null);
276         }
277
278         // ==============================================================================================================================
279         // Private services
280         // ==============================================================================================================================
281         /**
282          * Get the persistent set of knowledge elements.
283          * 
284          * @return the persistent set of scenario knowledge elements
285          */
286         public Set<KnowledgeElement> getKnowledgeElements() {
287                 return kelms;
288         }
289
290         /**
291          * Get the transient list of knowledge elements.
292          * 
293          * @return the transient list of knowledge elements
294          */
295         public List<KnowledgeElement> getKnowledgeElementsList() {
296                 return knowl;
297         }
298
299         /**
300          * Set the scenario transient "use case" knowledge element.
301          * 
302          * @param kelm
303          *            the scenario transient "use case" knowledge element
304          */
305         public void setUcase(KnowledgeElement kelm) {
306                 ucase = kelm;
307         }
308
309         /**
310          * Get the scenario transient "use case" knowledge element.
311          * 
312          * @return the scenario transient "use case" knowledge element
313          */
314         public KnowledgeElement getUcase() {
315                 return ucase;
316         }
317
318         /**
319          * Get the known.
320          * 
321          * @return the known
322          */
323 /* RKV: Not used
324  *      public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {
325                 return known;
326         }
327 */}