1 /*****************************************************************************
5 * Creation date 16.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.List;
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;
32 * Simulation context service implementation.
37 public class SimulationContextServiceImpl implements SimulationContextService {
40 * logger for the service.
42 public final static AppLogger logger = AppLogger
43 .getLogger(SimulationContextServiceImpl.class);
45 * Injected simulation context DAO.
47 private SimulationContextDAO _simulationContextDAO;
50 * Injected simulation context type DAO.
52 private SimulationContextTypeDAO _simulationContextTypeDAO;
57 * @see org.splat.service.SimulationContextService#selectSimulationContext(int)
59 public SimulationContext selectSimulationContext(long index) {
60 return getSimulationContextDAO().get(index);
66 * @see org.splat.service.SimulationContextService#selectSimulationContext(org.splat.dal.bo.som.SimulationContextType, java.lang.String)
68 public SimulationContext selectSimulationContext(
69 SimulationContextType celt, String value) {
70 SimulationContext result = null;
72 SimulationContext.Properties cprop = new SimulationContext.Properties();
73 List<SimulationContext> clist = selectSimulationContextsWhere(cprop
74 .setType(celt).setValue(value));
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.");
87 * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties)
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();
98 Criterion aCondition = 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");
110 // query = query.append(separator).append(" value='").append(value)
112 // separator = " and";
114 aCondition = Restrictions.and(aCondition, Restrictions.eq("value", value));
117 // query = query.append(separator).append(" state='").append(state)
120 // order = " order by type asc";
121 anOrder = Order.asc("type");
123 aCondition = Restrictions.and(aCondition, Restrictions.eq("state", state));
125 // query.append(order);
126 // return (List<SimulationContext>) Database.getSession().createQuery(
127 // query.toString()).list();
128 return getSimulationContextDAO().getFilteredList(aCondition, anOrder);
134 * @see org.splat.service.SimulationContextService#getSimulationContextList()
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
148 * @see org.splat.service.SimulationContextService#createType(java.lang.String, org.splat.service.technical.ProjectSettingsService.Step)
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);
163 * @see org.splat.service.SimulationContextService#selectAllTypes()
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();
177 * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
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())
190 query = query.append(" order by step asc");
191 return Database.getSession().createQuery(query.toString()).list();
197 * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
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";
209 query = query.append(separator).append(" step='").append(
210 step.getNumber()).append("'");
212 order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
215 query = query.append(separator).append(" state='").append(
216 state.toString()).append("'");
217 // separator = " and";
219 if (state != ProgressState.APPROVED)
220 order = " order by name asc";
222 order = ""; // Approved types are localized
225 query = query.append(order);
226 return Database.getSession().createQuery(query.toString()).list();
232 * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
234 @Transactional(readOnly=true)
235 public SimulationContextType selectType(String name) {
236 return getSimulationContextTypeDAO().findByCriteria(
237 Restrictions.eq("name", name));
243 * @see org.splat.service.SimulationContextService#selectType(long)
245 public SimulationContextType selectType(long index) {
246 return getSimulationContextTypeDAO().get(index);
252 * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
254 public void hold(SimulationContext simCtx) {
255 simCtx.setCounter(simCtx.getCounter() + 1);
256 if (simCtx.isSaved()) {
257 getSimulationContextDAO().update(simCtx);
264 * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
266 public void release(SimulationContext simCtx) {
267 simCtx.setCounter(simCtx.getCounter() - 1);
268 if (simCtx.isSaved()) {
269 getSimulationContextDAO().update(simCtx);
274 * Get the simulationContextDAO.
276 * @return the simulationContextDAO
278 public SimulationContextDAO getSimulationContextDAO() {
279 return _simulationContextDAO;
283 * Set the simulationContextDAO.
285 * @param simulationContextDAO
286 * the simulationContextDAO to set
288 public void setSimulationContextDAO(
289 SimulationContextDAO simulationContextDAO) {
290 _simulationContextDAO = simulationContextDAO;
294 * Get the simulationContextTypeDAO.
296 * @return the simulationContextTypeDAO
298 public SimulationContextTypeDAO getSimulationContextTypeDAO() {
299 return _simulationContextTypeDAO;
303 * Set the simulationContextTypeDAO.
305 * @param simulationContextTypeDAO
306 * the simulationContextTypeDAO to set
308 public void setSimulationContextTypeDAO(
309 SimulationContextTypeDAO simulationContextTypeDAO) {
310 _simulationContextTypeDAO = simulationContextTypeDAO;