--- /dev/null
+package org.splat.dal.bo.som;
+/**
+ *
+ * @author Daniel Brunier-Coulin
+ * @copyright OPEN CASCADE 2012
+ */
+
+import java.io.Serializable;
+import java.util.List;
+
+import org.hibernate.Session;
+
+import org.splat.dal.bo.kernel.Persistent;
+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.service.technical.ProjectSettingsService;
+import org.splat.som.Step;
+
+
+public class SimulationContext extends Persistent implements Serializable {
+
+ private SimulationContextType type; // User extendable types
+ private int step;
+ private ProgressState state;
+ private String value;
+ private int counter;
+
+ private static final long serialVersionUID = 422889133378471949L;
+
+// ==============================================================================================================================
+// Construction
+// ==============================================================================================================================
+
+// Fields initialization class
+ public static class Properties extends Persistent.Properties {
+// ------------------------------------------------------------
+ private SimulationContextType type = null;
+ private ProjectSettingsService.Step step = null;
+ private ProgressState state = null;
+ private String value = null;
+
+// - Public services
+
+ public void clear () {
+ super.clear();
+ type = null;
+ step = null;
+ state = null;
+ value = null;
+ }
+ public ProgressState getProgressState () {
+ return state;
+ }
+ public SimulationContextType getType () {
+ return type;
+ }
+ public String getValue () {
+ return value;
+ }
+
+// - Setters of SimulationContext properties
+
+ public Properties setState (ProgressState state) throws InvalidPropertyException
+ {
+ if (state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
+ throw new InvalidPropertyException("state");
+ }
+ this.state = state;
+ return this;
+ }
+ public Properties setStep (ProjectSettingsService.Step step) throws InvalidPropertyException
+ {
+ this.step = step;
+ return this;
+ }
+ public Properties setValue (String value) throws InvalidPropertyException
+ {
+ if (value.length() == 0) throw new InvalidPropertyException("value");
+ this.value = value;
+ return this;
+ }
+ public Properties setType (SimulationContextType type)
+ {
+ this.type = type;
+ return this;
+ }
+// - Global validity check
+
+ public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
+ {
+ if (type == null) throw new MissedPropertyException("type");
+ if (step == null) throw new MissedPropertyException("step");
+ if (value == null) throw new MissedPropertyException("value");
+ if (!type.isAttachedTo(step)) throw new InvalidPropertyException("step");
+ }
+ }
+// Database fetch constructor
+ protected SimulationContext () {
+ }
+// Internal constructor
+ public SimulationContext (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
+ super(kprop); // Throws one of the above exception if not valid
+ type = kprop.type;
+ step = kprop.step.getNumber();
+ value = kprop.value;
+ counter = 0;
+ state = kprop.state;
+ if (state == null) state = ProgressState.inCHECK;
+ }
+
+// ==============================================================================================================================
+// Public member functions
+// ==============================================================================================================================
+
+ public boolean approve () {
+// -------------------------
+ if (state != ProgressState.inCHECK) return false;
+ this.state = ProgressState.APPROVED; // The type name is supposed being localized
+ Database.getSession().update(this);
+ return true;
+ }
+
+ public boolean equals (SimulationContext given) {
+// -----------------------------------------------
+ if (isSaved()) return (this.getIndex() == given.getIndex());
+ if (!this.getType().getName().equals(given.getType().getName())) return false;
+ if (this.getValue().equals(given.getValue())) return true;
+ return false;
+ }
+
+ public String getValue () {
+// -------------------------
+ return value;
+ }
+
+ public ProgressState getProgressState () {
+// ----------------------------------------
+ return state;
+ }
+
+ public SimulationContextType getType () {
+// ---------------------------------------
+ return type;
+ }
+
+ public boolean isInto (Step container) {
+// --------------------------------------
+ return (step == container.getNumber());
+ }
+
+ public boolean isShared () {
+// --------------------------
+ return (counter > 1);
+ }
+
+// ==============================================================================================================================
+// Public services
+// ==============================================================================================================================
+
+ public static SimulationContextType createType (String name, ProjectSettingsService.Step step) throws InvalidPropertyException, RuntimeException {
+// ---------------------------------------------------------------------------------------
+//TODO: Check for duplicate definition
+ SimulationContextType type = new SimulationContextType(name, step);
+ Session session = Database.getSession();
+ session.save(type);
+
+ return type;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static List<SimulationContextType> selectAllTypes () {
+// -----------------------------------------------------------
+ StringBuffer query = new StringBuffer("from SimulationContextType"); // Useless to order by names as the result mixes localized and non localized types
+ query = query.append(" order by step asc");
+ return Database.getSession().createQuery(query.toString()).list();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static List<SimulationContextType> selectTypesOf (ProjectSettingsService.Step... step) {
+// --------------------------------------------------------------------------------------
+ StringBuffer query = new StringBuffer("from SimulationContextType where step='").append(step[0].getNumber()).append("'");
+ for (int i=1; i<step.length; i++) { // Useless to order as the result mixes localized and non localized types
+ query = query.append(" or step='").append(step[i].getNumber()).append("'");
+ }
+ query = query.append(" order by step asc");
+ return Database.getSession().createQuery(query.toString()).list();
+ }
+
+ @SuppressWarnings("unchecked")
+ public static List<SimulationContextType> selectTypesWhere (SimulationContextType.Properties sprop) {
+// ---------------------------------------------------------------------------------------------------
+ StringBuffer query = new StringBuffer("from SimulationContextType");
+ String separator = " where";
+ ProjectSettingsService.Step step = sprop.getStep();
+ ProgressState state = sprop.getProgressState();
+ String order = " order by step asc";
+
+ if (step != null) {
+ query = query.append(separator).append(" step='").append(step.getNumber()).append("'");
+ separator = " and";
+ order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
+ }
+ if (state != null) {
+ query = query.append(separator).append(" state='").append(state.toString()).append("'");
+// separator = " and";
+ if (step != null) {
+ if (state != ProgressState.APPROVED) order = " order by name asc";
+ else order = ""; // Approved types are localized
+ }
+ }
+ query = query.append(order);
+ return Database.getSession().createQuery(query.toString()).list();
+ }
+
+ public static SimulationContextType selectType (String name) {
+// ------------------------------------------------------------
+ StringBuffer query = new StringBuffer("from SimulationContextType where name='").append(name).append("'");
+ return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
+ }
+
+ public static SimulationContextType selectType (int index) {
+// ----------------------------------------------------------
+ StringBuffer query = new StringBuffer("from SimulationContextType where rid='").append(index).append("'");
+ return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
+ }
+
+// ==============================================================================================================================
+// Protected services
+// ==============================================================================================================================
+
+ public void hold () {
+// ----------------------
+ counter += 1;
+ if (this.isSaved()) Database.getSession().update(this);
+ }
+
+ public void release () {
+// -------------------------
+ counter -= 1;
+ if (this.isSaved()) Database.getSession().update(this);
+ }
+}
\ No newline at end of file