Salome HOME
Wrong logger creation is fixed.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / StepServiceImpl.java
1 /*****************************************************************************
2  * Company         EURIWARE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   06.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.util.Iterator;
13 import java.util.List;
14
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;
26
27 /**
28  * @author RKV
29  * 
30  */
31 public class StepServiceImpl implements StepService {
32
33         private IndexService _indexService;
34
35         public SimulationContext addSimulationContext(Step aStep,
36                         SimulationContext.Properties dprop) throws MissedPropertyException,
37                         InvalidPropertyException, MultiplyDefinedException,
38                         RuntimeException {
39                 // ----------------------------------------------------------------------------------
40                 SimulationContext context = new SimulationContext(dprop.setStep(aStep
41                                 .getStep()));
42                 return addSimulationContext(aStep, context);
43         }
44
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())
50                         try {
51                                 Session session = Database.getSession();
52                                 IndexService lucin = getIndexService();
53
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) {
61                                 return null;
62                         }
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
67                 }
68                 return context;
69         }
70
71         private void updateKnowledgeElementsIndex(Step aStep, IndexService lucin) {
72                 // ------------------------------------------------------
73                 Scenario[] scenarii;
74                 if (aStep.getOwner() instanceof Scenario) {
75                         scenarii = new Scenario[1];
76                         scenarii[0] = (Scenario) aStep.getOwner();
77                 } else {
78                         scenarii = aStep.getOwnerStudy().getScenarii();
79                 }
80                 try {
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
85                                                 .hasNext();) {
86                                         KnowledgeElement kelm = j.next();
87                                         lucin.update(kelm);
88                                 }
89                                 scene.updateMyIndex(lucin);
90                         }
91                 } catch (Exception error) {
92                         // logger.error("Unable to re-index Knowledge Elements, reason:", error);
93                 }
94         }
95
96     public boolean removeSimulationContext (Step aStep, SimulationContext context) {
97 //  ------------------------------------------------------------------
98       SimulationContext  torem   = aStep.getSimulationContext(context.getIndex());
99       Session            session = Database.getSession();
100
101       if (torem == null)        return false;
102       if (!aStep.getOwner().remove(torem)) return false;
103
104       aStep.getContex().remove(torem);
105       session.update(aStep.getOwner());
106       if (torem.isShared()) {
107         torem.release();
108         session.update(torem);
109       } else {
110         session.delete(torem);
111       }
112       return true;
113     }
114
115         /**
116          * @return
117          */
118         public IndexService getIndexService() {
119                 return _indexService;
120         }
121
122         public void setIndexService(IndexService indexService) {
123                 _indexService = indexService;
124         }
125 }