Salome HOME
Beans initialization is fixed. Document can be added to study now.
[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.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                         // Update of my persistent data
108                         aScenario.getKnowledgeElements().add(kelm);
109                         // Update of my transient data
110                         List<KnowledgeElement> known = aScenario
111                                         .getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done
112                         known.add(kelm);
113                         if (kelm.getType().equals("usecase")) {
114                                 aScenario.setUcase(kelm);
115                         } else if (aScenario.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
116                                 aScenario.getKnowledgeElementsList().add(kelm);
117                         }
118                         // Update of the index of Knowledge Elements
119                         getIndexService().add(kelm);
120                         update(aScenario);
121                         return kelm;
122                 } catch (RuntimeException e) {
123                         if (transax != null && transax.isActive()) {
124                                 // Second try-catch as the rollback could fail as well
125                                 try {
126                                         transax.rollback();
127                                 } catch (HibernateException error) {
128                                         logger.debug("Error rolling back transaction", error);
129                                 }
130                                 // Throw again the first exception
131                                 throw e;
132                         }
133                         return null;
134                 } catch (IOException error) {
135                         logger.error(
136                                         "Unable to index the knowedge element '" + kelm.getIndex()
137                                                         + "', reason:", error);
138                         return null;
139                 }
140         }
141
142         private boolean update(Scenario aScenario) {
143                 // ---------------------------
144                 try {
145                         Database.getSession().update(aScenario); // Update of relational base
146                         return true;
147                 } catch (Exception error) {
148                         logger.error("Unable to re-index the knowledge element '"
149                                         + aScenario.getIndex() + "', reason:", error);
150                         return false;
151                 }
152         }
153
154         /**
155          * @return
156          */
157         public IndexService getIndexService() {
158                 return _indexService;
159         }
160
161         public void setIndexService(IndexService indexService) {
162                 _indexService = indexService;
163         }
164
165         public void checkin(Scenario aScenario) {
166                 // ----------------------
167                 aScenario.setUser(null);
168                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
169                 Database.getSession().update(aScenario);
170         }
171
172         public boolean checkout(Scenario aScenario, User user) {
173                 // -----------------------------------
174                 if (!aScenario.getOwnerStudy().isStaffedBy(user))
175                         return false;
176
177                 aScenario.setUser(user);
178                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
179                 Database.getSession().update(aScenario);
180                 return true;
181         }
182
183         //  ==============================================================================================================================
184         //  Private services
185         //  ==============================================================================================================================
186         
187             public void copyContentsUpTo (Scenario scenario, Step lastep) {
188         //  -------------------------------------------
189               Scenario base = (Scenario)lastep.getOwner();
190               Step[]   from = getProjectElementService().getSteps(base);
191               Step[]   to   = getProjectElementService().getSteps(scenario);
192               for (int i=0; i<from.length; i++) {
193                 Step step = from[i];
194                 if (step.getNumber() > lastep.getNumber()) break;
195         
196                 List<Publication> docs = step.getAllDocuments();
197                 for (Iterator<Publication> j=docs.iterator(); j.hasNext(); ) {
198                   Publication doc = getPublicationService().copy(j.next(), scenario);   // Creation of a new reference to the document
199         //        Database.getSession().save(doc);            Publications MUST be saved later through cascading when saving the scenario
200                   to[i].add(doc);
201                 }
202                 List<SimulationContext> ctex = step.getAllSimulationContexts();
203                 for (Iterator<SimulationContext> j=ctex.iterator(); j.hasNext(); ) {
204                   getStepService().addSimulationContext(to[i], j.next());
205                 }
206               }
207             }
208
209         public boolean isEmpty (Scenario scenario) {
210         //  -------------------------
211               Step[] mystep = getProjectElementService().getSteps(scenario);
212               for (int i=0; i<mystep.length; i++) if (mystep[i].isStarted()) return false;
213               return true;
214             }
215
216         public boolean isFinished (Scenario scenario) {
217         //  ----------------------------
218               Step[]  mystep   = getProjectElementService().getSteps(scenario);
219               boolean notempty = false;   // If this is empty, this is not finished
220               for (int i=0; i<mystep.length; i++) {
221                 if (!mystep[i].isStarted())  continue;
222                 if (!mystep[i].isFinished()) return false;
223                 notempty = true;
224               }
225               return notempty;
226             }
227
228 }