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.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;
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,
112 Restrictions.eq("value", 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,
122 Restrictions.eq("state", 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 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"));
173 * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
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())
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()));
190 return getSimulationContextTypeDAO().getFilteredList(aCondition,
197 * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
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";
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)
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";
221 // order = ""; // Approved types are localized
224 // query = query.append(order);
225 Criterion aCondition = null;
226 Order anOrder = Order.asc("step");
228 aCondition = Restrictions.eq("step", step.getNumber());
229 anOrder = Order.desc("state"); // APPROVED (upper case A) is grater than inCHECK (lower case i)
232 aCondition = Restrictions.and(aCondition,
233 Restrictions.eq("state", state));
235 if (state != ProgressState.APPROVED) {
236 anOrder = Order.asc("name");
238 anOrder = null; // Approved types are localized
242 return getSimulationContextTypeDAO().getFilteredList(aCondition,
249 * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
251 @Transactional(readOnly = true)
252 public SimulationContextType selectType(String name) {
253 return getSimulationContextTypeDAO().findByCriteria(
254 Restrictions.eq("name", name));
260 * @see org.splat.service.SimulationContextService#selectType(long)
262 public SimulationContextType selectType(long index) {
263 return getSimulationContextTypeDAO().get(index);
269 * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
271 public void hold(SimulationContext simCtx) {
272 simCtx.setCounter(simCtx.getCounter() + 1);
273 if (simCtx.isSaved()) {
274 getSimulationContextDAO().update(simCtx);
281 * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
283 public void release(SimulationContext simCtx) {
284 simCtx.setCounter(simCtx.getCounter() - 1);
285 if (simCtx.isSaved()) {
286 getSimulationContextDAO().update(simCtx);
291 * Approve the simulation context.
294 * the context to approve
295 * @return true if approval succeeded
297 public boolean approve(SimulationContext simCtx) {
298 if (simCtx.getProgressState() != ProgressState.inCHECK)
300 simCtx.setProgressState(ProgressState.APPROVED); // The type name is supposed being localized
301 if (simCtx.isSaved()) {
302 getSimulationContextDAO().update(simCtx);
308 * Approve the simulation context type.
311 * the type to approve
312 * @return true if approval succeeded
314 public boolean approve(SimulationContextType simCtxType) {
315 if (simCtxType.getState() != ProgressState.inCHECK)
317 simCtxType.setState(ProgressState.APPROVED); // The type name is supposed being localized
318 getSimulationContextTypeDAO().update(simCtxType);
326 public ProjectSettingsService.Step getAttachedStep(
327 SimulationContextType simCtxType) {
328 return ProjectSettingsServiceImpl.getStep(simCtxType.getStep());
332 * Get the simulationContextDAO.
334 * @return the simulationContextDAO
336 public SimulationContextDAO getSimulationContextDAO() {
337 return _simulationContextDAO;
341 * Set the simulationContextDAO.
343 * @param simulationContextDAO
344 * the simulationContextDAO to set
346 public void setSimulationContextDAO(
347 SimulationContextDAO simulationContextDAO) {
348 _simulationContextDAO = simulationContextDAO;
352 * Get the simulationContextTypeDAO.
354 * @return the simulationContextTypeDAO
356 public SimulationContextTypeDAO getSimulationContextTypeDAO() {
357 return _simulationContextTypeDAO;
361 * Set the simulationContextTypeDAO.
363 * @param simulationContextTypeDAO
364 * the simulationContextTypeDAO to set
366 public void setSimulationContextTypeDAO(
367 SimulationContextTypeDAO simulationContextTypeDAO) {
368 _simulationContextTypeDAO = simulationContextTypeDAO;