package org.splat.dal.bo.som;
+
/**
*
* @author Daniel Brunier-Coulin
import org.splat.dal.bo.kernel.Persistent;
import org.splat.dal.bo.kernel.User;
-import org.splat.dal.dao.som.Database;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
import org.splat.som.Step;
-
+/**
+ * Scenario persistent object.
+ */
public class Scenario extends ProjectElement {
-// Persistent fields
- private Study owner;
- private int sid; // Identifier unique in the scope of owner study
- private User cuser; // User having checked-out the scenario, if done
- /**
- * The persistent set of scenario knowledge elements.
- */
- private Set<KnowledgeElement> kelms;
-
-// Transient fields
- /**
- * The transient map of knowledge elements grouped by types.
- */
- transient private HashMap<Long, List<KnowledgeElement>> known;
- /**
- * The scenario transient list of knowledge elements.
- */
- transient private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
+ // Persistent fields
+ /**
+ * Persistent property owner. It is a parent study of the scenario.
+ */
+ private Study owner;
+ /**
+ * Persistent property sid. It is an unique identifier in the scope of owner study.
+ */
+ private int sid;
+ /**
+ * Persistent property cuser. It is a user having checked-out the scenario, if done.
+ */
+ private User cuser;
+ /**
+ * The persistent set of scenario knowledge elements.
+ */
+ private Set<KnowledgeElement> kelms;
+
+ // Transient fields
+ /**
+ * The transient map of knowledge elements grouped by types.
+ */
+ transient private HashMap<Long, List<KnowledgeElement>> known;
+ /**
+ * The scenario transient list of knowledge elements.
+ */
+ transient private List<KnowledgeElement> knowl; // Copy of kelms excluding the internal Knowledge Element (ucase below)
/**
* The scenario transient "use case" knowledge element.
*/
- transient private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
-
-
-// ==============================================================================================================================
-// Construction
-// ==============================================================================================================================
-
-
- /**
- * Fields initialization class.
- */
- public static class Properties extends Persistent.Properties {
-// ------------------------------------------------------------
- private Study owner = null;
- private Scenario previous = null;
- private Step base = null;
- private String title = null;
- private String summary = null;
- private User manager = null;
- private Date date = null;
-
-// - Public services
-
- public void clear () {
- super.clear();
- owner = null;
- previous = null;
- base = null;
- title = null;
- summary = null;
- manager = null;
- date = null;
- }
-// - Protected services
-
- public Step getBaseStep () {
- return base; // May be null
- }
- public Scenario getInsertAfter () {
- return previous; // May be null
- }
- public User getManager () {
- return manager;
- }
- public Properties setOwnerStudy (Study owner)
- {
- this.owner = owner;
- return this;
- }
-// - Setters of Scenario properties
-
- public Properties setBaseStep (Step base) throws InvalidPropertyException
- {
- if (!(base.getOwner() instanceof Scenario)) throw new InvalidPropertyException("step");
- this.base = base;
- return this;
- }
- public Properties setDate (Date date)
- {
- this.date = date;
- return this;
- }
- public Properties setDescription (String summary)
- {
- if (summary.length() > 0) this.summary = summary;
- return this;
- }
- public Properties setInsertAfter (Scenario previous)
- {
- this.previous = previous;
- return this;
- }
- public Properties setManager (User user)
- {
- this.manager = user;
- return this;
- }
- public Properties setTitle (String title) throws InvalidPropertyException
- {
- if (title.length() == 0) throw new InvalidPropertyException("title");
- this.title = title;
- return this;
- }
-// - Global validity check
-
- public void checkValidity() throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
- {
- if (owner == null) throw new MissedPropertyException("owner");
- if (title == null) throw new MissedPropertyException("title");
- if (manager == null) throw new MissedPropertyException("manager");
- }
- }
-// Database fetch constructor
- protected Scenario () {
-// ---------------------
- known = null;
- knowl = null;
- ucase = null;
- }
-// Internal constructor
- public Scenario (Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
-// -------------------------------------
- super(sprop); // Throws one of the above exception if not valid
- owner = sprop.owner;
- sid = 0;
- cuser = null;
- title = sprop.title; // Inherited attribute
- known = null;
- knowl = null; // Initialized when getting all Knowledge Elements
- ucase = null;
- kelms = new HashSet<KnowledgeElement>();
-
- manager = sprop.manager;
-//RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new InvalidPropertyException("manager");
-
- credate = sprop.date; // Inherited attribute
- if (credate == null) {
- Calendar current = Calendar.getInstance();
- credate = current.getTime(); // Today
- }
- lasdate = credate; // Inherited attribute
-
- if (sprop.summary != null) this.setAttribute( new DescriptionAttribute(this, sprop.summary) );
-
- Scenario[] scene = owner.getScenarii();
- for (int i=0; i<scene.length; i++) if (scene[i].sid > this.sid) this.sid = scene[i].sid;
- sid += 1;
- }
-
-// ==============================================================================================================================
-// Public member functions
-// ==============================================================================================================================
-
- public List<KnowledgeElement> getAllKnowledgeElements () {
-// --------------------------------------------------------
- if (knowl == null) {
- knowl = new Vector<KnowledgeElement>(kelms.size());
- for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext(); ) {
- KnowledgeElement kelm = i.next();
- if (kelm.getType().equals("usecase")) ucase = kelm;
- else knowl.add(kelm);
- }
- }
- return Collections.unmodifiableList(knowl);
- }
-
- public KnowledgeElement getKnowledgeElement (long l) {
-// -------------------------------------------------------
- for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
- KnowledgeElement mykelm = i.next();
- if (mykelm.getIndex() == l) return mykelm;
- }
- return null;
- }
-
- public List<KnowledgeElement> getKnowledgeElementsOf (KnowledgeElementType type) {
-// --------------------------------------------------------------------------------
- if (kelms.isEmpty()) return new Vector<KnowledgeElement>(); // Smarter than returning null
- if (known == null) known = new HashMap<Long, List<KnowledgeElement>>();
-
- long numtype = type.getIndex();
- List<KnowledgeElement> listype = known.get(numtype);
- if (listype == null) {
- listype = new Vector<KnowledgeElement>();
- for (Iterator<KnowledgeElement> i=kelms.iterator(); i.hasNext();) {
- KnowledgeElement kelm = i.next();
- if (kelm.getType().getIndex() == numtype) listype.add(kelm);
- }
- known.put(numtype, listype);
- }
- return listype; // No protection against this object corruption as it would not corrupt the database
- }
-
- public Study getOwnerStudy () {
-// -----------------------------
- return owner;
- }
-/**
- * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
- */
- public String getReference () {
-// -----------------------------
- return String.valueOf(sid);
- }
-
- public User getUser () {
- return cuser; // Null if the scenario has not been checked-out
- }
-
- public void setUser (User aUser) {
- cuser = aUser; // Null if the scenario has not been checked-out
- }
-
- public boolean removeKnowledgeElement (KnowledgeElement kelm) {
-// -------------------------------------------------------------
- KnowledgeElement torem = getKnowledgeElement(kelm.getIndex());
- if (torem == null) return false;
- boolean done = kelms.remove(torem);
- if (done) {
-// Update of my transient data
- List<KnowledgeElement> kelms = known.get(kelm.getType().getIndex());
- kelms.remove(torem);
- if (knowl != null) knowl.remove(torem);
- Database.getSession().update(this);
-//TODO: If the owner study is not private, remove the knowledge from the Lucene index
- return true;
- } else {
- return false;
- }
- }
-
- public boolean isCheckedout () {
-// ------------------------------
- return (cuser != null);
- }
-
-// ==============================================================================================================================
-// Private services
-// ==============================================================================================================================
+ transient private KnowledgeElement ucase; // Internal Knowledge Element for accessing to all used simulation contexts
+
+ // ==============================================================================================================================
+ // Construction
+ // ==============================================================================================================================
+
+ /**
+ * Fields initialization class.
+ */
+ public static class Properties extends Persistent.Properties {
+ // ------------------------------------------------------------
+ private Study owner = null;
+ private Scenario previous = null;
+ private Step base = null;
+ private String title = null;
+ private String summary = null;
+ private User manager = null;
+ private Date date = null;
+
+ // - Public services
+
+ public void clear() {
+ super.clear();
+ owner = null;
+ previous = null;
+ base = null;
+ title = null;
+ summary = null;
+ manager = null;
+ date = null;
+ }
+
+ // - Protected services
+
+ public Step getBaseStep() {
+ return base; // May be null
+ }
+
+ public Scenario getInsertAfter() {
+ return previous; // May be null
+ }
+
+ public User getManager() {
+ return manager;
+ }
+
+ public Properties setOwnerStudy(Study owner) {
+ this.owner = owner;
+ return this;
+ }
+
+ // - Setters of Scenario properties
+
+ public Properties setBaseStep(Step base)
+ throws InvalidPropertyException {
+ if (!(base.getOwner() instanceof Scenario))
+ throw new InvalidPropertyException("step");
+ this.base = base;
+ return this;
+ }
+
+ public Properties setDate(Date date) {
+ this.date = date;
+ return this;
+ }
+
+ public Properties setDescription(String summary) {
+ if (summary.length() > 0)
+ this.summary = summary;
+ return this;
+ }
+
+ public Properties setInsertAfter(Scenario previous) {
+ this.previous = previous;
+ return this;
+ }
+
+ public Properties setManager(User user) {
+ this.manager = user;
+ return this;
+ }
+
+ public Properties setTitle(String title)
+ throws InvalidPropertyException {
+ if (title.length() == 0)
+ throw new InvalidPropertyException("title");
+ this.title = title;
+ return this;
+ }
+
+ // - Global validity check
+
+ public void checkValidity() throws MissedPropertyException,
+ InvalidPropertyException, MultiplyDefinedException {
+ if (owner == null)
+ throw new MissedPropertyException("owner");
+ if (title == null)
+ throw new MissedPropertyException("title");
+ if (manager == null)
+ throw new MissedPropertyException("manager");
+ }
+ }
+
+ // Database fetch constructor
+ protected Scenario() {
+ // ---------------------
+ known = null;
+ knowl = null;
+ ucase = null;
+ }
+
+ // Internal constructor
+ public Scenario(Properties sprop) throws MissedPropertyException,
+ InvalidPropertyException, MultiplyDefinedException {
+ // -------------------------------------
+ super(sprop); // Throws one of the above exception if not valid
+ owner = sprop.owner;
+ sid = 0;
+ cuser = null;
+ title = sprop.title; // Inherited attribute
+ known = null;
+ knowl = null; // Initialized when getting all Knowledge Elements
+ ucase = null;
+ kelms = new HashSet<KnowledgeElement>();
+
+ manager = sprop.manager;
+ // RKV: The business logic is moved to business services: if (!owner.isStaffedBy(manager)) throw new
+ // InvalidPropertyException("manager");
+
+ credate = sprop.date; // Inherited attribute
+ if (credate == null) {
+ Calendar current = Calendar.getInstance();
+ credate = current.getTime(); // Today
+ }
+ lasdate = credate; // Inherited attribute
+
+ if (sprop.summary != null)
+ this.setAttribute(new DescriptionAttribute(this, sprop.summary));
+
+ Scenario[] scene = owner.getScenarii();
+ for (int i = 0; i < scene.length; i++)
+ if (scene[i].sid > this.sid)
+ this.sid = scene[i].sid;
+ sid += 1;
+ }
+
+ // ==============================================================================================================================
+ // Public member functions
+ // ==============================================================================================================================
+
+ public List<KnowledgeElement> getAllKnowledgeElements() {
+ // --------------------------------------------------------
+ if (knowl == null) {
+ knowl = new Vector<KnowledgeElement>(kelms.size());
+ for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
+ KnowledgeElement kelm = i.next();
+ if (kelm.getType().equals("usecase"))
+ ucase = kelm;
+ else
+ knowl.add(kelm);
+ }
+ }
+ return Collections.unmodifiableList(knowl);
+ }
+
+ public KnowledgeElement getKnowledgeElement(long l) {
+ // -------------------------------------------------------
+ for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
+ KnowledgeElement mykelm = i.next();
+ if (mykelm.getIndex() == l)
+ return mykelm;
+ }
+ return null;
+ }
+
+ public List<KnowledgeElement> getKnowledgeElementsOf(
+ KnowledgeElementType type) {
+ // --------------------------------------------------------------------------------
+ if (kelms.isEmpty())
+ return new Vector<KnowledgeElement>(); // Smarter than returning null
+ if (known == null)
+ known = new HashMap<Long, List<KnowledgeElement>>();
+
+ long numtype = type.getIndex();
+ List<KnowledgeElement> listype = known.get(numtype);
+ if (listype == null) {
+ listype = new Vector<KnowledgeElement>();
+ for (Iterator<KnowledgeElement> i = kelms.iterator(); i.hasNext();) {
+ KnowledgeElement kelm = i.next();
+ if (kelm.getType().getIndex() == numtype)
+ listype.add(kelm);
+ }
+ known.put(numtype, listype);
+ }
+ return listype; // No protection against this object corruption as it would not corrupt the database
+ }
+
+ public Study getOwnerStudy() {
+ // -----------------------------
+ return owner;
+ }
+
+ /**
+ * Returns the local reference of this scenario. This reference is unique in the scope of the owner study.
+ */
+ public String getReference() {
+ // -----------------------------
+ return String.valueOf(sid);
+ }
+
+ public User getUser() {
+ return cuser; // Null if the scenario has not been checked-out
+ }
+
+ public void setUser(User aUser) {
+ cuser = aUser; // Null if the scenario has not been checked-out
+ }
+
+ public boolean isCheckedout() {
+ // ------------------------------
+ return (cuser != null);
+ }
+
+ // ==============================================================================================================================
+ // Private services
+ // ==============================================================================================================================
/**
* Get the persistent set of knowledge elements.
+ *
* @return the persistent set of scenario knowledge elements
*/
public Set<KnowledgeElement> getKnowledgeElements() {
return kelms;
}
- /**
+
+ /**
* Get the transient list of knowledge elements.
+ *
* @return the transient list of knowledge elements
*/
public List<KnowledgeElement> getKnowledgeElementsList() {
return knowl;
}
+
/**
* Set the scenario transient "use case" knowledge element.
- * @param kelm the scenario transient "use case" knowledge element
+ *
+ * @param kelm
+ * the scenario transient "use case" knowledge element
*/
public void setUcase(KnowledgeElement kelm) {
ucase = kelm;
}
+
/**
* Get the scenario transient "use case" knowledge element.
+ *
* @return the scenario transient "use case" knowledge element
*/
public KnowledgeElement getUcase() {
return ucase;
}
+
+ /**
+ * Get the known.
+ *
+ * @return the known
+ */
+ public HashMap<Long, List<KnowledgeElement>> getKnowledgeByType() {
+ return known;
+ }
}
\ No newline at end of file