1 /*****************************************************************************
5 * Creation date 16.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Vector;
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;
31 * Simulation context service implementation.
36 public class SimulationContextServiceImpl implements SimulationContextService {
39 * logger for the service.
41 public final static AppLogger LOG = AppLogger
42 .getLogger(SimulationContextServiceImpl.class);
47 public final static String STEP_PROP = "step";
50 * Injected simulation context DAO.
52 private SimulationContextDAO _simulationContextDAO;
55 * Injected simulation context type DAO.
57 private SimulationContextTypeDAO _simulationContextTypeDAO;
59 * Injected project settings service.
61 private ProjectSettingsService _projectSettings;
64 * Get simulation contexts which are currently in the given state.
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
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
79 Vector<SimulationContextFacade> result = new Vector<SimulationContextFacade>(
81 for (Iterator<SimulationContext> i = context.iterator(); i.hasNext();) {
82 result.add(new SimulationContextFacade(i.next(),
83 getProjectSettings().getAllSteps()));
91 * @see org.splat.service.SimulationContextService#selectSimulationContext(int)
93 public SimulationContext selectSimulationContext(final long index) {
94 return getSimulationContextDAO().get(index);
100 * @see org.splat.service.SimulationContextService#selectSimulationContext(org.splat.dal.bo.som.SimulationContextType, java.lang.String)
102 public SimulationContext selectSimulationContext(
103 final SimulationContextType celt, final String value) {
104 SimulationContext result = null;
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
112 } catch (InvalidPropertyException error) {
113 LOG.info("Attempt to select a simulation context \""
114 + celt.getName() + "\" with an invalid value.");
122 * @see org.splat.service.SimulationContextService#selectSimulationContextsWhere(org.splat.dal.bo.som.SimulationContext.Properties)
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;
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");
145 // query = query.append(separator).append(" value='").append(value)
147 // separator = " and";
149 aCondition = Restrictions.and(aCondition, Restrictions.eq("value",
153 // query = query.append(separator).append(" state='").append(state)
156 // order = " order by type asc";
157 anOrder = Order.asc("type");
159 aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
162 // query.append(order);
163 return getSimulationContextDAO().getFilteredList(aCondition, anOrder);
169 * @see org.splat.service.SimulationContextService#getSimulationContextList()
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
183 * @see org.splat.service.SimulationContextService#selectAllTypes()
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));
194 * @see org.splat.service.SimulationContextService#selectTypesOf(org.splat.service.technical.ProjectSettingsService.Step[])
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())
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()));
211 return getSimulationContextTypeDAO().getFilteredList(aCondition,
212 Order.asc(STEP_PROP));
218 * @see org.splat.service.SimulationContextService#selectTypesWhere(org.splat.dal.bo.som.SimulationContextType.Properties)
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";
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)
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";
242 // order = ""; // Approved types are localized
245 // query = query.append(order);
246 Criterion aCondition = null;
247 Order anOrder = Order.asc(STEP_PROP);
249 aCondition = Restrictions.eq(STEP_PROP, step.getNumber());
250 anOrder = Order.desc("state"); // APPROVED (upper case A) is grater than inCHECK (lower case i)
253 aCondition = Restrictions.and(aCondition, Restrictions.eq("state",
256 if (state == ProgressState.APPROVED) {
257 anOrder = null; // Approved types are localized
259 anOrder = Order.asc("name");
263 return getSimulationContextTypeDAO().getFilteredList(aCondition,
270 * @see org.splat.service.SimulationContextService#selectType(java.lang.String)
272 @Transactional(readOnly = true)
273 public SimulationContextType selectType(final String name) {
274 return getSimulationContextTypeDAO().findByCriteria(
275 Restrictions.eq("name", name));
281 * @see org.splat.service.SimulationContextService#selectType(long)
283 public SimulationContextType selectType(final long index) {
284 return getSimulationContextTypeDAO().get(index);
290 * @see org.splat.service.SimulationContextService#hold(org.splat.dal.bo.som.SimulationContext)
292 public void hold(final SimulationContext simCtx) {
293 simCtx.setCounter(simCtx.getCounter() + 1);
294 if (simCtx.isSaved()) {
295 getSimulationContextDAO().update(simCtx);
302 * @see org.splat.service.SimulationContextService#release(org.splat.dal.bo.som.SimulationContext)
304 public void release(final SimulationContext simCtx) {
305 simCtx.setCounter(simCtx.getCounter() - 1);
306 if (simCtx.isSaved()) {
307 getSimulationContextDAO().update(simCtx);
312 * Approve the simulation context.
315 * the context to approve
316 * @return true if approval succeeded
318 public boolean approve(final SimulationContext simCtx) {
319 boolean res = (simCtx.getProgressState() == ProgressState.inCHECK);
321 simCtx.setProgressState(ProgressState.APPROVED); // The type name is supposed being localized
322 if (simCtx.isSaved()) {
323 getSimulationContextDAO().update(simCtx);
333 public ProjectSettingsService.Step getAttachedStep(
334 final SimulationContextType simCtxType) {
335 return getProjectSettings().getStep(simCtxType.getStep());
339 * Get the simulationContextDAO.
341 * @return the simulationContextDAO
343 public SimulationContextDAO getSimulationContextDAO() {
344 return _simulationContextDAO;
348 * Set the simulationContextDAO.
350 * @param simulationContextDAO
351 * the simulationContextDAO to set
353 public void setSimulationContextDAO(
354 final SimulationContextDAO simulationContextDAO) {
355 _simulationContextDAO = simulationContextDAO;
359 * Get the simulationContextTypeDAO.
361 * @return the simulationContextTypeDAO
363 public SimulationContextTypeDAO getSimulationContextTypeDAO() {
364 return _simulationContextTypeDAO;
368 * Set the simulationContextTypeDAO.
370 * @param simulationContextTypeDAO
371 * the simulationContextTypeDAO to set
373 public void setSimulationContextTypeDAO(
374 final SimulationContextTypeDAO simulationContextTypeDAO) {
375 _simulationContextTypeDAO = simulationContextTypeDAO;
379 * Get project settings.
381 * @return Project settings service
383 private ProjectSettingsService getProjectSettings() {
384 return _projectSettings;
388 * Set project settings service.
390 * @param projectSettingsService
391 * project settings service
393 public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
394 _projectSettings = projectSettingsService;