Salome HOME
0b6ba1280823adf33519e446d193b6060c5bd65f
[tools/siman.git] / ScenarioServiceImpl.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.io.IOException;
13 import java.util.Calendar;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.hibernate.HibernateException;
19 import org.hibernate.Session;
20 import org.hibernate.Transaction;
21 import org.splat.dal.bo.kernel.User;
22 import org.splat.dal.bo.som.KnowledgeElement;
23 import org.splat.dal.bo.som.ProjectElement;
24 import org.splat.dal.bo.som.Publication;
25 import org.splat.dal.bo.som.Scenario;
26 import org.splat.dal.bo.som.SimulationContext;
27 import org.splat.dal.bo.som.Study;
28 import org.splat.dal.dao.som.Database;
29 import org.splat.kernel.InvalidPropertyException;
30 import org.splat.kernel.MissedPropertyException;
31 import org.splat.kernel.MultiplyDefinedException;
32 import org.splat.service.technical.IndexService;
33 import org.splat.som.Step;
34
35 /**
36  * @author RKV
37  * 
38  */
39 public class ScenarioServiceImpl implements ScenarioService {
40
41         protected final static Logger logger = Logger
42                         .getLogger(ScenarioServiceImpl.class);
43
44         private IndexService _indexService;
45         private StepService _stepService;
46         private PublicationService _publicationService;
47         private ProjectElementService _projectElementService;
48
49         /**
50          * Get the projectElementService.
51          * @return the projectElementService
52          */
53         public ProjectElementService getProjectElementService() {
54                 return _projectElementService;
55         }
56
57         /**
58          * Set the projectElementService.
59          * @param projectElementService the projectElementService to set
60          */
61         public void setProjectElementService(ProjectElementService projectElementService) {
62                 _projectElementService = projectElementService;
63         }
64
65         /**
66          * Get the publicationService.
67          * @return the publicationService
68          */
69         public PublicationService getPublicationService() {
70                 return _publicationService;
71         }
72
73         /**
74          * Set the publicationService.
75          * @param publicationService the publicationService to set
76          */
77         public void setPublicationService(PublicationService publicationService) {
78                 _publicationService = publicationService;
79         }
80
81         /**
82          * Get the stepService.
83          * @return the stepService
84          */
85         public StepService getStepService() {
86                 return _stepService;
87         }
88
89         /**
90          * Set the stepService.
91          * @param stepService the stepService to set
92          */
93         public void setStepService(StepService stepService) {
94                 _stepService = stepService;
95         }
96
97         public KnowledgeElement addKnowledgeElement(Scenario aScenario,
98                         KnowledgeElement.Properties kprop) throws MissedPropertyException,
99                         InvalidPropertyException, MultiplyDefinedException {
100                 // -------------------------------------------------------------------------------
101                 KnowledgeElement kelm = new KnowledgeElement(
102                                 kprop.setOwnerScenario(aScenario));
103                 Session session = Database.getSession();
104                 Transaction transax = session.getTransaction();
105                 try {
106                         session.save(kelm);
107                         session.flush(); //RKV
108                         // Update of my persistent data
109                         aScenario.getKnowledgeElements().add(kelm);
110                         session.merge(aScenario); //RKV
111                         // Update of my transient data
112                         List<KnowledgeElement> known = aScenario
113                                         .getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done
114                         known.add(kelm);
115                         if (kelm.getType().equals("usecase")) {
116                                 aScenario.setUcase(kelm);
117                         } else if (aScenario.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
118                                 aScenario.getKnowledgeElementsList().add(kelm);
119                         }
120                         // Update of the index of Knowledge Elements
121                         getIndexService().add(kelm);
122                         update(aScenario);
123                         return kelm;
124                 } catch (RuntimeException e) {
125                         if (transax != null && transax.isActive()) {
126                                 // Second try-catch as the rollback could fail as well
127                                 try {
128                                         transax.rollback();
129                                 } catch (HibernateException error) {
130                                         logger.debug("Error rolling back transaction", error);
131                                 }
132                                 // Throw again the first exception
133                                 throw e;
134                         }
135                         return null;
136                 } catch (IOException error) {
137                         logger.error(
138                                         "Unable to index the knowedge element '" + kelm.getIndex()
139                                                         + "', reason:", error);
140                         return null;
141                 }
142         }
143
144         private boolean update(Scenario aScenario) {
145                 // ---------------------------
146                 try {
147                         Database.getSession().update(aScenario); // Update of relational base
148                         return true;
149                 } catch (Exception error) {
150                         logger.error("Unable to re-index the knowledge element '"
151                                         + aScenario.getIndex() + "', reason:", error);
152                         return false;
153                 }
154         }
155
156         /**
157          * @return
158          */
159         public IndexService getIndexService() {
160                 return _indexService;
161         }
162
163         public void setIndexService(IndexService indexService) {
164                 _indexService = indexService;
165         }
166
167         public void checkin(Scenario aScenario) {
168                 // ----------------------
169                 aScenario.setUser(null);
170                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
171                 Database.getSession().update(aScenario);
172         }
173
174         public boolean checkout(Scenario aScenario, User user) {
175                 // -----------------------------------
176                 if (!aScenario.getOwnerStudy().isStaffedBy(user))
177                         return false;
178
179                 aScenario.setUser(user);
180                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
181                 Database.getSession().update(aScenario);
182                 return true;
183         }
184
185         //  ==============================================================================================================================
186         //  Private services
187         //  ==============================================================================================================================
188         
189             public void copyContentsUpTo (Scenario scenario, Step lastep) {
190         //  -------------------------------------------
191               Scenario base = (Scenario)lastep.getOwner();
192               Step[]   from = getProjectElementService().getSteps(base);
193               Step[]   to   = getProjectElementService().getSteps(scenario);
194               for (int i=0; i<from.length; i++) {
195                 Step step = from[i];
196                 if (step.getNumber() > lastep.getNumber()) break;
197         
198                 List<Publication> docs = step.getAllDocuments();
199                 for (Iterator<Publication> j=docs.iterator(); j.hasNext(); ) {
200                   Publication doc = getPublicationService().copy(j.next(), scenario);   // Creation of a new reference to the document
201         //        Database.getSession().save(doc);            Publications MUST be saved later through cascading when saving the scenario
202                   to[i].add(doc);
203                 }
204                 List<SimulationContext> ctex = step.getAllSimulationContexts();
205                 for (Iterator<SimulationContext> j=ctex.iterator(); j.hasNext(); ) {
206                   getStepService().addSimulationContext(to[i], j.next());
207                 }
208               }
209             }
210
211         public boolean isEmpty (Scenario scenario) {
212         //  -------------------------
213               Step[] mystep = getProjectElementService().getSteps(scenario);
214               for (int i=0; i<mystep.length; i++) if (mystep[i].isStarted()) return false;
215               return true;
216             }
217
218         public boolean isFinished (Scenario scenario) {
219         //  ----------------------------
220               Step[]  mystep   = getProjectElementService().getSteps(scenario);
221               boolean notempty = false;   // If this is empty, this is not finished
222               for (int i=0; i<mystep.length; i++) {
223                 if (!mystep[i].isStarted())  continue;
224                 if (!mystep[i].isFinished()) return false;
225                 notempty = true;
226               }
227               return notempty;
228             }
229
230 }