Salome HOME
Default document types mappings are moved from application settings (my.xml) to proje...
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / ScenarioServiceImpl.java
index 10b5ba93b72c8982120a6cb7570b46b7990e1840..4dc064893d08da5f68ac4c445de8e7c894372a9d 100644 (file)
 package org.splat.service;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
+import org.splat.common.properties.MessageKeyEnum;
+import org.splat.dal.bo.kernel.Relation;
 import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ConvertsRelation;
+import org.splat.dal.bo.som.Document;
+import org.splat.dal.bo.som.DocumentType;
+import org.splat.dal.bo.som.File;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.KnowledgeElementType;
+import org.splat.dal.bo.som.ProgressState;
 import org.splat.dal.bo.som.Publication;
 import org.splat.dal.bo.som.Scenario;
 import org.splat.dal.bo.som.SimulationContext;
 import org.splat.dal.bo.som.Study;
+import org.splat.dal.bo.som.UsesRelation;
+import org.splat.dal.dao.kernel.UserDAO;
 import org.splat.dal.dao.som.KnowledgeElementDAO;
+import org.splat.dal.dao.som.KnowledgeElementTypeDAO;
 import org.splat.dal.dao.som.ScenarioDAO;
 import org.splat.dal.dao.som.StudyDAO;
 import org.splat.kernel.InvalidPropertyException;
+import org.splat.kernel.MismatchException;
 import org.splat.kernel.MissedPropertyException;
 import org.splat.kernel.MultiplyDefinedException;
-import org.splat.kernel.UserDirectory;
+import org.splat.kernel.NotApplicableException;
+import org.splat.service.dto.DocumentDTO;
+import org.splat.service.dto.FileDTO;
+import org.splat.service.dto.StepDTO;
 import org.splat.service.technical.IndexService;
+import org.splat.service.technical.ProjectSettingsService;
 import org.splat.som.Step;
+import org.splat.util.BeanHelper;
 import org.springframework.transaction.annotation.Transactional;
 
 /**
@@ -43,7 +63,7 @@ public class ScenarioServiceImpl implements ScenarioService {
        /**
         * Logger for this class.
         */
-       protected final static Logger logger = Logger
+       protected final static Logger LOG = Logger
                        .getLogger(ScenarioServiceImpl.class);
 
        /**
@@ -85,6 +105,36 @@ public class ScenarioServiceImpl implements ScenarioService {
         */
        private KnowledgeElementTypeService _knowledgeElementTypeService;
 
+       /**
+        * Injected user service.
+        */
+       private UserService _userService;
+
+       /**
+        * Injected user DAO.
+        */
+       private UserDAO _userDAO;
+
+       /**
+        * Injected knowledge element type DAO.
+        */
+       private KnowledgeElementTypeDAO _knowledgeElementTypeDAO;
+
+       /**
+        * Injected simulation context service.
+        */
+       private SimulationContextService _simulationContextService;
+
+       /**
+        * Injected project service.
+        */
+       private ProjectSettingsService _projectSettings;
+
+       /**
+        * Injected document type service.
+        */
+       private DocumentTypeService _documentTypeService;
+
        /**
         * Get the projectElementService.
         * 
@@ -101,7 +151,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         *            the projectElementService to set
         */
        public void setProjectElementService(
-                       ProjectElementService projectElementService) {
+                       final ProjectElementService projectElementService) {
                _projectElementService = projectElementService;
        }
 
@@ -120,7 +170,8 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param publicationService
         *            the publicationService to set
         */
