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 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
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);
120 // Update of the index of Knowledge Elements
121 getIndexService().add(kelm);
124 } catch (RuntimeException e) {
125 if (transax != null && transax.isActive()) {
126 // Second try-catch as the rollback could fail as well
129 } catch (HibernateException error) {
130 logger.debug("Error rolling back transaction", error);
132 // Throw again the first exception
136 } catch (IOException error) {
138 "Unable to index the knowedge element '" + kelm.getIndex()
139 + "', reason:", error);
144 private boolean update(Scenario aScenario) {
145 // ---------------------------
147 Database.getSession().update(aScenario); // Update of relational base
149 } catch (Exception error) {
150 logger.error("Unable to re-index the knowledge element '"
151 + aScenario.getIndex() + "', reason:", error);
159 public IndexService getIndexService() {
160 return _indexService;
163 public void setIndexService(IndexService indexService) {
164 _indexService = indexService;
167 public void checkin(Scenario aScenario) {
168 // ----------------------
169 aScenario.setUser(null);
170 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
171 Database.getSession().update(aScenario);
174 public boolean checkout(Scenario aScenario, User user) {
175 // -----------------------------------
176 if (!aScenario.getOwnerStudy().isStaffedBy(user))
179 aScenario.setUser(user);
180 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
181 Database.getSession().update(aScenario);
185 // ==============================================================================================================================
187 // ==============================================================================================================================
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++) {
196 if (step.getNumber() > lastep.getNumber()) break;
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
204 List<SimulationContext> ctex = step.getAllSimulationContexts();
205 for (Iterator<SimulationContext> j=ctex.iterator(); j.hasNext(); ) {
206 getStepService().addSimulationContext(to[i], j.next());
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;
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;