1 package org.splat.dal.bo.som;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.io.Serializable;
11 import org.hibernate.Session;
13 import org.splat.dal.bo.kernel.Persistent;
14 import org.splat.dal.dao.som.Database;
15 import org.splat.kernel.InvalidPropertyException;
16 import org.splat.kernel.MissedPropertyException;
17 import org.splat.kernel.MultiplyDefinedException;
18 import org.splat.service.technical.ProjectSettingsService;
19 import org.splat.som.Step;
22 public class SimulationContext extends Persistent implements Serializable {
24 private SimulationContextType type; // User extendable types
26 private ProgressState state;
30 private static final long serialVersionUID = 422889133378471949L;
32 // ==============================================================================================================================
34 // ==============================================================================================================================
36 // Fields initialization class
37 public static class Properties extends Persistent.Properties {
38 // ------------------------------------------------------------
39 private SimulationContextType type = null;
40 private ProjectSettingsService.Step step = null;
41 private ProgressState state = null;
42 private String value = null;
46 public void clear () {
53 public ProgressState getProgressState () {
56 public SimulationContextType getType () {
59 public String getValue () {
63 // - Setters of SimulationContext properties
65 public Properties setState (ProgressState state) throws InvalidPropertyException
67 if (state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
68 throw new InvalidPropertyException("state");
73 public Properties setStep (ProjectSettingsService.Step step) throws InvalidPropertyException
78 public Properties setValue (String value) throws InvalidPropertyException
80 if (value.length() == 0) throw new InvalidPropertyException("value");
84 public Properties setType (SimulationContextType type)
89 // - Global validity check
91 public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
93 if (type == null) throw new MissedPropertyException("type");
94 if (step == null) throw new MissedPropertyException("step");
95 if (value == null) throw new MissedPropertyException("value");
96 if (!type.isAttachedTo(step)) throw new InvalidPropertyException("step");
99 // Database fetch constructor
100 protected SimulationContext () {
102 // Internal constructor
103 public SimulationContext (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
104 super(kprop); // Throws one of the above exception if not valid
106 step = kprop.step.getNumber();
110 if (state == null) state = ProgressState.inCHECK;
113 // ==============================================================================================================================
114 // Public member functions
115 // ==============================================================================================================================
117 public boolean approve () {
118 // -------------------------
119 if (state != ProgressState.inCHECK) return false;
120 this.state = ProgressState.APPROVED; // The type name is supposed being localized
121 Database.getSession().update(this);
125 public boolean equals (SimulationContext given) {
126 // -----------------------------------------------
127 if (isSaved()) return (this.getIndex() == given.getIndex());
128 if (!this.getType().getName().equals(given.getType().getName())) return false;
129 if (this.getValue().equals(given.getValue())) return true;
133 public String getValue () {
134 // -------------------------
138 public ProgressState getProgressState () {
139 // ----------------------------------------
143 public SimulationContextType getType () {
144 // ---------------------------------------
148 public boolean isInto (Step container) {
149 // --------------------------------------
150 return (step == container.getNumber());
153 public boolean isShared () {
154 // --------------------------
155 return (counter > 1);
158 // ==============================================================================================================================
160 // ==============================================================================================================================
162 public static SimulationContextType createType (String name, ProjectSettingsService.Step step) throws InvalidPropertyException, RuntimeException {
163 // ---------------------------------------------------------------------------------------
164 //TODO: Check for duplicate definition
165 SimulationContextType type = new SimulationContextType(name, step);
166 Session session = Database.getSession();
172 @SuppressWarnings("unchecked")
173 public static List<SimulationContextType> selectAllTypes () {
174 // -----------------------------------------------------------
175 StringBuffer query = new StringBuffer("from SimulationContextType"); // Useless to order by names as the result mixes localized and non localized types
176 query = query.append(" order by step asc");
177 return Database.getSession().createQuery(query.toString()).list();
180 @SuppressWarnings("unchecked")
181 public static List<SimulationContextType> selectTypesOf (ProjectSettingsService.Step... step) {
182 // --------------------------------------------------------------------------------------
183 StringBuffer query = new StringBuffer("from SimulationContextType where step='").append(step[0].getNumber()).append("'");
184 for (int i=1; i<step.length; i++) { // Useless to order as the result mixes localized and non localized types
185 query = query.append(" or step='").append(step[i].getNumber()).append("'");
187 query = query.append(" order by step asc");
188 return Database.getSession().createQuery(query.toString()).list();
191 @SuppressWarnings("unchecked")
192 public static List<SimulationContextType> selectTypesWhere (SimulationContextType.Properties sprop) {
193 // ---------------------------------------------------------------------------------------------------
194 StringBuffer query = new StringBuffer("from SimulationContextType");
195 String separator = " where";
196 ProjectSettingsService.Step step = sprop.getStep();
197 ProgressState state = sprop.getProgressState();
198 String order = " order by step asc";
201 query = query.append(separator).append(" step='").append(step.getNumber()).append("'");
203 order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
206 query = query.append(separator).append(" state='").append(state.toString()).append("'");
207 // separator = " and";
209 if (state != ProgressState.APPROVED) order = " order by name asc";
210 else order = ""; // Approved types are localized
213 query = query.append(order);
214 return Database.getSession().createQuery(query.toString()).list();
217 public static SimulationContextType selectType (String name) {
218 // ------------------------------------------------------------
219 StringBuffer query = new StringBuffer("from SimulationContextType where name='").append(name).append("'");
220 return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
223 public static SimulationContextType selectType (int index) {
224 // ----------------------------------------------------------
225 StringBuffer query = new StringBuffer("from SimulationContextType where rid='").append(index).append("'");
226 return (SimulationContextType)Database.getSession().createQuery(query.toString()).uniqueResult();
229 // ==============================================================================================================================
230 // Protected services
231 // ==============================================================================================================================
233 public void hold () {
234 // ----------------------
236 if (this.isSaved()) Database.getSession().update(this);
239 public void release () {
240 // -------------------------
242 if (this.isSaved()) Database.getSession().update(this);