Salome HOME
Update copyrights 2014.
[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-2014
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.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Set;
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 final 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 final 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                 @Override
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(final Study owner) {
108                         this.owner = owner;
109                         return this;
110                 }
111
112                 // - Setters of Scenario properties
113
114                 public Properties setBaseStep(final Step base)
115                                 throws InvalidPropertyException {
116                         if (!(base.getOwner() instanceof Scenario)) {
117                                 throw new InvalidPropertyException("step");
118                         }
119                         this.base = base;
120                         return this;
121                 }
122
123                 public Properties setDate(final Date date) {
124                         this.date = date;
125                         return this;
126                 }
127
128                 public Properties setDescription(final String summary) {
129                         if (summary.length() > 0) {
130                                 this.summary = summary;
131                         }
132                         return this;
133                 }
134
135                 public Properties setInsertAfter(final Scenario previous) {
136                         this.previous = previous;
137                         return this;
138                 }
139
140                 public Properties setManager(final User user) {
141                         this.manager = user;
142                         return this;
143                 }
144
145                 public Properties setTitle(final String title)
146                                 throws InvalidPropertyException {
147                         if (title.length() == 0) {
148                                 throw new InvalidPropertyException("title");
149                         }
150                         this.title = title;
151                         return this;
152                 }
153
154                 // - Global validity check
155
156                 public void checkValidity() throws MissedPropertyException,
157                                 InvalidPropertyException, MultiplyDefinedException {
158                         if (owner == null) {
159                                 throw new MissedPropertyException("owner");
160                         }
161                         if (title == null) {
162                                 throw new MissedPropertyException("title");
163                         }
164                         if (manager == null) {
165                                 throw new MissedPropertyException("manager");
166                         }
167                 }
168         }
169
170         // Database fetch constructor
171         protected Scenario() {
172                 known = null;
173                 knowl = null;
174                 ucase = null;
175         }
176
177         // Internal constructor
178         public Scenario(final Properties sprop) throws MissedPropertyException,
179                         InvalidPropertyException, MultiplyDefinedException {
180                 super(sprop); // Throws one of the above exception if not valid
181                 owner = sprop.owner;
182                 sid = 0;
183                 cuser = null;
184                 title = sprop.title; // Inherited attribute
185                 known = null;
186                 knowl = null; // Initialized when getting all Knowledge Elements
187                 ucase = null;
188
189                 manager = sprop.manager;
190                 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
191                 // InvalidPropertyException("manager");
192
193                 credate = sprop.date; // Inherited attribute
194                 if (credate == null) {
195                         Calendar current = Calendar.getInstance();
196                         credate = current.getTime(); // Today
197                 }
198                 lasdate = credate; // Inherited attribute
199
200                 if (sprop.summary != null) {
201                         this.setAttribute(new DescriptionAttribute(this, sprop.summary));
202                 }
203
204                 Scenario[] scene = owner.getScenarii();
205                 for (int i = 0; i < scene.length; i++) {
206                         if (scene[i].sid > this.sid) {
207                                 this.sid = scene[i].sid;
208                         }
209                 }
210                 sid += 1;
211         }
212
213         // ==============================================================================================================================
214         // Public member functions
215         // ==============================================================================================================================
216
217         public List<KnowledgeElement> getAllKnowledgeElements() {
218                 if (knowl == null) {
219                         knowl = new Vector<KnowledgeElement>(kelms.size());
220                         for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
221                                 KnowledgeElement kelm = i.next();
222                                 if (kelm.getType().equals("usecase")) {
223                                         ucase = kelm;
224                                 } else {
225                                         knowl.add(kelm);
226                                 }
227                         }
228                 }
229                 return Collections.unmodifiableList(knowl);
230         }
231
232         public KnowledgeElement getKnowledgeElement(final long l) {
233                 for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
234                         KnowledgeElement mykelm = i.next();
235                         if (l == mykelm.getIndex()) {
236                                 return mykelm;
237                         }
238                 }
239                 return null;
240         }
241
242         /**
243          * {@inheritDoc}
244          * 
245          * @see org.splat.dal.bo.som.ProjectElement#getOwnerStudy()
246          */
247         @Override
248         public Study getOwnerStudy() {
249                 return owner;
250         }
251
252         /**
253          * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
254          */
255         public String getReference() {
256                 return String.valueOf(sid);
257         }
258
259         public User getUser() {
260                 return cuser; // Null if the scenario has not been checked-out
261         }
262
263         public void setUser(final User aUser) {
264                 cuser = aUser; // Null if the scenario has not been checked-out
265         }
266
267         public boolean isCheckedout() {
268                 return (cuser != null);
269         }
270
271         // ==============================================================================================================================
272         // Private services
273         // ==============================================================================================================================
274         /**
275          * Get the persistent set of knowledge elements.
276          * 
277          * @return the persistent set of scenario knowledge elements
278          */
279         public Set<KnowledgeElement> getKnowledgeElements() {
280                 return kelms;
281         }
282
283         /**
284          * Get the transient list of knowledge elements.
285          * 
286          * @return the transient list of knowledge elements
287          */
288         public List<KnowledgeElement> getKnowledgeElementsList() {
289                 return knowl;
290         }
291
292         /**
293          * Set the scenario transient "use case" knowledge element.
294          * 
295          * @param kelm
296          *            the scenario transient "use case" knowledge element
297          */
298         public void setUcase(final KnowledgeElement kelm) {
299                 ucase = kelm;
300         }
301
302         /**
303          * Get the scenario transient "use case" knowledge element.
304          * 
305          * @return the scenario transient "use case" knowledge element
306          */
307         public KnowledgeElement getUcase() {
308                 return ucase;
309         }
310 }