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 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;
92 // - Protected services
94 public Step getBaseStep() {
95 return base; // May be null
98 public Scenario getInsertAfter() {
99 return previous; // May be null
102 public User getManager() {
106 public Properties setOwnerStudy(Study owner) {
111 // - Setters of Scenario properties
113 public Properties setBaseStep(Step base)
114 throws InvalidPropertyException {
115 if (!(base.getOwner() instanceof Scenario))
116 throw new InvalidPropertyException("step");
121 public Properties setDate(Date date) {
126 public Properties setDescription(String summary) {
127 if (summary.length() > 0)
128 this.summary = summary;
132 public Properties setInsertAfter(Scenario previous) {
133 this.previous = previous;
137 public Properties setManager(User user) {
142 public Properties setTitle(String title)
143 throws InvalidPropertyException {
144 if (title.length() == 0)
145 throw new InvalidPropertyException("title");
150 // - Global validity check
152 public void checkValidity() throws MissedPropertyException,
153 InvalidPropertyException, MultiplyDefinedException {
155 throw new MissedPropertyException("owner");
157 throw new MissedPropertyException("title");
159 throw new MissedPropertyException("manager");
163 // Database fetch constructor
164 protected Scenario() {
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
177 title = sprop.title; // Inherited attribute
179 knowl = null; // Initialized when getting all Knowledge Elements
181 kelms = new HashSet<KnowledgeElement>();
183 manager = sprop.manager;
184 // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
185 // InvalidPropertyException("manager");
187 credate = sprop.date; // Inherited attribute
188 if (credate == null) {
189 Calendar current = Calendar.getInstance();
190 credate = current.getTime(); // Today
192 lasdate = credate; // Inherited attribute
194 if (sprop.summary != null)
195 this.setAttribute(new DescriptionAttribute(this, sprop.summary));
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;
204 // ==============================================================================================================================
205 // Public member functions
206 // ==============================================================================================================================
208 public List<KnowledgeElement> getAllKnowledgeElements() {
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"))
219 return Collections.unmodifiableList(knowl);
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()) {
233 * public List<KnowledgeElement> getKnowledgeElementsOf(
234 KnowledgeElementType type) {
236 known = new HashMap<Long, List<KnowledgeElement>>();
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)
247 known.put(numtype, listype);
249 return listype; // No protection against this object corruption as it would not corrupt the database
252 public Study getOwnerStudy() {
253 // -----------------------------
258 * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
260 public String getReference() {
261 // -----------------------------
262 return String.valueOf(sid);
265 public User getUser() {
266 return cuser; // Null if the scenario has not been checked-out
269 public void setUser(User aUser) {
270 cuser = aUser; // Null if the scenario has not been checked-out
273 public boolean isCheckedout() {
274 // ------------------------------
275 return (cuser != null);
278 // ==============================================================================================================================
280 // ==============================================================================================================================
282 * Get the persistent set of knowledge elements.
284 * @return the persistent set of scenario knowledge elements
286 public Set<KnowledgeElement> getKnowledgeElements() {
291 * Get the transient list of knowledge elements.
293 * @return the transient list of knowledge elements
295 public List<KnowledgeElement> getKnowledgeElementsList() {
300 * Set the scenario transient "use case" knowledge element.
303 * the scenario transient "use case" knowledge element
305 public void setUcase(KnowledgeElement kelm) {
310 * Get the scenario transient "use case" knowledge element.
312 * @return the scenario transient "use case" knowledge element
314 public KnowledgeElement getUcase() {
324 * public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {