1 /*****************************************************************************
5 * Creation date 16.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.List;
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;
29 * Simulation context service implementation.
34 public class SimulationContextServiceImpl implements SimulationContextService {
37 * logger for the service.
39 public final static AppLogger logger = AppLogger
40 .getLogger(SimulationContextServiceImpl.class);
42 * Injected simulation context DAO.
44 private SimulationContextDAO _simulationContextDAO;
47 * Injected simulation context type DAO.
49 private SimulationContextTypeDAO _simulationContextTypeDAO;
54 * @see org.splat.service.SimulationContextService#selectSimulationContext(int)
56 public SimulationContext selectSimulationContext(long index) {
57 return getSimulationContextDAO().get(index);
63 * @see org.splat.service.SimulationContextService#selectSimulationContext(org.splat.dal.bo.som.SimulationContextType, java.lang.String)
65 public SimulationContext selectSimulationContext(
66 SimulationContextType celt, String value) {
67 SimulationContext result = null;
69 SimulationContext.Properties cprop = new SimulationContext.Properties();
70 List<SimulationContext> clist = selectSimulationContextsWhere(cprop
71 .setType(celt).setValue(value));
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.");
84 * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties)
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();
95 Criterion aCondition = 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");
107 // query = query.append(separator).append(" value='").append(value)
109 // separator = " and";
111 aCondition = Restrictions.and(aCondition, Restrictions.eq("value",
115 // query = query.append(separator).append(" state='").append(state)
118 // order = " order by type asc";
119 anOrder = Order.asc("type");
121 aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
124 // query.append(order);
125 // return (List<SimulationContext>) Database.getSession().createQuery(
126 // query.toString()).list();
127 return getSimulationContextDAO().getFilteredList(aCondition, anOrder);
133 * @see org.splat.service.SimulationContextService#getSimulationContextList()
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
147 * @see org.splat.service.SimulationContextService#createType(java.lang.String, org.splat.service.technical.ProjectSettingsService.Step)
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);
162 * @see org.splat.service.SimulationContextService#selectAllTypes()
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,
175 * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
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())
188 query = query.append(" order by step asc");
189 return Database.getSession().createQuery(query.toString()).list();
195 * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
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";
207 query = query.append(separator).append(" step='").append(
208 step.getNumber()).append("'");
210 order = " order by state desc"; // APPROVED (upper case A) is grater than inCHECK (lower case i)
213 query = query.append(separator).append(" state='").append(
214 state.toString()).append("'");
215 // separator = " and";
217 if (state != ProgressState.APPROVED)
218 order = " order by name asc";
220 order = ""; // Approved types are localized
223 query = query.append(order);
224 return Database.getSession().createQuery(query.toString()).list();
230 * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
232 @Transactional(readOnly = true)
233 public SimulationContextType selectType(String name) {
234 return getSimulationContextTypeDAO().findByCriteria(
235 Restrictions.eq("name", name));
241 * @see org.splat.service.SimulationContextService#selectType(long)
243 public SimulationContextType selectType(long index) {
244 return getSimulationContextTypeDAO().get(index);
250 * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
252 public void hold(SimulationContext simCtx) {
253 simCtx.setCounter(simCtx.getCounter() + 1);
254 if (simCtx.isSaved()) {
255 getSimulationContextDAO().update(simCtx);
262 * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
264 public void release(SimulationContext simCtx) {
265 simCtx.setCounter(simCtx.getCounter() - 1);
266 if (simCtx.isSaved()) {
267 getSimulationContextDAO().update(simCtx);
272 * Get the simulationContextDAO.
274 * @return the simulationContextDAO
276 public SimulationContextDAO getSimulationContextDAO() {
277 return _simulationContextDAO;
281 * Set the simulationContextDAO.
283 * @param simulationContextDAO
284 * the simulationContextDAO to set
286 public void setSimulationContextDAO(
287 SimulationContextDAO simulationContextDAO) {
288 _simulationContextDAO = simulationContextDAO;
292 * Get the simulationContextTypeDAO.
294 * @return the simulationContextTypeDAO
296 public SimulationContextTypeDAO getSimulationContextTypeDAO() {
297 return _simulationContextTypeDAO;
301 * Set the simulationContextTypeDAO.
303 * @param simulationContextTypeDAO
304 * the simulationContextTypeDAO to set
306 public void setSimulationContextTypeDAO(
307 SimulationContextTypeDAO simulationContextTypeDAO) {
308 _simulationContextTypeDAO = simulationContextTypeDAO;