Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / dal / bo / som / SimulationContext.java
1 package org.splat.dal.bo.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.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;
20
21
22 public class SimulationContext extends Persistent implements Serializable {
23
24         private SimulationContextType  type;     // User extendable types
25     private int                    step;
26     private ProgressState          state;
27     private String                 value;
28     private int                    counter;
29
30     private static final long serialVersionUID = 422889133378471949L;
31
32 //  ==============================================================================================================================
33 //  Construction
34 //  ==============================================================================================================================
35
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;
43
44 //  - Public services
45
46       public void clear () {
47         super.clear();
48         type  = null;
49         step  = null;
50         state = null;
51         value = null;
52       }
53           public ProgressState getProgressState () {
54         return state;
55       }
56       public SimulationContextType getType () {
57         return type;
58       }
59       public String getValue () {
60         return value;
61       }
62       
63 //  - Setters of SimulationContext properties
64
65       public Properties setState (ProgressState state) throws InvalidPropertyException
66       {
67         if (state != ProgressState.inCHECK && state != ProgressState.APPROVED) {
68           throw new InvalidPropertyException("state");
69         }
70         this.state = state;
71         return this;
72       }
73       public Properties setStep (ProjectSettingsService.Step step) throws InvalidPropertyException
74       {
75         this.step = step;
76         return this;
77       }
78       public Properties setValue (String value) throws InvalidPropertyException
79       {
80         if (value.length() == 0) throw new InvalidPropertyException("value");
81         this.value = value;
82         return this;
83       }
84       public Properties setType (SimulationContextType type)
85       {
86         this.type = type;
87         return this;
88       }
89 //  - Global validity check
90         
91       public void checkValidity () throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException
92       {
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");
97       }
98     }
99 //  Database fetch constructor
100     protected SimulationContext () {            
101     }
102 //  Internal constructor
103     public SimulationContext (Properties kprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException {
104       super(kprop);   // Throws one of the above exception if not valid
105       type    = kprop.type;
106       step    = kprop.step.getNumber();
107       value   = kprop.value;
108       counter = 0;
109       state   = kprop.state;
110       if (state == null) state = ProgressState.inCHECK;
111     }
112     
113 //  ==============================================================================================================================
114 //  Public member functions
115 //  ==============================================================================================================================
116
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);
122           return true;
123     }
124
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;
130       return false;      
131     }
132
133     public String getValue () {
134 //  -------------------------
135       return value;
136     }
137
138     public ProgressState getProgressState () {
139 //  ----------------------------------------
140       return state;
141     }
142
143     public SimulationContextType getType () {
144 //  ---------------------------------------
145       return type;
146     }
147
148     public boolean isInto (Step container) {
149 //  --------------------------------------
150       return (step == container.getNumber());
151     }
152
153     public boolean isShared () {
154 //  --------------------------
155       return (counter > 1);
156     }
157
158 //  ==============================================================================================================================
159 //  Public services
160 //  ==============================================================================================================================
161
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();
167       session.save(type);
168           
169       return type;
170     }
171
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();
178     }
179
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("'");
186           }
187           query = query.append(" order by step asc");
188       return  Database.getSession().createQuery(query.toString()).list();
189     }
190
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";
199
200       if (step != null) {
201         query     = query.append(separator).append(" step='").append(step.getNumber()).append("'");
202         separator = " and";
203         order     = " order by state desc";         // APPROVED (upper case A) is grater than inCHECK (lower case i)
204       }
205       if (state != null) {
206         query     = query.append(separator).append(" state='").append(state.toString()).append("'");
207 //      separator = " and";
208         if (step != null) {
209           if (state != ProgressState.APPROVED) order = " order by name asc";
210           else  order = "";                         // Approved types are localized
211         }
212       }
213       query = query.append(order);
214       return  Database.getSession().createQuery(query.toString()).list();
215     }
216
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();
221     }
222     
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();
227     }
228     
229 //  ==============================================================================================================================
230 //  Protected services
231 //  ==============================================================================================================================
232
233     public void hold () {
234 //  ----------------------
235       counter += 1;
236       if (this.isSaved()) Database.getSession().update(this);
237     }
238
239     public void release () {
240 //  -------------------------
241       counter -= 1;
242       if (this.isSaved()) Database.getSession().update(this);
243     }
244 }