Salome HOME
SIMAN Eclipse workspace first version
[tools/siman.git] / Workspace / SPlat / src / org / splat / som / SimulationContext.java
1 package org.splat.som;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.Serializable;
9 import java.util.List;
10
11 import org.hibernate.Session;
12
13 import org.splat.kernel.Persistent;
14 import org.splat.kernel.InvalidPropertyException;
15 import org.splat.kernel.MissedPropertyException;
16 import org.splat.kernel.MultiplyDefinedException;
17
18
19 public class SimulationContext extends Persistent implements Serializable {
20
21         private SimulationContextType  type;     // User extendable types
22     private int                    step;
23     private ProgressState          state;
24     private String                 value;
25     private int                    counter;
26
27     private static final long serialVersionUID = 422889133378471949L;
28
29 //  ==============================================================================================================================
30 //  Construction
31 //  ==============================================================================================================================
32
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;
40
41 //  - Public services
42
43       public void clear () {
44         super.clear();
45         type  = null;
46         step  = null;
47         state = null;
48         value = null;
49       }
50           protected ProgressState getProgressState () {
51         return state;
52       }
53       public SimulationContextType getType () {
54         return type;
55       }
56       public String getValue () {
57         return value;
58       }
59       
60 //  - Setters of SimulationContext properties
61
62       public Properties setState (ProgressState state) throws InvalidPropertyException
63       {
64         if (state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
65           throw new InvalidPropertyException("state");
66         }
67         this.state = state;
68         return this;
69       }
70       protected Properties setStep (ProjectSettings.Step step) throws InvalidPropertyException
71       {
72         this.step = step;
73         return this;
74       }
75       public Properties setValue (String value) throws InvalidPropertyException
76       {
77         if (value.length() == 0) throw new InvalidPropertyException("value");
78         this.value = value;
79         return this;
80       }
81       public Properties setType (SimulationContextType type)
82       {
83         this.type = type;
84         return this;
85       }
86 //  - Global validity check
87         
88       public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
89       {
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");
94       }
95     }
96 //  Database fetch constructor
97     protected SimulationContext () {            
98     }
99 //  Internal constructor
100     protected SimulationContext (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
101       super(kprop);   // Throws one of the above exception if not valid
102       type    = kprop.type;
103       step    = kprop.step.getNumber();
104       value   = kprop.value;
105       counter = 0;
106       state   = kprop.state;
107       if (state == null) state = ProgressState.inCHECK;
108     }
109     
110 //  ==============================================================================================================================
111 //  Public member functions
112 //  ==============================================================================================================================
113
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);
119           return true;
120     }
121
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;
127       return false;      
128     }
129
130     public String getValue () {
131 //  -------------------------
132       return value;
133     }
134
135     public ProgressState getProgressState () {
136 //  ----------------------------------------
137       return state;
138     }
139
140     public SimulationContextType getType () {
141 //  ---------------------------------------
142       return type;
143     }
144
145     public boolean isInto (Step container) {
146 //  --------------------------------------
147       return (step == container.getNumber());
148     }
149
150     public boolean isShared () {
151 //  --------------------------
152       return (counter > 1);
153     }
154
155 //  ==============================================================================================================================
156 //  Public services
157 //  ==============================================================================================================================
158
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();
164       session.save(type);
165           
166       return type;
167     }
168
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();
175     }
176
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("'");
183           }
184           query = query.append(" order by step asc");
185       return  Database.getSession().createQuery(query.toString()).list();
186     }
187
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";
196
197       if (step != null) {
198         query     = query.append(separator).append(" step='").append(step.getNumber()).append("'");
199         separator = " and";
200         order     = " order by state desc";         // APPROVED (upper case A) is grater than inCHECK (lower case i)
201       }
202       if (state != null) {
203         query     = query.append(separator).append(" state='").append(state.toString()).append("'");
204 //      separator = " and";
205         if (step != null) {
206           if (state != ProgressState.APPROVED) order = " order by name asc";
207           else  order = "";                         // Approved types are localized
208         }
209       }
210       query = query.append(order);
211       return  Database.getSession().createQuery(query.toString()).list();
212     }
213
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();
218     }
219     
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();
224     }
225     
226 //  ==============================================================================================================================
227 //  Protected services
228 //  ==============================================================================================================================
229
230     protected void hold () {
231 //  ----------------------
232       counter += 1;
233       if (this.isSaved()) Database.getSession().update(this);
234     }
235
236     protected void release () {
237 //  -------------------------
238       counter -= 1;
239       if (this.isSaved()) Database.getSession().update(this);
240     }
241 }