1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.util.Iterator;
13 import java.util.List;
15 import org.hibernate.Session;
16 import org.splat.dal.bo.som.KnowledgeElement;
17 import org.splat.dal.bo.som.Scenario;
18 import org.splat.dal.bo.som.SimulationContext;
19 import org.splat.dal.dao.som.Database;
20 import org.splat.kernel.InvalidPropertyException;
21 import org.splat.kernel.MissedPropertyException;
22 import org.splat.kernel.MultiplyDefinedException;
23 import org.splat.service.technical.IndexService;
24 import org.splat.service.technical.IndexServiceImpl;
25 import org.splat.som.Step;
31 public class StepServiceImpl implements StepService {
33 private IndexService _indexService;
35 public SimulationContext addSimulationContext(Step aStep,
36 SimulationContext.Properties dprop) throws MissedPropertyException,
37 InvalidPropertyException, MultiplyDefinedException,
39 // ----------------------------------------------------------------------------------
40 SimulationContext context = new SimulationContext(dprop.setStep(aStep
42 return addSimulationContext(aStep, context);
45 public SimulationContext addSimulationContext(Step aStep,
46 SimulationContext context) {
47 // -------------------------------------------------------------------------
48 context.hold(); // Increments the reference count of simulation context
49 if (aStep.getOwner().isSaved())
51 Session session = Database.getSession();
52 IndexService lucin = getIndexService();
54 if (!context.isSaved())
55 session.save(context);
56 aStep.getOwner().add(context);
57 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
58 session.update(aStep.getOwner());
59 updateKnowledgeElementsIndex(aStep, lucin);
60 } catch (Exception error) {
63 else { // Happens when copying a scenario
64 aStep.getOwner().add(context);
65 aStep.getContex().add(context); // The context is also referenced from this (transient) Step
66 // In case of owner scenario, the Knowledge Element index will be updated later, when saving the scenario
71 private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
72 // ------------------------------------------------------
74 if (aStep.getOwner() instanceof Scenario) {
75 scenarii = new Scenario[1];
76 scenarii[0] = (Scenario) aStep.getOwner();
78 scenarii = aStep.getOwnerStudy().getScenarii();
81 for (int i = 0; i < scenarii.length; i++) {
82 Scenario scene = scenarii[i];
83 List<KnowledgeElement> knelm = scene.getAllKnowledgeElements();
84 for (Iterator<KnowledgeElement> j = knelm.iterator(); j
86 KnowledgeElement kelm = j.next();
89 scene.updateMyIndex(lucin);
91 } catch (Exception error) {
92 // logger.error("Unable to re-index Knowledge Elements, reason:", error);
96 public boolean removeSimulationContext (Step aStep, SimulationContext context) {
97 // ------------------------------------------------------------------
98 SimulationContext torem = aStep.getSimulationContext(context.getIndex());
99 Session session = Database.getSession();
101 if (torem == null) return false;
102 if (!aStep.getOwner().remove(torem)) return false;
104 aStep.getContex().remove(torem);
105 session.update(aStep.getOwner());
106 if (torem.isShared()) {
108 session.update(torem);
110 session.delete(torem);
118 public IndexService getIndexService() {
119 return _indexService;
122 public void setIndexService(IndexService indexService) {
123 _indexService = indexService;