4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.io.Serializable;
11 import org.hibernate.Session;
13 import org.splat.kernel.Persistent;
14 import org.splat.kernel.InvalidPropertyException;
15 import org.splat.kernel.MissedPropertyException;
16 import org.splat.kernel.MultiplyDefinedException;
19 public class SimulationContext extends Persistent implements Serializable {
21 private SimulationContextType type; // User extendable types
23 private ProgressState state;
27 private static final long serialVersionUID = 422889133378471949L;
29 // ==============================================================================================================================
31 // ==============================================================================================================================
33 // Fields initialization class
34 public static class Properties extends Persistent.Properties {
35 // ------------------------------------------------------------
36 private SimulationContextType type = null;
37 private ProjectSettings.Step step = null;
38 private ProgressState state = null;
39 private String value = null;
43 public void clear () {
50 protected ProgressState getProgressState () {
53 public SimulationContextType getType () {
56 public String getValue () {
60 // - Setters of SimulationContext properties
62 public Properties setState (ProgressState state) throws InvalidPropertyException
64 if (state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
65 throw new InvalidPropertyException("state");
70 protected Properties setStep (ProjectSettings.Step step) throws InvalidPropertyException
75 public Properties setValue (String value) throws InvalidPropertyException
77 if (value.length() == 0) throw new InvalidPropertyException("value");
81 public Properties setType (SimulationContextType type)
86 // - Global validity check
88 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
90 if (type == null) throw new MissedPropertyException("type");
91 if (step == null) throw new MissedPropertyException("step");
92 if (value == null) throw new MissedPropertyException("value");
93 if (!type.isAttachedTo(step)) throw new InvalidPropertyException("step");
96 // Database fetch constructor
97 protected SimulationContext () {
99 // Internal constructor
100 protected SimulationContext (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
101 super(kprop); // Throws one of the above exception if not valid
103 step = kprop.step.getNumber();
107 if (state == null) state = ProgressState.inCHECK;
110 // ==============================================================================================================================
111 // Public member functions
112 // ==============================================================================================================================
114 public boolean approve () {
115 // -------------------------
116 if (state != ProgressState.inCHECK) return false;
117 this.state = ProgressState.APPROVED; // The type name is supposed being localized
118 Database.getSession().update(this);
122 public boolean equals (SimulationContext given) {
123 // -----------------------------------------------
124 if (isSaved()) return (this.getIndex() == given.getIndex());
125 if (!this.getType().getName().equals(given.getType().getName())) return false;
126 if (this.getValue().equals(given.getValue())) return true;
130 public String getValue () {
131 // -------------------------
135 public ProgressState getProgressState () {
136 // ----------------------------------------
140 public SimulationContextType getType () {
141 // ---------------------------------------
145 public boolean isInto (Step container) {
146 // --------------------------------------
147 return (step == container.getNumber());
150 public boolean isShared () {
151 // --------------------------
152 return (counter > 1);
155 // ==============================================================================================================================
157 // ==============================================================================================================================
159 public static SimulationContextType createType (String name, ProjectSettings.Step step) throws InvalidPropertyException, RuntimeException {
160 // ---------------------------------------------------------------------------------------
161 //TODO: Check for duplicate definition
162 SimulationContextType type = new SimulationContextType(name, step);
163 Session session = Database.getSession();
169 @SuppressWarnings("unchecked")
170 public static List<SimulationContextType> selectAllTypes () {
171 // -----------------------------------------------------------
172 StringBuffer query = new StringBuffer("from SimulationContextType"); // Useless to order by names as the result mixes localized and non localized types
173 query = query.append(" order by step asc");
174 return Database.getSession().createQuery(query.toString()).list();
177 @SuppressWarnings("unchecked")
178 public static List<SimulationContextType> selectTypesOf (ProjectSettings.Step... step) {
179 // --------------------------------------------------------------------------------------
180 StringBuffer query = new StringBuffer("from SimulationContextType where step='").append(step[0].getNumber()).append("'");
181 for (int i=1; i<step.length; i++) { // Useless to order as the result mixes localized and non localized types
182 query = query.append(" or step='").append(step[i].getNumber()).append("'");
184 query = query.append(" order by step asc");
185 return Database.getSession().createQuery(query.toString()).list();
188 @SuppressWarnings("unchecked")
189 public static List<SimulationContextType> selectTypesWhere (SimulationContextType.Properties sprop) {
190 // ---------------------------------------------------------------------------------------------------
191 StringBuffer query = new StringBuffer("from SimulationContextType");
192 String separator = " where";
193 ProjectSettings.Step step = sprop.getStep();
194 ProgressState state = sprop.getProgressState();
195 String order = " order by step asc";
198 query = query.append(separator).append(" step='").append(step.getNumber()).append("'");
200 order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
203 query = query.append(separator).append(" state='").append(state.toString()).append("'");
204 // separator = " and";
206 if (state != ProgressState.APPROVED) order = " order by name asc";
207 else order = ""; // Approved types are localized
210 query = query.append(order);
211 return Database.getSession().createQuery(query.toString()).list();
214 public static SimulationContextType selectType (String name) {
215 // ------------------------------------------------------------
216 StringBuffer query = new StringBuffer("from SimulationContextType where name='").append(name).append("'");
217 return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
220 public static SimulationContextType selectType (int index) {
221 // ----------------------------------------------------------
222 StringBuffer query = new StringBuffer("from SimulationContextType where rid='").append(index).append("'");
223 return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
226 // ==============================================================================================================================
227 // Protected services
228 // ==============================================================================================================================
230 protected void hold () {
231 // ----------------------
233 if (this.isSaved()) Database.getSession().update(this);
236 protected void release () {
237 // -------------------------
239 if (this.isSaved()) Database.getSession().update(this);