Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[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          * Get counter.
159          * @return counter
160          */
161         public int getCounter() {
162                 return counter;
163         }
164         /**
165          * Set counter.
166          * @param aValue a counter value to set
167          */
168         public void setCounter(int aValue) {
169                 counter = aValue;
170         }
171 }