1 package org.splat.dal.bo.som;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.util.Calendar;
9 import java.util.Collections;
10 import java.util.Date;
11 import java.util.HashMap;
12 import java.util.Iterator;
13 import java.util.List;
15 import java.util.HashSet;
16 import java.util.Vector;
18 import org.splat.dal.bo.kernel.Persistent;
19 import org.splat.dal.bo.kernel.User;
20 import org.splat.dal.dao.som.Database;
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 public class Scenario extends ProjectElement {
31 private int sid; // Identifier unique in the scope of owner study
32 private User cuser; // User having checked-out the scenario, if done
34 * The persistent set of scenario knowledge elements.
36 private Set<KnowledgeElement> kelms;
40 * The transient map of knowledge elements grouped by types.
42 transient private HashMap<Long, List<KnowledgeElement>> known;
44 * The scenario transient list of knowledge elements.
46 transient private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
48 * The scenario transient "use case" knowledge element.
50 transient private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
53 // ==============================================================================================================================
55 // ==============================================================================================================================
59 * Fields initialization class.
61 public static class Properties extends Persistent.Properties {
62 // ------------------------------------------------------------
63 private Study owner = null;
64 private Scenario previous = null;
65 private Step base = null;
66 private String title = null;
67 private String summary = null;
68 private User manager = null;
69 private Date date = null;
73 public void clear () {
83 // - Protected services
85 public Step getBaseStep () {
86 return base; // May be null
88 public Scenario getInsertAfter () {
89 return previous; // May be null
91 public User getManager () {
94 public Properties setOwnerStudy (Study owner)
99 // - Setters of Scenario properties
101 public Properties setBaseStep (Step base) throws InvalidPropertyException
103 if (!(base.getOwner() instanceof Scenario)) throw new InvalidPropertyException("step");
107 public Properties setDate (Date date)
112 public Properties setDescription (String summary)
114 if (summary.length() > 0) this.summary = summary;
117 public Properties setInsertAfter (Scenario previous)
119 this.previous = previous;
122 public Properties setManager (User user)
127 public Properties setTitle (String title) throws InvalidPropertyException
129 if (title.length() == 0) throw new InvalidPropertyException("title");
133 // - Global validity check
135 public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
137 if (owner == null) throw new MissedPropertyException("owner");
138 if (title == null) throw new MissedPropertyException("title");
139 if (manager == null) throw new MissedPropertyException("manager");
142 // Database fetch constructor
143 protected Scenario () {
144 // ---------------------
149 // Internal constructor
150 public Scenario (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
151 // -------------------------------------
152 super(sprop); // Throws one of the above exception if not valid
156 title = sprop.title; // Inherited attribute
158 knowl = null; // Initialized when getting all Knowledge Elements
160 kelms = new HashSet<KnowledgeElement>();
162 manager = sprop.manager;
163 //RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new InvalidPropertyException("manager");
165 credate = sprop.date; // Inherited attribute
166 if (credate == null) {
167 Calendar current = Calendar.getInstance();
168 credate = current.getTime(); // Today
170 lasdate = credate; // Inherited attribute
172 if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
174 Scenario[] scene = owner.getScenarii();
175 for (int i=0; i<scene.length; i++) if (scene[i].sid > this.sid) this.sid = scene[i].sid;
179 // ==============================================================================================================================
180 // Public member functions
181 // ==============================================================================================================================
183 public List<KnowledgeElement> getAllKnowledgeElements () {
184 // --------------------------------------------------------
186 knowl = new Vector<KnowledgeElement>(kelms.size());
187 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
188 KnowledgeElement kelm = i.next();
189 if (kelm.getType().equals("usecase")) ucase = kelm;
190 else knowl.add(kelm);
193 return Collections.unmodifiableList(knowl);
196 public KnowledgeElement getKnowledgeElement (long l) {
197 // -------------------------------------------------------
198 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
199 KnowledgeElement mykelm = i.next();
200 if (mykelm.getIndex() == l) return mykelm;
205 public List<KnowledgeElement> getKnowledgeElementsOf (KnowledgeElementType type) {
206 // --------------------------------------------------------------------------------
207 if (kelms.isEmpty()) return new Vector<KnowledgeElement>(); // Smarter than returning null
208 if (known == null) known = new HashMap<Long, List<KnowledgeElement>>();
210 long numtype = type.getIndex();
211 List<KnowledgeElement> listype = known.get(numtype);
212 if (listype == null) {
213 listype = new Vector<KnowledgeElement>();
214 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
215 KnowledgeElement kelm = i.next();
216 if (kelm.getType().getIndex() == numtype) listype.add(kelm);
218 known.put(numtype, listype);
220 return listype; // No protection against this object corruption as it would not corrupt the database
223 public Study getOwnerStudy () {
224 // -----------------------------
228 * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
230 public String getReference () {
231 // -----------------------------
232 return String.valueOf(sid);
235 public User getUser () {
236 return cuser; // Null if the scenario has not been checked-out
239 public void setUser (User aUser) {
240 cuser = aUser; // Null if the scenario has not been checked-out
243 public boolean removeKnowledgeElement (KnowledgeElement kelm) {
244 // -------------------------------------------------------------
245 KnowledgeElement torem = getKnowledgeElement(kelm.getIndex());
246 if (torem == null) return false;
247 boolean done = kelms.remove(torem);
249 // Update of my transient data
250 List<KnowledgeElement> kelms = known.get(kelm.getType().getIndex());
252 if (knowl != null) knowl.remove(torem);
253 Database.getSession().update(this);
254 //TODO: If the owner study is not private, remove the knowledge from the Lucene index
261 public boolean isCheckedout () {
262 // ------------------------------
263 return (cuser != null);
266 // ==============================================================================================================================
268 // ==============================================================================================================================
270 * Get the persistent set of knowledge elements.
271 * @return the persistent set of scenario knowledge elements
273 public Set<KnowledgeElement> getKnowledgeElements() {
277 * Get the transient list of knowledge elements.
278 * @return the transient list of knowledge elements
280 public List<KnowledgeElement> getKnowledgeElementsList() {
284 * Set the scenario transient "use case" knowledge element.
285 * @param kelm the scenario transient "use case" knowledge element
287 public void setUcase(KnowledgeElement kelm) {
291 * Get the scenario transient "use case" knowledge element.
292 * @return the scenario transient "use case" knowledge element
294 public KnowledgeElement getUcase() {