]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java
Salome HOME
Creation of a new study is fixed. Database.getSessoin is not used now during creation...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / 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.Publication;
24 import org.splat.dal.bo.som.Scenario;
25 import org.splat.dal.bo.som.SimulationContext;
26 import org.splat.dal.dao.som.Database;
27 import org.splat.dal.dao.som.KnowledgeElementDAO;
28 import org.splat.dal.dao.som.ScenarioDAO;
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 import org.springframework.transaction.annotation.Transactional;
35
36 /**
37  * Scenario service implementation.
38  * 
39  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
40  */
41 public class ScenarioServiceImpl implements ScenarioService {
42
43         /**
44          * Logger for this class.
45          */
46         protected final static Logger logger = Logger
47                         .getLogger(ScenarioServiceImpl.class);
48
49         /**
50          * Injected index service.
51          */
52         private IndexService _indexService;
53         /**
54          * Injected step service.
55          */
56         private StepService _stepService;
57         /**
58          * Injected publication service.
59          */
60         private PublicationService _publicationService;
61         /**
62          * Injected project element service.
63          */
64         private ProjectElementService _projectElementService;
65         /**
66          * Injected knowledge element DAO.
67          */
68         private KnowledgeElementDAO _knowledgeElementDAO;
69         /**
70          * Injected scenario DAO.
71          */
72         private ScenarioDAO _scenarioDAO;
73
74         /**
75          * Get the projectElementService.
76          * 
77          * @return the projectElementService
78          */
79         public ProjectElementService getProjectElementService() {
80                 return _projectElementService;
81         }
82
83         /**
84          * Set the projectElementService.
85          * 
86          * @param projectElementService
87          *            the projectElementService to set
88          */
89         public void setProjectElementService(
90                         ProjectElementService projectElementService) {
91                 _projectElementService = projectElementService;
92         }
93
94         /**
95          * Get the publicationService.
96          * 
97          * @return the publicationService
98          */
99         public PublicationService getPublicationService() {
100                 return _publicationService;
101         }
102
103         /**
104          * Set the publicationService.
105          * 
106          * @param publicationService
107          *            the publicationService to set
108          */
109         public void setPublicationService(PublicationService publicationService) {
110                 _publicationService = publicationService;
111         }
112
113         /**
114          * Get the stepService.
115          * 
116          * @return the stepService
117          */
118         public StepService getStepService() {
119                 return _stepService;
120         }
121
122         /**
123          * Set the stepService.
124          * 
125          * @param stepService
126          *            the stepService to set
127          */
128         public void setStepService(StepService stepService) {
129                 _stepService = stepService;
130         }
131
132         /**
133          * {@inheritDoc}
134          * 
135          * @see org.splat.service.ScenarioService#addKnowledgeElement(org.splat.dal.bo.som.Scenario,
136          *      org.splat.dal.bo.som.KnowledgeElement.Properties)
137          */
138         @Transactional
139         public KnowledgeElement addKnowledgeElement(Scenario aScenario,
140                         KnowledgeElement.Properties kprop) throws MissedPropertyException,
141                         InvalidPropertyException, MultiplyDefinedException {
142                 KnowledgeElement kelm = new KnowledgeElement(kprop
143                                 .setOwnerScenario(aScenario));
144                 try {
145                         getKnowledgeElementDAO().create(kelm);
146                         // RKV: commented because of BatchUpdateException during creation of a new study: session.flush(); //RKV
147                         // Update of my persistent data
148                         aScenario.getKnowledgeElements().add(kelm);
149                         // RKV: commented because of NullPointerException during creation of a new study: session.merge(aScenario); //RKV
150                         // Update of my transient data
151                         List<KnowledgeElement> known = aScenario
152                                         .getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done
153                         known.add(kelm);
154                         if (kelm.getType().equals("usecase")) {
155                                 aScenario.setUcase(kelm);
156                         } else if (aScenario.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
157                                 aScenario.getKnowledgeElementsList().add(kelm);
158                         }
159                         // Update of the index of Knowledge Elements
160                         getIndexService().add(kelm);
161                         update(aScenario);
162                         
163                 } catch (IOException error) {
164                         logger.error("Unable to index the knowedge element '"
165                                         + kelm.getIndex() + "', reason:", error);
166                         kelm = null;
167                 }
168                 
169                 return kelm;
170         }
171
172         /**
173          * Update the scenario in the database.
174          * 
175          * @param aScenario
176          *            the scenario to update
177          * @return true if updating succeeded
178          */
179         @Transactional
180         private boolean update(Scenario aScenario) {
181                 boolean isOk = false;
182                 try {
183                         getScenarioDAO().update(aScenario); // Update of relational base
184                         isOk = true;
185                 } catch (Exception error) {
186                         logger.error("Unable to re-index the knowledge element '"
187                                         + aScenario.getIndex() + "', reason:", error);
188                 }
189                 return isOk;
190         }
191
192         /**
193          * {@inheritDoc}
194          * 
195          * @see org.splat.service.ScenarioService#checkin(org.splat.dal.bo.som.Scenario)
196          */
197         public void checkin(Scenario aScenario) {
198                 aScenario.setUser(null);
199                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
200                 Database.getSession().update(aScenario);
201         }
202
203         /**
204          * @param aScenario
205          * @param user
206          * @return
207          */
208         public boolean checkout(Scenario aScenario, User user) {
209                 if (!aScenario.getOwnerStudy().isStaffedBy(user))
210                         return false;
211
212                 aScenario.setUser(user);
213                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
214                 Database.getSession().update(aScenario);
215                 return true;
216         }
217
218         /**
219          * {@inheritDoc}
220          * 
221          * @see org.splat.service.ScenarioService#copyContentsUpTo(org.splat.dal.bo.som.Scenario, org.splat.som.Step)
222          */
223         public void copyContentsUpTo(Scenario scenario, Step lastep) {
224                 Scenario base = (Scenario) lastep.getOwner();
225                 Step[] from = getProjectElementService().getSteps(base);
226                 Step[] to = getProjectElementService().getSteps(scenario);
227                 for (int i = 0; i < from.length; i++) {
228                         Step step = from[i];
229                         if (step.getNumber() > lastep.getNumber())
230                                 break;
231
232                         List<Publication> docs = step.getAllDocuments();
233                         for (Iterator<Publication> j = docs.iterator(); j.hasNext();) {
234                                 Publication doc = getPublicationService().copy(j.next(),
235                                                 scenario); // Creation of a new reference to the document
236                                 // Database.getSession().save(doc); Publications MUST be saved later through cascading when saving the scenario
237                                 to[i].add(doc);
238                         }
239                         List<SimulationContext> ctex = step.getAllSimulationContexts();
240                         for (Iterator<SimulationContext> j = ctex.iterator(); j.hasNext();) {
241                                 getStepService().addSimulationContext(to[i], j.next());
242                         }
243                 }
244         }
245
246         /**
247          * {@inheritDoc}
248          * 
249          * @see org.splat.service.ScenarioService#isEmpty(org.splat.dal.bo.som.Scenario)
250          */
251         public boolean isEmpty(Scenario scenario) {
252                 Step[] mystep = getProjectElementService().getSteps(scenario);
253                 boolean isEmp = true;
254                 for (int i = 0; i < mystep.length; i++) {
255                         if (mystep[i].isStarted()) {
256                                 isEmp = false;
257                                 break;
258                         }
259                 }
260                 return isEmp;
261         }
262
263         /**
264          * @param scenario
265          * @return
266          */
267         public boolean isFinished(Scenario scenario) {
268                 Step[] mystep = getProjectElementService().getSteps(scenario);
269                 boolean notempty = false; // If this is empty, this is not finished
270                 for (int i = 0; i < mystep.length; i++) {
271                         if (!mystep[i].isStarted())
272                                 continue;
273                         if (!mystep[i].isFinished())
274                                 return false;
275                         notempty = true;
276                 }
277                 return notempty;
278         }
279
280         /**
281          * Get the knowledgeElementDAO.
282          * 
283          * @return the knowledgeElementDAO
284          */
285         public KnowledgeElementDAO getKnowledgeElementDAO() {
286                 return _knowledgeElementDAO;
287         }
288
289         /**
290          * Set the knowledgeElementDAO.
291          * 
292          * @param knowledgeElementDAO
293          *            the knowledgeElementDAO to set
294          */
295         public void setKnowledgeElementDAO(KnowledgeElementDAO knowledgeElementDAO) {
296                 _knowledgeElementDAO = knowledgeElementDAO;
297         }
298
299         /**
300          * Get the indexService.
301          * 
302          * @return the indexService
303          */
304         public IndexService getIndexService() {
305                 return _indexService;
306         }
307
308         /**
309          * Set the indexService.
310          * 
311          * @param indexService
312          *            the indexService to set
313          */
314         public void setIndexService(IndexService indexService) {
315                 _indexService = indexService;
316         }
317
318         /**
319          * Get the scenarioDAO.
320          * 
321          * @return the scenarioDAO
322          */
323         public ScenarioDAO getScenarioDAO() {
324                 return _scenarioDAO;
325         }
326
327         /**
328          * Set the scenarioDAO.
329          * 
330          * @param scenarioDAO
331          *            the scenarioDAO to set
332          */
333         public void setScenarioDAO(ScenarioDAO scenarioDAO) {
334                 _scenarioDAO = scenarioDAO;
335         }
336
337 }