]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman-Common/src/org/splat/service/ScenarioServiceImpl.java
Salome HOME
The draft of the "Copy from existing study" action is added. The YACS step is introdu...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / ScenarioServiceImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
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.ArrayList;
14 import java.util.Calendar;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.hibernate.criterion.DetachedCriteria;
23 import org.hibernate.criterion.Order;
24 import org.hibernate.criterion.Projections;
25 import org.hibernate.criterion.Restrictions;
26 import org.hibernate.transform.Transformers;
27 import org.splat.common.properties.MessageKeyEnum;
28 import org.splat.dal.bo.kernel.Relation;
29 import org.splat.dal.bo.kernel.Role;
30 import org.splat.dal.bo.kernel.User;
31 import org.splat.dal.bo.som.ConvertsRelation;
32 import org.splat.dal.bo.som.Document;
33 import org.splat.dal.bo.som.DocumentType;
34 import org.splat.dal.bo.som.File;
35 import org.splat.dal.bo.som.KnowledgeElement;
36 import org.splat.dal.bo.som.KnowledgeElementType;
37 import org.splat.dal.bo.som.ProgressState;
38 import org.splat.dal.bo.som.ProjectElement;
39 import org.splat.dal.bo.som.Publication;
40 import org.splat.dal.bo.som.Scenario;
41 import org.splat.dal.bo.som.SimulationContext;
42 import org.splat.dal.bo.som.SimulationContextType;
43 import org.splat.dal.bo.som.Study;
44 import org.splat.dal.bo.som.UsedByRelation;
45 import org.splat.dal.bo.som.UsesRelation;
46 import org.splat.dal.bo.som.ValidationCycle;
47 import org.splat.dal.bo.som.Document.Properties;
48 import org.splat.dal.dao.kernel.RoleDAO;
49 import org.splat.dal.dao.kernel.UserDAO;
50 import org.splat.dal.dao.som.KnowledgeElementDAO;
51 import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
52 import org.splat.dal.dao.som.ScenarioDAO;
53 import org.splat.dal.dao.som.StudyDAO;
54 import org.splat.dal.dao.som.ValidationCycleDAO;
55 import org.splat.exception.InvalidParameterException;
56 import org.splat.i18n.I18nUtils;
57 import org.splat.kernel.InvalidPropertyException;
58 import org.splat.kernel.MismatchException;
59 import org.splat.kernel.MissedPropertyException;
60 import org.splat.kernel.MultiplyDefinedException;
61 import org.splat.kernel.NotApplicableException;
62 import org.splat.log.AppLogger;
63 import org.splat.service.dto.DocumentDTO;
64 import org.splat.service.dto.FileDTO;
65 import org.splat.service.dto.ScenarioDTO;
66 import org.splat.service.dto.StepDTO;
67 import org.splat.service.technical.IndexService;
68 import org.splat.service.technical.ProjectSettingsService;
69 import org.splat.service.technical.StepsConfigService;
70 import org.splat.som.Step;
71 import org.splat.util.BeanHelper;
72 import org.splat.util.IOUtils;
73 import org.springframework.transaction.annotation.Transactional;
74
75 /**
76  * Scenario service implementation.
77  * 
78  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
79  */
80 public class ScenarioServiceImpl implements ScenarioService {
81
82         /**
83          * The logger for the service.
84          */
85         public final static AppLogger LOG = AppLogger
86                         .getLogger(ScenarioServiceImpl.class);
87
88         /**
89          * " to " literal.
90          */
91         private static final String TO = " to ";
92         /**
93          * Injected index service.
94          */
95         private IndexService _indexService;
96         /**
97          * Injected step service.
98          */
99         private StepService _stepService;
100         /**
101          * Injected study service.
102          */
103         private StudyService _studyService;
104         /**
105          * Injected publication service.
106          */
107         private PublicationService _publicationService;
108         /**
109          * Injected project element service.
110          */
111         private ProjectElementService _projectElementService;
112         /**
113          * Injected knowledge element DAO.
114          */
115         private KnowledgeElementDAO _knowledgeElementDAO;
116         /**
117          * Injected scenario DAO.
118          */
119         private ScenarioDAO _scenarioDAO;
120
121         /**
122          * Injected study DAO.
123          */
124         private StudyDAO _studyDAO;
125
126         /**
127          * Injected knowledge element service.
128          */
129         private KnowledgeElementTypeService _knowledgeElementTypeService;
130
131         /**
132          * Injected user service.
133          */
134         private UserService _userService;
135
136         /**
137          * Injected user DAO.
138          */
139         private UserDAO _userDAO;
140
141         /**
142          * Injected role DAO.
143          */
144         private RoleDAO _roleDAO;
145
146         /**
147          * Injected knowledge element type DAO.
148          */
149         private KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
150
151         /**
152          * Injected simulation context service.
153          */
154         private SimulationContextService _simulationContextService;
155
156         /**
157          * Injected simulation context type service.
158          */
159         private SimulationContextTypeService _simulationContextTypeService;
160
161         /**
162          * Injected project service.
163          */
164         private ProjectSettingsService _projectSettings;
165
166         /**
167          * Injected document type service.
168          */
169         private DocumentTypeService _documentTypeService;
170
171         /**
172          * Injected validation cycle DAO.
173          */
174         private ValidationCycleDAO _validationCycleDAO;
175
176         /**
177          * Injected project settings service.
178          */
179         private StepsConfigService _stepsConfigService;
180
181         /**
182          * Get the projectElementService.
183          * 
184          * @return the projectElementService
185          */
186         public ProjectElementService getProjectElementService() {
187                 return _projectElementService;
188         }
189
190         /**
191          * Set the projectElementService.
192          * 
193          * @param projectElementService
194          *            the projectElementService to set
195          */
196         public void setProjectElementService(
197                         final ProjectElementService projectElementService) {
198                 _projectElementService = projectElementService;
199         }
200
201         /**
202          * Get the publicationService.
203          * 
204          * @return the publicationService
205          */
206         public PublicationService getPublicationService() {
207                 return _publicationService;
208         }
209
210         /**
211          * Set the publicationService.
212          * 
213          * @param publicationService
214          *            the publicationService to set
215          */
216         public void setPublicationService(
217                         final PublicationService publicationService) {
218                 _publicationService = publicationService;
219         }
220
221         /**
222          * Get the stepService.
223          * 
224          * @return the stepService
225          */
226         public StepService getStepService() {
227                 return _stepService;
228         }
229
230         /**
231          * Set the stepService.
232          * 
233          * @param stepService
234          *            the stepService to set
235          */
236         public void setStepService(final StepService stepService) {
237                 _stepService = stepService;
238         }
239
240         /**
241          * {@inheritDoc}
242          * 
243          * @see org.splat.service.ScenarioService#getStudyScenarios(java.lang.Long)
244          */
245         @Override
246         @Transactional(readOnly = true)
247         public List<ScenarioDTO> getStudyScenarios(final Long studyId) {
248                 DetachedCriteria query = DetachedCriteria
249                                 .forClass(Scenario.class, "scen")
250                                 .add(Restrictions.eq("owner.rid", studyId))
251                                 .setProjection(
252                                                 Projections.projectionList().add(
253                                                                 Projections.property("scen.title"), "title")
254                                                                 .add(Projections.property("scen.rid"), "index"))
255                                 .setResultTransformer(
256                                                 Transformers.aliasToBean(ScenarioDTO.class));
257                 return getScenarioDAO().getFilteredDTOList(query);
258         }
259
260         /**
261          * {@inheritDoc}
262          * 
263          * @see org.splat.service.ScenarioService#copyStudyContent(long, long, int, long)
264          */
265         @Override
266         @Transactional
267         public void copyStudyContent(final long fromStudyId, final long fromScenId,
268                         final int finalStepNum, final long toStudyId)
269                         throws InvalidParameterException, MissedPropertyException,
270                         InvalidPropertyException, MultiplyDefinedException,
271                         NotApplicableException, IOException {
272                 Study fromStudy = getStudyService().selectStudy(fromStudyId);
273                 if (fromStudy == null) {
274                         throw new InvalidParameterException(MessageKeyEnum.STD_000002
275                                         .toString(), String.valueOf(fromStudyId));
276                 }
277                 Scenario fromScen = null;
278                 for (Scenario scen : fromStudy.getScenariiList()) {
279                         if (scen.getIndex() == fromScenId) {
280                                 fromScen = scen;
281                         }
282                 }
283
284                 Study toStudy = getStudyService().selectStudy(toStudyId);
285                 if (toStudy == null) {
286                         throw new InvalidParameterException(MessageKeyEnum.STD_000002
287                                         .toString(), String.valueOf(toStudy));
288                 }
289
290                 // Check if the step is applied to a scenario and scenario is defined
291                 if (fromScen == null
292                                 && getStepsConfigService().stepInvolves(finalStepNum,
293                                                 Scenario.class)) {
294                         throw new InvalidParameterException(MessageKeyEnum.SCN_000006
295                                         .toString(), String.valueOf(fromScenId));
296                 }
297
298                 // Copy validation cycles
299                 for (ValidationCycle fromCycle : fromStudy.getValidationCycles()
300                                 .values()) {
301                         ValidationCycle cycle = new ValidationCycle(toStudy, fromCycle);
302                         getValidationCycleDAO().create(cycle);
303                         toStudy.addRelation(cycle.getContext());
304                         toStudy.getValidationCycles().put(
305                                         cycle.getDocumentType().getName(), cycle); // Replaces the cycle if exists as default,
306                 }
307
308                 // Copy content of the study up to the given step
309                 Map<Publication, Publication> oldToNewPub = new HashMap<Publication, Publication>();
310                 copyDocs(fromStudy, toStudy, finalStepNum, oldToNewPub);
311                 if (fromScen != null) {
312                         copyDocs(fromScen, toStudy.getScenariiList().get(0), finalStepNum,
313                                         oldToNewPub);
314                 }
315                 copyDependencies(fromStudy, finalStepNum, oldToNewPub);
316                 if (fromScen != null) {
317                         copyDependencies(fromScen, finalStepNum, oldToNewPub);
318                 }
319         }
320
321         /**
322          * Copy dependencies between documents from the given project element up to <BR>
323          * the given step according to the given map of old publications to new publications.
324          * 
325          * @param from
326          *            the source project element
327          * @param finalStepNum
328          *            the final step for copy processing
329          * @param oldToNewPub
330          *            the old to new publications map
331          */
332         private void copyDependencies(final ProjectElement from,
333                         final int finalStepNum,
334                         final Map<Publication, Publication> oldToNewPub) {
335                 // Copy dependencies between copied documents
336                 for (Publication pub : from.getDocums()) {
337                         // If the document in the step before the final one
338                         if (pub.value().getStep() <= finalStepNum) {
339                                 Publication newPub = oldToNewPub.get(pub);
340                                 for (Publication used : pub.getRelations(UsesRelation.class)) {
341                                         newPub.addDependency(oldToNewPub.get(used));
342                                 }
343                         }
344                 }
345         }
346
347         /**
348          * Copy documents with dependencies up to the given step.
349          * 
350          * @param from
351          *            the source project element
352          * @param to
353          *            the destination project element
354          * @param finalStepNum
355          *            the final step for copy process
356          * @param oldToNewPub2
357          * @throws MissedPropertyException
358          *             if document creation is failed
359          * @throws InvalidPropertyException
360          *             if document creation is failed
361          * @throws MultiplyDefinedException
362          *             if document creation is failed
363          * @throws IOException
364          *             if document file creation is failed
365          * @throws NotApplicableException
366          *             if document state is not applicable
367          * @param oldToNewPub
368          *            the old to new publications map
369          * 
370          */
371         private void copyDocs(final ProjectElement from, final ProjectElement to,
372                         final int finalStepNum,
373                         final Map<Publication, Publication> oldToNewPub)
374                         throws MissedPropertyException, InvalidPropertyException,
375                         MultiplyDefinedException, NotApplicableException, IOException {
376                 Map<Integer, Step> steps = getProjectElementService().getStepsMap(to);
377                 // Copy publications without old versions and relations to not copied steps documents
378                 for (Publication pub : from.getDocums()) {
379                         // If the document in the step before the final one
380                         if (pub.value().getStep() <= finalStepNum) {
381                                 // Copy the document
382                                 oldToNewPub.put(pub, createDoc(pub.value(), steps.get(pub
383                                                 .value().getStep())));
384                         }
385                 }
386         }
387
388         /**
389          * Create a copy of the given document and publish it in the given step.
390          * 
391          * @param fromDoc
392          *            the source document
393          * @param step
394          *            the destination step
395          * @return the created publication
396          * @throws MissedPropertyException
397          *             if document creation is failed
398          * @throws InvalidPropertyException
399          *             if document creation is failed
400          * @throws MultiplyDefinedException
401          *             if document creation is failed
402          * @throws IOException
403          *             if document file creation is failed
404          * @throws NotApplicableException
405          *             if document state is not applicable
406          */
407         private Publication createDoc(final Document fromDoc, final Step step)
408                         throws MissedPropertyException, InvalidPropertyException,
409                         MultiplyDefinedException, IOException, NotApplicableException {
410
411                 java.io.File upfile = fromDoc.getSourceFile().asFile();
412                 // Creation of the document
413                 Document.Properties dprop = new Document.Properties().setName(
414                                 fromDoc.getTitle()).setType(fromDoc.getType()).setFormat(
415                                 fromDoc.getFormat()).setAuthor(fromDoc.getAuthor());
416                 dprop.setLocalPath(upfile.getPath());
417                 Publication addoc = getStepService().createDocument(step, dprop);
418                 copyFile(upfile, addoc.getSourceFile());
419                 getPublicationService().saveAs(addoc, fromDoc.getProgressState());
420
421                 // Copy attached files
422                 for (Relation rel : addoc.value().getRelations(ConvertsRelation.class)) {
423                         File attach = ((ConvertsRelation) rel).getTo();
424                         ConvertsRelation export = getPublicationService().attach(addoc,
425                                         attach.getFormat());
426                         copyFile(attach.asFile(), export.getTo());
427                 }
428                 return addoc;
429         }
430
431         /**
432          * Copy a file. Print info message.
433          * 
434          * @param upfile
435          *            the source file.
436          * @param targetFile
437          *            the target file
438          * @throws IOException
439          *             if failed
440          */
441         private void copyFile(final java.io.File upfile, final File targetFile)
442                         throws IOException {
443                 if (LOG.isInfoEnabled()) {
444                         LOG.info("Copy " + upfile.getAbsolutePath() + TO
445                                         + targetFile.asFile().getPath());
446                 }
447                 IOUtils.copy(upfile, targetFile.asFile());
448         }
449
450         /**
451          * {@inheritDoc}
452          * 
453          * @see org.splat.service.ScenarioService#getScenarioInfo(long)
454          */
455         @Transactional(readOnly = true)
456         public List<StepDTO> getScenarioInfo(final long scenarioId) {
457                 List<StepDTO> res = new ArrayList<StepDTO>();
458                 // Get the scenario from the database by id
459                 Scenario scen = getScenarioDAO().get(scenarioId);
460                 if (LOG.isDebugEnabled()) {
461                         LOG.debug("Scenario[" + scenarioId + "]: Number of publications: "
462                                         + scen.getDocums().size());
463                 }
464                 // Get activities of the scenario
465                 Step[] steps = getProjectElementService().getSteps(scen);
466                 StepDTO stepDTO;
467                 DocumentDTO docDTO;
468                 String docType, fileFormat;
469                 String processing;
470                 boolean doImport;
471                 // For each activity create a step DTO and add it to the result list
472                 for (Step step : steps) {
473                         stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class);
474                         res.add(stepDTO);
475                         if (LOG.isDebugEnabled()) {
476                                 LOG.debug("Step[" + stepDTO.getNumber()
477                                                 + "]: Number of documents: "
478                                                 + step.getDocuments().size());
479                         }
480                         // For each publication of the activity create a document DTO.
481                         // Each file is considered as a source file.
482                         for (Publication tag : step.getDocuments()) {
483                                 docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value()
484                                                 .getTitle());
485                                 char aState = tag.getIsnew();
486                                 docType = tag.value().getType().getName();
487                                 // For each file of the document create a file DTO
488                                 // Process source file of the document
489                                 fileFormat = tag.value().getFile().getFormat();
490                                 doImport = getProjectSettings().doImport(docType, fileFormat);
491                                 if (doImport && (!tag.isOutdated())) {
492                                         processing = "file-import";
493                                 } else {
494                                         processing = "file-download";
495                                 }
496                                 File aFile = tag.value().getFile();
497                                 docDTO.addFile(aFile.getIndex(), aFile.getRelativePath(),
498                                                 aState, processing, false);
499                                 // Process all exported files
500                                 for (Relation rel : tag.value().getRelations(
501                                                 ConvertsRelation.class)) {
502                                         aFile = ((ConvertsRelation) rel).getTo();
503                                         fileFormat = aFile.getFormat();
504                                         doImport = getProjectSettings().doImport(docType,
505                                                         fileFormat);
506                                         if (doImport && (!tag.isOutdated())) {
507                                                 processing = "file-import";
508                                         } else {
509                                                 processing = "file-download";
510                                         }
511                                         docDTO.addFile(aFile.getIndex(), aFile.getRelativePath(),
512                                                         aState, processing, false);
513                                 }
514                         }
515                 }
516                 return res;
517         }
518
519         /**
520          * {@inheritDoc}
521          * 
522          * @see org.splat.service.ScenarioService#createStudy(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
523          */
524         @Transactional
525         public long createStudy(final String username, final String title,
526                         final String productName, final String description)
527                         throws InvalidPropertyException, MissedPropertyException,
528                         MultiplyDefinedException {
529                 long id = 0;
530
531                 // Find the author
532                 User author = getUserService().selectUser(username);
533                 if (author == null) {
534                         // User is not found
535                         throw new InvalidPropertyException(MessageKeyEnum.USR_000001
536                                         .toString(), username);
537                 }
538
539                 // Set the study properties
540                 Study.Properties sprop = new Study.Properties();
541                 sprop.setTitle(title).setManager(author);
542                 sprop.setDescription(description);
543
544                 // Find the product simulation context
545                 SimulationContextType productContextType = getSimulationContextService()
546                                 .selectType("product");
547                 SimulationContext.Properties cprop = new SimulationContext.Properties();
548                 cprop.setType(productContextType).setValue(productName);
549                 SimulationContext productCtx = getSimulationContextService()
550                                 .selectSimulationContext(productContextType, productName);
551                 if (productCtx != null) {
552                         cprop.setIndex(productCtx.getIndex());
553                 }
554
555                 // Set a first scenario properties
556                 Scenario.Properties oprop = new Scenario.Properties();
557                 oprop.setTitle(I18nUtils.getMessageLocaleDefault("label.scenario")
558                                 + " 1");
559
560                 Study study = createStudy(sprop, oprop, cprop);
561                 id = study.getIndex();
562
563                 return id;
564         }
565
566         /**
567          * Create a new study with one scenario and "product" simulation context.
568          * 
569          * @param sprop
570          *            the study properties
571          * @param oprop
572          *            the scenario properties
573          * @param cprop
574          *            the "product" simulation context properties
575          * @return the created study
576          * @throws MissedPropertyException
577          *             if a mandatory property is missed
578          * @throws InvalidPropertyException
579          *             if a property is invalid
580          * @throws MultiplyDefinedException
581          *             if some property occurs several times
582          */
583         @Transactional
584         public Study createStudy(final Study.Properties sprop,
585                         final Scenario.Properties oprop,
586                         final SimulationContext.Properties cprop)
587                         throws MissedPropertyException, InvalidPropertyException,
588                         MultiplyDefinedException {
589                 Study study = getStudyService().createStudy(sprop);
590                 addScenario(study, oprop);
591                 if (cprop.getIndex() == 0) { // Input of new project context
592                         cprop.setType(getSimulationContextService().selectType("product"))
593                                         .setValue(cprop.getValue());
594                         getStudyService().addProjectContext(study, cprop);
595                 } else { // Selection of existing project context
596                         SimulationContext context = getSimulationContextService()
597                                         .selectSimulationContext(cprop.getIndex());
598                         getStudyService().addProjectContext(study, context);
599                 }
600                 return study;
601         }
602
603         /**
604          * {@inheritDoc}
605          * 
606          * @see org.splat.service.ScenarioService#assignStudyContext(java.lang.Long, java.lang.String, java.lang.String)
607          */
608         @Transactional
609         public void assignStudyContext(final Long studyId, final String ctxType,
610                         final String ctxValue) throws MissedPropertyException,
611                         InvalidPropertyException, MultiplyDefinedException {
612                 // Find the study by the given id
613                 Study study = getStudyDAO().get(studyId);
614                 if (study == null) {
615                         throw new InvalidPropertyException(MessageKeyEnum.STD_000002
616                                         .toString(), studyId);
617                 }
618                 // Find the context type by its name
619                 SimulationContextType celt = getSimulationContextService().selectType(
620                                 ctxType);
621                 if (celt == null) {
622                         // Creation of a new context type
623                         celt = getSimulationContextTypeService().createType(ctxType,
624                                         getProjectElementService().getFirstStep(study).getStep());
625                 }
626                 // Find the given context value of the given type
627                 SimulationContext context = getSimulationContextService()
628                                 .selectSimulationContext(celt, ctxValue);
629                 if (context == null) { // Input of a new project context
630                         SimulationContext.Properties cprop = new SimulationContext.Properties();
631                         cprop.setType(celt).setValue(ctxValue);
632                         getStudyService().addProjectContext(study, cprop);
633                 } else { // Selection of existing project context
634                         getStudyService().addProjectContext(study, context);
635                 }
636         }
637
638         /**
639          * {@inheritDoc}
640          * 
641          * @see org.splat.service.ScenarioService#addKnowledgeElement(org.splat.dal.bo.som.Scenario,
642          *      org.splat.dal.bo.som.KnowledgeElement.Properties)
643          */
644         @Transactional
645         public KnowledgeElement addKnowledgeElement(final Scenario aScenarioDTO,
646                         final KnowledgeElement.Properties kprop)
647                         throws MissedPropertyException, InvalidPropertyException,
648                         MultiplyDefinedException {
649                 KnowledgeElement kelm = null;
650                 // try {
651                 long aScenarioId = aScenarioDTO.getIndex();
652                 if (LOG.isDebugEnabled()) {
653                         LOG
654                                         .debug("Add a knowledge element to the scenario #"
655                                                         + aScenarioId);
656                 }
657                 // Get the persistent scenario.
658                 Scenario aScenario = getScenarioDAO().get(aScenarioId);
659                 // Get persistent objects for creating a new knowledge.
660                 // TODO: Actions must use DTO instead of persistent objects.
661                 getUserDAO().merge(kprop.getAuthor());
662                 getKnowledgeElementTypeDAO().merge(kprop.getType());
663                 // Create a transient knowledge element related to the given scenario.
664                 kelm = new KnowledgeElement(kprop.setOwnerScenario(aScenario));
665                 // Save the new knowledge in the database.
666                 getKnowledgeElementDAO().create(kelm);
667                 // Update scenario transient data.
668                 if (kelm.getType().equals("usecase")) {
669                         aScenarioDTO.setUcase(kelm);
670                 } else if (aScenarioDTO.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
671                         aScenarioDTO.getKnowledgeElementsList().add(kelm);
672                 }
673
674                 // Load the workflow for the parent study to take into account
675                 // all study actors durng reindexing.
676                 getStudyService().loadWorkflow(aScenario.getOwnerStudy());
677
678                 // // Update the lucene index of knowledge elements.
679                 // getIndexService().add(kelm);
680                 if (LOG.isDebugEnabled()) {
681                         LOG.debug("A knowledge element #" + kelm.getIndex()
682                                         + " is added to the scenario #" + aScenario.getIndex());
683                 }
684                 // } catch (IOException error) {
685                 // LOG.error("Unable to index the knowedge element '"
686                 // + kelm.getIndex() + "', reason:", error);
687                 // kelm = null;
688                 // }
689
690                 return kelm;
691         }
692
693         /**
694          * {@inheritDoc}
695          * 
696          * @see org.splat.service.ScenarioService#checkin(long, long, java.util.List)
697          */
698         @Transactional
699         public void checkin(final long scenId, final long userId,
700                         final List<StepDTO> scInfo) throws InvalidPropertyException,
701                         MissedPropertyException, MultiplyDefinedException,
702                         MismatchException, IOException, NotApplicableException {
703                 // Get the scenario from the database by id
704                 Scenario aScenario = getScenarioDAO().get(scenId);
705                 // Get the user who perform this check-in operation
706                 User aUser = getUserService().selectUser(userId);
707                 // Get activities of the scenario
708                 Step[] steps = getProjectElementService().getSteps(aScenario);
709                 // Find result document types
710                 List<DocumentType> resTypes = getDocumentTypeService()
711                                 .selectResultTypes();
712
713                 // Keep newly created documents to create uses relations to results of a previous step.
714                 // For each processed existing document keep its new version
715                 Map<Document, Document> newVersion = new HashMap<Document, Document>();
716                 // Created publications of new created versions of existing documents
717                 List<Publication> newVers = new ArrayList<Publication>();
718                 // The list of publications of new created documents not existing before the checkin
719                 List<Publication> newDocs = new ArrayList<Publication>();
720                 // For each step DTO
721                 DocumentType resType;
722                 Date aDate = new Date(); // The timestamp of the checkin operation
723                 for (StepDTO stepDTO : scInfo) {
724                         if (LOG.isDebugEnabled()) {
725                                 LOG.debug("Checkin the step:\n" + stepDTO);
726                         }
727                         // Find a result document type of the step
728                         int i = 0;
729                         resType = null;
730                         do {
731                                 if (resTypes.get(i).isResultOf(
732                                                 getProjectSettings().getStep(stepDTO.getNumber()))) {
733                                         resType = resTypes.get(i);
734                                 }
735                                 i++;
736                         } while ((resType == null) && (i < resTypes.size()));
737
738                         // Find the appropriate scenario step
739                         Step step = findStep(stepDTO, steps);
740
741                         // Process each document of the step
742                         for (DocumentDTO doc : stepDTO.getDocs()) {
743                                 checkinDoc(step, doc, aUser, resType, aDate, newVersion,
744                                                 newVers, newDocs);
745                         }
746                 }
747
748                 // Set uses/used relations
749                 updateRelationsAfterCheckin(aScenario, newVersion, newVers, newDocs);
750
751                 // Mark the scenario as checked in
752                 checkin(aScenario);
753         }
754
755         /**
756          * Updated uses/used relations after checkin operation:<BR>
757          * <ul>
758          * <li>For each new version copy uses relations from the previous version.</li>
759          * <li>Outdate documents which depend from the previous version and were not checked in during this operation.</li>
760          * <li>For each new document create uses relation to the last versions of results of the previous step.</li>
761          * </ul>
762          * 
763          * @param aScenario
764          *            the checked in scenario
765          * @param newVersion
766          *            the mapping of documents existed before the checkin to their new created versions
767          * @param newVers
768          *            the list of publications of new created versions of documents existed before the checkin
769          * @param newDocs
770          *            the list of publications of new created documents not existed before the checkin
771          */
772         private void updateRelationsAfterCheckin(final Scenario aScenario,
773                         final Map<Document, Document> newVersion,
774                         final List<Publication> newVers, final List<Publication> newDocs) {
775                 // For each new version copy uses relations from the previous version.
776                 for (Publication newVer : newVers) {
777                         // For each Uses relation of the previous version
778                         Document prevDoc = newVer.value().getPreviousVersion();// prevVersion.get(newVer);
779                         if (LOG.isDebugEnabled()) {
780                                 LOG.debug("Previous version for publication #"
781                                                 + newVer.getIndex() + " is found: " + prevDoc);
782                         }
783                         List<Relation> usesRelations = prevDoc
784                                         .getRelations(UsesRelation.class);
785                         for (Relation rel : usesRelations) {
786                                 // If used document has been also versioned then refer to its new version.
787                                 Document usedDoc = ((UsesRelation) rel).getTo();
788                                 if (newVersion.containsKey(usedDoc)) {
789                                         usedDoc = newVersion.get(usedDoc);
790                                 }
791                                 // Build the appropriate relation for the new version.
792                                 newVer.addDependency(usedDoc);
793                         }
794                         // Outdate documents which depend from the previous version and
795                         // were not checked in during this operation.
796                         // 1. Get all usedBy relations of the previous document version
797                         for (Relation rel : prevDoc.getRelations(UsedByRelation.class)) {
798                                 Document using = ((UsedByRelation) rel).getTo();
799                                 // Check that not checked in dependent documents became outdated
800                                 Publication usingPub = aScenario.getPublication(using);
801                                 if (usingPub != null) { // if the document using the old version is still published
802                                         usingPub.setIsnew('O');
803                                 }
804                         }
805                 }
806
807                 // For each new document create uses relation to the last versions of
808                 // results of the previous step.
809                 for (Publication newPub : newDocs) {
810                         // Find used document type according to the configuration.
811                         Set<DocumentType> usedTypes = newPub.value().getType()
812                                         .getDefaultUses();
813                         // Find documents of used type in the previous study step.
814                         for (Publication pub : aScenario.getDocums()) {
815                                 if ((pub.getStep().getNumber() <= newPub.getStep().getNumber())
816                                                 && (!pub.isOutdated())
817                                                 && usedTypes.contains(pub.value().getType())) {
818                                         // Create uses relation from the new document
819                                         // to the found document in the previous step.
820                                         newPub.addDependency(pub);
821                                 }
822                         }
823                 }
824         }
825
826         /**
827          * Pure checkin of the document without creation of uses/usedBy relations. For an existing document a new version is created. New
828          * documents become published in the given step of the appropriate scenario. The appropriate uploaded file is attached to the created
829          * document and the document is published in the scenario. The publication of the old version is removed from the scenario.
830          * 
831          * @param step
832          *            the destination scenario step
833          * @param doc
834          *            the DTO of the document to checkin
835          * @param aUser
836          *            the user who performs checkin
837          * @param resType
838          *            the result document type of the given step
839          * @param aDate
840          *            timestamp of the checkin operation
841          * @param newVersion
842          *            the mapping of existing documents to their new created versions
843          * @param newVers
844          *            the list of publications of new created versions of existing documents
845          * @param newDocs
846          *            the list of publications of new created documents not existing before the checkin
847          * @throws InvalidPropertyException
848          *             if the scenario hasn't some of given steps or documents
849          * @throws IOException
850          *             if a file can't be moved into the vault
851          * @throws MismatchException
852          *             if version creation in some of steps is failed
853          * @throws MissedPropertyException
854          *             if some mandatory property is missed when new document or new document version is created
855          * @throws MultiplyDefinedException
856          *             if some property is defined several times when new document or new document version is created
857          * @throws NotApplicableException
858          *             if failed saving of a new publication with a given state
859          */
860         private void checkinDoc(final Step step, final DocumentDTO doc,
861                         final User aUser, final DocumentType resType, final Date aDate,
862                         final Map<Document, Document> newVersion,
863                         final List<Publication> newVers, final List<Publication> newDocs)
864                         throws InvalidPropertyException, MismatchException,
865                         MissedPropertyException, MultiplyDefinedException, IOException,
866                         NotApplicableException {
867                 if (doc.getFiles().size() > 0) {
868                         Document.Properties dprop = new Document.Properties();
869                         // NOTE: Process only the first attached file for each document
870                         FileDTO file = doc.getFiles().get(0);
871                         dprop.setLocalPath(file.getPath());
872
873                         // Get document title as the file name
874                         java.io.File upfile = new java.io.File(file.getPath());
875                         String fileFormat = upfile.getName().substring(
876                                         upfile.getName().lastIndexOf('.') + 1);
877
878                         // Attach the file via ConvertsRelation, create a new document or
879                         // create a new version of the document
880                         dprop.setAuthor(aUser).setDate(aDate).setFormat(fileFormat);
881
882                         if (doc.getId() > 0) {
883                                 checkinExistingDoc(step, doc, dprop, fileFormat, upfile,
884                                                 newVersion, newVers);
885                         } else {
886
887                                 // Otherwise create a new document of the result type
888                                 // If result type is not found try to get type by file extension
889                                 if (resType == null) {
890                                         dprop.setType(getProjectSettings().getDefaultDocumentType(
891                                                         step.getStep(), fileFormat));
892                                 } else {
893                                         dprop.setType(resType);
894                                 }
895                                 // New document title generation as <document type name>_N
896                                 String docname = dprop.getType().getName();
897                                 int i = 1;
898                                 for (Publication scenPub : step.getOwner().getDocums()) {
899                                         if (scenPub.value().getTitle().startsWith(docname)) {
900                                                 i++;
901                                         }
902                                 }
903                                 docname += "_" + i; // The generated new document title
904
905                                 dprop.setDescription("Checked in").setName(docname);
906                                 Publication newPub = getStepService().createDocument(step,
907                                                 dprop);
908
909                                 // Remember the new document
910                                 newDocs.add(newPub);
911
912                                 saveFile(newPub, step, upfile);
913                         }
914                 }
915         }
916
917         /**
918          * Check in existing document.
919          * 
920          * @param step
921          *            study step to check in
922          * @param doc
923          *            document DTO to check in
924          * @param dprop
925          *            document properties
926          * @param fileFormat
927          *            checked in file format
928          * @param upfile
929          *            the file to check in
930          * @param newVersion
931          *            the map of created versions during this check in
932          * @param newVers
933          *            the list of new versions created during this check in
934          * @throws InvalidPropertyException
935          *             if publication of the document is not found in the step
936          * @throws MismatchException
937          *             if the found publication does not point to a document
938          * @throws IOException
939          *             if can not move the file into the vault
940          * @throws MultiplyDefinedException
941          *             thrown by versionDocument
942          * @throws MissedPropertyException
943          *             thrown by versionDocument
944          * @throws NotApplicableException
945          *             if failed saving of a new publication with a given state
946          */
947         private void checkinExistingDoc(final Step step, final DocumentDTO doc,
948                         final Properties dprop, final String fileFormat,
949                         final java.io.File upfile,
950                         final Map<Document, Document> newVersion,
951                         final List<Publication> newVers) throws InvalidPropertyException,
952                         MismatchException, MissedPropertyException,
953                         MultiplyDefinedException, IOException, NotApplicableException {
954                 // If the document already exists then
955                 // Attach the file via ConvertsRelation if the extension of the
956                 // new file differs from the old one.
957                 // If file format (i.e. extension) is the same then create a new
958                 // version of the document.
959                 // Find the document publication
960                 Publication pub = step.getDocument(doc.getId());
961                 if (pub == null) {
962                         throw new InvalidPropertyException(MessageKeyEnum.SCN_000002
963                                         .toString(), doc.getId());
964                 }
965                 if (pub.value() == null) {
966                         throw new MismatchException(MessageKeyEnum.SCN_000002.toString(),
967                                         doc.getId());
968                 }
969                 if (LOG.isDebugEnabled()) {
970                         LOG.debug("Old format: " + pub.value().getFormat()
971                                         + " => New format: " + fileFormat);
972                 }
973                 // If formats are same then create a new document version
974                 if (pub.value().getFormat() != null
975                                 && pub.value().getFormat().equals(fileFormat)) {
976                         Publication newPub = getStepService().versionDocument(step, pub,
977                                         dprop);
978                         if (LOG.isDebugEnabled()) {
979                                 LOG.debug("Created document type: "
980                                                 + newPub.value().getType().getName() + ", format: "
981                                                 + newPub.value().getFormat());
982                         }
983                         // Remeber the link from the old document to the new document version
984                         newVersion.put(pub.value(), newPub.value());
985                         // Remember the new version publication
986                         newVers.add(newPub);
987
988                         saveFile(newPub, step, upfile);
989
990                 } else { // If formats are different then attach the new file via ConvertsRelation
991                         File attach = pub.value().getAttachedFile(fileFormat);
992                         if (attach == null) {
993                                 // If there is no attachment with this extension then attach the new one
994                                 ConvertsRelation export = getPublicationService().attach(pub,
995                                                 fileFormat);
996                                 if (LOG.isDebugEnabled()) {
997                                         LOG.debug("Moving " + upfile.getName() + TO
998                                                         + export.getTo().asFile().getPath());
999                                 }
1000                                 upfile.renameTo(export.getTo().asFile());
1001                         } else {
1002                                 // If an attachment with this extension already exists then
1003                                 // replace it by the new one
1004                                 upfile.renameTo(attach.asFile());
1005                                 // Update attached file modification date
1006                                 attach.setDate(new Date());
1007                         }
1008                 }
1009         }
1010
1011         /**
1012          * Save the file in the vault and create its publication in the step.
1013          * 
1014          * @param newPub
1015          *            the new publication to save
1016          * @param step
1017          *            the study step to publish the document
1018          * @param upfile
1019          *            the downloaded file
1020          * @throws IOException
1021          *             if a file can't be moved into the vault
1022          * @throws NotApplicableException
1023          *             if failed saving of a new publication with a given state
1024          */
1025         private void saveFile(final Publication newPub, final Step step,
1026                         final java.io.File upfile) throws IOException,
1027                         NotApplicableException {
1028                 // Attach the file to the created document
1029                 java.io.File updir = newPub.getSourceFile().asFile();
1030                 if (LOG.isDebugEnabled()) {
1031                         LOG.debug("Moving \"" + upfile.getName() + "\" to \""
1032                                         + updir.getPath() + "\".");
1033                 }
1034                 if (updir.exists()) {
1035                         if (updir.delete()) {
1036                                 LOG.info(MessageKeyEnum.SCN_000003.toString(), updir
1037                                                 .getAbsoluteFile(), step.getOwner().getIndex());
1038                         } else {
1039                                 throw new IOException(
1040                                                 "Can't delete the existing destination file to move file from "
1041                                                                 + upfile.getAbsolutePath() + TO
1042                                                                 + updir.getAbsolutePath());
1043                         }
1044                 }
1045                 if (upfile.renameTo(updir)) {
1046                         // Save the new publication in the scenario.
1047                         // The old publication is removed from the scenario here.
1048                         getPublicationService().saveAs(newPub, ProgressState.inWORK); // May throw FileNotFound if rename was not done
1049                 } else {
1050                         throw new IOException("Can't move file from "
1051                                         + upfile.getAbsolutePath() + TO + updir.getAbsolutePath());
1052                 }
1053         }
1054
1055         /**
1056          * Find appropriate step in the array of scenario steps according to the given step DTO.
1057          * 
1058          * @param stepDTO
1059          *            the stepDTO
1060          * @param steps
1061          *            scenario steps
1062          * @return appropriate scenario step
1063          * @throws InvalidPropertyException
1064          *             if appropriate step is not found
1065          */
1066         private Step findStep(final StepDTO stepDTO, final Step[] steps)
1067                         throws InvalidPropertyException {
1068                 int i = 0;
1069                 Step step = null;
1070                 do {
1071                         if (steps[i].getNumber() == stepDTO.getNumber()) {
1072                                 step = steps[i];
1073                         }
1074                         i++;
1075                 } while ((step == null) && (i < steps.length));
1076
1077                 if (step == null) {
1078                         throw new InvalidPropertyException(MessageKeyEnum.SCN_000001
1079                                         .toString(), stepDTO.getNumber());
1080                 }
1081                 return step;
1082         }
1083
1084         /**
1085          * {@inheritDoc}
1086          * 
1087          * @see org.splat.service.ScenarioService#checkin(long)
1088          */
1089         @Transactional
1090         public void checkin(final long scenarioId) throws InvalidPropertyException {
1091                 Scenario aScenario = getScenarioDAO().get(scenarioId);
1092                 if (aScenario == null) {
1093                         // Scenario not found
1094                         throw new InvalidPropertyException(MessageKeyEnum.SCN_000006
1095                                         .toString(), scenarioId);
1096                 }
1097                 checkin(aScenario);
1098         }
1099
1100         /**
1101          * Mark the scenario as checked in.
1102          * 
1103          * @param aScenario
1104          *            the scenario to check in.
1105          */
1106         private void checkin(final Scenario aScenario) {
1107                 aScenario.setUser(null);
1108                 aScenario.setLastModificationDate(Calendar.getInstance().getTime());
1109                 // getScenarioDAO().update(aScenario);
1110         }
1111
1112         /**
1113          * {@inheritDoc}
1114          * 
1115          * @see org.splat.service.ScenarioService#checkout(org.splat.dal.bo.som.Scenario, org.splat.dal.bo.kernel.User)
1116          */
1117         public boolean checkout(final Scenario aScenario, final User user) {
1118                 boolean res = getStudyService().isStaffedBy(aScenario.getOwnerStudy(),
1119                                 user);
1120                 if (res) {
1121                         aScenario.setUser(user);
1122                         aScenario.setLastModificationDate(Calendar.getInstance().getTime());
1123                         // RKV: getScenarioDAO().update(aScenario);
1124                 }
1125                 return res;
1126         }
1127
1128         /**
1129          * Mark the given scenario as checked out by the given user.
1130          * 
1131          * @param scenarioId
1132          *            the scenario id
1133          * @param userId
1134          *            the id of the user performing the check out
1135          * @throws InvalidPropertyException
1136          *             if the user or the scenario is not found in the database
1137          * @throws NotApplicableException
1138          *             if the given user can not check out the scenario
1139          */
1140         @Transactional
1141         public void checkout(final long scenarioId, final long userId)
1142                         throws InvalidPropertyException, NotApplicableException {
1143                 User aUser = getUserService().selectUser(userId);
1144                 if (aUser == null) {
1145                         // User not found
1146                         throw new InvalidPropertyException(MessageKeyEnum.USR_000001
1147                                         .toString(), userId);
1148                 }
1149                 Scenario aScenario = getScenarioDAO().get(scenarioId);
1150                 if (aScenario == null) {
1151                         // Scenario not found
1152                         throw new InvalidPropertyException(MessageKeyEnum.SCN_000006
1153                                         .toString(), scenarioId);
1154                 }
1155                 boolean res = getStudyService().isStaffedBy(aScenario.getOwnerStudy(),
1156                                 aUser);
1157                 if (res) {
1158                         if (aScenario.isCheckedout()
1159                                         && (!aScenario.getUser().getUsername().equals(
1160                                                         aUser.getUsername()))) {
1161                                 throw new NotApplicableException(MessageKeyEnum.SCN_000008
1162                                                 .toString(), scenarioId, aScenario.getUser()
1163                                                 .getUsername());
1164                         }
1165                         aScenario.setUser(aUser);
1166                         aScenario.setLastModificationDate(Calendar.getInstance().getTime());
1167                 } else {
1168                         // User doesn't participate in the scenario
1169                         throw new NotApplicableException(MessageKeyEnum.SCN_000007
1170                                         .toString(), aUser.getUsername(), scenarioId);
1171                 }
1172         }
1173
1174         /**
1175          * {@inheritDoc}
1176          * 
1177          * @see org.splat.service.ScenarioService#copyContentsUpTo(org.splat.dal.bo.som.Scenario, org.splat.som.Step)
1178          */
1179         public void copyContentsUpTo(final Scenario scenario, final Step lastep) {
1180                 Scenario base = (Scenario) lastep.getOwner();
1181                 Step[] from = getProjectElementService().getSteps(base);
1182                 Step[] to = getProjectElementService().getSteps(scenario);
1183                 for (int i = 0; i < from.length; i++) {
1184                         Step step = from[i];
1185                         if (step.getNumber() > lastep.getNumber()) {
1186                                 break;
1187                         }
1188
1189                         List<Publication> docs = step.getAllDocuments();
1190                         for (Iterator<Publication> j = docs.iterator(); j.hasNext();) {
1191                                 Publication doc = getPublicationService().copy(j.next(),
1192                                                 scenario); // Creation of a new reference to the document
1193                                 // Database.getSession().save(doc); Publications MUST be saved later through cascading when saving the scenario
1194                                 getStepService().add(to[i], doc);
1195                         }
1196                         List<SimulationContext> ctex = step.getAllSimulationContexts();
1197                         for (Iterator<SimulationContext> j = ctex.iterator(); j.hasNext();) {
1198                                 getStepService().addSimulationContext(to[i], j.next());
1199                         }
1200                 }
1201         }
1202
1203         /**
1204          * {@inheritDoc}
1205          * 
1206          * @see org.splat.service.ScenarioService#isEmpty(org.splat.dal.bo.som.Scenario)
1207          */
1208         public boolean isEmpty(final Scenario scenario) {
1209                 Step[] mystep = getProjectElementService().getSteps(scenario);
1210                 boolean isEmp = true;
1211                 for (int i = 0; i < mystep.length; i++) {
1212                         if (mystep[i].isStarted()) {
1213                                 isEmp = false;
1214                                 break;
1215                         }
1216                 }
1217                 return isEmp;
1218         }
1219
1220         /**
1221          * @param scenario
1222          * @return
1223          */
1224         public boolean isFinished(final Scenario scenario) {
1225                 Step[] mystep = getProjectElementService().getSteps(scenario);
1226                 boolean notempty = false; // If this is empty, this is not finished
1227                 for (int i = 0; i < mystep.length; i++) {
1228                         if (!mystep[i].isStarted()) {
1229                                 continue;
1230                         }
1231                         if (!mystep[i].isFinished()) {
1232                                 return false;
1233                         }
1234                         notempty = true;
1235                 }
1236                 return notempty;
1237         }
1238
1239         /**
1240          * {@inheritDoc}
1241          * 
1242          * @see org.splat.service.StudyService#addScenario(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Scenario.Properties)
1243          */
1244         @Transactional
1245         public Scenario addScenario(final Study aStudy,
1246                         final Scenario.Properties sprop) throws MissedPropertyException,
1247                         InvalidPropertyException, MultiplyDefinedException {
1248                 if (sprop.getManager() == null) {
1249                         sprop.setManager(aStudy.getAuthor());
1250                 }
1251
1252                 Scenario scenario = new Scenario(sprop.setOwnerStudy(aStudy));
1253                 if (sprop.getBaseStep() != null) {
1254                         copyContentsUpTo(scenario, sprop.getBaseStep());
1255                 }
1256                 Scenario previous = sprop.getInsertAfter();
1257
1258                 if (previous == null) {
1259                         aStudy.getScenariiList().add(scenario);
1260                 } else {
1261                         aStudy.getScenariiList().add(
1262                                         aStudy.getScenariiList().indexOf(previous) + 1, scenario);
1263                 }
1264                 getStudyDAO().update(aStudy); // No need to update the Lucene index
1265                 getScenarioDAO().create(scenario); // Must be done after updating this study because of the back reference to the study
1266                 if (sprop.getBaseStep() != null) {
1267                         // No need to update the Knowledge Element index as Knowledge Elements are not copied
1268                         getProjectElementService().refresh(scenario); // Because saving the scenario changes the hashcode of copied Publications
1269                 }
1270                 KnowledgeElementType ucase = getKnowledgeElementTypeService()
1271                                 .selectType("usecase");
1272                 KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
1273                 // TODO: Get appropriate user by its role: UserService.getAdmin();
1274                 // User admin = getUserService().selectUser(1); // First user created when creating the database
1275                 Role adminRole = getRoleDAO().getFilteredList(
1276                                 Restrictions.like("role", "%sysadmin%")).get(0);
1277                 User admin = getUserDAO().getFilteredList(
1278                                 Restrictions.eq("role", adminRole), Order.asc("rid")).get(0); // First sysadmin in the database
1279
1280                 kprop.setType(ucase).setTitle(aStudy.getTitle()).setValue(
1281                                 scenario.getTitle()).setAuthor(admin); // Internal Knowledge Element required by the validation process of
1282                 // knowledges
1283                 addKnowledgeElement(scenario, kprop);
1284                 return scenario;
1285         }
1286
1287         /**
1288          * Remove a knowledge element from a scenario.
1289          * 
1290          * @param scenario
1291          *            the scenario
1292          * @param kelm
1293          *            the knowledge element to remove
1294          * @return true if removal succeeded
1295          */
1296         @Transactional
1297         public boolean removeKnowledgeElement(final Scenario scenario,
1298                         final KnowledgeElement kelm) {
1299                 KnowledgeElement torem = scenario.getKnowledgeElement(kelm.getIndex());
1300                 boolean isOk = (torem != null);
1301                 if (isOk) {
1302                         isOk = scenario.getKnowledgeElements().remove(torem);
1303                         if (isOk) {
1304                                 getScenarioDAO().merge(scenario);
1305                                 // Update of my transient data
1306                                 // RKV: These transient data are not used indeed.
1307                                 // RKV: List<KnowledgeElement> kelms = scenario.getKnowledgeByType().get(
1308                                 // RKV: kelm.getType().getIndex());
1309                                 // RKV: kelms.remove(torem);
1310                                 if (scenario.getKnowledgeElementsList() != null) {
1311                                         scenario.getKnowledgeElementsList().remove(torem);
1312                                 }
1313                                 // TODO: If the owner study is not private, remove the knowledge from the Lucene index
1314                         }
1315                 }
1316                 return isOk;
1317         }
1318
1319         /**
1320          * 
1321          * {@inheritDoc}
1322          * 
1323          * @see org.splat.service.ScenarioService#renameScenario(java.lang.String)
1324          */
1325         @Transactional
1326         public void renameScenario(final Scenario scenario) {
1327                 getScenarioDAO().merge(scenario);
1328         }
1329
1330         /**
1331          * Get the knowledgeElementDAO.
1332          * 
1333          * @return the knowledgeElementDAO
1334          */
1335         public KnowledgeElementDAO getKnowledgeElementDAO() {
1336                 return _knowledgeElementDAO;
1337         }
1338
1339         /**
1340          * Set the knowledgeElementDAO.
1341          * 
1342          * @param knowledgeElementDAO
1343          *            the knowledgeElementDAO to set
1344          */
1345         public void setKnowledgeElementDAO(
1346                         final KnowledgeElementDAO knowledgeElementDAO) {
1347                 _knowledgeElementDAO = knowledgeElementDAO;
1348         }
1349
1350         /**
1351          * Get the indexService.
1352          * 
1353          * @return the indexService
1354          */
1355         public IndexService getIndexService() {
1356                 return _indexService;
1357         }
1358
1359         /**
1360          * Set the indexService.
1361          * 
1362          * @param indexService
1363          *            the indexService to set
1364          */
1365         public void setIndexService(final IndexService indexService) {
1366                 _indexService = indexService;
1367         }
1368
1369         /**
1370          * Get the scenarioDAO.
1371          * 
1372          * @return the scenarioDAO
1373          */
1374         public ScenarioDAO getScenarioDAO() {
1375                 return _scenarioDAO;
1376         }
1377
1378         /**
1379          * Set the scenarioDAO.
1380          * 
1381          * @param scenarioDAO
1382          *            the scenarioDAO to set
1383          */
1384         public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
1385                 _scenarioDAO = scenarioDAO;
1386         }
1387
1388         /**
1389          * Get the studyDAO.
1390          * 
1391          * @return the studyDAO
1392          */
1393         public StudyDAO getStudyDAO() {
1394                 return _studyDAO;
1395         }
1396
1397         /**
1398          * Set the studyDAO.
1399          * 
1400          * @param studyDAO
1401          *            the studyDAO to set
1402          */
1403         public void setStudyDAO(final StudyDAO studyDAO) {
1404                 _studyDAO = studyDAO;
1405         }
1406
1407         /**
1408          * Get the knowledgeElementTypeService.
1409          * 
1410          * @return the knowledgeElementTypeService
1411          */
1412         public KnowledgeElementTypeService getKnowledgeElementTypeService() {
1413                 return _knowledgeElementTypeService;
1414         }
1415
1416         /**
1417          * Set the knowledgeElementTypeService.
1418          * 
1419          * @param knowledgeElementTypeService
1420          *            the knowledgeElementTypeService to set
1421          */
1422         public void setKnowledgeElementTypeService(
1423                         final KnowledgeElementTypeService knowledgeElementTypeService) {
1424                 _knowledgeElementTypeService = knowledgeElementTypeService;
1425         }
1426
1427         /**
1428          * Get the studyService.
1429          * 
1430          * @return the studyService
1431          */
1432         public StudyService getStudyService() {
1433                 return _studyService;
1434         }
1435
1436         /**
1437          * Set the studyService.
1438          * 
1439          * @param studyService
1440          *            the studyService to set
1441          */
1442         public void setStudyService(final StudyService studyService) {
1443                 _studyService = studyService;
1444         }
1445
1446         /**
1447          * Get the userService.
1448          * 
1449          * @return the userService
1450          */
1451         public UserService getUserService() {
1452                 return _userService;
1453         }
1454
1455         /**
1456          * Set the userService.
1457          * 
1458          * @param userService
1459          *            the userService to set
1460          */
1461         public void setUserService(final UserService userService) {
1462                 _userService = userService;
1463         }
1464
1465         /**
1466          * Get the userDAO.
1467          * 
1468          * @return the userDAO
1469          */
1470         public UserDAO getUserDAO() {
1471                 return _userDAO;
1472         }
1473
1474         /**
1475          * Set the userDAO.
1476          * 
1477          * @param userDAO
1478          *            the userDAO to set
1479          */
1480         public void setUserDAO(final UserDAO userDAO) {
1481                 _userDAO = userDAO;
1482         }
1483
1484         /**
1485          * Get the knowledgeElementTypeDAO.
1486          * 
1487          * @return the knowledgeElementTypeDAO
1488          */
1489         public KnowledgeElementTypeDAO getKnowledgeElementTypeDAO() {
1490                 return _knowledgeElementTypeDAO;
1491         }
1492
1493         /**
1494          * Set the knowledgeElementTypeDAO.
1495          * 
1496          * @param knowledgeElementTypeDAO
1497          *            the knowledgeElementTypeDAO to set
1498          */
1499         public void setKnowledgeElementTypeDAO(
1500                         final KnowledgeElementTypeDAO knowledgeElementTypeDAO) {
1501                 _knowledgeElementTypeDAO = knowledgeElementTypeDAO;
1502         }
1503
1504         /**
1505          * Get the simulationContextService.
1506          * 
1507          * @return the simulationContextService
1508          */
1509         public SimulationContextService getSimulationContextService() {
1510                 return _simulationContextService;
1511         }
1512
1513         /**
1514          * Set the simulationContextService.
1515          * 
1516          * @param simulationContextService
1517          *            the simulationContextService to set
1518          */
1519         public void setSimulationContextService(
1520                         final SimulationContextService simulationContextService) {
1521                 _simulationContextService = simulationContextService;
1522         }
1523
1524         /**
1525          * Get project settings.
1526          * 
1527          * @return Project settings service
1528          */
1529         private ProjectSettingsService getProjectSettings() {
1530                 return _projectSettings;
1531         }
1532
1533         /**
1534          * Set project settings service.
1535          * 
1536          * @param projectSettingsService
1537          *            project settings service
1538          */
1539         public void setProjectSettings(
1540                         final ProjectSettingsService projectSettingsService) {
1541                 _projectSettings = projectSettingsService;
1542         }
1543
1544         /**
1545          * Get the documentTypeService.
1546          * 
1547          * @return the documentTypeService
1548          */
1549         public DocumentTypeService getDocumentTypeService() {
1550                 return _documentTypeService;
1551         }
1552
1553         /**
1554          * Set the documentTypeService.
1555          * 
1556          * @param documentTypeService
1557          *            the documentTypeService to set
1558          */
1559         public void setDocumentTypeService(
1560                         final DocumentTypeService documentTypeService) {
1561                 _documentTypeService = documentTypeService;
1562         }
1563
1564         /**
1565          * Get the roleDAO.
1566          * 
1567          * @return the roleDAO
1568          */
1569         public RoleDAO getRoleDAO() {
1570                 return _roleDAO;
1571         }
1572
1573         /**
1574          * Set the roleDAO.
1575          * 
1576          * @param roleDAO
1577          *            the roleDAO to set
1578          */
1579         public void setRoleDAO(final RoleDAO roleDAO) {
1580                 _roleDAO = roleDAO;
1581         }
1582
1583         /**
1584          * Get the simulationContextTypeService.
1585          * 
1586          * @return the simulationContextTypeService
1587          */
1588         public SimulationContextTypeService getSimulationContextTypeService() {
1589                 return _simulationContextTypeService;
1590         }
1591
1592         /**
1593          * Set the simulationContextTypeService.
1594          * 
1595          * @param simulationContextTypeService
1596          *            the simulationContextTypeService to set
1597          */
1598         public void setSimulationContextTypeService(
1599                         final SimulationContextTypeService simulationContextTypeService) {
1600                 _simulationContextTypeService = simulationContextTypeService;
1601         }
1602
1603         /**
1604          * Get the validationCycleDAO.
1605          * 
1606          * @return the validationCycleDAO
1607          */
1608         public ValidationCycleDAO getValidationCycleDAO() {
1609                 return _validationCycleDAO;
1610         }
1611
1612         /**
1613          * Set the validationCycleDAO.
1614          * 
1615          * @param validationCycleDAO
1616          *            the validationCycleDAO to set
1617          */
1618         public void setValidationCycleDAO(
1619                         final ValidationCycleDAO validationCycleDAO) {
1620                 _validationCycleDAO = validationCycleDAO;
1621         }
1622
1623         /**
1624          * Get steps config.
1625          * 
1626          * @return steps config service
1627          */
1628         private StepsConfigService getStepsConfigService() {
1629                 return _stepsConfigService;
1630         }
1631
1632         /**
1633          * Set steps config service.
1634          * 
1635          * @param stepsConfigService
1636          *            steps config service
1637          */
1638         public void setStepsConfigService(
1639                         final StepsConfigService stepsConfigService) {
1640                 _stepsConfigService = stepsConfigService;
1641         }
1642
1643 }