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