-       public void setPublicationService(PublicationService publicationService) {
+       public void setPublicationService(
+                       final PublicationService publicationService) {
                _publicationService = publicationService;
        }
 
@@ -139,10 +190,115 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param stepService
         *            the stepService to set
         */
-       public void setStepService(StepService stepService) {
+       public void setStepService(final StepService stepService) {
                _stepService = stepService;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.ScenarioService#getScenarioInfo(long)
+        */
+       @Transactional(readOnly = true)
+       public List<StepDTO> getScenarioInfo(final long scenarioId) {
+               List<StepDTO> res = new ArrayList<StepDTO>();
+               // Get the scenario from the database by id
+               Scenario scen = getScenarioDAO().get(scenarioId);
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("Scenario[" + scenarioId + "]: Number of publications: "
+                                       + scen.getDocums().size());
+               }
+               // Get activities of the scenario
+               Step[] steps = getProjectElementService().getSteps(scen);
+               StepDTO stepDTO;
+               DocumentDTO docDTO;
+               String docType, fileFormat;
+               String processing;
+               boolean doImport;
+               // For each activity create a step DTO and add it to the result list
+               for (Step step : steps) {
+                       stepDTO = BeanHelper.copyBean(step.getStep(), StepDTO.class);
+                       res.add(stepDTO);
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Step[" + stepDTO.getNumber()
+                                               + "]: Number of documents: "
+                                               + step.getDocuments().size());
+                       }
+                       // For each publication of the activity create a document DTO.
+                       // Each file is considered as a source file.
+                       for (Publication tag : step.getDocuments()) {
+                               docDTO = stepDTO.addDoc(tag.value().getIndex(), tag.value()
+                                               .getTitle());
+                               char aState = tag.getIsnew();
+                               docType = tag.value().getType().getName();
+                               // For each file of the document create a file DTO
+                               // Process source file of the document
+                               fileFormat = tag.value().getFile().getFormat();
+                               doImport = getProjectSettings().doImport(docType, fileFormat);
+                               if (doImport && (!tag.isOutdated())) {
+                                       processing = "file-import";
+                               } else {
+                                       processing = "file-download";
+                               }
+                               docDTO.addFile(tag.value().getFile().getRelativePath(), aState,
+                                               processing, false);
+                               // Process all exported files
+                               for (Relation rel : tag.value().getRelations(
+                                               ConvertsRelation.class)) {
+                                       File aFile = ((ConvertsRelation) rel).getTo();
+                                       fileFormat = aFile.getFormat();
+                                       doImport = getProjectSettings().doImport(docType,
+                                                       fileFormat);
+                                       if (doImport && (!tag.isOutdated())) {
+                                               processing = "file-import";
+                                       } else {
+                                               processing = "file-download";
+                                       }
+                                       docDTO.addFile(aFile.getRelativePath(), aState, processing,
+                                                       false);
+                               }
+                       }
+               }
+               return res;
+       }
+
+       /**
+        * Create a new study with one scenario and "product" simulation context.
+        * 
+        * @param sprop
+        *            the study properties
+        * @param oprop
+        *            the scenario properties
+        * @param cprop
+        *            the "product" simulation context properties
+        * @return the created study
+        * @throws MissedPropertyException
+        *             if a mandatory property is missed
+        * @throws InvalidPropertyException
+        *             if a property is invalid
+        * @throws MultiplyDefinedException
+        *             if some property occurs several times
+        */
+       @Transactional
+       public Study createStudy(final Study.Properties sprop,
+                       final Scenario.Properties oprop,
+                       final SimulationContext.Properties cprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       MultiplyDefinedException {
+               Study study = getStudyService().createStudy(sprop);
+               addScenario(study, oprop);
+               if (cprop.getIndex() == 0) { // Input of new project context
+                       cprop.setType(getSimulationContextService().selectType("product"))
+                                       .setValue(cprop.getValue());
+                       getStudyService().addProjectContext(study, cprop);
+               } else { // Selection of existing project context
+                       SimulationContext context = getSimulationContextService()
+                                       .selectSimulationContext(cprop.getIndex());
+                       getStudyService().addProjectContext(study, context);
+               }
+               return study;
+       }
+
        /**
         * {@inheritDoc}
         * 
@@ -150,35 +306,46 @@ public class ScenarioServiceImpl implements ScenarioService {
         *      org.splat.dal.bo.som.KnowledgeElement.Properties)
         */
        @Transactional
-       public KnowledgeElement addKnowledgeElement(Scenario aScenario,
-                       KnowledgeElement.Properties kprop) throws MissedPropertyException,
-                       InvalidPropertyException, MultiplyDefinedException {
+       public KnowledgeElement addKnowledgeElement(final Scenario aScenarioDTO,
+                       final KnowledgeElement.Properties kprop)
+                       throws MissedPropertyException, InvalidPropertyException,
+                       MultiplyDefinedException {
                KnowledgeElement kelm = null;
                try {
-                       // Attach the detached scenario to the new hibernate session.
-                       aScenario = getScenarioDAO().merge(aScenario); // RKV
+                       long aScenarioId = aScenarioDTO.getIndex();
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("Add a knowledge element to the scenario #"
+                                               + aScenarioId);
+                       }
+                       // Get the persistent scenario.
+                       Scenario aScenario = getScenarioDAO().get(aScenarioId);
+                       // Get persistent objects for creating a new knowledge.
+                       // TODO: Actions must use DTO instead of persistent objects.
+                       getUserDAO().merge(kprop.getAuthor());
+                       getKnowledgeElementTypeDAO().merge(kprop.getType());
+                       // Create a transient knowledge element related to the given scenario.
                        kelm = new KnowledgeElement(kprop.setOwnerScenario(aScenario));
+                       // Save the new knowledge in the database.
                        getKnowledgeElementDAO().create(kelm);
-                       // RKV: commented because of BatchUpdateException during creation of a new study: session.flush(); //RKV
-                       // Update of my persistent data
-                       // RKV aScenario.getKnowledgeElements().add(kelm);
-                       // RKV getKnowledgeElementDAO().flush();
-                       // RKV: commented because of NullPointerException during creation of a new study: session.merge(aScenario); //RKV
-                       // Update of my transient data
-                       List<KnowledgeElement> known = aScenario
-                                       .getKnowledgeElementsOf(kelm.getType()); // Initializes this.known, if not yet done
-                       // RKV: knowledge is already added into persistent set and "known" is constructed from it in the above call: known.add(kelm);
+                       // Update scenario transient data.
                        if (kelm.getType().equals("usecase")) {
-                               aScenario.setUcase(kelm);
-                       } else if (aScenario.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
-                               aScenario.getKnowledgeElementsList().add(kelm);
+                               aScenarioDTO.setUcase(kelm);
+                       } else if (aScenarioDTO.getKnowledgeElementsList() != null) { // If null, knowl will be initialized when needed
+                               aScenarioDTO.getKnowledgeElementsList().add(kelm);
                        }
-                       // Update of the index of Knowledge Elements
-                       getIndexService().add(kelm);
-                       update(aScenario);
 
+                       // Load the workflow for the parent study to take into account
+                       // all study actors durng reindexing.
+                       getStudyService().loadWorkflow(aScenario.getOwnerStudy());
+
+                       // Update the lucene index of knowledge elements.
+                       getIndexService().add(kelm);
+                       if (LOG.isDebugEnabled()) {
+                               LOG.debug("A knowledge element #" + kelm.getIndex()
+                                               + " is added to the scenario #" + aScenario.getIndex());
+                       }
                } catch (IOException error) {
-                       logger.error("Unable to index the knowedge element '"
+                       LOG.error("Unable to index the knowedge element '"
                                        + kelm.getIndex() + "', reason:", error);
                        kelm = null;
                }
@@ -194,24 +361,204 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @return true if updating succeeded
         */
        @Transactional
-       private boolean update(Scenario aScenario) {
+       private boolean update(final Scenario aScenario) {
                boolean isOk = false;
                try {
                        getScenarioDAO().update(aScenario); // Update of relational base
                        isOk = true;
                } catch (Exception error) {
-                       logger.error("Unable to re-index the knowledge element '"
+                       LOG.error("Unable to re-index the knowledge element '"
                                        + aScenario.getIndex() + "', reason:", error);
                }
                return isOk;
        }
 
+       /**
+        * {@inheritDoc}
+        * 
+        * @see org.splat.service.ScenarioService#checkin(long, long, java.util.List)
+        */
+       @Transactional
+       public void checkin(final long scenId, final long userId,
+                       final List<StepDTO> scInfo) throws InvalidPropertyException,
+                       MissedPropertyException, MultiplyDefinedException,
+                       MismatchException, IOException, NotApplicableException {
+               // Get the scenario from the database by id
+               Scenario aScenario = getScenarioDAO().get(scenId);
+               // Get the user who perform this check-in operation
+               User aUser = getUserService().selectUser(userId);
+               // Get activities of the scenario
+               Step[] steps = getProjectElementService().getSteps(aScenario);
+               // Find result document types
+               List<DocumentType> resTypes = getDocumentTypeService()
+                               .selectResultTypes();
+
+               // For each processed existing document keep its new version
+               Map<Document, Document> newVersion = new HashMap<Document, Document>();
+               // For each processed existing document keep its previous version
+               Map<Publication, Document> prevVersion = new HashMap<Publication, Document>();
+               // Keep newly created documents to create uses relations to results of a previous step.
+               List<Publication> newVers = new ArrayList<Publication>();
+               List<Publication> newDocs = new ArrayList<Publication>();
+               // For each step DTO
+               DocumentType resType;
+               java.io.File updir;
+               Document.Properties dprop = new Document.Properties();
+               Date aDate = new Date();
+               for (StepDTO stepDTO : scInfo) {
+                       // Find a result document type of the step
+                       int i = 0;
+                       resType = null;
+                       do {
+                               if (resTypes.get(i).isResultOf(
+                                               getProjectSettings().getStep(stepDTO.getNumber()))) {
+                                       resType = resTypes.get(i);
+                               }
+                               i++;
+                       } while ((resType == null) && (i < resTypes.size()));
+
+                       // Find the appropriate scenario step
+                       Step step = findStep(stepDTO, steps);
+
+                       // Process documents of the step
+                       for (DocumentDTO doc : stepDTO.getDocs()) {
+                               if (doc.getFiles().size() > 0) {
+                                       // NOTE: Process only the first attached file for each document
+                                       FileDTO file = doc.getFiles().get(0);
+
+                                       // Get document title as the file name
+                                       java.io.File upfile = new java.io.File(file.getPath());
+                                       String fileFormat = upfile.getName().substring(
+                                                       upfile.getName().lastIndexOf('.') + 1);
+                                       String docname = upfile.getName().substring(0,
+                                                       upfile.getName().lastIndexOf('.'));
+
+                                       // Create a new document or a new version of the document
+                                       dprop.clear();
+                                       dprop.setAuthor(aUser).setDate(aDate);
+                                       Publication pub, newPub;
+
+                                       if (doc.getId() > 0) {
+                                               // If the document already exists then create a new version of it
+                                               // Find the document publication
+                                               pub = step.getDocument(doc.getId());
+                                               if (pub == null) {
+                                                       throw new InvalidPropertyException(
+                                                                       MessageKeyEnum.SCN_000002.toString(), doc
+                                                                                       .getId());
+                                               }
+                                               newPub = getStepService().versionDocument(step, pub,
+                                                               dprop);
+                                               // Remeber the link from the old document to the new document version
+                                               newVersion.put(pub.value(), newPub.value());
+                                               // Remember the new version publication
+                                               newVers.add(newPub);
+                                               // Remember the previouse document version for the new publication
+                                               prevVersion.put(newPub, pub.value());
+                                       } else {
+
+                                               // Otherwise create a new document of the result type
+                                               // If result type is not found try to get type by file extension
+                                               if (resType == null) {
+                                                       dprop.setType(getProjectSettings()
+                                                                       .getDefaultDocumentType(step.getStep(),
+                                                                                       fileFormat));
+                                               } else {
+                                                       dprop.setType(resType);
+                                               }
+                                               dprop.setDescription("Checked in").setName(docname)
+                                                               .setFormat(fileFormat);
+                                               newPub = getStepService().createDocument(step, dprop);
+
+                                               // Remeber the new document
+                                               newDocs.add(newPub);
+                                       }
+
+                                       // Attach the file to the created document
+                                       updir = newPub.getSourceFile().asFile();
+                                       if (LOG.isInfoEnabled()) {
+                                               LOG.info("Moving \"" + upfile.getName() + "\" to \""
+                                                               + updir.getPath() + "\".");
+                                       }
+                                       if (upfile.renameTo(updir)) {
+                                               // Save the new publication in the scenario.
+                                               // The old publication is removed from the scenario here.
+                                               getPublicationService().saveAs(newPub,
+                                                               ProgressState.inWORK); // May throw FileNotFound if rename was not done
+                                       }
+                               }
+                       }
+               }
+
+               // Set uses/used relations
+
+               // For each new version copy uses relations from the previous version.
+               for (Publication newVer : newVers) {
+                       // For each Uses relation of the previous version
+                       for (Relation rel : prevVersion.get(newVer).getRelations(
+                                       UsesRelation.class)) {
+                               // If used document has been also versioned then refer to its new version.
+                               Document usedDoc = ((UsesRelation) rel).getTo();
+                               if (newVersion.containsKey(usedDoc)) {
+                                       usedDoc = newVersion.get(usedDoc);
+                               }
+                               // Build the appropriate relation for the new version.
+                               newVer.addDependency(usedDoc);
+                       }
+                       // TODO: Outdate documents which depend from the previous version and were not checked in during this operation.
+
+               }
+
+               // For each new document create uses relation to the last versions of
+               // results of the previous step.
+               for (Publication newDoc : newDocs) {
+                       // Find used document type according to the configuration.
+                       // Find documents of used type in the previous study step.
+                       
+                       // Create uses relation from the new document 
+                       // to the found document in the previous step.
+                       //newDoc.addDependency(to);
+               }
+
+               // Mark the scenario as checked in
+               checkin(aScenario);
+       }
+
+       /**
+        * Find appropriate step in the array of scenario steps according to the given step DTO.
+        * 
+        * @param stepDTO
+        *            the stepDTO
+        * @param steps
+        *            scenario steps
+        * @return appropriate scenario step
+        * @throws InvalidPropertyException
+        *             if appropriate step is not found
+        */
+       private Step findStep(final StepDTO stepDTO, final Step[] steps)
+                       throws InvalidPropertyException {
+               int i = 0;
+               Step step = null;
+               do {
+                       if (steps[i].getNumber() == stepDTO.getNumber()) {
+                               step = steps[i];
+                       }
+                       i++;
+               } while ((step == null) && (i < steps.length));
+
+               if (step == null) {
+                       throw new InvalidPropertyException(MessageKeyEnum.SCN_000001
+                                       .toString(), stepDTO.getNumber());
+               }
+               return step;
+       }
+
        /**
         * {@inheritDoc}
         * 
         * @see org.splat.service.ScenarioService#checkin(org.splat.dal.bo.som.Scenario)
         */
-       public void checkin(Scenario aScenario) {
+       public void checkin(final Scenario aScenario) {
                aScenario.setUser(null);
                aScenario.setLastModificationDate(Calendar.getInstance().getTime());
                getScenarioDAO().update(aScenario);
@@ -222,14 +569,15 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @see org.splat.service.ScenarioService#checkout(org.splat.dal.bo.som.Scenario, org.splat.dal.bo.kernel.User)
         */
-       public boolean checkout(Scenario aScenario, User user) {
-               if (!getStudyService().isStaffedBy(aScenario.getOwnerStudy(), user))
-                       return false;
-
-               aScenario.setUser(user);
-               aScenario.setLastModificationDate(Calendar.getInstance().getTime());
-               getScenarioDAO().update(aScenario);
-               return true;
+       public boolean checkout(final Scenario aScenario, final User user) {
+               boolean res = getStudyService().isStaffedBy(aScenario.getOwnerStudy(),
+                               user);
+               if (res) {
+                       aScenario.setUser(user);
+                       aScenario.setLastModificationDate(Calendar.getInstance().getTime());
+                       getScenarioDAO().update(aScenario);
+               }
+               return res;
        }
 
        /**
@@ -237,14 +585,15 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @see org.splat.service.ScenarioService#copyContentsUpTo(org.splat.dal.bo.som.Scenario, org.splat.som.Step)
         */
-       public void copyContentsUpTo(Scenario scenario, Step lastep) {
+       public void copyContentsUpTo(final Scenario scenario, final Step lastep) {
                Scenario base = (Scenario) lastep.getOwner();
                Step[] from = getProjectElementService().getSteps(base);
                Step[] to = getProjectElementService().getSteps(scenario);
                for (int i = 0; i < from.length; i++) {
                        Step step = from[i];
-                       if (step.getNumber() > lastep.getNumber())
+                       if (step.getNumber() > lastep.getNumber()) {
                                break;
+                       }
 
                        List<Publication> docs = step.getAllDocuments();
                        for (Iterator<Publication> j = docs.iterator(); j.hasNext();) {
@@ -265,7 +614,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * 
         * @see org.splat.service.ScenarioService#isEmpty(org.splat.dal.bo.som.Scenario)
         */
-       public boolean isEmpty(Scenario scenario) {
+       public boolean isEmpty(final Scenario scenario) {
                Step[] mystep = getProjectElementService().getSteps(scenario);
                boolean isEmp = true;
                for (int i = 0; i < mystep.length; i++) {
@@ -281,14 +630,16 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param scenario
         * @return
         */
-       public boolean isFinished(Scenario scenario) {
+       public boolean isFinished(final Scenario scenario) {
                Step[] mystep = getProjectElementService().getSteps(scenario);
                boolean notempty = false; // If this is empty, this is not finished
                for (int i = 0; i < mystep.length; i++) {
-                       if (!mystep[i].isStarted())
+                       if (!mystep[i].isStarted()) {
                                continue;
-                       if (!mystep[i].isFinished())
+                       }
+                       if (!mystep[i].isFinished()) {
                                return false;
+                       }
                        notempty = true;
                }
                return notempty;
@@ -300,15 +651,17 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @see org.splat.service.StudyService#addScenario(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Scenario.Properties)
         */
        @Transactional
-       public Scenario addScenario(Study aStudy, Scenario.Properties sprop)
-                       throws MissedPropertyException, InvalidPropertyException,
-                       MultiplyDefinedException {
-               if (sprop.getManager() == null)
+       public Scenario addScenario(final Study aStudy,
+                       final Scenario.Properties sprop) throws MissedPropertyException,
+                       InvalidPropertyException, MultiplyDefinedException {
+               if (sprop.getManager() == null) {
                        sprop.setManager(aStudy.getAuthor());
+               }
 
                Scenario scenario = new Scenario(sprop.setOwnerStudy(aStudy));
-               if (sprop.getBaseStep() != null)
+               if (sprop.getBaseStep() != null) {
                        copyContentsUpTo(scenario, sprop.getBaseStep());
+               }
                Scenario previous = sprop.getInsertAfter();
 
                if (previous == null) {
@@ -321,12 +674,12 @@ public class ScenarioServiceImpl implements ScenarioService {
                getScenarioDAO().create(scenario); // Must be done after updating this study because of the back reference to the study
                if (sprop.getBaseStep() != null) {
                        // No need to update the Knowledge Element index as Knowledge Elements are not copied
-                       scenario.refresh(); // Because saving the scenario changes the hashcode of copied Publications
+                       getProjectElementService().refresh(scenario); // Because saving the scenario changes the hashcode of copied Publications
                }
-               KnowledgeElementType ucase = getKnowledgeElementTypeService().selectType(
-                               "usecase");
+               KnowledgeElementType ucase = getKnowledgeElementTypeService()
+                               .selectType("usecase");
                KnowledgeElement.Properties kprop = new KnowledgeElement.Properties();
-               User admin = UserDirectory.selectUser(1); // First user created when creating the database
+               User admin = getUserService().selectUser(1); // First user created when creating the database
                kprop.setType(ucase).setTitle(aStudy.getTitle()).setValue(
                                scenario.getTitle()).setAuthor(admin); // Internal Knowledge Element required by the validation process of
                // knowledges
@@ -334,6 +687,39 @@ public class ScenarioServiceImpl implements ScenarioService {
                return scenario;
        }
 
+       /**
+        * Remove a knowledge element from a scenario.
+        * 
+        * @param scenario
+        *            the scenario
+        * @param kelm
+        *            the knowledge element to remove
+        * @return true if removal succeeded
+        */
+       public boolean removeKnowledgeElement(final Scenario scenario,
+                       final KnowledgeElement kelm) {
+               KnowledgeElement torem = scenario.getKnowledgeElement(kelm.getIndex());
+               if (torem == null) {
+                       return false;
+               }
+               boolean done = scenario.getKnowledgeElements().remove(torem);
+               if (done) {
+                       // Update of my transient data
+                       // RKV: These transient data are not used indeed.
+                       // RKV: List<KnowledgeElement> kelms = scenario.getKnowledgeByType().get(
+                       // RKV: kelm.getType().getIndex());
+                       // RKV: kelms.remove(torem);
+                       if (scenario.getKnowledgeElementsList() != null) {
+                               scenario.getKnowledgeElementsList().remove(torem);
+                       }
+                       getScenarioDAO().update(scenario);
+                       // TODO: If the owner study is not private, remove the knowledge from the Lucene index
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
        /**
         * Get the knowledgeElementDAO.
         * 
@@ -349,7 +735,8 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param knowledgeElementDAO
         *            the knowledgeElementDAO to set
         */
-       public void setKnowledgeElementDAO(KnowledgeElementDAO knowledgeElementDAO) {
+       public void setKnowledgeElementDAO(
+                       final KnowledgeElementDAO knowledgeElementDAO) {
                _knowledgeElementDAO = knowledgeElementDAO;
        }
 
@@ -368,7 +755,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param indexService
         *            the indexService to set
         */
-       public void setIndexService(IndexService indexService) {
+       public void setIndexService(final IndexService indexService) {
                _indexService = indexService;
        }
 
@@ -387,7 +774,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param scenarioDAO
         *            the scenarioDAO to set
         */
-       public void setScenarioDAO(ScenarioDAO scenarioDAO) {
+       public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
                _scenarioDAO = scenarioDAO;
        }
 
@@ -406,7 +793,7 @@ public class ScenarioServiceImpl implements ScenarioService {
         * @param studyDAO
         *            the studyDAO to set
         */
-       public void setStudyDAO(StudyDAO studyDAO) {
+       public void setStudyDAO(final StudyDAO studyDAO) {
                _studyDAO = studyDAO;
        }
 
@@ -426,12 +813,13 @@ public class ScenarioServiceImpl implements ScenarioService {
         *            the knowledgeElementTypeService to set
         */
        public void setKnowledgeElementTypeService(
-                       KnowledgeElementTypeService knowledgeElementTypeService) {
+                       final KnowledgeElementTypeService knowledgeElementTypeService) {
                _knowledgeElementTypeService = knowledgeElementTypeService;
        }
 
        /**
         * Get the studyService.
+        * 
         * @return the studyService
         */
        public StudyService getStudyService() {
@@ -440,10 +828,130 @@ public class ScenarioServiceImpl implements ScenarioService {
 
        /**
         * Set the studyService.
-        * @param studyService the studyService to set
+        * 
+        * @param studyService
+        *            the studyService to set
         */
-       public void setStudyService(StudyService studyService) {
+       public void setStudyService(final StudyService studyService) {
                _studyService = studyService;
        }
 
+       /**
+        * Get the userService.
+        * 
+        * @return the userService
+        */
+       public UserService getUserService() {
+               return _userService;
+       }
+
+       /**
+        * Set the userService.
+        * 
+        * @param userService
+        *            the userService to set
+        */
+       public void setUserService(final UserService userService) {
+               _userService = userService;
+       }
+
+       /**
+        * Get the userDAO.
+        * 
+        * @return the userDAO
+        */
+       public UserDAO getUserDAO() {
+               return _userDAO;
+       }
+
+       /**
+        * Set the userDAO.
+        * 
+        * @param userDAO
+        *            the userDAO to set
+        */
+       public void setUserDAO(final UserDAO userDAO) {
+               _userDAO = userDAO;
+       }
+
+       /**
+        * Get the knowledgeElementTypeDAO.
+        * 
+        * @return the knowledgeElementTypeDAO
+        */
+       public KnowledgeElementTypeDAO getKnowledgeElementTypeDAO() {
+               return _knowledgeElementTypeDAO;
+       }
+
+       /**
+        * Set the knowledgeElementTypeDAO.
+        * 
+        * @param knowledgeElementTypeDAO
+        *            the knowledgeElementTypeDAO to set
+        */
+       public void setKnowledgeElementTypeDAO(
+                       final KnowledgeElementTypeDAO knowledgeElementTypeDAO) {
+               _knowledgeElementTypeDAO = knowledgeElementTypeDAO;
+       }
+
+       /**
+        * Get the simulationContextService.
+        * 
+        * @return the simulationContextService
+        */
+       public SimulationContextService getSimulationContextService() {
+               return _simulationContextService;
+       }
+
+       /**
+        * Set the simulationContextService.
+        * 
+        * @param simulationContextService
+        *            the simulationContextService to set
+        */
+       public void setSimulationContextService(
+                       final SimulationContextService simulationContextService) {
+               _simulationContextService = simulationContextService;
+       }
+
+       /**
+        * Get project settings.
+        * 
+        * @return Project settings service
+        */
+       private ProjectSettingsService getProjectSettings() {
+               return _projectSettings;
+       }
+
+       /**
+        * Set project settings service.
+        * 
+        * @param projectSettingsService
+        *            project settings service
+        */
+       public void setProjectSettings(
+                       final ProjectSettingsService projectSettingsService) {
+               _projectSettings = projectSettingsService;
+       }
+
+       /**
+        * Get the documentTypeService.
+        * 
+        * @return the documentTypeService
+        */
+       public DocumentTypeService getDocumentTypeService() {
+               return _documentTypeService;
+       }
+
+       /**
+        * Set the documentTypeService.
+        * 
+        * @param documentTypeService
+        *            the documentTypeService to set
+        */
+       public void setDocumentTypeService(
+                       final DocumentTypeService documentTypeService) {
+               _documentTypeService = documentTypeService;
+       }
+
 }