Salome HOME
Creation of a new study is fixed. Database.getSessoin is not used now during creation...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / SimulationContextServiceImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   16.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.util.List;
13
14 import org.hibernate.criterion.Criterion;
15 import org.hibernate.criterion.Order;
16 import org.hibernate.criterion.Restrictions;
17 import org.splat.dal.bo.som.ProgressState;
18 import org.splat.dal.bo.som.SimulationContext;
19 import org.splat.dal.bo.som.SimulationContextType;
20 import org.splat.dal.dao.som.Database;
21 import org.splat.dal.dao.som.SimulationContextDAO;
22 import org.splat.dal.dao.som.SimulationContextTypeDAO;
23 import org.splat.kernel.InvalidPropertyException;
24 import org.splat.log.AppLogger;
25 import org.splat.service.technical.ProjectSettingsService;
26 import org.springframework.transaction.annotation.Transactional;
27
28 /**
29  * Simulation context service implementation.
30  * 
31  * @author rkv
32  * 
33  */
34 public class SimulationContextServiceImpl implements SimulationContextService {
35
36         /**
37          * logger for the service.
38          */
39         public final static AppLogger logger = AppLogger
40                         .getLogger(SimulationContextServiceImpl.class);
41         /**
42          * Injected simulation context DAO.
43          */
44         private SimulationContextDAO _simulationContextDAO;
45
46         /**
47          * Injected simulation context type DAO.
48          */
49         private SimulationContextTypeDAO _simulationContextTypeDAO;
50
51         /**
52          * {@inheritDoc}
53          * 
54          * @see org.splat.service.SimulationContextService#selectSimulationContext(int)
55          */
56         public SimulationContext selectSimulationContext(long index) {
57                 return getSimulationContextDAO().get(index);
58         }
59
60         /**
61          * {@inheritDoc}
62          * 
63          * @see org.splat.service.SimulationContextService#selectSimulationContext(org.splat.dal.bo.som.SimulationContextType, java.lang.String)
64          */
65         public SimulationContext selectSimulationContext(
66                         SimulationContextType celt, String value) {
67                 SimulationContext result = null;
68                 try {
69                         SimulationContext.Properties cprop = new SimulationContext.Properties();
70                         List<SimulationContext> clist = selectSimulationContextsWhere(cprop
71                                         .setType(celt).setValue(value));
72                         if (!clist.isEmpty())
73                                 result = clist.get(0); // Supposed being the most used one if many exist
74                 } catch (InvalidPropertyException error) {
75                         logger.info("Attempt to select a simulation context \""
76                                         + celt.getName() + "\" with an invalid value.");
77                 }
78                 return result;
79         }
80
81         /**
82          * {@inheritDoc}
83          * 
84          * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties)
85          */
86         @Transactional(readOnly = true)
87         public List<SimulationContext> selectSimulationContextsWhere(
88                         SimulationContext.Properties cprop) {
89                 // StringBuffer query = new StringBuffer("from SimulationContext");
90                 // String separator = " where";
91                 SimulationContextType celt = cprop.getType();
92                 String value = cprop.getValue();
93                 ProgressState state = cprop.getProgressState();
94                 // String order = "";
95                 Criterion aCondition = null;
96                 Order anOrder = null;
97
98                 if (celt != null) {
99                         // query = query.append(separator).append(" type='").append(
100                         // celt.getIndex()).append("'");
101                         // separator = " and";
102                         // order = " order by value asc";
103                         aCondition = Restrictions.eq("type", celt);
104                         anOrder = Order.asc("value");
105                 }
106                 if (value != null) {
107                         // query = query.append(separator).append(" value='").append(value)
108                         // .append("'");
109                         // separator = " and";
110
111                         aCondition = Restrictions.and(aCondition, Restrictions.eq("value",
112                                         value));
113                 }
114                 if (state != null) {
115                         // query = query.append(separator).append(" state='").append(state)
116                         // .append("'");
117                         if (celt == null) {
118                                 // order = " order by type asc";
119                                 anOrder = Order.asc("type");
120                         }
121                         aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
122                                         state));
123                 }
124                 // query.append(order);
125                 // return (List<SimulationContext>) Database.getSession().createQuery(
126                 // query.toString()).list();
127                 return getSimulationContextDAO().getFilteredList(aCondition, anOrder);
128         }
129
130         /**
131          * {@inheritDoc}
132          * 
133          * @see org.splat.service.SimulationContextService#getSimulationContextList()
134          */
135         @Transactional(readOnly = true)
136         public List<SimulationContext> getSimulationContextList() {
137                 SimulationContext.Properties cprop = new SimulationContext.Properties();
138                 SimulationContextType product = selectType("product");
139                 List<SimulationContext> resList = selectSimulationContextsWhere(cprop
140                                 .setType(product));
141                 return resList;
142         }
143
144         /**
145          * {@inheritDoc}
146          * 
147          * @see org.splat.service.SimulationContextService#createType(java.lang.String, org.splat.service.technical.ProjectSettingsService.Step)
148          */
149         @Transactional
150         public SimulationContextType createType(String name,
151                         ProjectSettingsService.Step step) throws InvalidPropertyException {
152                 // TODO: Check for duplicate definition
153                 SimulationContextType type = new SimulationContextType(name, step);
154                 getSimulationContextTypeDAO().create(type);
155
156                 return type;
157         }
158
159         /**
160          * {@inheritDoc}
161          * 
162          * @see org.splat.service.SimulationContextService#selectAllTypes()
163          */
164         @SuppressWarnings("unchecked")
165         public List<SimulationContextType> selectAllTypes() {
166                 // Useless to order by names as the result mixes localized
167                 // and non localized types
168                 return getSimulationContextTypeDAO().getFilteredList(null,
169                                 Order.asc("step"));
170         }
171
172         /**
173          * {@inheritDoc}
174          * 
175          * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
176          */
177         @SuppressWarnings("unchecked")
178         public List<SimulationContextType> selectTypesOf(
179                         ProjectSettingsService.Step... step) {
180                 // --------------------------------------------------------------------------------------
181                 StringBuffer query = new StringBuffer(
182                                 "from SimulationContextType where step='").append(
183                                 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())
186                                         .append("'");
187                 }
188                 query = query.append(" order by step asc");
189                 return Database.getSession().createQuery(query.toString()).list();
190         }
191
192         /**
193          * {@inheritDoc}
194          * 
195          * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
196          */
197         @SuppressWarnings("unchecked")
198         public List<SimulationContextType> selectTypesWhere(
199                         SimulationContextType.Properties sprop) {
200                 StringBuffer query = new StringBuffer("from SimulationContextType");
201                 String separator = " where";
202                 ProjectSettingsService.Step step = sprop.getStep();
203                 ProgressState state = sprop.getProgressState();
204                 String order = " order by step asc";
205
206                 if (step != null) {
207                         query = query.append(separator).append(" step='").append(
208                                         step.getNumber()).append("'");
209                         separator = " and";
210                         order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
211                 }
212                 if (state != null) {
213                         query = query.append(separator).append(" state='").append(
214                                         state.toString()).append("'");
215                         // separator = " and";
216                         if (step != null) {
217                                 if (state != ProgressState.APPROVED)
218                                         order = " order by name asc";
219                                 else
220                                         order = ""; // Approved types are localized
221                         }
222                 }
223                 query = query.append(order);
224                 return Database.getSession().createQuery(query.toString()).list();
225         }
226
227         /**
228          * {@inheritDoc}
229          * 
230          * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
231          */
232         @Transactional(readOnly = true)
233         public SimulationContextType selectType(String name) {
234                 return getSimulationContextTypeDAO().findByCriteria(
235                                 Restrictions.eq("name", name));
236         }
237
238         /**
239          * {@inheritDoc}
240          * 
241          * @see org.splat.service.SimulationContextService#selectType(long)
242          */
243         public SimulationContextType selectType(long index) {
244                 return getSimulationContextTypeDAO().get(index);
245         }
246
247         /**
248          * {@inheritDoc}
249          * 
250          * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
251          */
252         public void hold(SimulationContext simCtx) {
253                 simCtx.setCounter(simCtx.getCounter() + 1);
254                 if (simCtx.isSaved()) {
255                         getSimulationContextDAO().update(simCtx);
256                 }
257         }
258
259         /**
260          * {@inheritDoc}
261          * 
262          * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
263          */
264         public void release(SimulationContext simCtx) {
265                 simCtx.setCounter(simCtx.getCounter() - 1);
266                 if (simCtx.isSaved()) {
267                         getSimulationContextDAO().update(simCtx);
268                 }
269         }
270
271         /**
272          * Get the simulationContextDAO.
273          * 
274          * @return the simulationContextDAO
275          */
276         public SimulationContextDAO getSimulationContextDAO() {
277                 return _simulationContextDAO;
278         }
279
280         /**
281          * Set the simulationContextDAO.
282          * 
283          * @param simulationContextDAO
284          *            the simulationContextDAO to set
285          */
286         public void setSimulationContextDAO(
287                         SimulationContextDAO simulationContextDAO) {
288                 _simulationContextDAO = simulationContextDAO;
289         }
290
291         /**
292          * Get the simulationContextTypeDAO.
293          * 
294          * @return the simulationContextTypeDAO
295          */
296         public SimulationContextTypeDAO getSimulationContextTypeDAO() {
297                 return _simulationContextTypeDAO;
298         }
299
300         /**
301          * Set the simulationContextTypeDAO.
302          * 
303          * @param simulationContextTypeDAO
304          *            the simulationContextTypeDAO to set
305          */
306         public void setSimulationContextTypeDAO(
307                         SimulationContextTypeDAO simulationContextTypeDAO) {
308                 _simulationContextTypeDAO = simulationContextTypeDAO;
309         }
310 }