1 package org.splat.dal.bo.som;
5 * @author Daniel Brunier-Coulin
6 * @copyright OPEN CASCADE 2012
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;
16 import java.util.HashSet;
17 import java.util.Vector;
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;
27 * Scenario persistent object.
29 public class Scenario extends ProjectElement {
33 * Persistent property owner. It is a parent study of the scenario.
37 * Persistent property sid. It is an unique identifier in the scope of owner study.
41 * Persistent property cuser. It is a user having checked-out the scenario, if done.
45 * The persistent set of scenario knowledge elements.
47 private Set<KnowledgeElement> kelms;
51 * The transient map of knowledge elements grouped by types.
53 transient private HashMap<Long, List<KnowledgeElement>> known;
55 * The scenario transient list of knowledge elements.
57 transient private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
59 * The scenario transient "use case" knowledge element.
61 transient private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
63 // ==============================================================================================================================
65 // ==============================================================================================================================
68 * Fields initialization class.
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;
93 // - Protected services
95 public Step getBaseStep() {
96 return base; // May be null
99 public Scenario getInsertAfter() {
100 return previous; // May be null
103 public User getManager() {
107 public Properties setOwnerStudy(Study owner) {
112 // - Setters of Scenario properties
114 public Properties setBaseStep(Step base)
115 throws InvalidPropertyException {
116 if (!(base.getOwner() instanceof Scenario))
117 throw new InvalidPropertyException("step");
122 public Properties setDate(Date date) {
127 public Properties setDescription(String summary) {
128 if (summary.length() > 0)
129 this.summary = summary;
133 public Properties setInsertAfter(Scenario previous) {
134 this.previous = previous;
138 public Properties setManager(User user) {
143 public Properties setTitle(String title)
144 throws InvalidPropertyException {
145 if (title.length() == 0)
146 throw new InvalidPropertyException("title");
151 // - Global validity check
153 public void checkValidity() throws MissedPropertyException,
154 InvalidPropertyException, MultiplyDefinedException {
156 throw new MissedPropertyException("owner");
158 throw new MissedPropertyException("title");
160 throw new MissedPropertyException("manager");
164 // Database fetch constructor
165 protected Scenario() {
166 // ---------------------
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
180 title = sprop.title; // Inherited attribute
182 knowl = null; // Initialized when getting all Knowledge Elements
184 kelms = new HashSet<KnowledgeElement>();
186 manager = sprop.manager;
187 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
188 // InvalidPropertyException("manager");
190 credate = sprop.date; // Inherited attribute
191 if (credate == null) {
192 Calendar current = Calendar.getInstance();
193 credate = current.getTime(); // Today
195 lasdate = credate; // Inherited attribute
197 if (sprop.summary != null)
198 this.setAttribute(new DescriptionAttribute(this, sprop.summary));
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;
207 // ==============================================================================================================================
208 // Public member functions
209 // ==============================================================================================================================
211 public List<KnowledgeElement> getAllKnowledgeElements() {
212 // --------------------------------------------------------
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"))
223 return Collections.unmodifiableList(knowl);
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)
236 public List<KnowledgeElement> getKnowledgeElementsOf(
237 KnowledgeElementType type) {
238 // --------------------------------------------------------------------------------
240 return new Vector<KnowledgeElement>(); // Smarter than returning null
242 known = new HashMap<Long, List<KnowledgeElement>>();
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)
253 known.put(numtype, listype);
255 return listype; // No protection against this object corruption as it would not corrupt the database
258 public Study getOwnerStudy() {
259 // -----------------------------
264 * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
266 public String getReference() {
267 // -----------------------------
268 return String.valueOf(sid);
271 public User getUser() {
272 return cuser; // Null if the scenario has not been checked-out
275 public void setUser(User aUser) {
276 cuser = aUser; // Null if the scenario has not been checked-out
279 public boolean isCheckedout() {
280 // ------------------------------
281 return (cuser != null);
284 // ==============================================================================================================================
286 // ==============================================================================================================================
288 * Get the persistent set of knowledge elements.
290 * @return the persistent set of scenario knowledge elements
292 public Set<KnowledgeElement> getKnowledgeElements() {
297 * Get the transient list of knowledge elements.
299 * @return the transient list of knowledge elements
301 public List<KnowledgeElement> getKnowledgeElementsList() {
306 * Set the scenario transient "use case" knowledge element.
309 * the scenario transient "use case" knowledge element
311 public void setUcase(KnowledgeElement kelm) {
316 * Get the scenario transient "use case" knowledge element.
318 * @return the scenario transient "use case" knowledge element
320 public KnowledgeElement getUcase() {
329 public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {