1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
12 import java.io.IOException;
13 import java.util.Calendar;
14 import java.util.Iterator;
15 import java.util.List;
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;
39 public class ScenarioServiceImpl implements ScenarioService {
41 protected final static Logger logger = Logger
42 .getLogger(ScenarioServiceImpl.class);
44 private IndexService _indexService;
45 private StepService _stepService;
46 private PublicationService _publicationService;
47 private ProjectElementService _projectElementService;
50 * Get the projectElementService.
51 * @return the projectElementService
53 public ProjectElementService getProjectElementService() {
54 return _projectElementService;
58 * Set the projectElementService.
59 * @param projectElementService the projectElementService to set
61 public void setProjectElementService(ProjectElementService projectElementService) {
62 _projectElementService = projectElementService;
66 * Get the publicationService.
67 * @return the publicationService
69 public PublicationService getPublicationService() {
70 return _publicationService;
74 * Set the publicationService.
75 * @param publicationService the publicationService to set
77 public void setPublicationService(PublicationService publicationService) {
78 _publicationService = publicationService;
82 * Get the stepService.
83 * @return the stepService
85 public StepService getStepService() {
90 * Set the stepService.
91 * @param stepService the stepService to set
93 public void setStepService(StepService stepService) {
94 _stepService = stepService;
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();
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
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);
118 // Update of the index of Knowledge Elements
119 getIndexService().add(kelm);
122 } catch (RuntimeException e) {
123 if (transax != null && transax.isActive()) {
124 // Second try-catch as the rollback could fail as well
127 } catch (HibernateException error) {
128 logger.debug("Error rolling back transaction", error);
130 // Throw again the first exception
134 } catch (IOException error) {
136 "Unable to index the knowedge element '" + kelm.getIndex()
137 + "', reason:", error);
142 private boolean update(Scenario aScenario) {
143 // ---------------------------
145 Database.getSession().update(aScenario); // Update of relational base
147 } catch (Exception error) {
148 logger.error("Unable to re-index the knowledge element '"
149 + aScenario.getIndex() + "', reason:", error);
157 public IndexService getIndexService() {
158 return _indexService;
161 public void setIndexService(IndexService indexService) {
162 _indexService = indexService;
165 public void checkin(Scenario aScenario) {
166 // ----------------------
167 aScenario.setUser(null);
168 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
169 Database.getSession().update(aScenario);
172 public boolean checkout(Scenario aScenario, User user) {
173 // -----------------------------------
174 if (!aScenario.getOwnerStudy().isStaffedBy(user))
177 aScenario.setUser(user);
178 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
179 Database.getSession().update(aScenario);
183 // ==============================================================================================================================
185 // ==============================================================================================================================
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++) {
194 if (step.getNumber() > lastep.getNumber()) break;
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
202 List<SimulationContext> ctex = step.getAllSimulationContexts();
203 for (Iterator<SimulationContext> j=ctex.iterator(); j.hasNext(); ) {
204 getStepService().addSimulationContext(to[i], j.next());
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;
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;