]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/SimulationContextServiceImpl.java
Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / SimulationContextServiceImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
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.SimulationContextDAO;
21 import org.splat.dal.dao.som.SimulationContextTypeDAO;
22 import org.splat.kernel.InvalidPropertyException;
23 import org.splat.log.AppLogger;
24 import org.splat.service.technical.ProjectSettingsService;
25 import org.splat.service.technical.ProjectSettingsServiceImpl;
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,
112                                         Restrictions.eq("value", 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,
122                                         Restrictions.eq("state", 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         public List<SimulationContextType> selectAllTypes() {
165                 // Useless to order by names as the result mixes localized
166                 // and non localized types
167                 return getSimulationContextTypeDAO().getAll(Order.asc("step"));
168         }
169
170         /**
171          * {@inheritDoc}
172          * 
173          * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
174          */
175         public List<SimulationContextType> selectTypesOf(
176                         ProjectSettingsService.Step... step) {
177                 // StringBuffer query = new StringBuffer(
178                 // "from SimulationContextType where step='").append(
179                 // step[0].getNumber()).append("'");
180                 // for (int i = 1; i < step.length; i++) { // Useless to order as the result mixes localized and non localized types
181                 // query = query.append(" or step='").append(step[i].getNumber())
182                 // .append("'");
183                 // }
184                 // query = query.append(" order by step asc");
185                 Criterion aCondition = Restrictions.eq("step", step[0].getNumber());
186                 for (int i = 1; i < step.length; i++) { // Useless to order as the result mixes localized and non localized types
187                         aCondition = Restrictions.or(aCondition,
188                                         Restrictions.eq("step", step[i].getNumber()));
189                 }
190                 return getSimulationContextTypeDAO().getFilteredList(aCondition,
191                                 Order.asc("step"));
192         }
193
194         /**
195          * {@inheritDoc}
196          * 
197          * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
198          */
199         public List<SimulationContextType> selectTypesWhere(
200                         SimulationContextType.Properties sprop) {
201                 // StringBuffer query = new StringBuffer("from SimulationContextType");
202                 // String separator = " where";
203                 ProjectSettingsService.Step step = sprop.getStep();
204                 ProgressState state = sprop.getProgressState();
205                 // String order = " order by step asc";
206                 //
207                 // if (step != null) {
208                 // query = query.append(separator).append(" step='")
209                 // .append(step.getNumber()).append("'");
210                 // separator = " and";
211                 // order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
212                 // }
213                 // if (state != null) {
214                 // query = query.append(separator).append(" state='")
215                 // .append(state.toString()).append("'");
216                 // // separator = " and";
217                 // if (step != null) {
218                 // if (state != ProgressState.APPROVED)
219                 // order = " order by name asc";
220                 // else
221                 // order = ""; // Approved types are localized
222                 // }
223                 // }
224                 // query = query.append(order);
225                 Criterion aCondition = null;
226                 Order anOrder = Order.asc("step");
227                 if (step != null) {
228                         aCondition = Restrictions.eq("step", step.getNumber());
229                         anOrder = Order.desc("state"); // APPROVED (upper case A) is grater than inCHECK (lower case i)
230                 }
231                 if (state != null) {
232                         aCondition = Restrictions.and(aCondition,
233                                         Restrictions.eq("state", state));
234                         if (step != null) {
235                                 if (state != ProgressState.APPROVED) {
236                                         anOrder = Order.asc("name");
237                                 } else {
238                                         anOrder = null; // Approved types are localized
239                                 }
240                         }
241                 }
242                 return getSimulationContextTypeDAO().getFilteredList(aCondition,
243                                 anOrder);
244         }
245
246         /**
247          * {@inheritDoc}
248          * 
249          * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
250          */
251         @Transactional(readOnly = true)
252         public SimulationContextType selectType(String name) {
253                 return getSimulationContextTypeDAO().findByCriteria(
254                                 Restrictions.eq("name", name));
255         }
256
257         /**
258          * {@inheritDoc}
259          * 
260          * @see org.splat.service.SimulationContextService#selectType(long)
261          */
262         public SimulationContextType selectType(long index) {
263                 return getSimulationContextTypeDAO().get(index);
264         }
265
266         /**
267          * {@inheritDoc}
268          * 
269          * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
270          */
271         public void hold(SimulationContext simCtx) {
272                 simCtx.setCounter(simCtx.getCounter() + 1);
273                 if (simCtx.isSaved()) {
274                         getSimulationContextDAO().update(simCtx);
275                 }
276         }
277
278         /**
279          * {@inheritDoc}
280          * 
281          * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
282          */
283         public void release(SimulationContext simCtx) {
284                 simCtx.setCounter(simCtx.getCounter() - 1);
285                 if (simCtx.isSaved()) {
286                         getSimulationContextDAO().update(simCtx);
287                 }
288         }
289
290         /**
291          * Approve the simulation context.
292          * 
293          * @param simCtx
294          *            the context to approve
295          * @return true if approval succeeded
296          */
297         public boolean approve(SimulationContext simCtx) {
298                 if (simCtx.getProgressState() != ProgressState.inCHECK)
299                         return false;
300                 simCtx.setProgressState(ProgressState.APPROVED); // The type name is supposed being localized
301                 if (simCtx.isSaved()) {
302                         getSimulationContextDAO().update(simCtx);
303                 }
304                 return true;
305         }
306
307         /**
308          * Approve the simulation context type.
309          * 
310          * @param simCtxType
311          *            the type to approve
312          * @return true if approval succeeded
313          */
314         public boolean approve(SimulationContextType simCtxType) {
315                 if (simCtxType.getState() != ProgressState.inCHECK)
316                         return false;
317                 simCtxType.setState(ProgressState.APPROVED); // The type name is supposed being localized
318                 getSimulationContextTypeDAO().update(simCtxType);
319                 return true;
320         }
321
322         /**
323          * @param simCtxType
324          * @return
325          */
326         public ProjectSettingsService.Step getAttachedStep(
327                         SimulationContextType simCtxType) {
328                 return ProjectSettingsServiceImpl.getStep(simCtxType.getStep());
329         }
330
331         /**
332          * Get the simulationContextDAO.
333          * 
334          * @return the simulationContextDAO
335          */
336         public SimulationContextDAO getSimulationContextDAO() {
337                 return _simulationContextDAO;
338         }
339
340         /**
341          * Set the simulationContextDAO.
342          * 
343          * @param simulationContextDAO
344          *            the simulationContextDAO to set
345          */
346         public void setSimulationContextDAO(
347                         SimulationContextDAO simulationContextDAO) {
348                 _simulationContextDAO = simulationContextDAO;
349         }
350
351         /**
352          * Get the simulationContextTypeDAO.
353          * 
354          * @return the simulationContextTypeDAO
355          */
356         public SimulationContextTypeDAO getSimulationContextTypeDAO() {
357                 return _simulationContextTypeDAO;
358         }
359
360         /**
361          * Set the simulationContextTypeDAO.
362          * 
363          * @param simulationContextTypeDAO
364          *            the simulationContextTypeDAO to set
365          */
366         public void setSimulationContextTypeDAO(
367                         SimulationContextTypeDAO simulationContextTypeDAO) {
368                 _simulationContextTypeDAO = simulationContextTypeDAO;
369         }
370 }