1 package org.splat.dal.bo.som;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.io.IOException;
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.hibernate.HibernateException;
20 import org.hibernate.Session;
21 import org.hibernate.Transaction;
22 import org.splat.dal.bo.kernel.Persistent;
23 import org.splat.dal.bo.kernel.User;
24 import org.splat.dal.dao.som.Database;
25 import org.splat.kernel.InvalidPropertyException;
26 import org.splat.kernel.MissedPropertyException;
27 import org.splat.kernel.MultiplyDefinedException;
28 import org.splat.service.StepService;
29 import org.splat.service.technical.IndexService;
30 import org.splat.som.Step;
33 public class Scenario extends ProjectElement {
37 private int sid; // Identifier unique in the scope of owner study
38 private User cuser; // User having checked-out the scenario, if done
39 private Set<KnowledgeElement> kelms;
42 private HashMap<Integer, List<KnowledgeElement>> known;
43 private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
44 private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
47 // ==============================================================================================================================
49 // ==============================================================================================================================
51 // Fields initialization class
52 public static class Properties extends Persistent.Properties {
53 // ------------------------------------------------------------
54 private Study owner = null;
55 private Scenario previous = null;
56 private Step base = null;
57 private String title = null;
58 private String summary = null;
59 private User manager = null;
60 private Date date = null;
64 public void clear () {
74 // - Protected services
76 public Step getBaseStep () {
77 return base; // May be null
79 public Scenario getInsertAfter () {
80 return previous; // May be null
82 public User getManager () {
85 public Properties setOwnerStudy (Study owner)
90 // - Setters of Scenario properties
92 public Properties setBaseStep (Step base) throws InvalidPropertyException
94 if (!(base.getOwner() instanceof Scenario)) throw new InvalidPropertyException("step");
98 public Properties setDate (Date date)
103 public Properties setDescription (String summary)
105 if (summary.length() > 0) this.summary = summary;
108 public Properties setInsertAfter (Scenario previous)
110 this.previous = previous;
113 public Properties setManager (User user)
118 public Properties setTitle (String title) throws InvalidPropertyException
120 if (title.length() == 0) throw new InvalidPropertyException("title");
124 // - Global validity check
126 public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
128 if (owner == null) throw new MissedPropertyException("owner");
129 if (title == null) throw new MissedPropertyException("title");
130 if (manager == null) throw new MissedPropertyException("manager");
133 // Database fetch constructor
134 protected Scenario () {
135 // ---------------------
140 // Internal constructor
141 public Scenario (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
142 // -------------------------------------
143 super(sprop); // Throws one of the above exception if not valid
147 title = sprop.title; // Inherited attribute
149 knowl = null; // Initialized when getting all Knowledge Elements
151 kelms = new HashSet<KnowledgeElement>();
153 manager = sprop.manager;
154 if (!owner.isStaffedBy(manager)) throw new InvalidPropertyException("manager");
156 credate = sprop.date; // Inherited attribute
157 if (credate == null) {
158 Calendar current = Calendar.getInstance();
159 credate = current.getTime(); // Today
161 lasdate = credate; // Inherited attribute
163 if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
165 Scenario[] scene = owner.getScenarii();
166 for (int i=0; i<scene.length; i++) if (scene[i].sid > this.sid) this.sid = scene[i].sid;
170 // ==============================================================================================================================
171 // Public member functions
172 // ==============================================================================================================================
174 public List<KnowledgeElement> getAllKnowledgeElements () {
175 // --------------------------------------------------------
177 knowl = new Vector<KnowledgeElement>(kelms.size());
178 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
179 KnowledgeElement kelm = i.next();
180 if (kelm.getType().equals("usecase")) ucase = kelm;
181 else knowl.add(kelm);
184 return Collections.unmodifiableList(knowl);
187 public KnowledgeElement getKnowledgeElement (int index) {
188 // -------------------------------------------------------
189 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
190 KnowledgeElement mykelm = i.next();
191 if (mykelm.getIndex() == index) return mykelm;
196 public List<KnowledgeElement> getKnowledgeElementsOf (KnowledgeElementType type) {
197 // --------------------------------------------------------------------------------
198 if (kelms.isEmpty()) return new Vector<KnowledgeElement>(); // Smarter than returning null
199 if (known == null) known = new HashMap<Integer, List<KnowledgeElement>>();
201 int numtype = type.getIndex();
202 List<KnowledgeElement> listype = known.get(numtype);
203 if (listype == null) {
204 listype = new Vector<KnowledgeElement>();
205 for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
206 KnowledgeElement kelm = i.next();
207 if (kelm.getType().getIndex() == numtype) listype.add(kelm);
209 known.put(numtype, listype);
211 return listype; // No protection against this object corruption as it would not corrupt the database
214 public Study getOwnerStudy () {
215 // -----------------------------
219 * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
221 public String getReference () {
222 // -----------------------------
223 return String.valueOf(sid);
226 public User getUser () {
227 return cuser; // Null if the scenario has not been checked-out
230 public void setUser (User aUser) {
231 cuser = aUser; // Null if the scenario has not been checked-out
234 public boolean removeKnowledgeElement (KnowledgeElement kelm) {
235 // -------------------------------------------------------------
236 KnowledgeElement torem = getKnowledgeElement(kelm.getIndex());
237 if (torem == null) return false;
238 boolean done = kelms.remove(torem);
240 // Update of my transient data
241 List<KnowledgeElement> kelms = known.get(kelm.getType().getIndex());
243 if (knowl != null) knowl.remove(torem);
244 Database.getSession().update(this);
245 //TODO: If the owner study is not private, remove the knowledge from the Lucene index
252 public boolean isCheckedout () {
253 // ------------------------------
254 return (cuser != null);
259 // ==============================================================================================================================
260 // Protected member function
261 // ==============================================================================================================================
263 public void updateMyIndex (IndexService lucin) throws IOException {
264 // ------------------------------------------
265 if (ucase == null) for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
266 KnowledgeElement kelm = i.next();
267 if (!kelm.getType().equals("usecase")) continue;
274 // ==============================================================================================================================
276 // ==============================================================================================================================
280 public Set<KnowledgeElement> getKnowledgeElements() {
286 public void setUcase(KnowledgeElement kelm) {
292 public List<KnowledgeElement> getKnowledgeElementsList() {