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