Salome HOME
Study validation cycle operations are implemented according to the specification.
[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.Iterator;
13 import java.util.List;
14 import java.util.Vector;
15
16 import org.hibernate.criterion.Criterion;
17 import org.hibernate.criterion.Order;
18 import org.hibernate.criterion.Restrictions;
19 import org.splat.dal.bo.som.ProgressState;
20 import org.splat.dal.bo.som.SimulationContext;
21 import org.splat.dal.bo.som.SimulationContextType;
22 import org.splat.dal.dao.som.SimulationContextDAO;
23 import org.splat.dal.dao.som.SimulationContextTypeDAO;
24 import org.splat.kernel.InvalidPropertyException;
25 import org.splat.log.AppLogger;
26 import org.splat.service.dto.SimulationContextFacade;
27 import org.splat.service.technical.ProjectSettingsService;
28 import org.springframework.transaction.annotation.Transactional;
29
30 /**
31  * Simulation context service implementation.
32  * 
33  * @author rkv
34  * 
35  */
36 public class SimulationContextServiceImpl implements SimulationContextService {
37
38         /**
39          * logger for the service.
40          */
41         public final static AppLogger LOG = AppLogger
42                         .getLogger(SimulationContextServiceImpl.class);
43         
44         /**
45          * Step property name.
46          */
47         public final static String STEP_PROP = "step";
48         
49         /**
50          * Injected simulation context DAO.
51          */
52         private SimulationContextDAO _simulationContextDAO;
53
54         /**
55          * Injected simulation context type DAO.
56          */
57         private SimulationContextTypeDAO _simulationContextTypeDAO;
58         /**
59          * Injected project settings service.
60          */
61         private ProjectSettingsService _projectSettings;
62
63         /**
64          * Get simulation contexts which are currently in the given state.
65          * 
66          * @param aState
67          *            the progress state to filter contexts
68          * @return the list of found contexts as a vector of SimulationContextFacade
69          * @throws InvalidPropertyException
70          *             if the given state is invalid
71          */
72         @Transactional(readOnly=true)
73         public Vector<SimulationContextFacade> getSimulationContextsInState(
74                         final ProgressState aState) throws InvalidPropertyException {
75                 SimulationContext.Properties cprop = new SimulationContext.Properties();
76                 List<SimulationContext> context = selectSimulationContextsWhere(cprop
77                                 .setState(aState));
78
79                 Vector<SimulationContextFacade> result = new Vector<SimulationContextFacade>(
80                                 context.size());
81                 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
82                         result.add(new SimulationContextFacade(i.next(),
83                                         getProjectSettings().getAllSteps()));
84                 }
85                 return result;
86         }
87
88         /**
89          * {@inheritDoc}
90          * 
91          * @see org.splat.service.SimulationContextService#selectSimulationContext(int)
92          */
93         public SimulationContext selectSimulationContext(final long index) {
94                 return getSimulationContextDAO().get(index);
95         }
96
97         /**
98          * {@inheritDoc}
99          * 
100          * @see org.splat.service.SimulationContextService#selectSimulationContext(org.splat.dal.bo.som.SimulationContextType, java.lang.String)
101          */
102         public SimulationContext selectSimulationContext(
103                         final SimulationContextType celt, final String value) {
104                 SimulationContext result = null;
105                 try {
106                         SimulationContext.Properties cprop = new SimulationContext.Properties();
107                         List<SimulationContext> clist = selectSimulationContextsWhere(cprop
108                                         .setType(celt).setValue(value));
109                         if (!clist.isEmpty()) {
110                                 result = clist.get(0); // Supposed being the most used one if many exist
111                         }
112                 } catch (InvalidPropertyException error) {
113                         LOG.info("Attempt to select a simulation context \""
114                                         + celt.getName() + "\" with an invalid value.");
115                 }
116                 return result;
117         }
118
119         /**
120          * {@inheritDoc}
121          * 
122          * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties)
123          */
124         @Transactional(readOnly = true)
125         public List<SimulationContext> selectSimulationContextsWhere(
126                         final SimulationContext.Properties cprop) {
127                 // StringBuffer query = new StringBuffer("from SimulationContext");
128                 // String separator = " where";
129                 SimulationContextType celt = cprop.getType();
130                 String value = cprop.getValue();
131                 ProgressState state = cprop.getProgressState();
132                 // String order = "";
133                 Criterion aCondition = null;
134                 Order anOrder = null;
135
136                 if (celt != null) {
137                         // query = query.append(separator).append(" type='").append(
138                         // celt.getIndex()).append("'");
139                         // separator = " and";
140                         // order = " order by value asc";
141                         aCondition = Restrictions.eq("type", celt);
142                         anOrder = Order.asc("value");
143                 }
144                 if (value != null) {
145                         // query = query.append(separator).append(" value='").append(value)
146                         // .append("'");
147                         // separator = " and";
148
149                         aCondition = Restrictions.and(aCondition, Restrictions.eq("value",
150                                         value));
151                 }
152                 if (state != null) {
153                         // query = query.append(separator).append(" state='").append(state)
154                         // .append("'");
155                         if (celt == null) {
156                                 // order = " order by type asc";
157                                 anOrder = Order.asc("type");
158                         }
159                         aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
160                                         state));
161                 }
162                 // query.append(order);
163                 return getSimulationContextDAO().getFilteredList(aCondition, anOrder);
164         }
165
166         /**
167          * {@inheritDoc}
168          * 
169          * @see org.splat.service.SimulationContextService#getSimulationContextList()
170          */
171         @Transactional(readOnly = true)
172         public List<SimulationContext> getSimulationContextList() {
173                 SimulationContext.Properties cprop = new SimulationContext.Properties();
174                 SimulationContextType product = selectType("product");
175                 List<SimulationContext> resList = selectSimulationContextsWhere(cprop
176                                 .setType(product));
177                 return resList;
178         }
179
180         /**
181          * {@inheritDoc}
182          * 
183          * @see org.splat.service.SimulationContextService#selectAllTypes()
184          */
185         public List<SimulationContextType> selectAllTypes() {
186                 // Useless to order by names as the result mixes localized
187                 // and non localized types
188                 return getSimulationContextTypeDAO().getAll(Order.asc(STEP_PROP));
189         }
190
191         /**
192          * {@inheritDoc}
193          * 
194          * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
195          */
196         public List<SimulationContextType> selectTypesOf(
197                         final ProjectSettingsService.Step... step) {
198                 // StringBuffer query = new StringBuffer(
199                 // "from SimulationContextType where step='").append(
200                 // step[0].getNumber()).append("'");
201                 // for (int i = 1; i < step.length; i++) { // Useless to order as the result mixes localized and non localized types
202                 // query = query.append(" or step='").append(step[i].getNumber())
203                 // .append("'");
204                 // }
205                 // query = query.append(" order by step asc");
206                 Criterion aCondition = Restrictions.eq(STEP_PROP, step[0].getNumber());
207                 for (int i = 1; i < step.length; i++) { // Useless to order as the result mixes localized and non localized types
208                         aCondition = Restrictions.or(aCondition, Restrictions.eq(STEP_PROP,
209                                         step[i].getNumber()));
210                 }
211                 return getSimulationContextTypeDAO().getFilteredList(aCondition,
212                                 Order.asc(STEP_PROP));
213         }
214
215         /**
216          * {@inheritDoc}
217          * 
218          * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
219          */
220         public List<SimulationContextType> selectTypesWhere(
221                         final SimulationContextType.Properties sprop) {
222                 // StringBuffer query = new StringBuffer("from SimulationContextType");
223                 // String separator = " where";
224                 ProjectSettingsService.Step step = sprop.getStep();
225                 ProgressState state = sprop.getProgressState();
226                 // String order = " order by step asc";
227                 //
228                 // if (step != null) {
229                 // query = query.append(separator).append(" step='")
230                 // .append(step.getNumber()).append("'");
231                 // separator = " and";
232                 // order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
233                 // }
234                 // if (state != null) {
235                 // query = query.append(separator).append(" state='")
236                 // .append(state.toString()).append("'");
237                 // // separator = " and";
238                 // if (step != null) {
239                 // if (state != ProgressState.APPROVED)
240                 // order = " order by name asc";
241                 // else
242                 // order = ""; // Approved types are localized
243                 // }
244                 // }
245                 // query = query.append(order);
246                 Criterion aCondition = null;
247                 Order anOrder = Order.asc(STEP_PROP);
248                 if (step != null) {
249                         aCondition = Restrictions.eq(STEP_PROP, step.getNumber());
250                         anOrder = Order.desc("state"); // APPROVED (upper case A) is grater than inCHECK (lower case i)
251                 }
252                 if (state != null) {
253                         aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
254                                         state));
255                         if (step != null) {
256                                 if (state == ProgressState.APPROVED) {
257                                         anOrder = null; // Approved types are localized
258                                 } else {
259                                         anOrder = Order.asc("name");
260                                 }
261                         }
262                 }
263                 return getSimulationContextTypeDAO().getFilteredList(aCondition,
264                                 anOrder);
265         }
266
267         /**
268          * {@inheritDoc}
269          * 
270          * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
271          */
272         @Transactional(readOnly = true)
273         public SimulationContextType selectType(final String name) {
274                 return getSimulationContextTypeDAO().findByCriteria(
275                                 Restrictions.eq("name", name));
276         }
277
278         /**
279          * {@inheritDoc}
280          * 
281          * @see org.splat.service.SimulationContextService#selectType(long)
282          */
283         public SimulationContextType selectType(final long index) {
284                 return getSimulationContextTypeDAO().get(index);
285         }
286
287         /**
288          * {@inheritDoc}
289          * 
290          * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
291          */
292         public void hold(final SimulationContext simCtx) {
293                 simCtx.setCounter(simCtx.getCounter() + 1);
294                 if (simCtx.isSaved()) {
295                         getSimulationContextDAO().update(simCtx);
296                 }
297         }
298
299         /**
300          * {@inheritDoc}
301          * 
302          * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
303          */
304         public void release(final SimulationContext simCtx) {
305                 simCtx.setCounter(simCtx.getCounter() - 1);
306                 if (simCtx.isSaved()) {
307                         getSimulationContextDAO().update(simCtx);
308                 }
309         }
310
311         /**
312          * Approve the simulation context.
313          * 
314          * @param simCtx
315          *            the context to approve
316          * @return true if approval succeeded
317          */
318         public boolean approve(final SimulationContext simCtx) {
319                 boolean res = (simCtx.getProgressState() == ProgressState.inCHECK);
320                 if (res) {
321                         simCtx.setProgressState(ProgressState.APPROVED); // The type name is supposed being localized
322                         if (simCtx.isSaved()) {
323                                 getSimulationContextDAO().update(simCtx);
324                         }
325                 }
326                 return res;
327         }
328
329         /**
330          * @param simCtxType
331          * @return
332          */
333         public ProjectSettingsService.Step getAttachedStep(
334                         final SimulationContextType simCtxType) {
335                 return getProjectSettings().getStep(simCtxType.getStep());
336         }
337
338         /**
339          * Get the simulationContextDAO.
340          * 
341          * @return the simulationContextDAO
342          */
343         public SimulationContextDAO getSimulationContextDAO() {
344                 return _simulationContextDAO;
345         }
346
347         /**
348          * Set the simulationContextDAO.
349          * 
350          * @param simulationContextDAO
351          *            the simulationContextDAO to set
352          */
353         public void setSimulationContextDAO(
354                         final SimulationContextDAO simulationContextDAO) {
355                 _simulationContextDAO = simulationContextDAO;
356         }
357
358         /**
359          * Get the simulationContextTypeDAO.
360          * 
361          * @return the simulationContextTypeDAO
362          */
363         public SimulationContextTypeDAO getSimulationContextTypeDAO() {
364                 return _simulationContextTypeDAO;
365         }
366
367         /**
368          * Set the simulationContextTypeDAO.
369          * 
370          * @param simulationContextTypeDAO
371          *            the simulationContextTypeDAO to set
372          */
373         public void setSimulationContextTypeDAO(
374                         final SimulationContextTypeDAO simulationContextTypeDAO) {
375                 _simulationContextTypeDAO = simulationContextTypeDAO;
376         }
377
378         /**
379          * Get project settings.
380          * 
381          * @return Project settings service
382          */
383         private ProjectSettingsService getProjectSettings() {
384                 return _projectSettings;
385         }
386
387         /**
388          * Set project settings service.
389          * 
390          * @param projectSettingsService
391          *            project settings service
392          */
393         public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
394                 _projectSettings = projectSettingsService;
395         }
396 }