]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/dal/bo/som/Scenario.java
Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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                 // ------------------------------------------------------------
72                 private Study owner = null;
73                 private Scenario previous = null;
74                 private Step base = null;
75                 private String title = null;
76                 private String summary = null;
77                 private User manager = null;
78                 private Date date = null;
79
80                 // - Public services
81
82                 public void clear() {
83                         super.clear();
84                         owner = null;
85                         previous = null;
86                         base = null;
87                         title = null;
88                         summary = null;
89                         manager = null;
90                         date = null;
91                 }
92
93                 // - Protected services
94
95                 public Step getBaseStep() {
96                         return base; // May be null
97                 }
98
99                 public Scenario getInsertAfter() {
100                         return previous; // May be null
101                 }
102
103                 public User getManager() {
104                         return manager;
105                 }
106
107                 public Properties setOwnerStudy(Study owner) {
108                         this.owner = owner;
109                         return this;
110                 }
111
112                 // - Setters of Scenario properties
113
114                 public Properties setBaseStep(Step base)
115                                 throws InvalidPropertyException {
116                         if (!(base.getOwner() instanceof Scenario))
117                                 throw new InvalidPropertyException("step");
118                         this.base = base;
119                         return this;
120                 }
121
122                 public Properties setDate(Date date) {
123                         this.date = date;
124                         return this;
125                 }
126
127                 public Properties setDescription(String summary) {
128                         if (summary.length() > 0)
129                                 this.summary = summary;
130                         return this;
131                 }
132
133                 public Properties setInsertAfter(Scenario previous) {
134                         this.previous = previous;
135                         return this;
136                 }
137
138                 public Properties setManager(User user) {
139                         this.manager = user;
140                         return this;
141                 }
142
143                 public Properties setTitle(String title)
144                                 throws InvalidPropertyException {
145                         if (title.length() == 0)
146                                 throw new InvalidPropertyException("title");
147                         this.title = title;
148                         return this;
149                 }
150
151                 // - Global validity check
152
153                 public void checkValidity() throws MissedPropertyException,
154                                 InvalidPropertyException, MultiplyDefinedException {
155                         if (owner == null)
156                                 throw new MissedPropertyException("owner");
157                         if (title == null)
158                                 throw new MissedPropertyException("title");
159                         if (manager == null)
160                                 throw new MissedPropertyException("manager");
161                 }
162         }
163
164         // Database fetch constructor
165         protected Scenario() {
166                 // ---------------------
167                 known = null;
168                 knowl = null;
169                 ucase = null;
170         }
171
172         // Internal constructor
173         public Scenario(Properties sprop) throws MissedPropertyException,
174                         InvalidPropertyException, MultiplyDefinedException {
175                 // -------------------------------------
176                 super(sprop); // Throws one of the above exception if not valid
177                 owner = sprop.owner;
178                 sid = 0;
179                 cuser = null;
180                 title = sprop.title; // Inherited attribute
181                 known = null;
182                 knowl = null; // Initialized when getting all Knowledge Elements
183                 ucase = null;
184                 kelms = new HashSet<KnowledgeElement>();
185
186                 manager = sprop.manager;
187                 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
188                 // InvalidPropertyException("manager");
189
190                 credate = sprop.date; // Inherited attribute
191                 if (credate == null) {
192                         Calendar current = Calendar.getInstance();
193                         credate = current.getTime(); // Today
194                 }
195                 lasdate = credate; // Inherited attribute
196
197                 if (sprop.summary != null)
198                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
199
200                 Scenario[] scene = owner.getScenarii();
201                 for (int i = 0; i < scene.length; i++)
202                         if (scene[i].sid > this.sid)
203                                 this.sid = scene[i].sid;
204                 sid += 1;
205         }
206
207         // ==============================================================================================================================
208         // Public member functions
209         // ==============================================================================================================================
210
211         public List<KnowledgeElement> getAllKnowledgeElements() {
212                 // --------------------------------------------------------
213                 if (knowl == null) {
214                         knowl = new Vector<KnowledgeElement>(kelms.size());
215                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
216                                 KnowledgeElement kelm = i.next();
217                                 if (kelm.getType().equals("usecase"))
218                                         ucase = kelm;
219                                 else
220                                         knowl.add(kelm);
221                         }
222                 }
223                 return Collections.unmodifiableList(knowl);
224         }
225
226         public KnowledgeElement getKnowledgeElement(long l) {
227                 // -------------------------------------------------------
228                 for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
229                         KnowledgeElement mykelm = i.next();
230                         if (mykelm.getIndex() == l)
231                                 return mykelm;
232                 }
233                 return null;
234         }
235
236         public List<KnowledgeElement> getKnowledgeElementsOf(
237                         KnowledgeElementType type) {
238                 // --------------------------------------------------------------------------------
239                 if (kelms.isEmpty())
240                         return new Vector<KnowledgeElement>(); // Smarter than returning null
241                 if (known == null)
242                         known = new HashMap<Long, List<KnowledgeElement>>();
243
244                 long numtype = type.getIndex();
245                 List<KnowledgeElement> listype = known.get(numtype);
246                 if (listype == null) {
247                         listype = new Vector<KnowledgeElement>();
248                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
249                                 KnowledgeElement kelm = i.next();
250                                 if (kelm.getType().getIndex() == numtype)
251                                         listype.add(kelm);
252                         }
253                         known.put(numtype, listype);
254                 }
255                 return listype; // No protection against this object corruption as it would not corrupt the database
256         }
257
258         public Study getOwnerStudy() {
259                 // -----------------------------
260                 return owner;
261         }
262
263         /**
264          * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
265          */
266         public String getReference() {
267                 // -----------------------------
268                 return String.valueOf(sid);
269         }
270
271         public User getUser() {
272                 return cuser; // Null if the scenario has not been checked-out
273         }
274
275         public void setUser(User aUser) {
276                 cuser = aUser; // Null if the scenario has not been checked-out
277         }
278
279         public boolean isCheckedout() {
280                 // ------------------------------
281                 return (cuser != null);
282         }
283
284         // ==============================================================================================================================
285         // Private services
286         // ==============================================================================================================================
287         /**
288          * Get the persistent set of knowledge elements.
289          * 
290          * @return the persistent set of scenario knowledge elements
291          */
292         public Set<KnowledgeElement> getKnowledgeElements() {
293                 return kelms;
294         }
295
296         /**
297          * Get the transient list of knowledge elements.
298          * 
299          * @return the transient list of knowledge elements
300          */
301         public List<KnowledgeElement> getKnowledgeElementsList() {
302                 return knowl;
303         }
304
305         /**
306          * Set the scenario transient "use case" knowledge element.
307          * 
308          * @param kelm
309          *            the scenario transient "use case" knowledge element
310          */
311         public void setUcase(KnowledgeElement kelm) {
312                 ucase = kelm;
313         }
314
315         /**
316          * Get the scenario transient "use case" knowledge element.
317          * 
318          * @return the scenario transient "use case" knowledge element
319          */
320         public KnowledgeElement getUcase() {
321                 return ucase;
322         }
323
324         /**
325          * Get the known.
326          * 
327          * @return the known
328          */
329         public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {
330                 return known;
331         }
332 }